blob: 9049d7acbb9bbabe23b11019e4f397d665b7d938 [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
Marek Kurdejc9848142020-11-26 10:07:16 +0100161 void reserve(size_type res_arg);
162 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000164 void clear() noexcept;
165 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166
167 const_reference operator[](size_type pos) const;
168 reference operator[](size_type pos);
169
170 const_reference at(size_type n) const;
171 reference at(size_type n);
172
173 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000174 template <class T>
175 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000176 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177 basic_string& operator+=(value_type c);
178 basic_string& operator+=(initializer_list<value_type>);
179
180 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000181 template <class T>
182 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000183 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000184 template <class T>
185 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000186 basic_string& append(const value_type* s, size_type n);
187 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000189 template<class InputIterator>
190 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(initializer_list<value_type>);
192
193 void push_back(value_type c);
194 void pop_back();
195 reference front();
196 const_reference front() const;
197 reference back();
198 const_reference back() const;
199
200 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000201 template <class T>
202 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000203 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000204 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000205 template <class T>
206 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000207 basic_string& assign(const value_type* s, size_type n);
208 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000210 template<class InputIterator>
211 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(initializer_list<value_type>);
213
214 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000215 template <class T>
216 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000217 basic_string& insert(size_type pos1, const basic_string& str,
218 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000219 template <class T>
220 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000221 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000222 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 basic_string& insert(size_type pos, size_type n, value_type c);
224 iterator insert(const_iterator p, value_type c);
225 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000226 template<class InputIterator>
227 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 iterator insert(const_iterator p, initializer_list<value_type>);
229
230 basic_string& erase(size_type pos = 0, size_type n = npos);
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
234 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000235 template <class T>
236 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000238 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000239 template <class T>
240 basic_string& replace(size_type pos1, size_type n1, const T& t,
241 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000242 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
243 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000245 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000246 template <class T>
247 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
249 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000250 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000251 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000252 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
253 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254
Howard Hinnantd17880b2013-06-28 16:59:19 +0000255 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256 basic_string substr(size_type pos = 0, size_type n = npos) const;
257
Howard Hinnant3e276872011-06-03 18:40:47 +0000258 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000259 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
260 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261
Howard Hinnantd17880b2013-06-28 16:59:19 +0000262 const value_type* c_str() const noexcept;
263 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000264 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000266 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000268 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000269 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800270 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 +0000271 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
272 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000273 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000275 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000276 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800277 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 +0000278 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
279 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000280 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000282 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000283 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800284 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 +0000285 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
286 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000287 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000289 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000290 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800291 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 +0000292 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
293 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000294 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000296 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000297 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800298 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 +0000299 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
300 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000301 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000303 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000304 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800305 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 +0000306 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
307 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000308 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000310 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000311 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800312 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
315 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000317 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000318 template <class T>
319 int compare(size_type pos1, size_type n1, const T& t,
320 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000321 int compare(const value_type* s) const noexcept;
322 int compare(size_type pos1, size_type n1, const value_type* s) const;
323 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324
Marek Kurdej24b4c512021-01-07 12:29:04 +0100325 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
326 bool starts_with(charT c) const noexcept; // C++20
327 bool starts_with(const charT* s) const; // C++20
328 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool ends_with(charT c) const noexcept; // C++20
330 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000331
Wim Leflere023c3542021-01-19 14:33:30 -0500332 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
333 constexpr bool contains(charT c) const noexcept; // C++2b
334 constexpr bool contains(const charT* s) const; // C++2b
335
Howard Hinnantc51e1022010-05-11 19:42:16 +0000336 bool __invariants() const;
337};
338
Marshall Clowa0563332018-02-08 06:34:03 +0000339template<class InputIterator,
340 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
341basic_string(InputIterator, InputIterator, Allocator = Allocator())
342 -> basic_string<typename iterator_traits<InputIterator>::value_type,
343 char_traits<typename iterator_traits<InputIterator>::value_type>,
344 Allocator>; // C++17
345
Howard Hinnantc51e1022010-05-11 19:42:16 +0000346template<class charT, class traits, class Allocator>
347basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000348operator+(const basic_string<charT, traits, Allocator>& lhs,
349 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350
351template<class charT, class traits, class Allocator>
352basic_string<charT, traits, Allocator>
353operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
354
355template<class charT, class traits, class Allocator>
356basic_string<charT, traits, Allocator>
357operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
358
359template<class charT, class traits, class Allocator>
360basic_string<charT, traits, Allocator>
361operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
362
363template<class charT, class traits, class Allocator>
364basic_string<charT, traits, Allocator>
365operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
366
367template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000368bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000369 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370
371template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372bool operator==(const charT* lhs, 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 basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000377template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000378bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000379 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380
381template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382bool operator!=(const charT* lhs, 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 basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000388bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000389 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390
391template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* 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 charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000398bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000399 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400
401template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* 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 charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000408bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000409 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410
411template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* 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 charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000418bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000419 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420
421template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* 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 charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000428void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000429 basic_string<charT, traits, Allocator>& rhs)
430 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431
432template<class charT, class traits, class Allocator>
433basic_istream<charT, traits>&
434operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
435
436template<class charT, class traits, class Allocator>
437basic_ostream<charT, traits>&
438operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
439
440template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000441basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000442getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
443 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444
445template<class charT, class traits, class Allocator>
446basic_istream<charT, traits>&
447getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
448
Marshall Clow29b53f22018-12-14 18:49:35 +0000449template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200450typename basic_string<charT, traits, Allocator>::size_type
451erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456typedef basic_string<char> string;
457typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100458typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000459typedef basic_string<char16_t> u16string;
460typedef basic_string<char32_t> u32string;
461
Bruce Mitchener170d8972020-11-24 12:53:53 -0500462int stoi (const string& str, size_t* idx = nullptr, int base = 10);
463long stol (const string& str, size_t* idx = nullptr, int base = 10);
464unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
465long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
466unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000467
Bruce Mitchener170d8972020-11-24 12:53:53 -0500468float stof (const string& str, size_t* idx = nullptr);
469double stod (const string& str, size_t* idx = nullptr);
470long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000471
472string to_string(int val);
473string to_string(unsigned val);
474string to_string(long val);
475string to_string(unsigned long val);
476string to_string(long long val);
477string to_string(unsigned long long val);
478string to_string(float val);
479string to_string(double val);
480string to_string(long double val);
481
Bruce Mitchener170d8972020-11-24 12:53:53 -0500482int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
483long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
484unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
485long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
486unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000487
Bruce Mitchener170d8972020-11-24 12:53:53 -0500488float stof (const wstring& str, size_t* idx = nullptr);
489double stod (const wstring& str, size_t* idx = nullptr);
490long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000491
492wstring to_wstring(int val);
493wstring to_wstring(unsigned val);
494wstring to_wstring(long val);
495wstring to_wstring(unsigned long val);
496wstring to_wstring(long long val);
497wstring to_wstring(unsigned long long val);
498wstring to_wstring(float val);
499wstring to_wstring(double val);
500wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501
502template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100503template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504template <> struct hash<u16string>;
505template <> struct hash<u32string>;
506template <> struct hash<wstring>;
507
Marshall Clowcba751f2013-07-23 17:05:24 +0000508basic_string<char> operator "" s( const char *str, size_t len ); // C++14
509basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100510basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
512basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
513
Howard Hinnantc51e1022010-05-11 19:42:16 +0000514} // std
515
516*/
517
518#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400519#include <__debug>
520#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400521#include <__iterator/wrap_iter.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522#include <algorithm>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400523#include <compare>
524#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400525#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400526#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400527#include <initializer_list>
528#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530#include <memory>
531#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400532#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400534#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000535#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000536
Louis Dionne89258142021-08-23 15:32:36 -0400537#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
538# include <cwchar>
539#endif
540
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400541#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
542# include <cstdint>
543#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000544
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000545#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000547#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548
Eric Fiselierf4433a32017-05-31 22:07:49 +0000549_LIBCPP_PUSH_MACROS
550#include <__undef_macros>
551
552
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553_LIBCPP_BEGIN_NAMESPACE_STD
554
555// fpos
556
557template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000558class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559{
560private:
561 _StateT __st_;
562 streamoff __off_;
563public:
564 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
565
566 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
567
568 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
569 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
570
571 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
572 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
573 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
574 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
575};
576
577template <class _StateT>
578inline _LIBCPP_INLINE_VISIBILITY
579streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
580 {return streamoff(__x) - streamoff(__y);}
581
582template <class _StateT>
583inline _LIBCPP_INLINE_VISIBILITY
584bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
585 {return streamoff(__x) == streamoff(__y);}
586
587template <class _StateT>
588inline _LIBCPP_INLINE_VISIBILITY
589bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
590 {return streamoff(__x) != streamoff(__y);}
591
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592// basic_string
593
594template<class _CharT, class _Traits, class _Allocator>
595basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000596operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
597 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598
599template<class _CharT, class _Traits, class _Allocator>
600basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000601operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602
603template<class _CharT, class _Traits, class _Allocator>
604basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000605operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606
607template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000608inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000610operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611
612template<class _CharT, class _Traits, class _Allocator>
613basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000614operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615
Shoaib Meenaiea363712017-07-29 02:54:41 +0000616_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
617
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400619struct __basic_string_common;
620
621template <>
622struct __basic_string_common<true> {
623 // Both are defined in string.cpp
624 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
625 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000626};
627
Marshall Clow039b2f02016-01-13 21:54:34 +0000628template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400629struct __string_is_trivial_iterator : public false_type {};
630
631template <class _Tp>
632struct __string_is_trivial_iterator<_Tp*>
633 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000634
Louis Dionne173f29e2019-05-29 16:01:36 +0000635template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400636struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
637 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000638
Marshall Clow82513342016-09-24 22:45:42 +0000639template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500640struct __can_be_converted_to_string_view : public _BoolConstant<
641 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
642 !is_convertible<const _Tp&, const _CharT*>::value
643 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000644
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000645#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000646
647template <class _CharT, size_t = sizeof(_CharT)>
648struct __padding
649{
650 unsigned char __xx[sizeof(_CharT)-1];
651};
652
653template <class _CharT>
654struct __padding<_CharT, 1>
655{
656};
657
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400658#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000659
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400660#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800661typedef basic_string<char8_t> u8string;
662#endif
663
664#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
665typedef basic_string<char16_t> u16string;
666typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400667#endif
Richard Smith256954d2020-11-11 17:12:18 -0800668
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000669template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800670class
671 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400672#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800673 _LIBCPP_PREFERRED_NAME(u8string)
674#endif
675#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
676 _LIBCPP_PREFERRED_NAME(u16string)
677 _LIBCPP_PREFERRED_NAME(u32string)
678#endif
679 basic_string
Louis Dionneb6aabd92021-08-19 12:21:06 -0400680 : 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 +0000681{
682public:
683 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000684 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000686 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000688 typedef allocator_traits<allocator_type> __alloc_traits;
689 typedef typename __alloc_traits::size_type size_type;
690 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000691 typedef value_type& reference;
692 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000693 typedef typename __alloc_traits::pointer pointer;
694 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695
Marshall Clow79f33542018-03-21 00:36:05 +0000696 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
697 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
698 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
699 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000700 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000701 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000702 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000703
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704 typedef __wrap_iter<pointer> iterator;
705 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000706 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
707 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708
709private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000710
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000711#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000712
713 struct __long
714 {
715 pointer __data_;
716 size_type __size_;
717 size_type __cap_;
718 };
719
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000720#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000721 static const size_type __short_mask = 0x01;
722 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000723#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000724 static const size_type __short_mask = 0x80;
725 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400726#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000727
728 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
729 (sizeof(__long) - 1)/sizeof(value_type) : 2};
730
731 struct __short
732 {
733 value_type __data_[__min_cap];
734 struct
735 : __padding<value_type>
736 {
737 unsigned char __size_;
738 };
739 };
740
741#else
742
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743 struct __long
744 {
745 size_type __cap_;
746 size_type __size_;
747 pointer __data_;
748 };
749
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000750#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000751 static const size_type __short_mask = 0x80;
752 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000753#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000754 static const size_type __short_mask = 0x01;
755 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400756#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
759 (sizeof(__long) - 1)/sizeof(value_type) : 2};
760
761 struct __short
762 {
763 union
764 {
765 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000766 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 };
768 value_type __data_[__min_cap];
769 };
770
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400771#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000772
Howard Hinnant8ea98242013-08-23 17:37:05 +0000773 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774
Howard Hinnant8ea98242013-08-23 17:37:05 +0000775 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776
777 struct __raw
778 {
779 size_type __words[__n_words];
780 };
781
782 struct __rep
783 {
784 union
785 {
786 __long __l;
787 __short __s;
788 __raw __r;
789 };
790 };
791
792 __compressed_pair<__rep, allocator_type> __r_;
793
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200795 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796 static const size_type npos = -1;
797
Howard Hinnant3e276872011-06-03 18:40:47 +0000798 _LIBCPP_INLINE_VISIBILITY basic_string()
799 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000800
801 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
802#if _LIBCPP_STD_VER <= 14
803 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
804#else
805 _NOEXCEPT;
806#endif
807
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808 basic_string(const basic_string& __str);
809 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000810
Eric Fiselierfc92be82017-04-19 00:28:44 +0000811#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000813 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000814#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000815 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000816#else
817 _NOEXCEPT;
818#endif
819
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400822#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000823
Louis Dionne9ce598d2021-09-08 09:14:43 -0400824 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000825 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500826 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000827 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
828 __init(__s, traits_type::length(__s));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +0100829#if _LIBCPP_DEBUG_LEVEL == 2
830 if (!__libcpp_is_constant_evaluated())
831 __get_db()->__insert_c(this);
832#endif
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);
Eric Fiselier451d5582018-11-26 20:15:38 +0000976 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
977
Marek Kurdejc9848142020-11-26 10:07:16 +0100978 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
979 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000980 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100981 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000983 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000984 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
985 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000987 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
988 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989
990 const_reference at(size_type __n) const;
991 reference at(size_type __n);
992
993 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000994
995 template <class _Tp>
996 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400997 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000998 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500999 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1000 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001001 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001002 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001003 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001004 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001006#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001008#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009
Howard Hinnantcf823322010-12-17 14:46:43 +00001010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001012
1013 template <class _Tp>
1014 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001015 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001016 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1017 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001018 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001019 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001020 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001021 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001022
Marshall Clow62953962016-10-03 23:40:48 +00001023 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001024 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001025 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001026 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001027 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1028 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001029 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001030 >
Marshall Clow82513342016-09-24 22:45:42 +00001031 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001032 basic_string& append(const value_type* __s, size_type __n);
1033 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001035
1036 _LIBCPP_INLINE_VISIBILITY
1037 void __append_default_init(size_type __n);
1038
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001040 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001041 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001043 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001045 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001046 _LIBCPP_INLINE_VISIBILITY
1047 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001048 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001049 append(__temp.data(), __temp.size());
1050 return *this;
1051 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001053 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001054 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001056 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001058 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001059 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001060 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001061
Eric Fiselierfc92be82017-04-19 00:28:44 +00001062#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001065#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066
1067 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001070 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1071 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1072 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1073 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074
Marshall Clowe46031a2018-07-02 18:41:15 +00001075 template <class _Tp>
1076 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001077 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001078 <
1079 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1080 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001081 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001082 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001083 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001084 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001085#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001086 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001087 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001088 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001089 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001090#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001091 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001092 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001093 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001094 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001095 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001096 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1097 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001098 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001099 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001100 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001101 basic_string& assign(const value_type* __s, size_type __n);
1102 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 basic_string& assign(size_type __n, value_type __c);
1104 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001105 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001106 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001108 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001110 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 assign(_InputIterator __first, _InputIterator __last);
1112 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001113 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001114 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001116 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001118 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001120#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001123#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124
Howard Hinnantcf823322010-12-17 14:46:43 +00001125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001127
1128 template <class _Tp>
1129 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001130 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001131 <
1132 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1133 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001134 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001135 insert(size_type __pos1, const _Tp& __t)
1136 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1137
Marshall Clow82513342016-09-24 22:45:42 +00001138 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001139 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001140 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001141 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001142 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001143 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001144 >
Marshall Clow82513342016-09-24 22:45:42 +00001145 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001146 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001147 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1148 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1150 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1153 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001154 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001155 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001157 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001159 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1161 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001162 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001163 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001165 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001167 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001169#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1172 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001173#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174
1175 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 iterator erase(const_iterator __first, const_iterator __last);
1180
Howard Hinnantcf823322010-12-17 14:46:43 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001183
1184 template <class _Tp>
1185 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001186 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001187 <
1188 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1189 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001190 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001191 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 +00001192 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 +00001193 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001194 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001195 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001196 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001197 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001198 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001199 >
Marshall Clow82513342016-09-24 22:45:42 +00001200 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001201 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1202 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001205 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001206
1207 template <class _Tp>
1208 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001209 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001210 <
1211 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1212 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001213 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001214 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1215
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001217 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001219 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001221 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001223 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001224 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001226 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001228 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001229 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001230#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001232 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001234#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
Howard Hinnantd17880b2013-06-28 16:59:19 +00001236 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1239
Howard Hinnantcf823322010-12-17 14:46:43 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001241 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001242#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001243 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001244#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001245 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001246 __is_nothrow_swappable<allocator_type>::value);
1247#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248
Howard Hinnantcf823322010-12-17 14:46:43 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001250 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001251 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001252 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001253#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001254 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001255 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001256#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257
Howard Hinnantcf823322010-12-17 14:46:43 +00001258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001259 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260
Howard Hinnantcf823322010-12-17 14:46:43 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001262 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001263
1264 template <class _Tp>
1265 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001266 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001267 <
1268 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1269 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001270 >
zoecarver1997e0a2021-02-05 11:54:47 -08001271 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001272 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001274 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001275 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276
Howard Hinnantcf823322010-12-17 14:46:43 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001278 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001279
1280 template <class _Tp>
1281 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001282 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001283 <
1284 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1285 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001286 >
zoecarver1997e0a2021-02-05 11:54:47 -08001287 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001288 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001290 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001291 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292
Howard Hinnantcf823322010-12-17 14:46:43 +00001293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001294 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001295
1296 template <class _Tp>
1297 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001298 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001299 <
1300 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1301 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001302 >
zoecarver1997e0a2021-02-05 11:54:47 -08001303 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001304 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001306 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001308 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309
Howard Hinnantcf823322010-12-17 14:46:43 +00001310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001311 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001312
1313 template <class _Tp>
1314 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001315 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001316 <
1317 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1318 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001319 >
zoecarver1997e0a2021-02-05 11:54:47 -08001320 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001321 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001323 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001325 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326
Howard Hinnantcf823322010-12-17 14:46:43 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001328 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001329
1330 template <class _Tp>
1331 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001332 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001333 <
1334 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1335 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001336 >
zoecarver1997e0a2021-02-05 11:54:47 -08001337 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001338 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 +00001339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001340 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001341 _LIBCPP_INLINE_VISIBILITY
1342 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1343
1344 _LIBCPP_INLINE_VISIBILITY
1345 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001346
1347 template <class _Tp>
1348 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001349 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001350 <
1351 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1352 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001353 >
zoecarver1997e0a2021-02-05 11:54:47 -08001354 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001355 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 +00001356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001357 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001358 _LIBCPP_INLINE_VISIBILITY
1359 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1360
1361 _LIBCPP_INLINE_VISIBILITY
1362 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001363
1364 template <class _Tp>
1365 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001366 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001367 <
1368 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1369 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001370 >
zoecarver1997e0a2021-02-05 11:54:47 -08001371 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001372
1373 template <class _Tp>
1374 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001375 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001376 <
1377 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1378 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001379 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001380 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1381
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001384 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 +00001385
Marshall Clow82513342016-09-24 22:45:42 +00001386 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001387 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001388 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001389 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001390 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001391 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001392 >
Marshall Clow82513342016-09-24 22:45:42 +00001393 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 +00001394 int compare(const value_type* __s) const _NOEXCEPT;
1395 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1396 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397
Marshall Clow18c293b2017-12-04 20:11:38 +00001398#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001399 constexpr _LIBCPP_INLINE_VISIBILITY
1400 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001401 { return __self_view(data(), size()).starts_with(__sv); }
1402
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001403 constexpr _LIBCPP_INLINE_VISIBILITY
1404 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001405 { return !empty() && _Traits::eq(front(), __c); }
1406
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001407 constexpr _LIBCPP_INLINE_VISIBILITY
1408 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001409 { return starts_with(__self_view(__s)); }
1410
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001411 constexpr _LIBCPP_INLINE_VISIBILITY
1412 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001413 { return __self_view(data(), size()).ends_with( __sv); }
1414
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001415 constexpr _LIBCPP_INLINE_VISIBILITY
1416 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001417 { return !empty() && _Traits::eq(back(), __c); }
1418
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001419 constexpr _LIBCPP_INLINE_VISIBILITY
1420 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001421 { return ends_with(__self_view(__s)); }
1422#endif
1423
Wim Leflere023c3542021-01-19 14:33:30 -05001424#if _LIBCPP_STD_VER > 20
1425 constexpr _LIBCPP_INLINE_VISIBILITY
1426 bool contains(__self_view __sv) const noexcept
1427 { return __self_view(data(), size()).contains(__sv); }
1428
1429 constexpr _LIBCPP_INLINE_VISIBILITY
1430 bool contains(value_type __c) const noexcept
1431 { return __self_view(data(), size()).contains(__c); }
1432
1433 constexpr _LIBCPP_INLINE_VISIBILITY
1434 bool contains(const value_type* __s) const
1435 { return __self_view(data(), size()).contains(__s); }
1436#endif
1437
Howard Hinnantcf823322010-12-17 14:46:43 +00001438 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001439
Marshall Clowe60a7182018-05-29 17:04:37 +00001440 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001441
Marek Kurdejc9848142020-11-26 10:07:16 +01001442 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1443
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001444 _LIBCPP_INLINE_VISIBILITY
1445 bool __is_long() const _NOEXCEPT
1446 {return bool(__r_.first().__s.__size_ & __short_mask);}
1447
Louis Dionneba400782020-10-02 15:02:52 -04001448#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001449
1450 bool __dereferenceable(const const_iterator* __i) const;
1451 bool __decrementable(const const_iterator* __i) const;
1452 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1453 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1454
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001455#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001456
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001458 _LIBCPP_INLINE_VISIBILITY
1459 allocator_type& __alloc() _NOEXCEPT
1460 {return __r_.second();}
1461 _LIBCPP_INLINE_VISIBILITY
1462 const allocator_type& __alloc() const _NOEXCEPT
1463 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001465#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001466
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001468 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001469# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001471# else
1472 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1473# endif
1474
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001475 _LIBCPP_INLINE_VISIBILITY
1476 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001477# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001479# else
1480 {return __r_.first().__s.__size_;}
1481# endif
1482
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001483#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001484
1485 _LIBCPP_INLINE_VISIBILITY
1486 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001487# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001488 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1489# else
1490 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1491# endif
1492
1493 _LIBCPP_INLINE_VISIBILITY
1494 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001495# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001496 {return __r_.first().__s.__size_;}
1497# else
1498 {return __r_.first().__s.__size_ >> 1;}
1499# endif
1500
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001501#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001502
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001503 _LIBCPP_INLINE_VISIBILITY
1504 void __set_long_size(size_type __s) _NOEXCEPT
1505 {__r_.first().__l.__size_ = __s;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 size_type __get_long_size() const _NOEXCEPT
1508 {return __r_.first().__l.__size_;}
1509 _LIBCPP_INLINE_VISIBILITY
1510 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1512
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001513 _LIBCPP_INLINE_VISIBILITY
1514 void __set_long_cap(size_type __s) _NOEXCEPT
1515 {__r_.first().__l.__cap_ = __long_mask | __s;}
1516 _LIBCPP_INLINE_VISIBILITY
1517 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001518 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001520 _LIBCPP_INLINE_VISIBILITY
1521 void __set_long_pointer(pointer __p) _NOEXCEPT
1522 {__r_.first().__l.__data_ = __p;}
1523 _LIBCPP_INLINE_VISIBILITY
1524 pointer __get_long_pointer() _NOEXCEPT
1525 {return __r_.first().__l.__data_;}
1526 _LIBCPP_INLINE_VISIBILITY
1527 const_pointer __get_long_pointer() const _NOEXCEPT
1528 {return __r_.first().__l.__data_;}
1529 _LIBCPP_INLINE_VISIBILITY
1530 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001531 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 _LIBCPP_INLINE_VISIBILITY
1533 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001534 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001535 _LIBCPP_INLINE_VISIBILITY
1536 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001538 _LIBCPP_INLINE_VISIBILITY
1539 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1541
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001542 _LIBCPP_INLINE_VISIBILITY
1543 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544 {
1545 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1546 for (unsigned __i = 0; __i < __n_words; ++__i)
1547 __a[__i] = 0;
1548 }
1549
1550 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001552 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001553 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001555 static _LIBCPP_INLINE_VISIBILITY
1556 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001557 {
1558 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1559 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1560 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1561 if (__guess == __min_cap) ++__guess;
1562 return __guess;
1563 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001565 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001566 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001567 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001568 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001569 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001571
Martijn Vels5e7c9752020-03-04 17:52:46 -05001572 // Slow path for the (inlined) copy constructor for 'long' strings.
1573 // Always externally instantiated and not inlined.
1574 // Requires that __s is zero terminated.
1575 // The main reason for this function to exist is because for unstable, we
1576 // want to allow inlining of the copy constructor. However, we don't want
1577 // to call the __init() functions as those are marked as inline which may
1578 // result in over-aggressive inlining by the compiler, where our aim is
1579 // to only inline the fast path code directly in the ctor.
1580 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1581
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001583 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001584 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001586 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1587 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 __init(_InputIterator __first, _InputIterator __last);
1589
1590 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001591 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001592 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001594 __is_cpp17_forward_iterator<_ForwardIterator>::value
1595 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 __init(_ForwardIterator __first, _ForwardIterator __last);
1597
1598 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001599 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1601 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001602 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603
Martijn Vels596e3de2020-02-26 15:55:49 -05001604 // __assign_no_alias is invoked for assignment operations where we
1605 // have proof that the input does not alias the current instance.
1606 // For example, operator=(basic_string) performs a 'self' check.
1607 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001608 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001609
Howard Hinnantcf823322010-12-17 14:46:43 +00001610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 void __erase_to_end(size_type __pos);
1612
Martijn Velsa81fc792020-02-26 13:25:43 -05001613 // __erase_external_with_move is invoked for erase() invocations where
1614 // `n ~= npos`, likely requiring memory moves on the string data.
1615 void __erase_external_with_move(size_type __pos, size_type __n);
1616
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001617 _LIBCPP_INLINE_VISIBILITY
1618 void __copy_assign_alloc(const basic_string& __str)
1619 {__copy_assign_alloc(__str, integral_constant<bool,
1620 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1621
1622 _LIBCPP_INLINE_VISIBILITY
1623 void __copy_assign_alloc(const basic_string& __str, true_type)
1624 {
Marshall Clowf258c202017-01-31 03:40:52 +00001625 if (__alloc() == __str.__alloc())
1626 __alloc() = __str.__alloc();
1627 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001628 {
Marshall Clowf258c202017-01-31 03:40:52 +00001629 if (!__str.__is_long())
1630 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001631 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001632 __alloc() = __str.__alloc();
1633 }
1634 else
1635 {
1636 allocator_type __a = __str.__alloc();
1637 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001638 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001639 __alloc() = _VSTD::move(__a);
1640 __set_long_pointer(__p);
1641 __set_long_cap(__str.__get_long_cap());
1642 __set_long_size(__str.size());
1643 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001644 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001645 }
1646
1647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001648 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001649 {}
1650
Eric Fiselierfc92be82017-04-19 00:28:44 +00001651#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001652 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001653 void __move_assign(basic_string& __str, false_type)
1654 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001656 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001657#if _LIBCPP_STD_VER > 14
1658 _NOEXCEPT;
1659#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001660 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001661#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001662#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001663
1664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001665 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001666 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001667 _NOEXCEPT_(
1668 !__alloc_traits::propagate_on_container_move_assignment::value ||
1669 is_nothrow_move_assignable<allocator_type>::value)
1670 {__move_assign_alloc(__str, integral_constant<bool,
1671 __alloc_traits::propagate_on_container_move_assignment::value>());}
1672
1673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001674 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001675 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1676 {
1677 __alloc() = _VSTD::move(__c.__alloc());
1678 }
1679
1680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001681 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001682 _NOEXCEPT
1683 {}
1684
Martijn Velsda7d94f2020-06-19 14:24:03 -04001685 basic_string& __assign_external(const value_type* __s);
1686 basic_string& __assign_external(const value_type* __s, size_type __n);
1687
1688 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1689 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1690 pointer __p = __is_long()
1691 ? (__set_long_size(__n), __get_long_pointer())
1692 : (__set_short_size(__n), __get_short_pointer());
1693 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1694 traits_type::assign(__p[__n], value_type());
1695 return *this;
1696 }
1697
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001698 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1699 __set_size(__newsz);
1700 __invalidate_iterators_past(__newsz);
1701 traits_type::assign(__p[__newsz], value_type());
1702 return *this;
1703 }
1704
Howard Hinnantcf823322010-12-17 14:46:43 +00001705 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1706 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001708 template<class _Tp>
1709 _LIBCPP_INLINE_VISIBILITY
1710 bool __addr_in_range(_Tp&& __t) const {
1711 const volatile void *__p = _VSTD::addressof(__t);
1712 return data() <= __p && __p <= data() + size();
1713 }
1714
Louis Dionned24191c2021-08-19 12:39:16 -04001715 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1716 void __throw_length_error() const {
1717#ifndef _LIBCPP_NO_EXCEPTIONS
1718 __basic_string_common<true>::__throw_length_error();
1719#else
1720 _VSTD::abort();
1721#endif
1722 }
1723
1724 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1725 void __throw_out_of_range() const {
1726#ifndef _LIBCPP_NO_EXCEPTIONS
1727 __basic_string_common<true>::__throw_out_of_range();
1728#else
1729 _VSTD::abort();
1730#endif
1731 }
1732
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 friend basic_string operator+<>(const basic_string&, const basic_string&);
1734 friend basic_string operator+<>(const value_type*, const basic_string&);
1735 friend basic_string operator+<>(value_type, const basic_string&);
1736 friend basic_string operator+<>(const basic_string&, const value_type*);
1737 friend basic_string operator+<>(const basic_string&, value_type);
1738};
1739
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001740// These declarations must appear before any functions are implicitly used
1741// so that they have the correct visibility specifier.
1742#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001743 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1744# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1745 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1746# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001747#else
Louis Dionne89258142021-08-23 15:32:36 -04001748 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1749# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1750 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1751# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001752#endif
1753
1754
Louis Dionned59f8a52021-08-17 11:59:07 -04001755#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001756template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001757 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001758 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001759 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1760 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001761 >
1762basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1763 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001764
1765template<class _CharT,
1766 class _Traits,
1767 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001768 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001769 >
1770explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1771 -> basic_string<_CharT, _Traits, _Allocator>;
1772
1773template<class _CharT,
1774 class _Traits,
1775 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001776 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001777 class _Sz = typename allocator_traits<_Allocator>::size_type
1778 >
1779basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1780 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001781#endif
1782
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001784inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785void
1786basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1787{
Louis Dionneba400782020-10-02 15:02:52 -04001788#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001789 if (!__libcpp_is_constant_evaluated())
1790 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001791#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792}
1793
1794template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001795inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001797basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798{
Louis Dionneba400782020-10-02 15:02:52 -04001799#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001800 if (!__libcpp_is_constant_evaluated()) {
1801 __c_node* __c = __get_db()->__find_c_and_lock(this);
1802 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001804 const_pointer __new_last = __get_pointer() + __pos;
1805 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001807 --__p;
1808 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1809 if (__i->base() > __new_last)
1810 {
1811 (*__p)->__c_ = nullptr;
1812 if (--__c->end_ != __p)
1813 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1814 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001816 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 }
1818 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001819#else
1820 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001821#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822}
1823
1824template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001825inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001826basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001827 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001828 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829{
Louis Dionneba400782020-10-02 15:02:52 -04001830#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001831 if (!__libcpp_is_constant_evaluated())
1832 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001833#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834 __zero();
1835}
1836
1837template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001838inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001840#if _LIBCPP_STD_VER <= 14
1841 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1842#else
1843 _NOEXCEPT
1844#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001845: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846{
Louis Dionneba400782020-10-02 15:02:52 -04001847#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001848 if (!__libcpp_is_constant_evaluated())
1849 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001850#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 __zero();
1852}
1853
1854template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001855void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1856 size_type __sz,
1857 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858{
1859 if (__reserve > max_size())
1860 this->__throw_length_error();
1861 pointer __p;
1862 if (__reserve < __min_cap)
1863 {
1864 __set_short_size(__sz);
1865 __p = __get_short_pointer();
1866 }
1867 else
1868 {
1869 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001870 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 __set_long_pointer(__p);
1872 __set_long_cap(__cap+1);
1873 __set_long_size(__sz);
1874 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001875 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 traits_type::assign(__p[__sz], value_type());
1877}
1878
1879template <class _CharT, class _Traits, class _Allocator>
1880void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001881basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882{
1883 if (__sz > max_size())
1884 this->__throw_length_error();
1885 pointer __p;
1886 if (__sz < __min_cap)
1887 {
1888 __set_short_size(__sz);
1889 __p = __get_short_pointer();
1890 }
1891 else
1892 {
1893 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001894 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001895 __set_long_pointer(__p);
1896 __set_long_cap(__cap+1);
1897 __set_long_size(__sz);
1898 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001899 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900 traits_type::assign(__p[__sz], value_type());
1901}
1902
1903template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001904template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001905basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001906 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001908 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001910#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001911 if (!__libcpp_is_constant_evaluated())
1912 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001913#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914}
1915
1916template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001917inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001918basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001919 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920{
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001921 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1922 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001923#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001924 if (!__libcpp_is_constant_evaluated())
1925 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001926#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927}
1928
1929template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001930inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001931basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001932 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001934 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001936#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001937 if (!__libcpp_is_constant_evaluated())
1938 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001939#endif
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());
1951
Louis Dionneba400782020-10-02 15:02:52 -04001952#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001953 if (!__libcpp_is_constant_evaluated())
1954 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001955#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956}
1957
1958template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001959basic_string<_CharT, _Traits, _Allocator>::basic_string(
1960 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001961 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962{
1963 if (!__str.__is_long())
1964 __r_.first().__r = __str.__r_.first().__r;
1965 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001966 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1967 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001968#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001969 if (!__libcpp_is_constant_evaluated())
1970 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001971#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972}
1973
Martijn Vels5e7c9752020-03-04 17:52:46 -05001974template <class _CharT, class _Traits, class _Allocator>
1975void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1976 const value_type* __s, size_type __sz) {
1977 pointer __p;
1978 if (__sz < __min_cap) {
1979 __p = __get_short_pointer();
1980 __set_short_size(__sz);
1981 } else {
1982 if (__sz > max_size())
1983 this->__throw_length_error();
1984 size_t __cap = __recommend(__sz);
1985 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1986 __set_long_pointer(__p);
1987 __set_long_cap(__cap + 1);
1988 __set_long_size(__sz);
1989 }
1990 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1991}
1992
Eric Fiselierfc92be82017-04-19 00:28:44 +00001993#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994
1995template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001996inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001997basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001998#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001999 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00002000#else
2001 _NOEXCEPT
2002#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002003 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004{
2005 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04002006#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002007 if (!__libcpp_is_constant_evaluated()) {
2008 __get_db()->__insert_c(this);
2009 if (__is_long())
2010 __get_db()->swap(this, &__str);
2011 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012#endif
2013}
2014
2015template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002016inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002018 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002019{
Marshall Clowd2d24692014-07-17 15:32:20 +00002020 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002021 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002022 else
2023 {
2024 __r_.first().__r = __str.__r_.first().__r;
2025 __str.__zero();
2026 }
Louis Dionneba400782020-10-02 15:02:52 -04002027#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002028 if (!__libcpp_is_constant_evaluated()) {
2029 __get_db()->__insert_c(this);
2030 if (__is_long())
2031 __get_db()->swap(this, &__str);
2032 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033#endif
2034}
2035
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002036#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037
2038template <class _CharT, class _Traits, class _Allocator>
2039void
2040basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2041{
2042 if (__n > max_size())
2043 this->__throw_length_error();
2044 pointer __p;
2045 if (__n < __min_cap)
2046 {
2047 __set_short_size(__n);
2048 __p = __get_short_pointer();
2049 }
2050 else
2051 {
2052 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002053 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054 __set_long_pointer(__p);
2055 __set_long_cap(__cap+1);
2056 __set_long_size(__n);
2057 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002058 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059 traits_type::assign(__p[__n], value_type());
2060}
2061
2062template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002063inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002064basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002065 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002066{
2067 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002068#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002069 if (!__libcpp_is_constant_evaluated())
2070 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002071#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072}
2073
2074template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002075template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002076basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002077 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078{
2079 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002080#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002081 if (!__libcpp_is_constant_evaluated())
2082 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002083#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084}
2085
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002087basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2088 size_type __pos, size_type __n,
2089 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002090 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091{
2092 size_type __str_sz = __str.size();
2093 if (__pos > __str_sz)
2094 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002095 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002096#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002097 if (!__libcpp_is_constant_evaluated())
2098 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002099#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100}
2101
2102template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002103inline
Marshall Clow83445802016-04-07 18:13:41 +00002104basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002105 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002106 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002107{
2108 size_type __str_sz = __str.size();
2109 if (__pos > __str_sz)
2110 this->__throw_out_of_range();
2111 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002112#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002113 if (!__libcpp_is_constant_evaluated())
2114 __get_db()->__insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002115#endif
2116}
2117
2118template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002119template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002120basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002121 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002122 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002123{
Marshall Clowe46031a2018-07-02 18:41:15 +00002124 __self_view __sv0 = __t;
2125 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002126 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002127#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002128 if (!__libcpp_is_constant_evaluated())
2129 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002130#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002131}
2132
2133template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002134template <class _Tp, class>
2135basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002136 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002137{
Marshall Clowe46031a2018-07-02 18:41:15 +00002138 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002139 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002140#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002141 if (!__libcpp_is_constant_evaluated())
2142 __get_db()->__insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002143#endif
2144}
2145
2146template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002147template <class _Tp, class>
2148basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002149 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002150{
Marshall Clowe46031a2018-07-02 18:41:15 +00002151 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002152 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002153#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002154 if (!__libcpp_is_constant_evaluated())
2155 __get_db()->__insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002156#endif
2157}
2158
2159template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002161__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002163 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2164>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2166{
2167 __zero();
2168#ifndef _LIBCPP_NO_EXCEPTIONS
2169 try
2170 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002171#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 for (; __first != __last; ++__first)
2173 push_back(*__first);
2174#ifndef _LIBCPP_NO_EXCEPTIONS
2175 }
2176 catch (...)
2177 {
2178 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002179 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 throw;
2181 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002182#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183}
2184
2185template <class _CharT, class _Traits, class _Allocator>
2186template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002187__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002189 __is_cpp17_forward_iterator<_ForwardIterator>::value
2190>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2192{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002193 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 if (__sz > max_size())
2195 this->__throw_length_error();
2196 pointer __p;
2197 if (__sz < __min_cap)
2198 {
2199 __set_short_size(__sz);
2200 __p = __get_short_pointer();
2201 }
2202 else
2203 {
2204 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002205 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206 __set_long_pointer(__p);
2207 __set_long_cap(__cap+1);
2208 __set_long_size(__sz);
2209 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002210
2211#ifndef _LIBCPP_NO_EXCEPTIONS
2212 try
2213 {
2214#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002215 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 traits_type::assign(*__p, *__first);
2217 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002218#ifndef _LIBCPP_NO_EXCEPTIONS
2219 }
2220 catch (...)
2221 {
2222 if (__is_long())
2223 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2224 throw;
2225 }
2226#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227}
2228
2229template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002230template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002231inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002233 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234{
2235 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002236#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002237 if (!__libcpp_is_constant_evaluated())
2238 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002239#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240}
2241
2242template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002243template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002244inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2246 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002247 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248{
2249 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002250#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002251 if (!__libcpp_is_constant_evaluated())
2252 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002253#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254}
2255
Eric Fiselierfc92be82017-04-19 00:28:44 +00002256#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002257
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002259inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002260basic_string<_CharT, _Traits, _Allocator>::basic_string(
2261 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002262 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263{
2264 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002265#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002266 if (!__libcpp_is_constant_evaluated())
2267 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002268#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269}
2270
2271template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002272inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002273
Eric Fiselier812882b2017-02-17 01:17:10 +00002274basic_string<_CharT, _Traits, _Allocator>::basic_string(
2275 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002276 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277{
2278 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002279#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002280 if (!__libcpp_is_constant_evaluated())
2281 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002282#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283}
2284
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002285#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002286
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2289{
Louis Dionneba400782020-10-02 15:02:52 -04002290#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002291 if (!__libcpp_is_constant_evaluated())
2292 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002293#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002295 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296}
2297
2298template <class _CharT, class _Traits, class _Allocator>
2299void
2300basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2301 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002302 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 +00002303{
2304 size_type __ms = max_size();
2305 if (__delta_cap > __ms - __old_cap - 1)
2306 this->__throw_length_error();
2307 pointer __old_p = __get_pointer();
2308 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002309 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002311 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312 __invalidate_all_iterators();
2313 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002314 traits_type::copy(_VSTD::__to_address(__p),
2315 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002317 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2319 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002320 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2321 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002323 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324 __set_long_pointer(__p);
2325 __set_long_cap(__cap+1);
2326 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2327 __set_long_size(__old_sz);
2328 traits_type::assign(__p[__old_sz], value_type());
2329}
2330
2331template <class _CharT, class _Traits, class _Allocator>
2332void
2333basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2334 size_type __n_copy, size_type __n_del, size_type __n_add)
2335{
2336 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002337 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338 this->__throw_length_error();
2339 pointer __old_p = __get_pointer();
2340 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002341 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002343 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002344 __invalidate_all_iterators();
2345 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002346 traits_type::copy(_VSTD::__to_address(__p),
2347 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2349 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002350 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2351 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002352 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002354 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355 __set_long_pointer(__p);
2356 __set_long_cap(__cap+1);
2357}
2358
2359// assign
2360
2361template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002362template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002363basic_string<_CharT, _Traits, _Allocator>&
2364basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002365 const value_type* __s, size_type __n) {
2366 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2367 if (__n < __cap) {
2368 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2369 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2370 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2371 traits_type::assign(__p[__n], value_type());
2372 __invalidate_iterators_past(__n);
2373 } else {
2374 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2375 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2376 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002377 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002378}
2379
2380template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002382basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2383 const value_type* __s, size_type __n) {
2384 size_type __cap = capacity();
2385 if (__cap >= __n) {
2386 value_type* __p = _VSTD::__to_address(__get_pointer());
2387 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002388 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002389 } else {
2390 size_type __sz = size();
2391 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002392 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002393 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002394}
2395
2396template <class _CharT, class _Traits, class _Allocator>
2397basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002398basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002400 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002401 return (__builtin_constant_p(__n) && __n < __min_cap)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002402 ? __assign_short(__s, __n)
2403 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404}
2405
2406template <class _CharT, class _Traits, class _Allocator>
2407basic_string<_CharT, _Traits, _Allocator>&
2408basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2409{
2410 size_type __cap = capacity();
2411 if (__cap < __n)
2412 {
2413 size_type __sz = size();
2414 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2415 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002416 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002418 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419}
2420
2421template <class _CharT, class _Traits, class _Allocator>
2422basic_string<_CharT, _Traits, _Allocator>&
2423basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2424{
2425 pointer __p;
2426 if (__is_long())
2427 {
2428 __p = __get_long_pointer();
2429 __set_long_size(1);
2430 }
2431 else
2432 {
2433 __p = __get_short_pointer();
2434 __set_short_size(1);
2435 }
2436 traits_type::assign(*__p, __c);
2437 traits_type::assign(*++__p, value_type());
2438 __invalidate_iterators_past(1);
2439 return *this;
2440}
2441
2442template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002443basic_string<_CharT, _Traits, _Allocator>&
2444basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2445{
Martijn Vels596e3de2020-02-26 15:55:49 -05002446 if (this != &__str) {
2447 __copy_assign_alloc(__str);
2448 if (!__is_long()) {
2449 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002450 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002451 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002452 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002453 }
2454 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002455 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002456 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002457 }
2458 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002459}
2460
Eric Fiselierfc92be82017-04-19 00:28:44 +00002461#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002462
2463template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002464inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002465void
2466basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002467 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002468{
2469 if (__alloc() != __str.__alloc())
2470 assign(__str);
2471 else
2472 __move_assign(__str, true_type());
2473}
2474
2475template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002476inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002477void
2478basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002479#if _LIBCPP_STD_VER > 14
2480 _NOEXCEPT
2481#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002482 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002483#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002484{
marshall2ed41622019-11-27 07:13:00 -08002485 if (__is_long()) {
2486 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2487 __get_long_cap());
2488#if _LIBCPP_STD_VER <= 14
2489 if (!is_nothrow_move_assignable<allocator_type>::value) {
2490 __set_short_size(0);
2491 traits_type::assign(__get_short_pointer()[0], value_type());
2492 }
2493#endif
2494 }
2495 __move_assign_alloc(__str);
2496 __r_.first() = __str.__r_.first();
2497 __str.__set_short_size(0);
2498 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002499}
2500
2501template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002502inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002503basic_string<_CharT, _Traits, _Allocator>&
2504basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002505 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002506{
2507 __move_assign(__str, integral_constant<bool,
2508 __alloc_traits::propagate_on_container_move_assignment::value>());
2509 return *this;
2510}
2511
2512#endif
2513
2514template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002516__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002518 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002520>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2522{
Marshall Clow958362f2016-09-05 01:54:30 +00002523 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002524 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002525 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526}
2527
2528template <class _CharT, class _Traits, class _Allocator>
2529template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002530__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002532 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002534>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2536{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002538 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2539 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2540
2541 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2542 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002544 if (__cap < __n)
2545 {
2546 size_type __sz = size();
2547 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2548 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002549 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002550 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002551 traits_type::assign(*__p, *__first);
2552 traits_type::assign(*__p, value_type());
2553 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002554 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555 }
2556 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002557 {
2558 const basic_string __temp(__first, __last, __alloc());
2559 assign(__temp.data(), __temp.size());
2560 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561 return *this;
2562}
2563
2564template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565basic_string<_CharT, _Traits, _Allocator>&
2566basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2567{
2568 size_type __sz = __str.size();
2569 if (__pos > __sz)
2570 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002571 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572}
2573
2574template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002575template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002576__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002577<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002578 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2579 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002580 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002581>
Marshall Clow82513342016-09-24 22:45:42 +00002582basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002583{
Marshall Clow82513342016-09-24 22:45:42 +00002584 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002585 size_type __sz = __sv.size();
2586 if (__pos > __sz)
2587 this->__throw_out_of_range();
2588 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2589}
2590
2591
2592template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002594basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2595 return __assign_external(__s, traits_type::length(__s));
2596}
2597
2598template <class _CharT, class _Traits, class _Allocator>
2599basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002600basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002602 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002603 return __builtin_constant_p(*__s)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002604 ? (traits_type::length(__s) < __min_cap
2605 ? __assign_short(__s, traits_type::length(__s))
2606 : __assign_external(__s, traits_type::length(__s)))
2607 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609// append
2610
2611template <class _CharT, class _Traits, class _Allocator>
2612basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002613basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002615 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616 size_type __cap = capacity();
2617 size_type __sz = size();
2618 if (__cap - __sz >= __n)
2619 {
2620 if (__n)
2621 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002622 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623 traits_type::copy(__p + __sz, __s, __n);
2624 __sz += __n;
2625 __set_size(__sz);
2626 traits_type::assign(__p[__sz], value_type());
2627 }
2628 }
2629 else
2630 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2631 return *this;
2632}
2633
2634template <class _CharT, class _Traits, class _Allocator>
2635basic_string<_CharT, _Traits, _Allocator>&
2636basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2637{
2638 if (__n)
2639 {
2640 size_type __cap = capacity();
2641 size_type __sz = size();
2642 if (__cap - __sz < __n)
2643 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2644 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002645 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646 __sz += __n;
2647 __set_size(__sz);
2648 traits_type::assign(__p[__sz], value_type());
2649 }
2650 return *this;
2651}
2652
2653template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002654inline void
2655basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2656{
2657 if (__n)
2658 {
2659 size_type __cap = capacity();
2660 size_type __sz = size();
2661 if (__cap - __sz < __n)
2662 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2663 pointer __p = __get_pointer();
2664 __sz += __n;
2665 __set_size(__sz);
2666 traits_type::assign(__p[__sz], value_type());
2667 }
2668}
2669
2670template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671void
2672basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2673{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002674 bool __is_short = !__is_long();
2675 size_type __cap;
2676 size_type __sz;
2677 if (__is_short)
2678 {
2679 __cap = __min_cap - 1;
2680 __sz = __get_short_size();
2681 }
2682 else
2683 {
2684 __cap = __get_long_cap() - 1;
2685 __sz = __get_long_size();
2686 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002688 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002690 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002691 }
2692 pointer __p;
2693 if (__is_short)
2694 {
2695 __p = __get_short_pointer() + __sz;
2696 __set_short_size(__sz+1);
2697 }
2698 else
2699 {
2700 __p = __get_long_pointer() + __sz;
2701 __set_long_size(__sz+1);
2702 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 traits_type::assign(*__p, __c);
2704 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705}
2706
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707template <class _CharT, class _Traits, class _Allocator>
2708template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002709__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002710<
2711 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2712 basic_string<_CharT, _Traits, _Allocator>&
2713>
2714basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002715 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716{
2717 size_type __sz = size();
2718 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002719 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720 if (__n)
2721 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002722 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2723 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002724 {
2725 if (__cap - __sz < __n)
2726 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2727 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002728 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002729 traits_type::assign(*__p, *__first);
2730 traits_type::assign(*__p, value_type());
2731 __set_size(__sz + __n);
2732 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002733 else
2734 {
2735 const basic_string __temp(__first, __last, __alloc());
2736 append(__temp.data(), __temp.size());
2737 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738 }
2739 return *this;
2740}
2741
2742template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002743inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744basic_string<_CharT, _Traits, _Allocator>&
2745basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2746{
2747 return append(__str.data(), __str.size());
2748}
2749
2750template <class _CharT, class _Traits, class _Allocator>
2751basic_string<_CharT, _Traits, _Allocator>&
2752basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2753{
2754 size_type __sz = __str.size();
2755 if (__pos > __sz)
2756 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002757 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758}
2759
2760template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002761template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002762 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002763 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002764 __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 +00002765 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002766 >
Marshall Clow82513342016-09-24 22:45:42 +00002767basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002768{
Marshall Clow82513342016-09-24 22:45:42 +00002769 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002770 size_type __sz = __sv.size();
2771 if (__pos > __sz)
2772 this->__throw_out_of_range();
2773 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2774}
2775
2776template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002778basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002780 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 return append(__s, traits_type::length(__s));
2782}
2783
2784// insert
2785
2786template <class _CharT, class _Traits, class _Allocator>
2787basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002788basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002790 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 size_type __sz = size();
2792 if (__pos > __sz)
2793 this->__throw_out_of_range();
2794 size_type __cap = capacity();
2795 if (__cap - __sz >= __n)
2796 {
2797 if (__n)
2798 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002799 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800 size_type __n_move = __sz - __pos;
2801 if (__n_move != 0)
2802 {
2803 if (__p + __pos <= __s && __s < __p + __sz)
2804 __s += __n;
2805 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2806 }
2807 traits_type::move(__p + __pos, __s, __n);
2808 __sz += __n;
2809 __set_size(__sz);
2810 traits_type::assign(__p[__sz], value_type());
2811 }
2812 }
2813 else
2814 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2815 return *this;
2816}
2817
2818template <class _CharT, class _Traits, class _Allocator>
2819basic_string<_CharT, _Traits, _Allocator>&
2820basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2821{
2822 size_type __sz = size();
2823 if (__pos > __sz)
2824 this->__throw_out_of_range();
2825 if (__n)
2826 {
2827 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002828 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829 if (__cap - __sz >= __n)
2830 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002831 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 size_type __n_move = __sz - __pos;
2833 if (__n_move != 0)
2834 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2835 }
2836 else
2837 {
2838 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002839 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840 }
2841 traits_type::assign(__p + __pos, __n, __c);
2842 __sz += __n;
2843 __set_size(__sz);
2844 traits_type::assign(__p[__sz], value_type());
2845 }
2846 return *this;
2847}
2848
2849template <class _CharT, class _Traits, class _Allocator>
2850template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002851__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002853 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002854 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002855>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2857{
Nikolas Klausereed25832021-12-15 01:32:30 +01002858 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2859 "string::insert(iterator, range) called with an iterator not"
2860 " referring to this string");
2861 const basic_string __temp(__first, __last, __alloc());
2862 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863}
2864
2865template <class _CharT, class _Traits, class _Allocator>
2866template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002867__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002869 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002871>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2873{
Nikolas Klausereed25832021-12-15 01:32:30 +01002874 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2875 "string::insert(iterator, range) called with an iterator not"
2876 " referring to this string");
2877
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002879 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880 if (__n)
2881 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002882 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2883 !__addr_in_range(*__first))
2884 {
2885 size_type __sz = size();
2886 size_type __cap = capacity();
2887 value_type* __p;
2888 if (__cap - __sz >= __n)
2889 {
2890 __p = _VSTD::__to_address(__get_pointer());
2891 size_type __n_move = __sz - __ip;
2892 if (__n_move != 0)
2893 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2894 }
2895 else
2896 {
2897 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2898 __p = _VSTD::__to_address(__get_long_pointer());
2899 }
2900 __sz += __n;
2901 __set_size(__sz);
2902 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002903 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002904 traits_type::assign(*__p, *__first);
2905 }
2906 else
Marshall Clow958362f2016-09-05 01:54:30 +00002907 {
2908 const basic_string __temp(__first, __last, __alloc());
2909 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2910 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 }
2912 return begin() + __ip;
2913}
2914
2915template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002916inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917basic_string<_CharT, _Traits, _Allocator>&
2918basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2919{
2920 return insert(__pos1, __str.data(), __str.size());
2921}
2922
2923template <class _CharT, class _Traits, class _Allocator>
2924basic_string<_CharT, _Traits, _Allocator>&
2925basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2926 size_type __pos2, size_type __n)
2927{
2928 size_type __str_sz = __str.size();
2929 if (__pos2 > __str_sz)
2930 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002931 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932}
2933
2934template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002935template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002936__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002937<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002938 __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 +00002939 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002940>
Marshall Clow82513342016-09-24 22:45:42 +00002941basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002942 size_type __pos2, size_type __n)
2943{
Marshall Clow82513342016-09-24 22:45:42 +00002944 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002945 size_type __str_sz = __sv.size();
2946 if (__pos2 > __str_sz)
2947 this->__throw_out_of_range();
2948 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2949}
2950
2951template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002953basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002955 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956 return insert(__pos, __s, traits_type::length(__s));
2957}
2958
2959template <class _CharT, class _Traits, class _Allocator>
2960typename basic_string<_CharT, _Traits, _Allocator>::iterator
2961basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2962{
2963 size_type __ip = static_cast<size_type>(__pos - begin());
2964 size_type __sz = size();
2965 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002966 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967 if (__cap == __sz)
2968 {
2969 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002970 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 }
2972 else
2973 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002974 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975 size_type __n_move = __sz - __ip;
2976 if (__n_move != 0)
2977 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2978 }
2979 traits_type::assign(__p[__ip], __c);
2980 traits_type::assign(__p[++__sz], value_type());
2981 __set_size(__sz);
2982 return begin() + static_cast<difference_type>(__ip);
2983}
2984
2985template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002986inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987typename basic_string<_CharT, _Traits, _Allocator>::iterator
2988basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2989{
Nikolas Klausereed25832021-12-15 01:32:30 +01002990 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2991 "string::insert(iterator, n, value) called with an iterator not"
2992 " referring to this string");
2993 difference_type __p = __pos - begin();
2994 insert(static_cast<size_type>(__p), __n, __c);
2995 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002996}
2997
2998// replace
2999
3000template <class _CharT, class _Traits, class _Allocator>
3001basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003002basic_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 +00003003 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003005 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006 size_type __sz = size();
3007 if (__pos > __sz)
3008 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003009 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010 size_type __cap = capacity();
3011 if (__cap - __sz + __n1 >= __n2)
3012 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003013 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014 if (__n1 != __n2)
3015 {
3016 size_type __n_move = __sz - __pos - __n1;
3017 if (__n_move != 0)
3018 {
3019 if (__n1 > __n2)
3020 {
3021 traits_type::move(__p + __pos, __s, __n2);
3022 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003023 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024 }
3025 if (__p + __pos < __s && __s < __p + __sz)
3026 {
3027 if (__p + __pos + __n1 <= __s)
3028 __s += __n2 - __n1;
3029 else // __p + __pos < __s < __p + __pos + __n1
3030 {
3031 traits_type::move(__p + __pos, __s, __n1);
3032 __pos += __n1;
3033 __s += __n2;
3034 __n2 -= __n1;
3035 __n1 = 0;
3036 }
3037 }
3038 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3039 }
3040 }
3041 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003042 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043 }
3044 else
3045 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3046 return *this;
3047}
3048
3049template <class _CharT, class _Traits, class _Allocator>
3050basic_string<_CharT, _Traits, _Allocator>&
3051basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3052{
3053 size_type __sz = size();
3054 if (__pos > __sz)
3055 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003056 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003057 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003058 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059 if (__cap - __sz + __n1 >= __n2)
3060 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003061 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062 if (__n1 != __n2)
3063 {
3064 size_type __n_move = __sz - __pos - __n1;
3065 if (__n_move != 0)
3066 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3067 }
3068 }
3069 else
3070 {
3071 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003072 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 }
3074 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003075 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076}
3077
3078template <class _CharT, class _Traits, class _Allocator>
3079template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003080__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003082 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003084>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003085basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086 _InputIterator __j1, _InputIterator __j2)
3087{
Marshall Clow958362f2016-09-05 01:54:30 +00003088 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003089 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090}
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>&
3095basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3096{
3097 return replace(__pos1, __n1, __str.data(), __str.size());
3098}
3099
3100template <class _CharT, class _Traits, class _Allocator>
3101basic_string<_CharT, _Traits, _Allocator>&
3102basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3103 size_type __pos2, size_type __n2)
3104{
3105 size_type __str_sz = __str.size();
3106 if (__pos2 > __str_sz)
3107 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003108 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109}
3110
3111template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003112template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003113__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003114<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003115 __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 +00003116 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003117>
Marshall Clow82513342016-09-24 22:45:42 +00003118basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003119 size_type __pos2, size_type __n2)
3120{
Marshall Clow82513342016-09-24 22:45:42 +00003121 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003122 size_type __str_sz = __sv.size();
3123 if (__pos2 > __str_sz)
3124 this->__throw_out_of_range();
3125 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3126}
3127
3128template <class _CharT, class _Traits, class _Allocator>
3129basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003130basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003132 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 return replace(__pos, __n1, __s, traits_type::length(__s));
3134}
3135
3136template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003137inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003139basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140{
3141 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3142 __str.data(), __str.size());
3143}
3144
3145template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003146inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003148basic_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 +00003149{
3150 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3151}
3152
3153template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003154inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003156basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157{
3158 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3159}
3160
3161template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003162inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003164basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165{
3166 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3167}
3168
3169// erase
3170
Martijn Velsa81fc792020-02-26 13:25:43 -05003171// 'externally instantiated' erase() implementation, called when __n != npos.
3172// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003174void
3175basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3176 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 if (__n)
3179 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003180 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003181 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003182 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183 size_type __n_move = __sz - __pos - __n;
3184 if (__n_move != 0)
3185 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003186 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003188}
3189
3190template <class _CharT, class _Traits, class _Allocator>
3191basic_string<_CharT, _Traits, _Allocator>&
3192basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3193 size_type __n) {
3194 if (__pos > size()) this->__throw_out_of_range();
3195 if (__n == npos) {
3196 __erase_to_end(__pos);
3197 } else {
3198 __erase_external_with_move(__pos, __n);
3199 }
3200 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201}
3202
3203template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003204inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205typename basic_string<_CharT, _Traits, _Allocator>::iterator
3206basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3207{
Nikolas Klausereed25832021-12-15 01:32:30 +01003208 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3209 "string::erase(iterator) called with an iterator not"
3210 " referring to this string");
3211
3212 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3213 iterator __b = begin();
3214 size_type __r = static_cast<size_type>(__pos - __b);
3215 erase(__r, 1);
3216 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217}
3218
3219template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003220inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221typename basic_string<_CharT, _Traits, _Allocator>::iterator
3222basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3223{
Nikolas Klausereed25832021-12-15 01:32:30 +01003224 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3225 "string::erase(iterator, iterator) called with an iterator not"
3226 " referring to this string");
3227
3228 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3229 iterator __b = begin();
3230 size_type __r = static_cast<size_type>(__first - __b);
3231 erase(__r, static_cast<size_type>(__last - __first));
3232 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233}
3234
3235template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003236inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237void
3238basic_string<_CharT, _Traits, _Allocator>::pop_back()
3239{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003240 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003241 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003242}
3243
3244template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003245inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003247basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248{
3249 __invalidate_all_iterators();
3250 if (__is_long())
3251 {
3252 traits_type::assign(*__get_long_pointer(), value_type());
3253 __set_long_size(0);
3254 }
3255 else
3256 {
3257 traits_type::assign(*__get_short_pointer(), value_type());
3258 __set_short_size(0);
3259 }
3260}
3261
3262template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003263inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264void
3265basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3266{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003267 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268}
3269
3270template <class _CharT, class _Traits, class _Allocator>
3271void
3272basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3273{
3274 size_type __sz = size();
3275 if (__n > __sz)
3276 append(__n - __sz, __c);
3277 else
3278 __erase_to_end(__n);
3279}
3280
3281template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003282inline void
3283basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3284{
3285 size_type __sz = size();
3286 if (__n > __sz) {
3287 __append_default_init(__n - __sz);
3288 } else
3289 __erase_to_end(__n);
3290}
3291
3292template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003293inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003295basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003297 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003298#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003299 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003301 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302#endif
3303}
3304
3305template <class _CharT, class _Traits, class _Allocator>
3306void
Marek Kurdejc9848142020-11-26 10:07:16 +01003307basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308{
Marek Kurdejc9848142020-11-26 10:07:16 +01003309 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003311
3312#if _LIBCPP_STD_VER > 17
3313 // Reserve never shrinks as of C++20.
3314 if (__requested_capacity <= capacity()) return;
3315#endif
3316
3317 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3318 __target_capacity = __recommend(__target_capacity);
3319 if (__target_capacity == capacity()) return;
3320
3321 __shrink_or_extend(__target_capacity);
3322}
3323
3324template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003325inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003326void
3327basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3328{
3329 size_type __target_capacity = __recommend(size());
3330 if (__target_capacity == capacity()) return;
3331
3332 __shrink_or_extend(__target_capacity);
3333}
3334
3335template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003336inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003337void
3338basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3339{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340 size_type __cap = capacity();
3341 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003342
3343 pointer __new_data, __p;
3344 bool __was_long, __now_long;
3345 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003347 __was_long = true;
3348 __now_long = false;
3349 __new_data = __get_short_pointer();
3350 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003352 else
3353 {
3354 if (__target_capacity > __cap)
3355 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3356 else
3357 {
3358 #ifndef _LIBCPP_NO_EXCEPTIONS
3359 try
3360 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003361 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003362 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3363 #ifndef _LIBCPP_NO_EXCEPTIONS
3364 }
3365 catch (...)
3366 {
3367 return;
3368 }
3369 #else // _LIBCPP_NO_EXCEPTIONS
3370 if (__new_data == nullptr)
3371 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003372 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003373 }
3374 __now_long = true;
3375 __was_long = __is_long();
3376 __p = __get_pointer();
3377 }
3378 traits_type::copy(_VSTD::__to_address(__new_data),
3379 _VSTD::__to_address(__p), size()+1);
3380 if (__was_long)
3381 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3382 if (__now_long)
3383 {
3384 __set_long_cap(__target_capacity+1);
3385 __set_long_size(__sz);
3386 __set_long_pointer(__new_data);
3387 }
3388 else
3389 __set_short_size(__sz);
3390 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391}
3392
3393template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003394inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003396basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003398 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399 return *(data() + __pos);
3400}
3401
3402template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003403inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003405basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003406{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003407 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408 return *(__get_pointer() + __pos);
3409}
3410
3411template <class _CharT, class _Traits, class _Allocator>
3412typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3413basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3414{
3415 if (__n >= size())
3416 this->__throw_out_of_range();
3417 return (*this)[__n];
3418}
3419
3420template <class _CharT, class _Traits, class _Allocator>
3421typename basic_string<_CharT, _Traits, _Allocator>::reference
3422basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3423{
3424 if (__n >= size())
3425 this->__throw_out_of_range();
3426 return (*this)[__n];
3427}
3428
3429template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003430inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003432basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003433{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003434 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435 return *__get_pointer();
3436}
3437
3438template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003439inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003441basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003443 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444 return *data();
3445}
3446
3447template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003448inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003450basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003451{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003452 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453 return *(__get_pointer() + size() - 1);
3454}
3455
3456template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003457inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003459basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003461 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462 return *(data() + size() - 1);
3463}
3464
3465template <class _CharT, class _Traits, class _Allocator>
3466typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003467basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468{
3469 size_type __sz = size();
3470 if (__pos > __sz)
3471 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003472 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473 traits_type::copy(__s, data() + __pos, __rlen);
3474 return __rlen;
3475}
3476
3477template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003478inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479basic_string<_CharT, _Traits, _Allocator>
3480basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3481{
3482 return basic_string(*this, __pos, __n, __alloc());
3483}
3484
3485template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003486inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487void
3488basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003489#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003490 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003491#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003492 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003493 __is_nothrow_swappable<allocator_type>::value)
3494#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495{
Louis Dionneba400782020-10-02 15:02:52 -04003496#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003497 if (!__libcpp_is_constant_evaluated()) {
3498 if (!__is_long())
3499 __get_db()->__invalidate_all(this);
3500 if (!__str.__is_long())
3501 __get_db()->__invalidate_all(&__str);
3502 __get_db()->swap(this, &__str);
3503 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003504#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003505 _LIBCPP_ASSERT(
3506 __alloc_traits::propagate_on_container_swap::value ||
3507 __alloc_traits::is_always_equal::value ||
3508 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003509 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003510 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511}
3512
3513// find
3514
3515template <class _Traits>
3516struct _LIBCPP_HIDDEN __traits_eq
3517{
3518 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003519 _LIBCPP_INLINE_VISIBILITY
3520 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3521 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522};
3523
3524template<class _CharT, class _Traits, class _Allocator>
3525typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003526basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003527 size_type __pos,
3528 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003530 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003531 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003532 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533}
3534
3535template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003536inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003538basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3539 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003541 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003542 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543}
3544
3545template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003546template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003547__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003548<
3549 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3550 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003551>
Marshall Clowe46031a2018-07-02 18:41:15 +00003552basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003553 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003554{
Marshall Clowe46031a2018-07-02 18:41:15 +00003555 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003556 return __str_find<value_type, size_type, traits_type, npos>
3557 (data(), size(), __sv.data(), __pos, __sv.size());
3558}
3559
3560template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003561inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003562typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003563basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003564 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003566 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003567 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003568 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569}
3570
3571template<class _CharT, class _Traits, class _Allocator>
3572typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003573basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3574 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003576 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003577 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578}
3579
3580// rfind
3581
3582template<class _CharT, class _Traits, class _Allocator>
3583typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003584basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003585 size_type __pos,
3586 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003588 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003589 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003590 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591}
3592
3593template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003594inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003596basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3597 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003599 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003600 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601}
3602
3603template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003604template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003605__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003606<
3607 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3608 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003609>
Marshall Clowe46031a2018-07-02 18:41:15 +00003610basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003611 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003612{
Marshall Clowe46031a2018-07-02 18:41:15 +00003613 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003614 return __str_rfind<value_type, size_type, traits_type, npos>
3615 (data(), size(), __sv.data(), __pos, __sv.size());
3616}
3617
3618template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003619inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003620typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003621basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003622 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003624 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003625 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003626 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627}
3628
3629template<class _CharT, class _Traits, class _Allocator>
3630typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003631basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3632 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003634 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003635 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636}
3637
3638// find_first_of
3639
3640template<class _CharT, class _Traits, class _Allocator>
3641typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003642basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003643 size_type __pos,
3644 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003646 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003647 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003648 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649}
3650
3651template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003652inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003654basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3655 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003657 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003658 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659}
3660
3661template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003662template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003663__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003664<
3665 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3666 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003667>
Marshall Clowe46031a2018-07-02 18:41:15 +00003668basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003669 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003670{
Marshall Clowe46031a2018-07-02 18:41:15 +00003671 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003672 return __str_find_first_of<value_type, size_type, traits_type, npos>
3673 (data(), size(), __sv.data(), __pos, __sv.size());
3674}
3675
3676template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003677inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003678typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003679basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003680 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003682 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003683 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003684 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685}
3686
3687template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003688inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003690basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3691 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692{
3693 return find(__c, __pos);
3694}
3695
3696// find_last_of
3697
3698template<class _CharT, class _Traits, class _Allocator>
3699typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003700basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003701 size_type __pos,
3702 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003704 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003705 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003706 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707}
3708
3709template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003710inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003712basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3713 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003715 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003716 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717}
3718
3719template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003720template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003721__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003722<
3723 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3724 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003725>
Marshall Clowe46031a2018-07-02 18:41:15 +00003726basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003727 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003728{
Marshall Clowe46031a2018-07-02 18:41:15 +00003729 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003730 return __str_find_last_of<value_type, size_type, traits_type, npos>
3731 (data(), size(), __sv.data(), __pos, __sv.size());
3732}
3733
3734template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003735inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003736typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003737basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003738 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003739{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003740 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003741 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003742 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743}
3744
3745template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003746inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003748basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3749 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750{
3751 return rfind(__c, __pos);
3752}
3753
3754// find_first_not_of
3755
3756template<class _CharT, class _Traits, class _Allocator>
3757typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003758basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003759 size_type __pos,
3760 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003762 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003763 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003764 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765}
3766
3767template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003768inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003770basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3771 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003773 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003774 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775}
3776
3777template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003778template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003779__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003780<
3781 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3782 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003783>
Marshall Clowe46031a2018-07-02 18:41:15 +00003784basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003785 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003786{
Marshall Clowe46031a2018-07-02 18:41:15 +00003787 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003788 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3789 (data(), size(), __sv.data(), __pos, __sv.size());
3790}
3791
3792template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003793inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003794typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003795basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003796 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003798 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003799 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003800 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801}
3802
3803template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003804inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003805typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003806basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3807 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003808{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003809 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003810 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811}
3812
3813// find_last_not_of
3814
3815template<class _CharT, class _Traits, class _Allocator>
3816typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003817basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003818 size_type __pos,
3819 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003821 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003822 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003823 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824}
3825
3826template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003827inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003829basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3830 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003831{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003832 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003833 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834}
3835
3836template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003837template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003838__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003839<
3840 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3841 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003842>
Marshall Clowe46031a2018-07-02 18:41:15 +00003843basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003844 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003845{
Marshall Clowe46031a2018-07-02 18:41:15 +00003846 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003847 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3848 (data(), size(), __sv.data(), __pos, __sv.size());
3849}
3850
3851template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003852inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003853typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003854basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003855 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003857 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003858 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003859 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860}
3861
3862template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003863inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003865basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3866 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003868 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003869 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870}
3871
3872// compare
3873
3874template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003875template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003876__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003877<
3878 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3879 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003880>
zoecarver1997e0a2021-02-05 11:54:47 -08003881basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882{
Marshall Clowe46031a2018-07-02 18:41:15 +00003883 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003884 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003885 size_t __rhs_sz = __sv.size();
3886 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003887 _VSTD::min(__lhs_sz, __rhs_sz));
3888 if (__result != 0)
3889 return __result;
3890 if (__lhs_sz < __rhs_sz)
3891 return -1;
3892 if (__lhs_sz > __rhs_sz)
3893 return 1;
3894 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003895}
3896
3897template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003898inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003899int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003900basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003902 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003903}
3904
3905template <class _CharT, class _Traits, class _Allocator>
3906int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003907basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3908 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003909 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003910 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003912 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913 size_type __sz = size();
3914 if (__pos1 > __sz || __n2 == npos)
3915 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003916 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3917 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918 if (__r == 0)
3919 {
3920 if (__rlen < __n2)
3921 __r = -1;
3922 else if (__rlen > __n2)
3923 __r = 1;
3924 }
3925 return __r;
3926}
3927
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003928template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003929template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003930__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003931<
3932 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3933 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003934>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003935basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3936 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003937 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003938{
Marshall Clowe46031a2018-07-02 18:41:15 +00003939 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003940 return compare(__pos1, __n1, __sv.data(), __sv.size());
3941}
3942
3943template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003944inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003945int
3946basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3947 size_type __n1,
3948 const basic_string& __str) const
3949{
3950 return compare(__pos1, __n1, __str.data(), __str.size());
3951}
3952
3953template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003954template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003955__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003956<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003957 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3958 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003959 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003960>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003961basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3962 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003963 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003964 size_type __pos2,
3965 size_type __n2) const
3966{
Marshall Clow82513342016-09-24 22:45:42 +00003967 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003968 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3969}
3970
3971template <class _CharT, class _Traits, class _Allocator>
3972int
3973basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3974 size_type __n1,
3975 const basic_string& __str,
3976 size_type __pos2,
3977 size_type __n2) const
3978{
3979 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3980}
3981
3982template <class _CharT, class _Traits, class _Allocator>
3983int
3984basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3985{
3986 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3987 return compare(0, npos, __s, traits_type::length(__s));
3988}
3989
3990template <class _CharT, class _Traits, class _Allocator>
3991int
3992basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3993 size_type __n1,
3994 const value_type* __s) const
3995{
3996 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3997 return compare(__pos1, __n1, __s, traits_type::length(__s));
3998}
3999
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000// __invariants
4001
4002template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004003inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004bool
4005basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4006{
4007 if (size() > capacity())
4008 return false;
4009 if (capacity() < __min_cap - 1)
4010 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004011 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004013 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014 return false;
4015 return true;
4016}
4017
Vedant Kumar55e007e2018-03-08 21:15:26 +00004018// __clear_and_shrink
4019
4020template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004021inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004022void
Marshall Clowe60a7182018-05-29 17:04:37 +00004023basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004024{
4025 clear();
4026 if(__is_long())
4027 {
4028 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4029 __set_long_cap(0);
4030 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004031 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004032 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004033}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004034
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035// operator==
4036
4037template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004038inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039bool
4040operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004041 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004043 size_t __lhs_sz = __lhs.size();
4044 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4045 __rhs.data(),
4046 __lhs_sz) == 0;
4047}
4048
4049template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004050inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004051bool
4052operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4053 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4054{
4055 size_t __lhs_sz = __lhs.size();
4056 if (__lhs_sz != __rhs.size())
4057 return false;
4058 const char* __lp = __lhs.data();
4059 const char* __rp = __rhs.data();
4060 if (__lhs.__is_long())
4061 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4062 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4063 if (*__lp != *__rp)
4064 return false;
4065 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066}
4067
4068template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004069inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004071operator==(const _CharT* __lhs,
4072 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004074 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4075 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4076 size_t __lhs_len = _Traits::length(__lhs);
4077 if (__lhs_len != __rhs.size()) return false;
4078 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079}
4080
Howard Hinnantc51e1022010-05-11 19:42:16 +00004081template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004082inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004084operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4085 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004087 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4088 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4089 size_t __rhs_len = _Traits::length(__rhs);
4090 if (__rhs_len != __lhs.size()) return false;
4091 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092}
4093
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004094template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004095inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096bool
4097operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004098 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099{
4100 return !(__lhs == __rhs);
4101}
4102
4103template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004104inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004106operator!=(const _CharT* __lhs,
4107 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108{
4109 return !(__lhs == __rhs);
4110}
4111
4112template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004113inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004115operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4116 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117{
4118 return !(__lhs == __rhs);
4119}
4120
4121// operator<
4122
4123template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004124inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125bool
4126operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004127 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004129 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130}
4131
4132template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004133inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004135operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4136 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004138 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139}
4140
4141template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004142inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004144operator< (const _CharT* __lhs,
4145 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004146{
4147 return __rhs.compare(__lhs) > 0;
4148}
4149
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150// operator>
4151
4152template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004153inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154bool
4155operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004156 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157{
4158 return __rhs < __lhs;
4159}
4160
4161template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004162inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004163bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004164operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4165 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166{
4167 return __rhs < __lhs;
4168}
4169
4170template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004171inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004173operator> (const _CharT* __lhs,
4174 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004175{
4176 return __rhs < __lhs;
4177}
4178
4179// operator<=
4180
4181template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004182inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183bool
4184operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004185 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186{
4187 return !(__rhs < __lhs);
4188}
4189
4190template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004191inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004193operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4194 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195{
4196 return !(__rhs < __lhs);
4197}
4198
4199template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004200inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004202operator<=(const _CharT* __lhs,
4203 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204{
4205 return !(__rhs < __lhs);
4206}
4207
4208// operator>=
4209
4210template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004211inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212bool
4213operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004214 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215{
4216 return !(__lhs < __rhs);
4217}
4218
4219template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004222operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4223 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224{
4225 return !(__lhs < __rhs);
4226}
4227
4228template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004231operator>=(const _CharT* __lhs,
4232 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233{
4234 return !(__lhs < __rhs);
4235}
4236
4237// operator +
4238
4239template<class _CharT, class _Traits, class _Allocator>
4240basic_string<_CharT, _Traits, _Allocator>
4241operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4242 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4243{
4244 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4245 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4246 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4247 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4248 __r.append(__rhs.data(), __rhs_sz);
4249 return __r;
4250}
4251
4252template<class _CharT, class _Traits, class _Allocator>
4253basic_string<_CharT, _Traits, _Allocator>
4254operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4255{
4256 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4257 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4258 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4259 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4260 __r.append(__rhs.data(), __rhs_sz);
4261 return __r;
4262}
4263
4264template<class _CharT, class _Traits, class _Allocator>
4265basic_string<_CharT, _Traits, _Allocator>
4266operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4267{
4268 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4269 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4270 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4271 __r.append(__rhs.data(), __rhs_sz);
4272 return __r;
4273}
4274
4275template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004276inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004277basic_string<_CharT, _Traits, _Allocator>
4278operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4279{
4280 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4281 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4282 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4283 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4284 __r.append(__rhs, __rhs_sz);
4285 return __r;
4286}
4287
4288template<class _CharT, class _Traits, class _Allocator>
4289basic_string<_CharT, _Traits, _Allocator>
4290operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4291{
4292 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4293 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4294 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4295 __r.push_back(__rhs);
4296 return __r;
4297}
4298
Eric Fiselierfc92be82017-04-19 00:28:44 +00004299#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004300
4301template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004303basic_string<_CharT, _Traits, _Allocator>
4304operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4305{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004306 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307}
4308
4309template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004310inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311basic_string<_CharT, _Traits, _Allocator>
4312operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4313{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004314 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315}
4316
4317template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004318inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004319basic_string<_CharT, _Traits, _Allocator>
4320operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4321{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004322 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323}
4324
4325template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004326inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327basic_string<_CharT, _Traits, _Allocator>
4328operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4329{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004330 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331}
4332
4333template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004334inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335basic_string<_CharT, _Traits, _Allocator>
4336operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4337{
4338 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004339 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340}
4341
4342template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004343inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344basic_string<_CharT, _Traits, _Allocator>
4345operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4346{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004347 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348}
4349
4350template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004351inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352basic_string<_CharT, _Traits, _Allocator>
4353operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4354{
4355 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004356 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357}
4358
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004359#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360
4361// swap
4362
4363template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004364inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004366swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004367 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4368 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369{
4370 __lhs.swap(__rhs);
4371}
4372
Bruce Mitchener170d8972020-11-24 12:53:53 -05004373_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4374_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4375_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4376_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4377_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004378
Bruce Mitchener170d8972020-11-24 12:53:53 -05004379_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4380_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4381_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004382
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004383_LIBCPP_FUNC_VIS string to_string(int __val);
4384_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4385_LIBCPP_FUNC_VIS string to_string(long __val);
4386_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4387_LIBCPP_FUNC_VIS string to_string(long long __val);
4388_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4389_LIBCPP_FUNC_VIS string to_string(float __val);
4390_LIBCPP_FUNC_VIS string to_string(double __val);
4391_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004392
Louis Dionne89258142021-08-23 15:32:36 -04004393#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004394_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4395_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4396_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4397_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4398_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004399
Bruce Mitchener170d8972020-11-24 12:53:53 -05004400_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4401_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4402_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004403
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004404_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4405_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4406_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4407_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4408_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4409_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4410_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4411_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4412_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004413#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004414
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004416_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004417const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4418 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004419
Marshall Clow851b9ec2019-05-20 21:56:51 +00004420template <class _CharT, class _Allocator>
4421struct _LIBCPP_TEMPLATE_VIS
4422 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4423 : public unary_function<
4424 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004425{
4426 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004427 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4428 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004429};
4430
Howard Hinnantc51e1022010-05-11 19:42:16 +00004431
Howard Hinnantdc095972011-07-18 15:51:59 +00004432template<class _CharT, class _Traits, class _Allocator>
4433basic_ostream<_CharT, _Traits>&
4434operator<<(basic_ostream<_CharT, _Traits>& __os,
4435 const basic_string<_CharT, _Traits, _Allocator>& __str);
4436
4437template<class _CharT, class _Traits, class _Allocator>
4438basic_istream<_CharT, _Traits>&
4439operator>>(basic_istream<_CharT, _Traits>& __is,
4440 basic_string<_CharT, _Traits, _Allocator>& __str);
4441
4442template<class _CharT, class _Traits, class _Allocator>
4443basic_istream<_CharT, _Traits>&
4444getline(basic_istream<_CharT, _Traits>& __is,
4445 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4446
4447template<class _CharT, class _Traits, class _Allocator>
4448inline _LIBCPP_INLINE_VISIBILITY
4449basic_istream<_CharT, _Traits>&
4450getline(basic_istream<_CharT, _Traits>& __is,
4451 basic_string<_CharT, _Traits, _Allocator>& __str);
4452
Howard Hinnantdc095972011-07-18 15:51:59 +00004453template<class _CharT, class _Traits, class _Allocator>
4454inline _LIBCPP_INLINE_VISIBILITY
4455basic_istream<_CharT, _Traits>&
4456getline(basic_istream<_CharT, _Traits>&& __is,
4457 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4458
4459template<class _CharT, class _Traits, class _Allocator>
4460inline _LIBCPP_INLINE_VISIBILITY
4461basic_istream<_CharT, _Traits>&
4462getline(basic_istream<_CharT, _Traits>&& __is,
4463 basic_string<_CharT, _Traits, _Allocator>& __str);
4464
Marshall Clow29b53f22018-12-14 18:49:35 +00004465#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004466template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004467inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004468 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4469 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4470 auto __old_size = __str.size();
4471 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4472 return __old_size - __str.size();
4473}
Marshall Clow29b53f22018-12-14 18:49:35 +00004474
Marek Kurdeja98b1412020-05-02 13:58:03 +02004475template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004476inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004477 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4478 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4479 _Predicate __pred) {
4480 auto __old_size = __str.size();
4481 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4482 __str.end());
4483 return __old_size - __str.size();
4484}
Marshall Clow29b53f22018-12-14 18:49:35 +00004485#endif
4486
Louis Dionneba400782020-10-02 15:02:52 -04004487#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004488
4489template<class _CharT, class _Traits, class _Allocator>
4490bool
4491basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4492{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004493 return this->data() <= _VSTD::__to_address(__i->base()) &&
4494 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004495}
4496
4497template<class _CharT, class _Traits, class _Allocator>
4498bool
4499basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4500{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004501 return this->data() < _VSTD::__to_address(__i->base()) &&
4502 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004503}
4504
4505template<class _CharT, class _Traits, class _Allocator>
4506bool
4507basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4508{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004509 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004510 return this->data() <= __p && __p <= this->data() + this->size();
4511}
4512
4513template<class _CharT, class _Traits, class _Allocator>
4514bool
4515basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4516{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004517 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004518 return this->data() <= __p && __p < this->data() + this->size();
4519}
4520
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004521#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004522
Louis Dionne173f29e2019-05-29 16:01:36 +00004523#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004524// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004525inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004526{
4527 inline namespace string_literals
4528 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004529 inline _LIBCPP_INLINE_VISIBILITY
4530 basic_string<char> operator "" s( const char *__str, size_t __len )
4531 {
4532 return basic_string<char> (__str, __len);
4533 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004534
Louis Dionne89258142021-08-23 15:32:36 -04004535#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004536 inline _LIBCPP_INLINE_VISIBILITY
4537 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4538 {
4539 return basic_string<wchar_t> (__str, __len);
4540 }
Louis Dionne89258142021-08-23 15:32:36 -04004541#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004542
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004543#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004544 inline _LIBCPP_INLINE_VISIBILITY
4545 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4546 {
4547 return basic_string<char8_t> (__str, __len);
4548 }
4549#endif
4550
Howard Hinnant5c167562013-08-07 19:39:48 +00004551 inline _LIBCPP_INLINE_VISIBILITY
4552 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4553 {
4554 return basic_string<char16_t> (__str, __len);
4555 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004556
Howard Hinnant5c167562013-08-07 19:39:48 +00004557 inline _LIBCPP_INLINE_VISIBILITY
4558 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4559 {
4560 return basic_string<char32_t> (__str, __len);
4561 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004562 } // namespace string_literals
4563} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004564#endif
4565
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566_LIBCPP_END_NAMESPACE_STD
4567
Eric Fiselierf4433a32017-05-31 22:07:49 +00004568_LIBCPP_POP_MACROS
4569
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004570#endif // _LIBCPP_STRING