blob: f53e1bfef75e836c0304862f868bd1d99ca97210 [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);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100982 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100983 }
984#endif
985
Eric Fiselier451d5582018-11-26 20:15:38 +0000986 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
987
Marek Kurdejc9848142020-11-26 10:07:16 +0100988 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
989 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000990 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100991 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000993 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000994 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
995 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000997 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
998 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999
1000 const_reference at(size_type __n) const;
1001 reference at(size_type __n);
1002
1003 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +00001004
1005 template <class _Tp>
1006 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001007 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001008 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001009 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1010 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001011 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001012 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001013 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001014 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001016#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001018#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019
Howard Hinnantcf823322010-12-17 14:46:43 +00001020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001022
1023 template <class _Tp>
1024 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001025 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1027 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001028 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001029 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001030 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001031 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001032
Marshall Clow62953962016-10-03 23:40:48 +00001033 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001034 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001035 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001036 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001037 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1038 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001039 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001040 >
Marshall Clow82513342016-09-24 22:45:42 +00001041 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001042 basic_string& append(const value_type* __s, size_type __n);
1043 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001045
1046 _LIBCPP_INLINE_VISIBILITY
1047 void __append_default_init(size_type __n);
1048
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001050 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001051 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001053 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001055 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001056 _LIBCPP_INLINE_VISIBILITY
1057 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001058 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001059 append(__temp.data(), __temp.size());
1060 return *this;
1061 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001063 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001064 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001066 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001068 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001069 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001070 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001071
Eric Fiselierfc92be82017-04-19 00:28:44 +00001072#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001075#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076
1077 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001080 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1081 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1082 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1083 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084
Marshall Clowe46031a2018-07-02 18:41:15 +00001085 template <class _Tp>
1086 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001087 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001088 <
1089 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1090 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001091 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001092 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001093 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001094 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001095#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001096 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001097 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001098 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001099 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001100#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001101 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001102 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001103 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001104 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001105 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1107 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001108 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001109 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001110 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001111 basic_string& assign(const value_type* __s, size_type __n);
1112 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113 basic_string& assign(size_type __n, value_type __c);
1114 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001115 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001116 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001118 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001120 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 assign(_InputIterator __first, _InputIterator __last);
1122 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001123 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001124 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001126 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001128 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001130#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001133#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134
Howard Hinnantcf823322010-12-17 14:46:43 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001137
1138 template <class _Tp>
1139 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001140 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001141 <
1142 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1143 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001144 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001145 insert(size_type __pos1, const _Tp& __t)
1146 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1147
Marshall Clow82513342016-09-24 22:45:42 +00001148 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001149 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001150 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001151 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001152 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001153 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001154 >
Marshall Clow82513342016-09-24 22:45:42 +00001155 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001156 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001157 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1158 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1160 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1163 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001164 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001165 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001167 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001169 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1171 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001172 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001173 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001175 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001177 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001179#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1182 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001183#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184
1185 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 iterator erase(const_iterator __first, const_iterator __last);
1190
Howard Hinnantcf823322010-12-17 14:46:43 +00001191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001193
1194 template <class _Tp>
1195 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001196 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001197 <
1198 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1199 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001200 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001201 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 +00001202 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 +00001203 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001204 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001205 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001206 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001207 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001208 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001209 >
Marshall Clow82513342016-09-24 22:45:42 +00001210 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001211 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1212 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001215 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001216
1217 template <class _Tp>
1218 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001219 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001220 <
1221 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1222 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001223 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001224 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1225
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001227 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001229 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001231 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001233 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001234 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001236 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001238 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001239 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001240#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001242 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001244#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
Howard Hinnantd17880b2013-06-28 16:59:19 +00001246 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1249
Howard Hinnantcf823322010-12-17 14:46:43 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001251 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001252#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001253 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001254#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001255 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001256 __is_nothrow_swappable<allocator_type>::value);
1257#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
Howard Hinnantcf823322010-12-17 14:46:43 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001260 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001261 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001262 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001263#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001264 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001265 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001266#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267
Howard Hinnantcf823322010-12-17 14:46:43 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001269 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Howard Hinnantcf823322010-12-17 14:46:43 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001272 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001273
1274 template <class _Tp>
1275 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001276 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001277 <
1278 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1279 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001280 >
zoecarver1997e0a2021-02-05 11:54:47 -08001281 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001282 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001284 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001285 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286
Howard Hinnantcf823322010-12-17 14:46:43 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001288 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001289
1290 template <class _Tp>
1291 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001292 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001293 <
1294 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1295 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001296 >
zoecarver1997e0a2021-02-05 11:54:47 -08001297 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001298 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001301 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302
Howard Hinnantcf823322010-12-17 14:46:43 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001304 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001305
1306 template <class _Tp>
1307 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001308 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001309 <
1310 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1311 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001312 >
zoecarver1997e0a2021-02-05 11:54:47 -08001313 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001314 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001318 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319
Howard Hinnantcf823322010-12-17 14:46:43 +00001320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001321 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001322
1323 template <class _Tp>
1324 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001325 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001326 <
1327 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1328 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001329 >
zoecarver1997e0a2021-02-05 11:54:47 -08001330 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001331 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001335 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336
Howard Hinnantcf823322010-12-17 14:46:43 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001338 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001339
1340 template <class _Tp>
1341 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001342 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001343 <
1344 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1345 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001346 >
zoecarver1997e0a2021-02-05 11:54:47 -08001347 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001348 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 +00001349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001350 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001351 _LIBCPP_INLINE_VISIBILITY
1352 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1353
1354 _LIBCPP_INLINE_VISIBILITY
1355 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001356
1357 template <class _Tp>
1358 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001359 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001360 <
1361 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1362 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001363 >
zoecarver1997e0a2021-02-05 11:54:47 -08001364 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001365 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 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001367 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001368 _LIBCPP_INLINE_VISIBILITY
1369 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1370
1371 _LIBCPP_INLINE_VISIBILITY
1372 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001373
1374 template <class _Tp>
1375 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001376 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001377 <
1378 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1379 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001380 >
zoecarver1997e0a2021-02-05 11:54:47 -08001381 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001382
1383 template <class _Tp>
1384 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001385 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001386 <
1387 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1388 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001389 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001390 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1391
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001394 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 +00001395
Marshall Clow82513342016-09-24 22:45:42 +00001396 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001397 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001398 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001399 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001400 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001401 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001402 >
Marshall Clow82513342016-09-24 22:45:42 +00001403 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 +00001404 int compare(const value_type* __s) const _NOEXCEPT;
1405 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1406 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407
Marshall Clow18c293b2017-12-04 20:11:38 +00001408#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001409 constexpr _LIBCPP_INLINE_VISIBILITY
1410 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001411 { return __self_view(data(), size()).starts_with(__sv); }
1412
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001413 constexpr _LIBCPP_INLINE_VISIBILITY
1414 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001415 { return !empty() && _Traits::eq(front(), __c); }
1416
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001417 constexpr _LIBCPP_INLINE_VISIBILITY
1418 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001419 { return starts_with(__self_view(__s)); }
1420
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001421 constexpr _LIBCPP_INLINE_VISIBILITY
1422 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001423 { return __self_view(data(), size()).ends_with( __sv); }
1424
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001425 constexpr _LIBCPP_INLINE_VISIBILITY
1426 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001427 { return !empty() && _Traits::eq(back(), __c); }
1428
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001429 constexpr _LIBCPP_INLINE_VISIBILITY
1430 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001431 { return ends_with(__self_view(__s)); }
1432#endif
1433
Wim Leflere023c3542021-01-19 14:33:30 -05001434#if _LIBCPP_STD_VER > 20
1435 constexpr _LIBCPP_INLINE_VISIBILITY
1436 bool contains(__self_view __sv) const noexcept
1437 { return __self_view(data(), size()).contains(__sv); }
1438
1439 constexpr _LIBCPP_INLINE_VISIBILITY
1440 bool contains(value_type __c) const noexcept
1441 { return __self_view(data(), size()).contains(__c); }
1442
1443 constexpr _LIBCPP_INLINE_VISIBILITY
1444 bool contains(const value_type* __s) const
1445 { return __self_view(data(), size()).contains(__s); }
1446#endif
1447
Howard Hinnantcf823322010-12-17 14:46:43 +00001448 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001449
Marshall Clowe60a7182018-05-29 17:04:37 +00001450 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001451
Marek Kurdejc9848142020-11-26 10:07:16 +01001452 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1453
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001454 _LIBCPP_INLINE_VISIBILITY
1455 bool __is_long() const _NOEXCEPT
1456 {return bool(__r_.first().__s.__size_ & __short_mask);}
1457
Louis Dionneba400782020-10-02 15:02:52 -04001458#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001459
1460 bool __dereferenceable(const const_iterator* __i) const;
1461 bool __decrementable(const const_iterator* __i) const;
1462 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1463 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1464
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001465#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001466
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001468 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1469 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1470 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1471 }
1472
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001473 _LIBCPP_INLINE_VISIBILITY
1474 allocator_type& __alloc() _NOEXCEPT
1475 {return __r_.second();}
1476 _LIBCPP_INLINE_VISIBILITY
1477 const allocator_type& __alloc() const _NOEXCEPT
1478 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001480#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001481
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001483 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001484# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001486# else
1487 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1488# endif
1489
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001490 _LIBCPP_INLINE_VISIBILITY
1491 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001492# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001494# else
1495 {return __r_.first().__s.__size_;}
1496# endif
1497
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001498#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001499
1500 _LIBCPP_INLINE_VISIBILITY
1501 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001502# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001503 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1504# else
1505 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1506# endif
1507
1508 _LIBCPP_INLINE_VISIBILITY
1509 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001510# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001511 {return __r_.first().__s.__size_;}
1512# else
1513 {return __r_.first().__s.__size_ >> 1;}
1514# endif
1515
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001516#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001517
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001518 _LIBCPP_INLINE_VISIBILITY
1519 void __set_long_size(size_type __s) _NOEXCEPT
1520 {__r_.first().__l.__size_ = __s;}
1521 _LIBCPP_INLINE_VISIBILITY
1522 size_type __get_long_size() const _NOEXCEPT
1523 {return __r_.first().__l.__size_;}
1524 _LIBCPP_INLINE_VISIBILITY
1525 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1527
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001528 _LIBCPP_INLINE_VISIBILITY
1529 void __set_long_cap(size_type __s) _NOEXCEPT
1530 {__r_.first().__l.__cap_ = __long_mask | __s;}
1531 _LIBCPP_INLINE_VISIBILITY
1532 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001533 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001535 _LIBCPP_INLINE_VISIBILITY
1536 void __set_long_pointer(pointer __p) _NOEXCEPT
1537 {__r_.first().__l.__data_ = __p;}
1538 _LIBCPP_INLINE_VISIBILITY
1539 pointer __get_long_pointer() _NOEXCEPT
1540 {return __r_.first().__l.__data_;}
1541 _LIBCPP_INLINE_VISIBILITY
1542 const_pointer __get_long_pointer() const _NOEXCEPT
1543 {return __r_.first().__l.__data_;}
1544 _LIBCPP_INLINE_VISIBILITY
1545 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001546 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001547 _LIBCPP_INLINE_VISIBILITY
1548 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001549 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001550 _LIBCPP_INLINE_VISIBILITY
1551 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001553 _LIBCPP_INLINE_VISIBILITY
1554 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1556
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001557 _LIBCPP_INLINE_VISIBILITY
1558 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 {
1560 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1561 for (unsigned __i = 0; __i < __n_words; ++__i)
1562 __a[__i] = 0;
1563 }
1564
1565 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001567 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001568 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001570 static _LIBCPP_INLINE_VISIBILITY
1571 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001572 {
1573 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1574 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1575 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1576 if (__guess == __min_cap) ++__guess;
1577 return __guess;
1578 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001580 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001581 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001582 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001583 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001584 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001586
Martijn Vels5e7c9752020-03-04 17:52:46 -05001587 // Slow path for the (inlined) copy constructor for 'long' strings.
1588 // Always externally instantiated and not inlined.
1589 // Requires that __s is zero terminated.
1590 // The main reason for this function to exist is because for unstable, we
1591 // want to allow inlining of the copy constructor. However, we don't want
1592 // to call the __init() functions as those are marked as inline which may
1593 // result in over-aggressive inlining by the compiler, where our aim is
1594 // to only inline the fast path code directly in the ctor.
1595 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1596
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001598 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001599 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001601 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1602 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 __init(_InputIterator __first, _InputIterator __last);
1604
1605 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001606 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001607 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001609 __is_cpp17_forward_iterator<_ForwardIterator>::value
1610 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 __init(_ForwardIterator __first, _ForwardIterator __last);
1612
1613 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001614 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1616 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001617 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618
Martijn Vels596e3de2020-02-26 15:55:49 -05001619 // __assign_no_alias is invoked for assignment operations where we
1620 // have proof that the input does not alias the current instance.
1621 // For example, operator=(basic_string) performs a 'self' check.
1622 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001623 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001624
Howard Hinnantcf823322010-12-17 14:46:43 +00001625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626 void __erase_to_end(size_type __pos);
1627
Martijn Velsa81fc792020-02-26 13:25:43 -05001628 // __erase_external_with_move is invoked for erase() invocations where
1629 // `n ~= npos`, likely requiring memory moves on the string data.
1630 void __erase_external_with_move(size_type __pos, size_type __n);
1631
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001632 _LIBCPP_INLINE_VISIBILITY
1633 void __copy_assign_alloc(const basic_string& __str)
1634 {__copy_assign_alloc(__str, integral_constant<bool,
1635 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1636
1637 _LIBCPP_INLINE_VISIBILITY
1638 void __copy_assign_alloc(const basic_string& __str, true_type)
1639 {
Marshall Clowf258c202017-01-31 03:40:52 +00001640 if (__alloc() == __str.__alloc())
1641 __alloc() = __str.__alloc();
1642 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001643 {
Marshall Clowf258c202017-01-31 03:40:52 +00001644 if (!__str.__is_long())
1645 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001646 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001647 __alloc() = __str.__alloc();
1648 }
1649 else
1650 {
1651 allocator_type __a = __str.__alloc();
1652 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001653 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001654 __alloc() = _VSTD::move(__a);
1655 __set_long_pointer(__p);
1656 __set_long_cap(__str.__get_long_cap());
1657 __set_long_size(__str.size());
1658 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001659 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001660 }
1661
1662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001663 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001664 {}
1665
Eric Fiselierfc92be82017-04-19 00:28:44 +00001666#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001667 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001668 void __move_assign(basic_string& __str, false_type)
1669 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001671 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001672#if _LIBCPP_STD_VER > 14
1673 _NOEXCEPT;
1674#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001675 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001676#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001677#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001678
1679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001680 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001681 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001682 _NOEXCEPT_(
1683 !__alloc_traits::propagate_on_container_move_assignment::value ||
1684 is_nothrow_move_assignable<allocator_type>::value)
1685 {__move_assign_alloc(__str, integral_constant<bool,
1686 __alloc_traits::propagate_on_container_move_assignment::value>());}
1687
1688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001689 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001690 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1691 {
1692 __alloc() = _VSTD::move(__c.__alloc());
1693 }
1694
1695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001696 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001697 _NOEXCEPT
1698 {}
1699
Martijn Velsda7d94f2020-06-19 14:24:03 -04001700 basic_string& __assign_external(const value_type* __s);
1701 basic_string& __assign_external(const value_type* __s, size_type __n);
1702
1703 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1704 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1705 pointer __p = __is_long()
1706 ? (__set_long_size(__n), __get_long_pointer())
1707 : (__set_short_size(__n), __get_short_pointer());
1708 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1709 traits_type::assign(__p[__n], value_type());
1710 return *this;
1711 }
1712
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001713 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1714 __set_size(__newsz);
1715 __invalidate_iterators_past(__newsz);
1716 traits_type::assign(__p[__newsz], value_type());
1717 return *this;
1718 }
1719
Howard Hinnantcf823322010-12-17 14:46:43 +00001720 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1721 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001723 template<class _Tp>
1724 _LIBCPP_INLINE_VISIBILITY
1725 bool __addr_in_range(_Tp&& __t) const {
1726 const volatile void *__p = _VSTD::addressof(__t);
1727 return data() <= __p && __p <= data() + size();
1728 }
1729
Louis Dionned24191c2021-08-19 12:39:16 -04001730 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1731 void __throw_length_error() const {
1732#ifndef _LIBCPP_NO_EXCEPTIONS
1733 __basic_string_common<true>::__throw_length_error();
1734#else
1735 _VSTD::abort();
1736#endif
1737 }
1738
1739 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1740 void __throw_out_of_range() const {
1741#ifndef _LIBCPP_NO_EXCEPTIONS
1742 __basic_string_common<true>::__throw_out_of_range();
1743#else
1744 _VSTD::abort();
1745#endif
1746 }
1747
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748 friend basic_string operator+<>(const basic_string&, const basic_string&);
1749 friend basic_string operator+<>(const value_type*, const basic_string&);
1750 friend basic_string operator+<>(value_type, const basic_string&);
1751 friend basic_string operator+<>(const basic_string&, const value_type*);
1752 friend basic_string operator+<>(const basic_string&, value_type);
1753};
1754
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001755// These declarations must appear before any functions are implicitly used
1756// so that they have the correct visibility specifier.
1757#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001758 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1759# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1760 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1761# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001762#else
Louis Dionne89258142021-08-23 15:32:36 -04001763 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1764# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1765 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1766# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001767#endif
1768
1769
Louis Dionned59f8a52021-08-17 11:59:07 -04001770#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001771template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001772 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001773 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001774 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1775 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001776 >
1777basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1778 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001779
1780template<class _CharT,
1781 class _Traits,
1782 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001783 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001784 >
1785explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1786 -> basic_string<_CharT, _Traits, _Allocator>;
1787
1788template<class _CharT,
1789 class _Traits,
1790 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001791 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001792 class _Sz = typename allocator_traits<_Allocator>::size_type
1793 >
1794basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1795 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001796#endif
1797
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001799inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800void
1801basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1802{
Louis Dionneba400782020-10-02 15:02:52 -04001803#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001804 if (!__libcpp_is_constant_evaluated())
1805 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001806#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807}
1808
1809template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001810inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001812basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813{
Louis Dionneba400782020-10-02 15:02:52 -04001814#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001815 if (!__libcpp_is_constant_evaluated()) {
1816 __c_node* __c = __get_db()->__find_c_and_lock(this);
1817 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001819 const_pointer __new_last = __get_pointer() + __pos;
1820 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001822 --__p;
1823 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1824 if (__i->base() > __new_last)
1825 {
1826 (*__p)->__c_ = nullptr;
1827 if (--__c->end_ != __p)
1828 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1829 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001831 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 }
1833 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001834#else
1835 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001836#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837}
1838
1839template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001840inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001841basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001842 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001843 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001844{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001845 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846 __zero();
1847}
1848
1849template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001850inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001852#if _LIBCPP_STD_VER <= 14
1853 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1854#else
1855 _NOEXCEPT
1856#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001857: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001859 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860 __zero();
1861}
1862
1863template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001864void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1865 size_type __sz,
1866 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867{
1868 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001869 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001871 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872 {
1873 __set_short_size(__sz);
1874 __p = __get_short_pointer();
1875 }
1876 else
1877 {
1878 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001879 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880 __set_long_pointer(__p);
1881 __set_long_cap(__cap+1);
1882 __set_long_size(__sz);
1883 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001884 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 traits_type::assign(__p[__sz], value_type());
1886}
1887
1888template <class _CharT, class _Traits, class _Allocator>
1889void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001890basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891{
1892 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001893 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001895 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 {
1897 __set_short_size(__sz);
1898 __p = __get_short_pointer();
1899 }
1900 else
1901 {
1902 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001903 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904 __set_long_pointer(__p);
1905 __set_long_cap(__cap+1);
1906 __set_long_size(__sz);
1907 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001908 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 traits_type::assign(__p[__sz], value_type());
1910}
1911
1912template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001913template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001914basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001915 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001917 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001919 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920}
1921
1922template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001923inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001924basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001925 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001927 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1928 __init(__s, __n);
1929 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930}
1931
1932template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001933inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001934basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001935 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001937 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001939 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940}
1941
1942template <class _CharT, class _Traits, class _Allocator>
1943basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001944 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945{
1946 if (!__str.__is_long())
1947 __r_.first().__r = __str.__r_.first().__r;
1948 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001949 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1950 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001951 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952}
1953
1954template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001955basic_string<_CharT, _Traits, _Allocator>::basic_string(
1956 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001957 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958{
1959 if (!__str.__is_long())
1960 __r_.first().__r = __str.__r_.first().__r;
1961 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001962 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1963 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001964 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965}
1966
Martijn Vels5e7c9752020-03-04 17:52:46 -05001967template <class _CharT, class _Traits, class _Allocator>
1968void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1969 const value_type* __s, size_type __sz) {
1970 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001971 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001972 __p = __get_short_pointer();
1973 __set_short_size(__sz);
1974 } else {
1975 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001976 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001977 size_t __cap = __recommend(__sz);
1978 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1979 __set_long_pointer(__p);
1980 __set_long_cap(__cap + 1);
1981 __set_long_size(__sz);
1982 }
1983 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1984}
1985
Eric Fiselierfc92be82017-04-19 00:28:44 +00001986#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987
1988template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001989inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001990basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001991#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001992 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001993#else
1994 _NOEXCEPT
1995#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001996 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997{
1998 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001999 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002000#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002001 if (!__libcpp_is_constant_evaluated() && __is_long())
2002 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003#endif
2004}
2005
2006template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002007inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002009 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010{
Marshall Clowd2d24692014-07-17 15:32:20 +00002011 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002012 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002013 else
2014 {
2015 __r_.first().__r = __str.__r_.first().__r;
2016 __str.__zero();
2017 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01002018 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002019#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002020 if (!__libcpp_is_constant_evaluated() && __is_long())
2021 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022#endif
2023}
2024
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002025#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026
2027template <class _CharT, class _Traits, class _Allocator>
2028void
2029basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2030{
2031 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002032 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002034 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035 {
2036 __set_short_size(__n);
2037 __p = __get_short_pointer();
2038 }
2039 else
2040 {
2041 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002042 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043 __set_long_pointer(__p);
2044 __set_long_cap(__cap+1);
2045 __set_long_size(__n);
2046 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002047 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002048 traits_type::assign(__p[__n], value_type());
2049}
2050
2051template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002052inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002053basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002054 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055{
2056 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002057 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058}
2059
2060template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002061template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002062basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002063 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064{
2065 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002066 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067}
2068
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002070basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2071 size_type __pos, size_type __n,
2072 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002073 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074{
2075 size_type __str_sz = __str.size();
2076 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002077 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002078 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002079 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080}
2081
2082template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002083inline
Marshall Clow83445802016-04-07 18:13:41 +00002084basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002085 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002086 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002087{
2088 size_type __str_sz = __str.size();
2089 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002090 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002091 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002092 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002093}
2094
2095template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002096template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002097basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002098 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002099 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002100{
Marshall Clowe46031a2018-07-02 18:41:15 +00002101 __self_view __sv0 = __t;
2102 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002103 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002104 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002105}
2106
2107template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002108template <class _Tp, class>
2109basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002110 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002111{
Marshall Clowe46031a2018-07-02 18:41:15 +00002112 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002113 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002114 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002115}
2116
2117template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002118template <class _Tp, class>
2119basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002120 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002121{
Marshall Clowe46031a2018-07-02 18:41:15 +00002122 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002123 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002124 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002125}
2126
2127template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002129__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002131 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2132>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2134{
2135 __zero();
2136#ifndef _LIBCPP_NO_EXCEPTIONS
2137 try
2138 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002139#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140 for (; __first != __last; ++__first)
2141 push_back(*__first);
2142#ifndef _LIBCPP_NO_EXCEPTIONS
2143 }
2144 catch (...)
2145 {
2146 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002147 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148 throw;
2149 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002150#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151}
2152
2153template <class _CharT, class _Traits, class _Allocator>
2154template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002155__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002157 __is_cpp17_forward_iterator<_ForwardIterator>::value
2158>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2160{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002161 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002163 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002165 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166 {
2167 __set_short_size(__sz);
2168 __p = __get_short_pointer();
2169 }
2170 else
2171 {
2172 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002173 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 __set_long_pointer(__p);
2175 __set_long_cap(__cap+1);
2176 __set_long_size(__sz);
2177 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002178
2179#ifndef _LIBCPP_NO_EXCEPTIONS
2180 try
2181 {
2182#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002183 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 traits_type::assign(*__p, *__first);
2185 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002186#ifndef _LIBCPP_NO_EXCEPTIONS
2187 }
2188 catch (...)
2189 {
2190 if (__is_long())
2191 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2192 throw;
2193 }
2194#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195}
2196
2197template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002198template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002199inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002201 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202{
2203 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002204 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205}
2206
2207template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002208template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002209inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2211 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002212 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213{
2214 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002215 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216}
2217
Eric Fiselierfc92be82017-04-19 00:28:44 +00002218#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002219
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002221inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002222basic_string<_CharT, _Traits, _Allocator>::basic_string(
2223 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002224 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225{
2226 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002227 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228}
2229
2230template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002231inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002232
Eric Fiselier812882b2017-02-17 01:17:10 +00002233basic_string<_CharT, _Traits, _Allocator>::basic_string(
2234 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002235 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236{
2237 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002238 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239}
2240
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002241#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002242
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2245{
Louis Dionneba400782020-10-02 15:02:52 -04002246#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002247 if (!__libcpp_is_constant_evaluated())
2248 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002249#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002251 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252}
2253
2254template <class _CharT, class _Traits, class _Allocator>
2255void
2256basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2257 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002258 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 +00002259{
2260 size_type __ms = max_size();
2261 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002262 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 pointer __old_p = __get_pointer();
2264 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002265 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002267 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 __invalidate_all_iterators();
2269 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002270 traits_type::copy(_VSTD::__to_address(__p),
2271 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002273 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2275 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002276 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2277 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002279 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280 __set_long_pointer(__p);
2281 __set_long_cap(__cap+1);
2282 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2283 __set_long_size(__old_sz);
2284 traits_type::assign(__p[__old_sz], value_type());
2285}
2286
2287template <class _CharT, class _Traits, class _Allocator>
2288void
2289basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2290 size_type __n_copy, size_type __n_del, size_type __n_add)
2291{
2292 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002293 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002294 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295 pointer __old_p = __get_pointer();
2296 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002297 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002299 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300 __invalidate_all_iterators();
2301 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002302 traits_type::copy(_VSTD::__to_address(__p),
2303 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2305 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002306 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2307 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002308 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002310 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311 __set_long_pointer(__p);
2312 __set_long_cap(__cap+1);
2313}
2314
2315// assign
2316
2317template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002318template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002319basic_string<_CharT, _Traits, _Allocator>&
2320basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002321 const value_type* __s, size_type __n) {
2322 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2323 if (__n < __cap) {
2324 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2325 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2326 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2327 traits_type::assign(__p[__n], value_type());
2328 __invalidate_iterators_past(__n);
2329 } else {
2330 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2331 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2332 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002333 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002334}
2335
2336template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002338basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2339 const value_type* __s, size_type __n) {
2340 size_type __cap = capacity();
2341 if (__cap >= __n) {
2342 value_type* __p = _VSTD::__to_address(__get_pointer());
2343 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002344 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002345 } else {
2346 size_type __sz = size();
2347 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002348 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002349 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002350}
2351
2352template <class _CharT, class _Traits, class _Allocator>
2353basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002354basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002356 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002357 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002358 ? __assign_short(__s, __n)
2359 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360}
2361
2362template <class _CharT, class _Traits, class _Allocator>
2363basic_string<_CharT, _Traits, _Allocator>&
2364basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2365{
2366 size_type __cap = capacity();
2367 if (__cap < __n)
2368 {
2369 size_type __sz = size();
2370 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2371 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002372 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002374 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375}
2376
2377template <class _CharT, class _Traits, class _Allocator>
2378basic_string<_CharT, _Traits, _Allocator>&
2379basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2380{
2381 pointer __p;
2382 if (__is_long())
2383 {
2384 __p = __get_long_pointer();
2385 __set_long_size(1);
2386 }
2387 else
2388 {
2389 __p = __get_short_pointer();
2390 __set_short_size(1);
2391 }
2392 traits_type::assign(*__p, __c);
2393 traits_type::assign(*++__p, value_type());
2394 __invalidate_iterators_past(1);
2395 return *this;
2396}
2397
2398template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002399basic_string<_CharT, _Traits, _Allocator>&
2400basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2401{
Martijn Vels596e3de2020-02-26 15:55:49 -05002402 if (this != &__str) {
2403 __copy_assign_alloc(__str);
2404 if (!__is_long()) {
2405 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002406 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002407 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002408 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002409 }
2410 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002411 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002412 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002413 }
2414 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002415}
2416
Eric Fiselierfc92be82017-04-19 00:28:44 +00002417#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002418
2419template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002420inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002421void
2422basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002423 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002424{
2425 if (__alloc() != __str.__alloc())
2426 assign(__str);
2427 else
2428 __move_assign(__str, true_type());
2429}
2430
2431template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002432inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002433void
2434basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002435#if _LIBCPP_STD_VER > 14
2436 _NOEXCEPT
2437#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002438 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002439#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002440{
marshall2ed41622019-11-27 07:13:00 -08002441 if (__is_long()) {
2442 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2443 __get_long_cap());
2444#if _LIBCPP_STD_VER <= 14
2445 if (!is_nothrow_move_assignable<allocator_type>::value) {
2446 __set_short_size(0);
2447 traits_type::assign(__get_short_pointer()[0], value_type());
2448 }
2449#endif
2450 }
2451 __move_assign_alloc(__str);
2452 __r_.first() = __str.__r_.first();
2453 __str.__set_short_size(0);
2454 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002455}
2456
2457template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002458inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002459basic_string<_CharT, _Traits, _Allocator>&
2460basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002461 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002462{
2463 __move_assign(__str, integral_constant<bool,
2464 __alloc_traits::propagate_on_container_move_assignment::value>());
2465 return *this;
2466}
2467
2468#endif
2469
2470template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002472__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002474 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002476>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2478{
Marshall Clow958362f2016-09-05 01:54:30 +00002479 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002480 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002481 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482}
2483
2484template <class _CharT, class _Traits, class _Allocator>
2485template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002486__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002488 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002490>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2492{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002494 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2495 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2496
2497 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2498 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002500 if (__cap < __n)
2501 {
2502 size_type __sz = size();
2503 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2504 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002505 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002506 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002507 traits_type::assign(*__p, *__first);
2508 traits_type::assign(*__p, value_type());
2509 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002510 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511 }
2512 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002513 {
2514 const basic_string __temp(__first, __last, __alloc());
2515 assign(__temp.data(), __temp.size());
2516 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517 return *this;
2518}
2519
2520template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521basic_string<_CharT, _Traits, _Allocator>&
2522basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2523{
2524 size_type __sz = __str.size();
2525 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002526 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002527 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528}
2529
2530template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002531template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002532__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002533<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002534 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2535 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002536 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002537>
Marshall Clow82513342016-09-24 22:45:42 +00002538basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002539{
Marshall Clow82513342016-09-24 22:45:42 +00002540 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002541 size_type __sz = __sv.size();
2542 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002543 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002544 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2545}
2546
2547
2548template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002550basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2551 return __assign_external(__s, traits_type::length(__s));
2552}
2553
2554template <class _CharT, class _Traits, class _Allocator>
2555basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002556basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002558 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002559 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002560 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002561 ? __assign_short(__s, traits_type::length(__s))
2562 : __assign_external(__s, traits_type::length(__s)))
2563 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565// append
2566
2567template <class _CharT, class _Traits, class _Allocator>
2568basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002569basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002571 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572 size_type __cap = capacity();
2573 size_type __sz = size();
2574 if (__cap - __sz >= __n)
2575 {
2576 if (__n)
2577 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002578 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579 traits_type::copy(__p + __sz, __s, __n);
2580 __sz += __n;
2581 __set_size(__sz);
2582 traits_type::assign(__p[__sz], value_type());
2583 }
2584 }
2585 else
2586 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2587 return *this;
2588}
2589
2590template <class _CharT, class _Traits, class _Allocator>
2591basic_string<_CharT, _Traits, _Allocator>&
2592basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2593{
2594 if (__n)
2595 {
2596 size_type __cap = capacity();
2597 size_type __sz = size();
2598 if (__cap - __sz < __n)
2599 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2600 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002601 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602 __sz += __n;
2603 __set_size(__sz);
2604 traits_type::assign(__p[__sz], value_type());
2605 }
2606 return *this;
2607}
2608
2609template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002610inline void
2611basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2612{
2613 if (__n)
2614 {
2615 size_type __cap = capacity();
2616 size_type __sz = size();
2617 if (__cap - __sz < __n)
2618 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2619 pointer __p = __get_pointer();
2620 __sz += __n;
2621 __set_size(__sz);
2622 traits_type::assign(__p[__sz], value_type());
2623 }
2624}
2625
2626template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627void
2628basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2629{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002630 bool __is_short = !__is_long();
2631 size_type __cap;
2632 size_type __sz;
2633 if (__is_short)
2634 {
2635 __cap = __min_cap - 1;
2636 __sz = __get_short_size();
2637 }
2638 else
2639 {
2640 __cap = __get_long_cap() - 1;
2641 __sz = __get_long_size();
2642 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002644 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002646 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002647 }
2648 pointer __p;
2649 if (__is_short)
2650 {
2651 __p = __get_short_pointer() + __sz;
2652 __set_short_size(__sz+1);
2653 }
2654 else
2655 {
2656 __p = __get_long_pointer() + __sz;
2657 __set_long_size(__sz+1);
2658 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 traits_type::assign(*__p, __c);
2660 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661}
2662
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663template <class _CharT, class _Traits, class _Allocator>
2664template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002665__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002666<
2667 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2668 basic_string<_CharT, _Traits, _Allocator>&
2669>
2670basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002671 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672{
2673 size_type __sz = size();
2674 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002675 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676 if (__n)
2677 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002678 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2679 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002680 {
2681 if (__cap - __sz < __n)
2682 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2683 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002684 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002685 traits_type::assign(*__p, *__first);
2686 traits_type::assign(*__p, value_type());
2687 __set_size(__sz + __n);
2688 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002689 else
2690 {
2691 const basic_string __temp(__first, __last, __alloc());
2692 append(__temp.data(), __temp.size());
2693 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 }
2695 return *this;
2696}
2697
2698template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002699inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700basic_string<_CharT, _Traits, _Allocator>&
2701basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2702{
2703 return append(__str.data(), __str.size());
2704}
2705
2706template <class _CharT, class _Traits, class _Allocator>
2707basic_string<_CharT, _Traits, _Allocator>&
2708basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2709{
2710 size_type __sz = __str.size();
2711 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002712 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002713 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714}
2715
2716template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002717template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002718 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002719 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002720 __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 +00002721 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002722 >
Marshall Clow82513342016-09-24 22:45:42 +00002723basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002724{
Marshall Clow82513342016-09-24 22:45:42 +00002725 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002726 size_type __sz = __sv.size();
2727 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002728 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002729 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2730}
2731
2732template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002734basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002736 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737 return append(__s, traits_type::length(__s));
2738}
2739
2740// insert
2741
2742template <class _CharT, class _Traits, class _Allocator>
2743basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002744basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002746 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 size_type __sz = size();
2748 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002749 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 size_type __cap = capacity();
2751 if (__cap - __sz >= __n)
2752 {
2753 if (__n)
2754 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002755 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 size_type __n_move = __sz - __pos;
2757 if (__n_move != 0)
2758 {
2759 if (__p + __pos <= __s && __s < __p + __sz)
2760 __s += __n;
2761 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2762 }
2763 traits_type::move(__p + __pos, __s, __n);
2764 __sz += __n;
2765 __set_size(__sz);
2766 traits_type::assign(__p[__sz], value_type());
2767 }
2768 }
2769 else
2770 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2771 return *this;
2772}
2773
2774template <class _CharT, class _Traits, class _Allocator>
2775basic_string<_CharT, _Traits, _Allocator>&
2776basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2777{
2778 size_type __sz = size();
2779 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002780 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 if (__n)
2782 {
2783 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002784 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785 if (__cap - __sz >= __n)
2786 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002787 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 size_type __n_move = __sz - __pos;
2789 if (__n_move != 0)
2790 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2791 }
2792 else
2793 {
2794 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002795 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796 }
2797 traits_type::assign(__p + __pos, __n, __c);
2798 __sz += __n;
2799 __set_size(__sz);
2800 traits_type::assign(__p[__sz], value_type());
2801 }
2802 return *this;
2803}
2804
2805template <class _CharT, class _Traits, class _Allocator>
2806template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002807__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002809 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002810 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002811>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2813{
Nikolas Klausereed25832021-12-15 01:32:30 +01002814 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2815 "string::insert(iterator, range) called with an iterator not"
2816 " referring to this string");
2817 const basic_string __temp(__first, __last, __alloc());
2818 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819}
2820
2821template <class _CharT, class _Traits, class _Allocator>
2822template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002823__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002825 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002827>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2829{
Nikolas Klausereed25832021-12-15 01:32:30 +01002830 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2831 "string::insert(iterator, range) called with an iterator not"
2832 " referring to this string");
2833
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002835 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 if (__n)
2837 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002838 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2839 !__addr_in_range(*__first))
2840 {
2841 size_type __sz = size();
2842 size_type __cap = capacity();
2843 value_type* __p;
2844 if (__cap - __sz >= __n)
2845 {
2846 __p = _VSTD::__to_address(__get_pointer());
2847 size_type __n_move = __sz - __ip;
2848 if (__n_move != 0)
2849 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2850 }
2851 else
2852 {
2853 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2854 __p = _VSTD::__to_address(__get_long_pointer());
2855 }
2856 __sz += __n;
2857 __set_size(__sz);
2858 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002859 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002860 traits_type::assign(*__p, *__first);
2861 }
2862 else
Marshall Clow958362f2016-09-05 01:54:30 +00002863 {
2864 const basic_string __temp(__first, __last, __alloc());
2865 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2866 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 }
2868 return begin() + __ip;
2869}
2870
2871template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002872inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873basic_string<_CharT, _Traits, _Allocator>&
2874basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2875{
2876 return insert(__pos1, __str.data(), __str.size());
2877}
2878
2879template <class _CharT, class _Traits, class _Allocator>
2880basic_string<_CharT, _Traits, _Allocator>&
2881basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2882 size_type __pos2, size_type __n)
2883{
2884 size_type __str_sz = __str.size();
2885 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002886 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002887 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888}
2889
2890template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002891template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002892__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002893<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002894 __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 +00002895 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002896>
Marshall Clow82513342016-09-24 22:45:42 +00002897basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002898 size_type __pos2, size_type __n)
2899{
Marshall Clow82513342016-09-24 22:45:42 +00002900 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002901 size_type __str_sz = __sv.size();
2902 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002903 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002904 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2905}
2906
2907template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002909basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002911 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 return insert(__pos, __s, traits_type::length(__s));
2913}
2914
2915template <class _CharT, class _Traits, class _Allocator>
2916typename basic_string<_CharT, _Traits, _Allocator>::iterator
2917basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2918{
2919 size_type __ip = static_cast<size_type>(__pos - begin());
2920 size_type __sz = size();
2921 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002922 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 if (__cap == __sz)
2924 {
2925 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002926 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 }
2928 else
2929 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002930 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 size_type __n_move = __sz - __ip;
2932 if (__n_move != 0)
2933 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2934 }
2935 traits_type::assign(__p[__ip], __c);
2936 traits_type::assign(__p[++__sz], value_type());
2937 __set_size(__sz);
2938 return begin() + static_cast<difference_type>(__ip);
2939}
2940
2941template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002942inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943typename basic_string<_CharT, _Traits, _Allocator>::iterator
2944basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2945{
Nikolas Klausereed25832021-12-15 01:32:30 +01002946 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2947 "string::insert(iterator, n, value) called with an iterator not"
2948 " referring to this string");
2949 difference_type __p = __pos - begin();
2950 insert(static_cast<size_type>(__p), __n, __c);
2951 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952}
2953
2954// replace
2955
2956template <class _CharT, class _Traits, class _Allocator>
2957basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002958basic_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 +00002959 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002961 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 size_type __sz = size();
2963 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002964 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002965 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 size_type __cap = capacity();
2967 if (__cap - __sz + __n1 >= __n2)
2968 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002969 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970 if (__n1 != __n2)
2971 {
2972 size_type __n_move = __sz - __pos - __n1;
2973 if (__n_move != 0)
2974 {
2975 if (__n1 > __n2)
2976 {
2977 traits_type::move(__p + __pos, __s, __n2);
2978 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002979 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980 }
2981 if (__p + __pos < __s && __s < __p + __sz)
2982 {
2983 if (__p + __pos + __n1 <= __s)
2984 __s += __n2 - __n1;
2985 else // __p + __pos < __s < __p + __pos + __n1
2986 {
2987 traits_type::move(__p + __pos, __s, __n1);
2988 __pos += __n1;
2989 __s += __n2;
2990 __n2 -= __n1;
2991 __n1 = 0;
2992 }
2993 }
2994 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2995 }
2996 }
2997 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002998 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999 }
3000 else
3001 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3002 return *this;
3003}
3004
3005template <class _CharT, class _Traits, class _Allocator>
3006basic_string<_CharT, _Traits, _Allocator>&
3007basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3008{
3009 size_type __sz = size();
3010 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003011 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003012 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003014 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015 if (__cap - __sz + __n1 >= __n2)
3016 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003017 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018 if (__n1 != __n2)
3019 {
3020 size_type __n_move = __sz - __pos - __n1;
3021 if (__n_move != 0)
3022 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3023 }
3024 }
3025 else
3026 {
3027 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003028 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029 }
3030 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003031 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032}
3033
3034template <class _CharT, class _Traits, class _Allocator>
3035template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003036__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003038 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003040>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003041basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042 _InputIterator __j1, _InputIterator __j2)
3043{
Marshall Clow958362f2016-09-05 01:54:30 +00003044 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003045 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046}
3047
3048template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003049inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050basic_string<_CharT, _Traits, _Allocator>&
3051basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3052{
3053 return replace(__pos1, __n1, __str.data(), __str.size());
3054}
3055
3056template <class _CharT, class _Traits, class _Allocator>
3057basic_string<_CharT, _Traits, _Allocator>&
3058basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3059 size_type __pos2, size_type __n2)
3060{
3061 size_type __str_sz = __str.size();
3062 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003063 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003064 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065}
3066
3067template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003068template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003069__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003070<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003071 __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 +00003072 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003073>
Marshall Clow82513342016-09-24 22:45:42 +00003074basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003075 size_type __pos2, size_type __n2)
3076{
Marshall Clow82513342016-09-24 22:45:42 +00003077 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003078 size_type __str_sz = __sv.size();
3079 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003080 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003081 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3082}
3083
3084template <class _CharT, class _Traits, class _Allocator>
3085basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003086basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003088 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089 return replace(__pos, __n1, __s, traits_type::length(__s));
3090}
3091
3092template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003093inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003095basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096{
3097 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3098 __str.data(), __str.size());
3099}
3100
3101template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003102inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003104basic_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 +00003105{
3106 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3107}
3108
3109template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003110inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003112basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113{
3114 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3115}
3116
3117template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003118inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003120basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121{
3122 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3123}
3124
3125// erase
3126
Martijn Velsa81fc792020-02-26 13:25:43 -05003127// 'externally instantiated' erase() implementation, called when __n != npos.
3128// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003130void
3131basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3132 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134 if (__n)
3135 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003136 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003137 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003138 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139 size_type __n_move = __sz - __pos - __n;
3140 if (__n_move != 0)
3141 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003142 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003144}
3145
3146template <class _CharT, class _Traits, class _Allocator>
3147basic_string<_CharT, _Traits, _Allocator>&
3148basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3149 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003150 if (__pos > size())
3151 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003152 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())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003267 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003268
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003269 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3270 // and later (since P0966R1), however we provide consistent behavior in all Standard
3271 // modes because this function is instantiated in the shared library.
3272 if (__requested_capacity <= capacity())
3273 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003274
3275 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3276 __target_capacity = __recommend(__target_capacity);
3277 if (__target_capacity == capacity()) return;
3278
3279 __shrink_or_extend(__target_capacity);
3280}
3281
3282template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003283inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003284void
3285basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3286{
3287 size_type __target_capacity = __recommend(size());
3288 if (__target_capacity == capacity()) return;
3289
3290 __shrink_or_extend(__target_capacity);
3291}
3292
3293template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003294inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003295void
3296basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3297{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298 size_type __cap = capacity();
3299 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003300
3301 pointer __new_data, __p;
3302 bool __was_long, __now_long;
3303 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003305 __was_long = true;
3306 __now_long = false;
3307 __new_data = __get_short_pointer();
3308 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003310 else
3311 {
3312 if (__target_capacity > __cap)
3313 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3314 else
3315 {
3316 #ifndef _LIBCPP_NO_EXCEPTIONS
3317 try
3318 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003319 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003320 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3321 #ifndef _LIBCPP_NO_EXCEPTIONS
3322 }
3323 catch (...)
3324 {
3325 return;
3326 }
3327 #else // _LIBCPP_NO_EXCEPTIONS
3328 if (__new_data == nullptr)
3329 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003330 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003331 }
3332 __now_long = true;
3333 __was_long = __is_long();
3334 __p = __get_pointer();
3335 }
3336 traits_type::copy(_VSTD::__to_address(__new_data),
3337 _VSTD::__to_address(__p), size()+1);
3338 if (__was_long)
3339 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3340 if (__now_long)
3341 {
3342 __set_long_cap(__target_capacity+1);
3343 __set_long_size(__sz);
3344 __set_long_pointer(__new_data);
3345 }
3346 else
3347 __set_short_size(__sz);
3348 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349}
3350
3351template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003352inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003354basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003355{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003356 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357 return *(data() + __pos);
3358}
3359
3360template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003361inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003362typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003363basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003365 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003366 return *(__get_pointer() + __pos);
3367}
3368
3369template <class _CharT, class _Traits, class _Allocator>
3370typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3371basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3372{
3373 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003374 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003375 return (*this)[__n];
3376}
3377
3378template <class _CharT, class _Traits, class _Allocator>
3379typename basic_string<_CharT, _Traits, _Allocator>::reference
3380basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3381{
3382 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003383 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384 return (*this)[__n];
3385}
3386
3387template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003388inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003390basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003392 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393 return *__get_pointer();
3394}
3395
3396template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003397inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003399basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003401 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003402 return *data();
3403}
3404
3405template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003406inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003408basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003410 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411 return *(__get_pointer() + size() - 1);
3412}
3413
3414template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003415inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003417basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003419 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420 return *(data() + size() - 1);
3421}
3422
3423template <class _CharT, class _Traits, class _Allocator>
3424typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003425basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426{
3427 size_type __sz = size();
3428 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003429 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003430 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431 traits_type::copy(__s, data() + __pos, __rlen);
3432 return __rlen;
3433}
3434
3435template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003436inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437basic_string<_CharT, _Traits, _Allocator>
3438basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3439{
3440 return basic_string(*this, __pos, __n, __alloc());
3441}
3442
3443template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003444inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445void
3446basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003447#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003448 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003449#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003450 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003451 __is_nothrow_swappable<allocator_type>::value)
3452#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453{
Louis Dionneba400782020-10-02 15:02:52 -04003454#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003455 if (!__libcpp_is_constant_evaluated()) {
3456 if (!__is_long())
3457 __get_db()->__invalidate_all(this);
3458 if (!__str.__is_long())
3459 __get_db()->__invalidate_all(&__str);
3460 __get_db()->swap(this, &__str);
3461 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003462#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003463 _LIBCPP_ASSERT(
3464 __alloc_traits::propagate_on_container_swap::value ||
3465 __alloc_traits::is_always_equal::value ||
3466 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003467 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003468 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469}
3470
3471// find
3472
3473template <class _Traits>
3474struct _LIBCPP_HIDDEN __traits_eq
3475{
3476 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003477 _LIBCPP_INLINE_VISIBILITY
3478 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3479 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480};
3481
3482template<class _CharT, class _Traits, class _Allocator>
3483typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003484basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003485 size_type __pos,
3486 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003488 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003489 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003490 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491}
3492
3493template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003494inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003496basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3497 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003499 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003500 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501}
3502
3503template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003504template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003505__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003506<
3507 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3508 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003509>
Marshall Clowe46031a2018-07-02 18:41:15 +00003510basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003511 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003512{
Marshall Clowe46031a2018-07-02 18:41:15 +00003513 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003514 return __str_find<value_type, size_type, traits_type, npos>
3515 (data(), size(), __sv.data(), __pos, __sv.size());
3516}
3517
3518template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003519inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003520typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003521basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003522 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003524 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003525 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003526 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527}
3528
3529template<class _CharT, class _Traits, class _Allocator>
3530typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003531basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3532 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003534 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003535 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536}
3537
3538// rfind
3539
3540template<class _CharT, class _Traits, class _Allocator>
3541typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003542basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003543 size_type __pos,
3544 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003546 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003547 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003548 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549}
3550
3551template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003552inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003554basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3555 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003557 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003558 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559}
3560
3561template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003562template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003563__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003564<
3565 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3566 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003567>
Marshall Clowe46031a2018-07-02 18:41:15 +00003568basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003569 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003570{
Marshall Clowe46031a2018-07-02 18:41:15 +00003571 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003572 return __str_rfind<value_type, size_type, traits_type, npos>
3573 (data(), size(), __sv.data(), __pos, __sv.size());
3574}
3575
3576template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003577inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003578typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003579basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003580 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003582 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003583 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003584 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585}
3586
3587template<class _CharT, class _Traits, class _Allocator>
3588typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003589basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3590 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003592 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003593 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594}
3595
3596// find_first_of
3597
3598template<class _CharT, class _Traits, class _Allocator>
3599typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003600basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003601 size_type __pos,
3602 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003604 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003605 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003606 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607}
3608
3609template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003610inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003612basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3613 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003615 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003616 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617}
3618
3619template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003620template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003621__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003622<
3623 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3624 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003625>
Marshall Clowe46031a2018-07-02 18:41:15 +00003626basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003627 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003628{
Marshall Clowe46031a2018-07-02 18:41:15 +00003629 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003630 return __str_find_first_of<value_type, size_type, traits_type, npos>
3631 (data(), size(), __sv.data(), __pos, __sv.size());
3632}
3633
3634template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003635inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003636typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003637basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003638 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003640 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003641 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003642 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643}
3644
3645template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003646inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003648basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3649 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650{
3651 return find(__c, __pos);
3652}
3653
3654// find_last_of
3655
3656template<class _CharT, class _Traits, class _Allocator>
3657typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003658basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003659 size_type __pos,
3660 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003662 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003663 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003664 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665}
3666
3667template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003668inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003670basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3671 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003673 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003674 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675}
3676
3677template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003678template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003679__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003680<
3681 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3682 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003683>
Marshall Clowe46031a2018-07-02 18:41:15 +00003684basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003685 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003686{
Marshall Clowe46031a2018-07-02 18:41:15 +00003687 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003688 return __str_find_last_of<value_type, size_type, traits_type, npos>
3689 (data(), size(), __sv.data(), __pos, __sv.size());
3690}
3691
3692template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003693inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003695basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003696 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003698 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003699 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003700 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701}
3702
3703template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003704inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003706basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3707 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
3709 return rfind(__c, __pos);
3710}
3711
3712// find_first_not_of
3713
3714template<class _CharT, class _Traits, class _Allocator>
3715typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003716basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003717 size_type __pos,
3718 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003720 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003721 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003722 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723}
3724
3725template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003726inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003728basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3729 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003731 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003732 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733}
3734
3735template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003736template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003737__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003738<
3739 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3740 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003741>
Marshall Clowe46031a2018-07-02 18:41:15 +00003742basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003743 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003744{
Marshall Clowe46031a2018-07-02 18:41:15 +00003745 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003746 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3747 (data(), size(), __sv.data(), __pos, __sv.size());
3748}
3749
3750template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003751inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003752typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003753basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003754 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003756 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003757 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003758 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759}
3760
3761template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003762inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003764basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3765 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003767 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003768 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769}
3770
3771// find_last_not_of
3772
3773template<class _CharT, class _Traits, class _Allocator>
3774typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003775basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003776 size_type __pos,
3777 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003779 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003780 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003781 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003782}
3783
3784template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003785inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003787basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3788 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003790 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003791 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792}
3793
3794template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003795template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003796__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003797<
3798 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3799 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003800>
Marshall Clowe46031a2018-07-02 18:41:15 +00003801basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003802 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003803{
Marshall Clowe46031a2018-07-02 18:41:15 +00003804 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003805 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3806 (data(), size(), __sv.data(), __pos, __sv.size());
3807}
3808
3809template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003810inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003811typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003812basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003813 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003815 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003816 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003817 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818}
3819
3820template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003821inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003823basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3824 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003826 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003827 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828}
3829
3830// compare
3831
3832template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003833template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003834__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003835<
3836 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3837 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003838>
zoecarver1997e0a2021-02-05 11:54:47 -08003839basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840{
Marshall Clowe46031a2018-07-02 18:41:15 +00003841 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003842 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003843 size_t __rhs_sz = __sv.size();
3844 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003845 _VSTD::min(__lhs_sz, __rhs_sz));
3846 if (__result != 0)
3847 return __result;
3848 if (__lhs_sz < __rhs_sz)
3849 return -1;
3850 if (__lhs_sz > __rhs_sz)
3851 return 1;
3852 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853}
3854
3855template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003856inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003858basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003860 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861}
3862
3863template <class _CharT, class _Traits, class _Allocator>
3864int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003865basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3866 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003867 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003868 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003870 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003871 size_type __sz = size();
3872 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003873 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003874 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3875 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876 if (__r == 0)
3877 {
3878 if (__rlen < __n2)
3879 __r = -1;
3880 else if (__rlen > __n2)
3881 __r = 1;
3882 }
3883 return __r;
3884}
3885
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003886template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003887template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003888__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003889<
3890 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3891 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003892>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003893basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3894 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003895 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003896{
Marshall Clowe46031a2018-07-02 18:41:15 +00003897 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003898 return compare(__pos1, __n1, __sv.data(), __sv.size());
3899}
3900
3901template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003902inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003903int
3904basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3905 size_type __n1,
3906 const basic_string& __str) const
3907{
3908 return compare(__pos1, __n1, __str.data(), __str.size());
3909}
3910
3911template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003912template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003913__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003914<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003915 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3916 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003917 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003918>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003919basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3920 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003921 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003922 size_type __pos2,
3923 size_type __n2) const
3924{
Marshall Clow82513342016-09-24 22:45:42 +00003925 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003926 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3927}
3928
3929template <class _CharT, class _Traits, class _Allocator>
3930int
3931basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3932 size_type __n1,
3933 const basic_string& __str,
3934 size_type __pos2,
3935 size_type __n2) const
3936{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003937 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003938}
3939
3940template <class _CharT, class _Traits, class _Allocator>
3941int
3942basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3943{
3944 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3945 return compare(0, npos, __s, traits_type::length(__s));
3946}
3947
3948template <class _CharT, class _Traits, class _Allocator>
3949int
3950basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3951 size_type __n1,
3952 const value_type* __s) const
3953{
3954 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3955 return compare(__pos1, __n1, __s, traits_type::length(__s));
3956}
3957
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958// __invariants
3959
3960template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003961inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003962bool
3963basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3964{
3965 if (size() > capacity())
3966 return false;
3967 if (capacity() < __min_cap - 1)
3968 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003969 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003970 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003971 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972 return false;
3973 return true;
3974}
3975
Vedant Kumar55e007e2018-03-08 21:15:26 +00003976// __clear_and_shrink
3977
3978template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003979inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003980void
Marshall Clowe60a7182018-05-29 17:04:37 +00003981basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003982{
3983 clear();
3984 if(__is_long())
3985 {
3986 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3987 __set_long_cap(0);
3988 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003989 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003990 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003991}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003992
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993// operator==
3994
3995template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003996inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997bool
3998operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003999 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004001 size_t __lhs_sz = __lhs.size();
4002 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4003 __rhs.data(),
4004 __lhs_sz) == 0;
4005}
4006
4007template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004008inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004009bool
4010operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4011 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4012{
4013 size_t __lhs_sz = __lhs.size();
4014 if (__lhs_sz != __rhs.size())
4015 return false;
4016 const char* __lp = __lhs.data();
4017 const char* __rp = __rhs.data();
4018 if (__lhs.__is_long())
4019 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4020 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4021 if (*__lp != *__rp)
4022 return false;
4023 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024}
4025
4026template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004027inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004029operator==(const _CharT* __lhs,
4030 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004032 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4033 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4034 size_t __lhs_len = _Traits::length(__lhs);
4035 if (__lhs_len != __rhs.size()) return false;
4036 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037}
4038
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004042operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4043 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004045 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4046 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4047 size_t __rhs_len = _Traits::length(__rhs);
4048 if (__rhs_len != __lhs.size()) return false;
4049 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050}
4051
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004052template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004053inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004054bool
4055operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004056 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057{
4058 return !(__lhs == __rhs);
4059}
4060
4061template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004062inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004064operator!=(const _CharT* __lhs,
4065 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066{
4067 return !(__lhs == __rhs);
4068}
4069
4070template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004071inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004073operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4074 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075{
4076 return !(__lhs == __rhs);
4077}
4078
4079// operator<
4080
4081template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004082inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083bool
4084operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004085 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004087 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088}
4089
4090template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004091inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004093operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4094 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004096 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097}
4098
4099template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004100inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004102operator< (const _CharT* __lhs,
4103 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104{
4105 return __rhs.compare(__lhs) > 0;
4106}
4107
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108// operator>
4109
4110template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004111inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112bool
4113operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004114 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115{
4116 return __rhs < __lhs;
4117}
4118
4119template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004120inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004122operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4123 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124{
4125 return __rhs < __lhs;
4126}
4127
4128template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004129inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004131operator> (const _CharT* __lhs,
4132 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133{
4134 return __rhs < __lhs;
4135}
4136
4137// operator<=
4138
4139template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004140inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141bool
4142operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004143 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144{
4145 return !(__rhs < __lhs);
4146}
4147
4148template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004149inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004151operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4152 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153{
4154 return !(__rhs < __lhs);
4155}
4156
4157template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004158inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004160operator<=(const _CharT* __lhs,
4161 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162{
4163 return !(__rhs < __lhs);
4164}
4165
4166// operator>=
4167
4168template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004169inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004170bool
4171operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004172 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173{
4174 return !(__lhs < __rhs);
4175}
4176
4177template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004178inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004180operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4181 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182{
4183 return !(__lhs < __rhs);
4184}
4185
4186template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004187inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004189operator>=(const _CharT* __lhs,
4190 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191{
4192 return !(__lhs < __rhs);
4193}
4194
4195// operator +
4196
4197template<class _CharT, class _Traits, class _Allocator>
4198basic_string<_CharT, _Traits, _Allocator>
4199operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4200 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4201{
4202 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4203 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4204 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4205 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4206 __r.append(__rhs.data(), __rhs_sz);
4207 return __r;
4208}
4209
4210template<class _CharT, class _Traits, class _Allocator>
4211basic_string<_CharT, _Traits, _Allocator>
4212operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4213{
4214 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4215 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4216 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4217 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4218 __r.append(__rhs.data(), __rhs_sz);
4219 return __r;
4220}
4221
4222template<class _CharT, class _Traits, class _Allocator>
4223basic_string<_CharT, _Traits, _Allocator>
4224operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4225{
4226 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4227 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4228 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4229 __r.append(__rhs.data(), __rhs_sz);
4230 return __r;
4231}
4232
4233template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004234inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235basic_string<_CharT, _Traits, _Allocator>
4236operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4237{
4238 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4239 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4240 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4241 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4242 __r.append(__rhs, __rhs_sz);
4243 return __r;
4244}
4245
4246template<class _CharT, class _Traits, class _Allocator>
4247basic_string<_CharT, _Traits, _Allocator>
4248operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4249{
4250 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4251 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4252 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4253 __r.push_back(__rhs);
4254 return __r;
4255}
4256
Eric Fiselierfc92be82017-04-19 00:28:44 +00004257#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258
4259template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004260inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261basic_string<_CharT, _Traits, _Allocator>
4262operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4263{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004264 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265}
4266
4267template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004268inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269basic_string<_CharT, _Traits, _Allocator>
4270operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4271{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004272 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273}
4274
4275template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004277basic_string<_CharT, _Traits, _Allocator>
4278operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4279{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004280 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281}
4282
4283template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004284inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285basic_string<_CharT, _Traits, _Allocator>
4286operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4287{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004288 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289}
4290
4291template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004292inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293basic_string<_CharT, _Traits, _Allocator>
4294operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4295{
4296 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004297 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004298}
4299
4300template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302basic_string<_CharT, _Traits, _Allocator>
4303operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4304{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004305 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306}
4307
4308template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310basic_string<_CharT, _Traits, _Allocator>
4311operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4312{
4313 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004314 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315}
4316
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004317#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318
4319// swap
4320
4321template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004322inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004324swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004325 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4326 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327{
4328 __lhs.swap(__rhs);
4329}
4330
Bruce Mitchener170d8972020-11-24 12:53:53 -05004331_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4332_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4333_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4334_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4335_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004336
Bruce Mitchener170d8972020-11-24 12:53:53 -05004337_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4338_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4339_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004340
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004341_LIBCPP_FUNC_VIS string to_string(int __val);
4342_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4343_LIBCPP_FUNC_VIS string to_string(long __val);
4344_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4345_LIBCPP_FUNC_VIS string to_string(long long __val);
4346_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4347_LIBCPP_FUNC_VIS string to_string(float __val);
4348_LIBCPP_FUNC_VIS string to_string(double __val);
4349_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004350
Louis Dionne89258142021-08-23 15:32:36 -04004351#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004352_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4353_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4354_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4355_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4356_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004357
Bruce Mitchener170d8972020-11-24 12:53:53 -05004358_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4359_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4360_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004361
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004362_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4363_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4364_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4365_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4366_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4367_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4368_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4369_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4370_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004371#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004372
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004374_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004375const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4376 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377
Marshall Clow851b9ec2019-05-20 21:56:51 +00004378template <class _CharT, class _Allocator>
4379struct _LIBCPP_TEMPLATE_VIS
4380 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4381 : public unary_function<
4382 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004383{
4384 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004385 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4386 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004387};
4388
Howard Hinnantc51e1022010-05-11 19:42:16 +00004389
Howard Hinnantdc095972011-07-18 15:51:59 +00004390template<class _CharT, class _Traits, class _Allocator>
4391basic_ostream<_CharT, _Traits>&
4392operator<<(basic_ostream<_CharT, _Traits>& __os,
4393 const basic_string<_CharT, _Traits, _Allocator>& __str);
4394
4395template<class _CharT, class _Traits, class _Allocator>
4396basic_istream<_CharT, _Traits>&
4397operator>>(basic_istream<_CharT, _Traits>& __is,
4398 basic_string<_CharT, _Traits, _Allocator>& __str);
4399
4400template<class _CharT, class _Traits, class _Allocator>
4401basic_istream<_CharT, _Traits>&
4402getline(basic_istream<_CharT, _Traits>& __is,
4403 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4404
4405template<class _CharT, class _Traits, class _Allocator>
4406inline _LIBCPP_INLINE_VISIBILITY
4407basic_istream<_CharT, _Traits>&
4408getline(basic_istream<_CharT, _Traits>& __is,
4409 basic_string<_CharT, _Traits, _Allocator>& __str);
4410
Howard Hinnantdc095972011-07-18 15:51:59 +00004411template<class _CharT, class _Traits, class _Allocator>
4412inline _LIBCPP_INLINE_VISIBILITY
4413basic_istream<_CharT, _Traits>&
4414getline(basic_istream<_CharT, _Traits>&& __is,
4415 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4416
4417template<class _CharT, class _Traits, class _Allocator>
4418inline _LIBCPP_INLINE_VISIBILITY
4419basic_istream<_CharT, _Traits>&
4420getline(basic_istream<_CharT, _Traits>&& __is,
4421 basic_string<_CharT, _Traits, _Allocator>& __str);
4422
Marshall Clow29b53f22018-12-14 18:49:35 +00004423#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004424template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004425inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004426 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4427 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4428 auto __old_size = __str.size();
4429 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4430 return __old_size - __str.size();
4431}
Marshall Clow29b53f22018-12-14 18:49:35 +00004432
Marek Kurdeja98b1412020-05-02 13:58:03 +02004433template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004434inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004435 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4436 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4437 _Predicate __pred) {
4438 auto __old_size = __str.size();
4439 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4440 __str.end());
4441 return __old_size - __str.size();
4442}
Marshall Clow29b53f22018-12-14 18:49:35 +00004443#endif
4444
Louis Dionneba400782020-10-02 15:02:52 -04004445#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004446
4447template<class _CharT, class _Traits, class _Allocator>
4448bool
4449basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4450{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004451 return data() <= _VSTD::__to_address(__i->base()) &&
4452 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004453}
4454
4455template<class _CharT, class _Traits, class _Allocator>
4456bool
4457basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4458{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004459 return data() < _VSTD::__to_address(__i->base()) &&
4460 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004461}
4462
4463template<class _CharT, class _Traits, class _Allocator>
4464bool
4465basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4466{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004467 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004468 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004469}
4470
4471template<class _CharT, class _Traits, class _Allocator>
4472bool
4473basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4474{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004475 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004476 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004477}
4478
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004479#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004480
Louis Dionne173f29e2019-05-29 16:01:36 +00004481#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004482// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004483inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004484{
4485 inline namespace string_literals
4486 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004487 inline _LIBCPP_INLINE_VISIBILITY
4488 basic_string<char> operator "" s( const char *__str, size_t __len )
4489 {
4490 return basic_string<char> (__str, __len);
4491 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004492
Louis Dionne89258142021-08-23 15:32:36 -04004493#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004494 inline _LIBCPP_INLINE_VISIBILITY
4495 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4496 {
4497 return basic_string<wchar_t> (__str, __len);
4498 }
Louis Dionne89258142021-08-23 15:32:36 -04004499#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004500
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004501#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004502 inline _LIBCPP_INLINE_VISIBILITY
4503 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4504 {
4505 return basic_string<char8_t> (__str, __len);
4506 }
4507#endif
4508
Howard Hinnant5c167562013-08-07 19:39:48 +00004509 inline _LIBCPP_INLINE_VISIBILITY
4510 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4511 {
4512 return basic_string<char16_t> (__str, __len);
4513 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004514
Howard Hinnant5c167562013-08-07 19:39:48 +00004515 inline _LIBCPP_INLINE_VISIBILITY
4516 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4517 {
4518 return basic_string<char32_t> (__str, __len);
4519 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004520 } // namespace string_literals
4521} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004522#endif
4523
Howard Hinnantc51e1022010-05-11 19:42:16 +00004524_LIBCPP_END_NAMESPACE_STD
4525
Eric Fiselierf4433a32017-05-31 22:07:49 +00004526_LIBCPP_POP_MACROS
4527
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004528#endif // _LIBCPP_STRING