blob: 53b4e09b68f34ff3b2909342c2c7bb3b6fcb1585 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- string -----------------------------------===//
3//
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));
Louis Dionneba400782020-10-02 15:02:52 -0400829# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000830 __get_db()->__insert_c(this);
831# endif
832 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000833
Louis Dionne9ce598d2021-09-08 09:14:43 -0400834 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000835 _LIBCPP_INLINE_VISIBILITY
836 basic_string(const _CharT* __s, const _Allocator& __a);
837
Marek Kurdej90d79712021-07-27 16:16:21 +0200838#if _LIBCPP_STD_VER > 20
839 basic_string(nullptr_t) = delete;
840#endif
841
Howard Hinnantcf823322010-12-17 14:46:43 +0000842 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000843 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000844 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000845 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000846 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000847 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000848
Louis Dionne9ce598d2021-09-08 09:14:43 -0400849 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000850 _LIBCPP_INLINE_VISIBILITY
851 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
852
Marshall Clow83445802016-04-07 18:13:41 +0000853 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000854 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000855 _LIBCPP_INLINE_VISIBILITY
856 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000857 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000858
Louis Dionne9ce598d2021-09-08 09:14:43 -0400859 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000860 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000861 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500862 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000863
Louis Dionne9ce598d2021-09-08 09:14:43 -0400864 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500865 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000866 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
867 explicit basic_string(const _Tp& __t);
868
Louis Dionne9ce598d2021-09-08 09:14:43 -0400869 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 +0000870 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
871 explicit basic_string(const _Tp& __t, const allocator_type& __a);
872
Louis Dionne9ce598d2021-09-08 09:14:43 -0400873 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400876 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000879#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000880 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000881 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000882 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000883 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400884#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000886 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000888 _LIBCPP_INLINE_VISIBILITY
889 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
890
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000891 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000892
Louis Dionne9ce598d2021-09-08 09:14:43 -0400893 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 +0000894 basic_string& operator=(const _Tp& __t)
895 {__self_view __sv = __t; return assign(__sv);}
896
Eric Fiselierfc92be82017-04-19 00:28:44 +0000897#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000899 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000900 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000901 _LIBCPP_INLINE_VISIBILITY
902 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000904 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200905#if _LIBCPP_STD_VER > 20
906 basic_string& operator=(nullptr_t) = delete;
907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909
Louis Dionneba400782020-10-02 15:02:52 -0400910#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000911 _LIBCPP_INLINE_VISIBILITY
912 iterator begin() _NOEXCEPT
913 {return iterator(this, __get_pointer());}
914 _LIBCPP_INLINE_VISIBILITY
915 const_iterator begin() const _NOEXCEPT
916 {return const_iterator(this, __get_pointer());}
917 _LIBCPP_INLINE_VISIBILITY
918 iterator end() _NOEXCEPT
919 {return iterator(this, __get_pointer() + size());}
920 _LIBCPP_INLINE_VISIBILITY
921 const_iterator end() const _NOEXCEPT
922 {return const_iterator(this, __get_pointer() + size());}
923#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000924 _LIBCPP_INLINE_VISIBILITY
925 iterator begin() _NOEXCEPT
926 {return iterator(__get_pointer());}
927 _LIBCPP_INLINE_VISIBILITY
928 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000929 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000930 _LIBCPP_INLINE_VISIBILITY
931 iterator end() _NOEXCEPT
932 {return iterator(__get_pointer() + size());}
933 _LIBCPP_INLINE_VISIBILITY
934 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000935 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400936#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000937 _LIBCPP_INLINE_VISIBILITY
938 reverse_iterator rbegin() _NOEXCEPT
939 {return reverse_iterator(end());}
940 _LIBCPP_INLINE_VISIBILITY
941 const_reverse_iterator rbegin() const _NOEXCEPT
942 {return const_reverse_iterator(end());}
943 _LIBCPP_INLINE_VISIBILITY
944 reverse_iterator rend() _NOEXCEPT
945 {return reverse_iterator(begin());}
946 _LIBCPP_INLINE_VISIBILITY
947 const_reverse_iterator rend() const _NOEXCEPT
948 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000950 _LIBCPP_INLINE_VISIBILITY
951 const_iterator cbegin() const _NOEXCEPT
952 {return begin();}
953 _LIBCPP_INLINE_VISIBILITY
954 const_iterator cend() const _NOEXCEPT
955 {return end();}
956 _LIBCPP_INLINE_VISIBILITY
957 const_reverse_iterator crbegin() const _NOEXCEPT
958 {return rbegin();}
959 _LIBCPP_INLINE_VISIBILITY
960 const_reverse_iterator crend() const _NOEXCEPT
961 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000963 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000965 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
966 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
967 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000968 {return (__is_long() ? __get_long_cap()
969 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970
971 void resize(size_type __n, value_type __c);
972 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
973
Marek Kurdejc9848142020-11-26 10:07:16 +0100974 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000975 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
976
Marek Kurdejc9848142020-11-26 10:07:16 +0100977 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
978 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000979 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100980 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000982 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000983 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
984 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000986 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
987 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988
989 const_reference at(size_type __n) const;
990 reference at(size_type __n);
991
992 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000993
994 template <class _Tp>
995 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400996 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000997 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500998 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
999 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001000 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001001 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001002 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001003 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001005#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001007#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008
Howard Hinnantcf823322010-12-17 14:46:43 +00001009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001011
1012 template <class _Tp>
1013 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001014 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001015 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1016 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001017 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001018 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001019 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001020 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001021
Marshall Clow62953962016-10-03 23:40:48 +00001022 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001023 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001024 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001025 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1027 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001028 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001029 >
Marshall Clow82513342016-09-24 22:45:42 +00001030 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001031 basic_string& append(const value_type* __s, size_type __n);
1032 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001034
1035 _LIBCPP_INLINE_VISIBILITY
1036 void __append_default_init(size_type __n);
1037
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001039 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001040 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001042 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001044 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001045 _LIBCPP_INLINE_VISIBILITY
1046 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001047 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001048 append(__temp.data(), __temp.size());
1049 return *this;
1050 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001052 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001053 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001055 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001057 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001058 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001059 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001060
Eric Fiselierfc92be82017-04-19 00:28:44 +00001061#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001064#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065
1066 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001069 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1070 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1071 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1072 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073
Marshall Clowe46031a2018-07-02 18:41:15 +00001074 template <class _Tp>
1075 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001076 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001077 <
1078 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1079 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001080 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001081 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001082 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001083 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001084#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001085 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001086 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001087 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001088 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001089#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001090 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001091 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001092 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001093 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001094 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1096 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001097 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001098 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001099 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001100 basic_string& assign(const value_type* __s, size_type __n);
1101 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 basic_string& assign(size_type __n, value_type __c);
1103 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001104 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001105 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001107 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001109 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 assign(_InputIterator __first, _InputIterator __last);
1111 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001112 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001113 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001115 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001117 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001119#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001122#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123
Howard Hinnantcf823322010-12-17 14:46:43 +00001124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001126
1127 template <class _Tp>
1128 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001129 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001130 <
1131 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1132 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001134 insert(size_type __pos1, const _Tp& __t)
1135 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1136
Marshall Clow82513342016-09-24 22:45:42 +00001137 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001138 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001139 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001140 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001141 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001142 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001143 >
Marshall Clow82513342016-09-24 22:45:42 +00001144 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001145 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001146 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1147 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1149 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1152 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001153 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001154 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001156 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001158 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1160 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001161 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001162 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001164 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001166 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001168#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1171 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001172#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173
1174 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 iterator erase(const_iterator __first, const_iterator __last);
1179
Howard Hinnantcf823322010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001182
1183 template <class _Tp>
1184 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001185 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001186 <
1187 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1188 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001189 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001190 replace(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 +00001191 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 +00001192 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001193 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001194 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001195 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001196 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001197 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001198 >
Marshall Clow82513342016-09-24 22:45:42 +00001199 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001200 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1201 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001204 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001205
1206 template <class _Tp>
1207 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001208 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001209 <
1210 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1211 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001212 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001213 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1214
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001216 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001218 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001220 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001222 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001223 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001225 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001227 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001228 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001229#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001231 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001233#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234
Howard Hinnantd17880b2013-06-28 16:59:19 +00001235 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1238
Howard Hinnantcf823322010-12-17 14:46:43 +00001239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001240 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001241#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001242 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001243#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001244 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001245 __is_nothrow_swappable<allocator_type>::value);
1246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247
Howard Hinnantcf823322010-12-17 14:46:43 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001249 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001250 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001251 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001252#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001253 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001254 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001255#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256
Howard Hinnantcf823322010-12-17 14:46:43 +00001257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001258 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259
Howard Hinnantcf823322010-12-17 14:46:43 +00001260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001261 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001262
1263 template <class _Tp>
1264 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001265 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001266 <
1267 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1268 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001269 >
zoecarver1997e0a2021-02-05 11:54:47 -08001270 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001271 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001273 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001274 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Howard Hinnantcf823322010-12-17 14:46:43 +00001276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001277 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001278
1279 template <class _Tp>
1280 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001281 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001282 <
1283 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1284 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001285 >
zoecarver1997e0a2021-02-05 11:54:47 -08001286 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001287 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001289 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001290 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291
Howard Hinnantcf823322010-12-17 14:46:43 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001293 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001294
1295 template <class _Tp>
1296 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001297 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001298 <
1299 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1300 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001301 >
zoecarver1997e0a2021-02-05 11:54:47 -08001302 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001303 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001305 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001307 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308
Howard Hinnantcf823322010-12-17 14:46:43 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001310 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001311
1312 template <class _Tp>
1313 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001314 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001315 <
1316 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1317 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001318 >
zoecarver1997e0a2021-02-05 11:54:47 -08001319 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001320 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001322 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001324 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
Howard Hinnantcf823322010-12-17 14:46:43 +00001326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001327 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001328
1329 template <class _Tp>
1330 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001331 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001332 <
1333 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1334 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001335 >
zoecarver1997e0a2021-02-05 11:54:47 -08001336 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001337 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 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001339 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001340 _LIBCPP_INLINE_VISIBILITY
1341 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1342
1343 _LIBCPP_INLINE_VISIBILITY
1344 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001345
1346 template <class _Tp>
1347 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001348 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001349 <
1350 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1351 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001352 >
zoecarver1997e0a2021-02-05 11:54:47 -08001353 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001354 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 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001356 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001357 _LIBCPP_INLINE_VISIBILITY
1358 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1359
1360 _LIBCPP_INLINE_VISIBILITY
1361 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001362
1363 template <class _Tp>
1364 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001365 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001366 <
1367 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1368 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001369 >
zoecarver1997e0a2021-02-05 11:54:47 -08001370 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001371
1372 template <class _Tp>
1373 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001374 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001375 <
1376 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1377 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001378 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001379 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1380
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001383 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 +00001384
Marshall Clow82513342016-09-24 22:45:42 +00001385 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001386 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001387 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001388 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001389 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001390 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001391 >
Marshall Clow82513342016-09-24 22:45:42 +00001392 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 +00001393 int compare(const value_type* __s) const _NOEXCEPT;
1394 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1395 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396
Marshall Clow18c293b2017-12-04 20:11:38 +00001397#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001398 constexpr _LIBCPP_INLINE_VISIBILITY
1399 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001400 { return __self_view(data(), size()).starts_with(__sv); }
1401
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001402 constexpr _LIBCPP_INLINE_VISIBILITY
1403 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001404 { return !empty() && _Traits::eq(front(), __c); }
1405
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001406 constexpr _LIBCPP_INLINE_VISIBILITY
1407 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001408 { return starts_with(__self_view(__s)); }
1409
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001410 constexpr _LIBCPP_INLINE_VISIBILITY
1411 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001412 { return __self_view(data(), size()).ends_with( __sv); }
1413
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001414 constexpr _LIBCPP_INLINE_VISIBILITY
1415 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001416 { return !empty() && _Traits::eq(back(), __c); }
1417
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001418 constexpr _LIBCPP_INLINE_VISIBILITY
1419 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001420 { return ends_with(__self_view(__s)); }
1421#endif
1422
Wim Leflere023c3542021-01-19 14:33:30 -05001423#if _LIBCPP_STD_VER > 20
1424 constexpr _LIBCPP_INLINE_VISIBILITY
1425 bool contains(__self_view __sv) const noexcept
1426 { return __self_view(data(), size()).contains(__sv); }
1427
1428 constexpr _LIBCPP_INLINE_VISIBILITY
1429 bool contains(value_type __c) const noexcept
1430 { return __self_view(data(), size()).contains(__c); }
1431
1432 constexpr _LIBCPP_INLINE_VISIBILITY
1433 bool contains(const value_type* __s) const
1434 { return __self_view(data(), size()).contains(__s); }
1435#endif
1436
Howard Hinnantcf823322010-12-17 14:46:43 +00001437 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001438
Marshall Clowe60a7182018-05-29 17:04:37 +00001439 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001440
Marek Kurdejc9848142020-11-26 10:07:16 +01001441 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1442
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001443 _LIBCPP_INLINE_VISIBILITY
1444 bool __is_long() const _NOEXCEPT
1445 {return bool(__r_.first().__s.__size_ & __short_mask);}
1446
Louis Dionneba400782020-10-02 15:02:52 -04001447#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001448
1449 bool __dereferenceable(const const_iterator* __i) const;
1450 bool __decrementable(const const_iterator* __i) const;
1451 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1452 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1453
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001454#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001455
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001457 _LIBCPP_INLINE_VISIBILITY
1458 allocator_type& __alloc() _NOEXCEPT
1459 {return __r_.second();}
1460 _LIBCPP_INLINE_VISIBILITY
1461 const allocator_type& __alloc() const _NOEXCEPT
1462 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001464#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001465
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001467 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001468# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001470# else
1471 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1472# endif
1473
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001474 _LIBCPP_INLINE_VISIBILITY
1475 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001476# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001478# else
1479 {return __r_.first().__s.__size_;}
1480# endif
1481
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001482#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001483
1484 _LIBCPP_INLINE_VISIBILITY
1485 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001486# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001487 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1488# else
1489 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1490# endif
1491
1492 _LIBCPP_INLINE_VISIBILITY
1493 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001494# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001495 {return __r_.first().__s.__size_;}
1496# else
1497 {return __r_.first().__s.__size_ >> 1;}
1498# endif
1499
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001500#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001501
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001502 _LIBCPP_INLINE_VISIBILITY
1503 void __set_long_size(size_type __s) _NOEXCEPT
1504 {__r_.first().__l.__size_ = __s;}
1505 _LIBCPP_INLINE_VISIBILITY
1506 size_type __get_long_size() const _NOEXCEPT
1507 {return __r_.first().__l.__size_;}
1508 _LIBCPP_INLINE_VISIBILITY
1509 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1511
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 void __set_long_cap(size_type __s) _NOEXCEPT
1514 {__r_.first().__l.__cap_ = __long_mask | __s;}
1515 _LIBCPP_INLINE_VISIBILITY
1516 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001517 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __set_long_pointer(pointer __p) _NOEXCEPT
1521 {__r_.first().__l.__data_ = __p;}
1522 _LIBCPP_INLINE_VISIBILITY
1523 pointer __get_long_pointer() _NOEXCEPT
1524 {return __r_.first().__l.__data_;}
1525 _LIBCPP_INLINE_VISIBILITY
1526 const_pointer __get_long_pointer() const _NOEXCEPT
1527 {return __r_.first().__l.__data_;}
1528 _LIBCPP_INLINE_VISIBILITY
1529 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001530 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001531 _LIBCPP_INLINE_VISIBILITY
1532 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001533 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001534 _LIBCPP_INLINE_VISIBILITY
1535 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001537 _LIBCPP_INLINE_VISIBILITY
1538 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1540
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001541 _LIBCPP_INLINE_VISIBILITY
1542 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 {
1544 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1545 for (unsigned __i = 0; __i < __n_words; ++__i)
1546 __a[__i] = 0;
1547 }
1548
1549 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001551 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001552 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001554 static _LIBCPP_INLINE_VISIBILITY
1555 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001556 {
1557 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1558 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1559 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1560 if (__guess == __min_cap) ++__guess;
1561 return __guess;
1562 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001564 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001565 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001566 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001567 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001568 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001570
Martijn Vels5e7c9752020-03-04 17:52:46 -05001571 // Slow path for the (inlined) copy constructor for 'long' strings.
1572 // Always externally instantiated and not inlined.
1573 // Requires that __s is zero terminated.
1574 // The main reason for this function to exist is because for unstable, we
1575 // want to allow inlining of the copy constructor. However, we don't want
1576 // to call the __init() functions as those are marked as inline which may
1577 // result in over-aggressive inlining by the compiler, where our aim is
1578 // to only inline the fast path code directly in the ctor.
1579 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1580
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001582 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001583 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001585 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1586 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 __init(_InputIterator __first, _InputIterator __last);
1588
1589 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001590 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001591 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001593 __is_cpp17_forward_iterator<_ForwardIterator>::value
1594 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 __init(_ForwardIterator __first, _ForwardIterator __last);
1596
1597 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001598 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1600 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001601 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602
Martijn Vels596e3de2020-02-26 15:55:49 -05001603 // __assign_no_alias is invoked for assignment operations where we
1604 // have proof that the input does not alias the current instance.
1605 // For example, operator=(basic_string) performs a 'self' check.
1606 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001607 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001608
Howard Hinnantcf823322010-12-17 14:46:43 +00001609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 void __erase_to_end(size_type __pos);
1611
Martijn Velsa81fc792020-02-26 13:25:43 -05001612 // __erase_external_with_move is invoked for erase() invocations where
1613 // `n ~= npos`, likely requiring memory moves on the string data.
1614 void __erase_external_with_move(size_type __pos, size_type __n);
1615
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001616 _LIBCPP_INLINE_VISIBILITY
1617 void __copy_assign_alloc(const basic_string& __str)
1618 {__copy_assign_alloc(__str, integral_constant<bool,
1619 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1620
1621 _LIBCPP_INLINE_VISIBILITY
1622 void __copy_assign_alloc(const basic_string& __str, true_type)
1623 {
Marshall Clowf258c202017-01-31 03:40:52 +00001624 if (__alloc() == __str.__alloc())
1625 __alloc() = __str.__alloc();
1626 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001627 {
Marshall Clowf258c202017-01-31 03:40:52 +00001628 if (!__str.__is_long())
1629 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001630 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001631 __alloc() = __str.__alloc();
1632 }
1633 else
1634 {
1635 allocator_type __a = __str.__alloc();
1636 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001637 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001638 __alloc() = _VSTD::move(__a);
1639 __set_long_pointer(__p);
1640 __set_long_cap(__str.__get_long_cap());
1641 __set_long_size(__str.size());
1642 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001643 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001644 }
1645
1646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001647 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001648 {}
1649
Eric Fiselierfc92be82017-04-19 00:28:44 +00001650#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001651 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001652 void __move_assign(basic_string& __str, false_type)
1653 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001655 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001656#if _LIBCPP_STD_VER > 14
1657 _NOEXCEPT;
1658#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001659 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001660#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001661#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001662
1663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001664 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001665 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001666 _NOEXCEPT_(
1667 !__alloc_traits::propagate_on_container_move_assignment::value ||
1668 is_nothrow_move_assignable<allocator_type>::value)
1669 {__move_assign_alloc(__str, integral_constant<bool,
1670 __alloc_traits::propagate_on_container_move_assignment::value>());}
1671
1672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001673 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001674 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1675 {
1676 __alloc() = _VSTD::move(__c.__alloc());
1677 }
1678
1679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001680 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001681 _NOEXCEPT
1682 {}
1683
Martijn Velsda7d94f2020-06-19 14:24:03 -04001684 basic_string& __assign_external(const value_type* __s);
1685 basic_string& __assign_external(const value_type* __s, size_type __n);
1686
1687 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1688 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1689 pointer __p = __is_long()
1690 ? (__set_long_size(__n), __get_long_pointer())
1691 : (__set_short_size(__n), __get_short_pointer());
1692 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1693 traits_type::assign(__p[__n], value_type());
1694 return *this;
1695 }
1696
Howard Hinnantcf823322010-12-17 14:46:43 +00001697 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1698 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001700 template<class _Tp>
1701 _LIBCPP_INLINE_VISIBILITY
1702 bool __addr_in_range(_Tp&& __t) const {
1703 const volatile void *__p = _VSTD::addressof(__t);
1704 return data() <= __p && __p <= data() + size();
1705 }
1706
Louis Dionned24191c2021-08-19 12:39:16 -04001707 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1708 void __throw_length_error() const {
1709#ifndef _LIBCPP_NO_EXCEPTIONS
1710 __basic_string_common<true>::__throw_length_error();
1711#else
1712 _VSTD::abort();
1713#endif
1714 }
1715
1716 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1717 void __throw_out_of_range() const {
1718#ifndef _LIBCPP_NO_EXCEPTIONS
1719 __basic_string_common<true>::__throw_out_of_range();
1720#else
1721 _VSTD::abort();
1722#endif
1723 }
1724
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725 friend basic_string operator+<>(const basic_string&, const basic_string&);
1726 friend basic_string operator+<>(const value_type*, const basic_string&);
1727 friend basic_string operator+<>(value_type, const basic_string&);
1728 friend basic_string operator+<>(const basic_string&, const value_type*);
1729 friend basic_string operator+<>(const basic_string&, value_type);
1730};
1731
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001732// These declarations must appear before any functions are implicitly used
1733// so that they have the correct visibility specifier.
1734#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001735 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1736# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1737 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1738# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001739#else
Louis Dionne89258142021-08-23 15:32:36 -04001740 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1741# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1742 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1743# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001744#endif
1745
1746
Louis Dionned59f8a52021-08-17 11:59:07 -04001747#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001748template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001749 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001750 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001751 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1752 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001753 >
1754basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1755 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001756
1757template<class _CharT,
1758 class _Traits,
1759 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001760 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001761 >
1762explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1763 -> basic_string<_CharT, _Traits, _Allocator>;
1764
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 class _Sz = typename allocator_traits<_Allocator>::size_type
1770 >
1771basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1772 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001773#endif
1774
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001776inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777void
1778basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1779{
Louis Dionneba400782020-10-02 15:02:52 -04001780#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001781 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001782#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783}
1784
1785template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001786inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001788basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789{
Louis Dionneba400782020-10-02 15:02:52 -04001790#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001791 __c_node* __c = __get_db()->__find_c_and_lock(this);
1792 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001794 const_pointer __new_last = __get_pointer() + __pos;
1795 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001797 --__p;
1798 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1799 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001801 (*__p)->__c_ = nullptr;
1802 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001803 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001806 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001808#else
1809 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001810#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811}
1812
1813template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001814inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001815basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001816 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001817 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818{
Louis Dionneba400782020-10-02 15:02:52 -04001819#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001820 __get_db()->__insert_c(this);
1821#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822 __zero();
1823}
1824
1825template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001826inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001828#if _LIBCPP_STD_VER <= 14
1829 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1830#else
1831 _NOEXCEPT
1832#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001833: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834{
Louis Dionneba400782020-10-02 15:02:52 -04001835#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001836 __get_db()->__insert_c(this);
1837#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 __zero();
1839}
1840
1841template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001842void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1843 size_type __sz,
1844 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845{
1846 if (__reserve > max_size())
1847 this->__throw_length_error();
1848 pointer __p;
1849 if (__reserve < __min_cap)
1850 {
1851 __set_short_size(__sz);
1852 __p = __get_short_pointer();
1853 }
1854 else
1855 {
1856 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001857 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 __set_long_pointer(__p);
1859 __set_long_cap(__cap+1);
1860 __set_long_size(__sz);
1861 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001862 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 traits_type::assign(__p[__sz], value_type());
1864}
1865
1866template <class _CharT, class _Traits, class _Allocator>
1867void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001868basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869{
1870 if (__sz > max_size())
1871 this->__throw_length_error();
1872 pointer __p;
1873 if (__sz < __min_cap)
1874 {
1875 __set_short_size(__sz);
1876 __p = __get_short_pointer();
1877 }
1878 else
1879 {
1880 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001881 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882 __set_long_pointer(__p);
1883 __set_long_cap(__cap+1);
1884 __set_long_size(__sz);
1885 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001886 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887 traits_type::assign(__p[__sz], value_type());
1888}
1889
1890template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001891template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001892basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001893 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001895 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001897#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001898 __get_db()->__insert_c(this);
1899#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900}
1901
1902template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001903inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001904basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001905 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001907 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001909#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001910 __get_db()->__insert_c(this);
1911#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912}
1913
1914template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001915inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001916basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001917 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001919 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001921#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001922 __get_db()->__insert_c(this);
1923#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924}
1925
1926template <class _CharT, class _Traits, class _Allocator>
1927basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001928 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929{
1930 if (!__str.__is_long())
1931 __r_.first().__r = __str.__r_.first().__r;
1932 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001933 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1934 __str.__get_long_size());
1935
Louis Dionneba400782020-10-02 15:02:52 -04001936#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001937 __get_db()->__insert_c(this);
1938#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939}
1940
1941template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001942basic_string<_CharT, _Traits, _Allocator>::basic_string(
1943 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001944 : __r_(__default_init_tag(), __a)
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());
Louis Dionneba400782020-10-02 15:02:52 -04001951#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001952 __get_db()->__insert_c(this);
1953#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954}
1955
Martijn Vels5e7c9752020-03-04 17:52:46 -05001956template <class _CharT, class _Traits, class _Allocator>
1957void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1958 const value_type* __s, size_type __sz) {
1959 pointer __p;
1960 if (__sz < __min_cap) {
1961 __p = __get_short_pointer();
1962 __set_short_size(__sz);
1963 } else {
1964 if (__sz > max_size())
1965 this->__throw_length_error();
1966 size_t __cap = __recommend(__sz);
1967 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1968 __set_long_pointer(__p);
1969 __set_long_cap(__cap + 1);
1970 __set_long_size(__sz);
1971 }
1972 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1973}
1974
Eric Fiselierfc92be82017-04-19 00:28:44 +00001975#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976
1977template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001978inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001979basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001980#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001981 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001982#else
1983 _NOEXCEPT
1984#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001985 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986{
1987 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001988#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001989 __get_db()->__insert_c(this);
1990 if (__is_long())
1991 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992#endif
1993}
1994
1995template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001996inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001998 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999{
Marshall Clowd2d24692014-07-17 15:32:20 +00002000 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002001 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002002 else
2003 {
2004 __r_.first().__r = __str.__r_.first().__r;
2005 __str.__zero();
2006 }
Louis Dionneba400782020-10-02 15:02:52 -04002007#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002008 __get_db()->__insert_c(this);
2009 if (__is_long())
2010 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011#endif
2012}
2013
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002014#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015
2016template <class _CharT, class _Traits, class _Allocator>
2017void
2018basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2019{
2020 if (__n > max_size())
2021 this->__throw_length_error();
2022 pointer __p;
2023 if (__n < __min_cap)
2024 {
2025 __set_short_size(__n);
2026 __p = __get_short_pointer();
2027 }
2028 else
2029 {
2030 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002031 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032 __set_long_pointer(__p);
2033 __set_long_cap(__cap+1);
2034 __set_long_size(__n);
2035 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002036 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037 traits_type::assign(__p[__n], value_type());
2038}
2039
2040template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002041inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002042basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002043 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044{
2045 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002046#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002047 __get_db()->__insert_c(this);
2048#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049}
2050
2051template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002052template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002053basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002054 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055{
2056 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002057#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002058 __get_db()->__insert_c(this);
2059#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060}
2061
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002063basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2064 size_type __pos, size_type __n,
2065 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002066 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067{
2068 size_type __str_sz = __str.size();
2069 if (__pos > __str_sz)
2070 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002071 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002072#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002073 __get_db()->__insert_c(this);
2074#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075}
2076
2077template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002078inline
Marshall Clow83445802016-04-07 18:13:41 +00002079basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002080 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002081 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002082{
2083 size_type __str_sz = __str.size();
2084 if (__pos > __str_sz)
2085 this->__throw_out_of_range();
2086 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002087#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002088 __get_db()->__insert_c(this);
2089#endif
2090}
2091
2092template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002093template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002094basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002095 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002096 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002097{
Marshall Clowe46031a2018-07-02 18:41:15 +00002098 __self_view __sv0 = __t;
2099 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002100 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002101#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002102 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002103#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002104}
2105
2106template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002107template <class _Tp, class>
2108basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002109 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002110{
Marshall Clowe46031a2018-07-02 18:41:15 +00002111 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002112 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002113#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002114 __get_db()->__insert_c(this);
2115#endif
2116}
2117
2118template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002119template <class _Tp, class>
2120basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002121 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002122{
Marshall Clowe46031a2018-07-02 18:41:15 +00002123 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002124 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002125#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002126 __get_db()->__insert_c(this);
2127#endif
2128}
2129
2130template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002132__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002134 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2135>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2137{
2138 __zero();
2139#ifndef _LIBCPP_NO_EXCEPTIONS
2140 try
2141 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002142#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 for (; __first != __last; ++__first)
2144 push_back(*__first);
2145#ifndef _LIBCPP_NO_EXCEPTIONS
2146 }
2147 catch (...)
2148 {
2149 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002150 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151 throw;
2152 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002153#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154}
2155
2156template <class _CharT, class _Traits, class _Allocator>
2157template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002158__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002160 __is_cpp17_forward_iterator<_ForwardIterator>::value
2161>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2163{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002164 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165 if (__sz > max_size())
2166 this->__throw_length_error();
2167 pointer __p;
2168 if (__sz < __min_cap)
2169 {
2170 __set_short_size(__sz);
2171 __p = __get_short_pointer();
2172 }
2173 else
2174 {
2175 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002176 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 __set_long_pointer(__p);
2178 __set_long_cap(__cap+1);
2179 __set_long_size(__sz);
2180 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002181
2182#ifndef _LIBCPP_NO_EXCEPTIONS
2183 try
2184 {
2185#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002186 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 traits_type::assign(*__p, *__first);
2188 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002189#ifndef _LIBCPP_NO_EXCEPTIONS
2190 }
2191 catch (...)
2192 {
2193 if (__is_long())
2194 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2195 throw;
2196 }
2197#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198}
2199
2200template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002201template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002202inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002204 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205{
2206 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002207#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002208 __get_db()->__insert_c(this);
2209#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210}
2211
2212template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002213template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002214inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2216 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002217 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218{
2219 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002220#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002221 __get_db()->__insert_c(this);
2222#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223}
2224
Eric Fiselierfc92be82017-04-19 00:28:44 +00002225#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002226
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002228inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002229basic_string<_CharT, _Traits, _Allocator>::basic_string(
2230 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002231 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232{
2233 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002234#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002235 __get_db()->__insert_c(this);
2236#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237}
2238
2239template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002240inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002241
Eric Fiselier812882b2017-02-17 01:17:10 +00002242basic_string<_CharT, _Traits, _Allocator>::basic_string(
2243 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002244 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245{
2246 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002247#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002248 __get_db()->__insert_c(this);
2249#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250}
2251
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002252#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002253
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2256{
Louis Dionneba400782020-10-02 15:02:52 -04002257#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002258 __get_db()->__erase_c(this);
2259#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002261 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262}
2263
2264template <class _CharT, class _Traits, class _Allocator>
2265void
2266basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2267 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002268 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 +00002269{
2270 size_type __ms = max_size();
2271 if (__delta_cap > __ms - __old_cap - 1)
2272 this->__throw_length_error();
2273 pointer __old_p = __get_pointer();
2274 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002275 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002277 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278 __invalidate_all_iterators();
2279 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002280 traits_type::copy(_VSTD::__to_address(__p),
2281 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002283 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2285 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002286 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2287 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002289 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290 __set_long_pointer(__p);
2291 __set_long_cap(__cap+1);
2292 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2293 __set_long_size(__old_sz);
2294 traits_type::assign(__p[__old_sz], value_type());
2295}
2296
2297template <class _CharT, class _Traits, class _Allocator>
2298void
2299basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2300 size_type __n_copy, size_type __n_del, size_type __n_add)
2301{
2302 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002303 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304 this->__throw_length_error();
2305 pointer __old_p = __get_pointer();
2306 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002307 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002309 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310 __invalidate_all_iterators();
2311 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002312 traits_type::copy(_VSTD::__to_address(__p),
2313 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2315 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002316 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2317 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002318 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002320 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 __set_long_pointer(__p);
2322 __set_long_cap(__cap+1);
2323}
2324
2325// assign
2326
2327template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002328template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002329basic_string<_CharT, _Traits, _Allocator>&
2330basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002331 const value_type* __s, size_type __n) {
2332 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2333 if (__n < __cap) {
2334 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2335 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2336 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2337 traits_type::assign(__p[__n], value_type());
2338 __invalidate_iterators_past(__n);
2339 } else {
2340 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2341 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2342 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002343 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002344}
2345
2346template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002348basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2349 const value_type* __s, size_type __n) {
2350 size_type __cap = capacity();
2351 if (__cap >= __n) {
2352 value_type* __p = _VSTD::__to_address(__get_pointer());
2353 traits_type::move(__p, __s, __n);
2354 traits_type::assign(__p[__n], value_type());
2355 __set_size(__n);
2356 __invalidate_iterators_past(__n);
2357 } else {
2358 size_type __sz = size();
2359 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2360 }
2361 return *this;
2362}
2363
2364template <class _CharT, class _Traits, class _Allocator>
2365basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002366basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002367{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002368 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002369 return (__builtin_constant_p(__n) && __n < __min_cap)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002370 ? __assign_short(__s, __n)
2371 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372}
2373
2374template <class _CharT, class _Traits, class _Allocator>
2375basic_string<_CharT, _Traits, _Allocator>&
2376basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2377{
2378 size_type __cap = capacity();
2379 if (__cap < __n)
2380 {
2381 size_type __sz = size();
2382 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2383 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002384 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385 traits_type::assign(__p, __n, __c);
2386 traits_type::assign(__p[__n], value_type());
2387 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002388 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389 return *this;
2390}
2391
2392template <class _CharT, class _Traits, class _Allocator>
2393basic_string<_CharT, _Traits, _Allocator>&
2394basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2395{
2396 pointer __p;
2397 if (__is_long())
2398 {
2399 __p = __get_long_pointer();
2400 __set_long_size(1);
2401 }
2402 else
2403 {
2404 __p = __get_short_pointer();
2405 __set_short_size(1);
2406 }
2407 traits_type::assign(*__p, __c);
2408 traits_type::assign(*++__p, value_type());
2409 __invalidate_iterators_past(1);
2410 return *this;
2411}
2412
2413template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002414basic_string<_CharT, _Traits, _Allocator>&
2415basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2416{
Martijn Vels596e3de2020-02-26 15:55:49 -05002417 if (this != &__str) {
2418 __copy_assign_alloc(__str);
2419 if (!__is_long()) {
2420 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002421 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002422 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002423 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002424 }
2425 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002426 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002427 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002428 }
2429 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002430}
2431
Eric Fiselierfc92be82017-04-19 00:28:44 +00002432#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002433
2434template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002435inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002436void
2437basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002438 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002439{
2440 if (__alloc() != __str.__alloc())
2441 assign(__str);
2442 else
2443 __move_assign(__str, true_type());
2444}
2445
2446template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002447inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002448void
2449basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002450#if _LIBCPP_STD_VER > 14
2451 _NOEXCEPT
2452#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002453 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002454#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002455{
marshall2ed41622019-11-27 07:13:00 -08002456 if (__is_long()) {
2457 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2458 __get_long_cap());
2459#if _LIBCPP_STD_VER <= 14
2460 if (!is_nothrow_move_assignable<allocator_type>::value) {
2461 __set_short_size(0);
2462 traits_type::assign(__get_short_pointer()[0], value_type());
2463 }
2464#endif
2465 }
2466 __move_assign_alloc(__str);
2467 __r_.first() = __str.__r_.first();
2468 __str.__set_short_size(0);
2469 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002470}
2471
2472template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002473inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002474basic_string<_CharT, _Traits, _Allocator>&
2475basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002476 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002477{
2478 __move_assign(__str, integral_constant<bool,
2479 __alloc_traits::propagate_on_container_move_assignment::value>());
2480 return *this;
2481}
2482
2483#endif
2484
2485template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002487__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002489 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002491>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2493{
Marshall Clow958362f2016-09-05 01:54:30 +00002494 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002495 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002496 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497}
2498
2499template <class _CharT, class _Traits, class _Allocator>
2500template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002501__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002503 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002505>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2507{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002508 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002509 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2510 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2511
2512 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2513 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002515 if (__cap < __n)
2516 {
2517 size_type __sz = size();
2518 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2519 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002520 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002521 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002522 traits_type::assign(*__p, *__first);
2523 traits_type::assign(*__p, value_type());
2524 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002525 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526 }
2527 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002528 {
2529 const basic_string __temp(__first, __last, __alloc());
2530 assign(__temp.data(), __temp.size());
2531 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532 return *this;
2533}
2534
2535template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536basic_string<_CharT, _Traits, _Allocator>&
2537basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2538{
2539 size_type __sz = __str.size();
2540 if (__pos > __sz)
2541 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002542 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543}
2544
2545template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002546template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002547__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002548<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002549 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2550 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002551 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002552>
Marshall Clow82513342016-09-24 22:45:42 +00002553basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002554{
Marshall Clow82513342016-09-24 22:45:42 +00002555 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002556 size_type __sz = __sv.size();
2557 if (__pos > __sz)
2558 this->__throw_out_of_range();
2559 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2560}
2561
2562
2563template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002565basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2566 return __assign_external(__s, traits_type::length(__s));
2567}
2568
2569template <class _CharT, class _Traits, class _Allocator>
2570basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002571basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002573 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002574 return __builtin_constant_p(*__s)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002575 ? (traits_type::length(__s) < __min_cap
2576 ? __assign_short(__s, traits_type::length(__s))
2577 : __assign_external(__s, traits_type::length(__s)))
2578 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580// append
2581
2582template <class _CharT, class _Traits, class _Allocator>
2583basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002584basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002586 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587 size_type __cap = capacity();
2588 size_type __sz = size();
2589 if (__cap - __sz >= __n)
2590 {
2591 if (__n)
2592 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002593 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594 traits_type::copy(__p + __sz, __s, __n);
2595 __sz += __n;
2596 __set_size(__sz);
2597 traits_type::assign(__p[__sz], value_type());
2598 }
2599 }
2600 else
2601 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2602 return *this;
2603}
2604
2605template <class _CharT, class _Traits, class _Allocator>
2606basic_string<_CharT, _Traits, _Allocator>&
2607basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2608{
2609 if (__n)
2610 {
2611 size_type __cap = capacity();
2612 size_type __sz = size();
2613 if (__cap - __sz < __n)
2614 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2615 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002616 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617 __sz += __n;
2618 __set_size(__sz);
2619 traits_type::assign(__p[__sz], value_type());
2620 }
2621 return *this;
2622}
2623
2624template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002625inline void
2626basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2627{
2628 if (__n)
2629 {
2630 size_type __cap = capacity();
2631 size_type __sz = size();
2632 if (__cap - __sz < __n)
2633 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2634 pointer __p = __get_pointer();
2635 __sz += __n;
2636 __set_size(__sz);
2637 traits_type::assign(__p[__sz], value_type());
2638 }
2639}
2640
2641template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642void
2643basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2644{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002645 bool __is_short = !__is_long();
2646 size_type __cap;
2647 size_type __sz;
2648 if (__is_short)
2649 {
2650 __cap = __min_cap - 1;
2651 __sz = __get_short_size();
2652 }
2653 else
2654 {
2655 __cap = __get_long_cap() - 1;
2656 __sz = __get_long_size();
2657 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002659 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002661 __is_short = !__is_long();
2662 }
2663 pointer __p;
2664 if (__is_short)
2665 {
2666 __p = __get_short_pointer() + __sz;
2667 __set_short_size(__sz+1);
2668 }
2669 else
2670 {
2671 __p = __get_long_pointer() + __sz;
2672 __set_long_size(__sz+1);
2673 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674 traits_type::assign(*__p, __c);
2675 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676}
2677
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678template <class _CharT, class _Traits, class _Allocator>
2679template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002680__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002681<
2682 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2683 basic_string<_CharT, _Traits, _Allocator>&
2684>
2685basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002686 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687{
2688 size_type __sz = size();
2689 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002690 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 if (__n)
2692 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002693 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2694 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002695 {
2696 if (__cap - __sz < __n)
2697 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2698 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002699 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002700 traits_type::assign(*__p, *__first);
2701 traits_type::assign(*__p, value_type());
2702 __set_size(__sz + __n);
2703 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002704 else
2705 {
2706 const basic_string __temp(__first, __last, __alloc());
2707 append(__temp.data(), __temp.size());
2708 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709 }
2710 return *this;
2711}
2712
2713template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002714inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715basic_string<_CharT, _Traits, _Allocator>&
2716basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2717{
2718 return append(__str.data(), __str.size());
2719}
2720
2721template <class _CharT, class _Traits, class _Allocator>
2722basic_string<_CharT, _Traits, _Allocator>&
2723basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2724{
2725 size_type __sz = __str.size();
2726 if (__pos > __sz)
2727 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002728 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729}
2730
2731template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002732template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002733 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002734 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002735 __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 +00002736 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002737 >
Marshall Clow82513342016-09-24 22:45:42 +00002738basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002739{
Marshall Clow82513342016-09-24 22:45:42 +00002740 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002741 size_type __sz = __sv.size();
2742 if (__pos > __sz)
2743 this->__throw_out_of_range();
2744 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2745}
2746
2747template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002749basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002751 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752 return append(__s, traits_type::length(__s));
2753}
2754
2755// insert
2756
2757template <class _CharT, class _Traits, class _Allocator>
2758basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002759basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002761 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762 size_type __sz = size();
2763 if (__pos > __sz)
2764 this->__throw_out_of_range();
2765 size_type __cap = capacity();
2766 if (__cap - __sz >= __n)
2767 {
2768 if (__n)
2769 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002770 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 size_type __n_move = __sz - __pos;
2772 if (__n_move != 0)
2773 {
2774 if (__p + __pos <= __s && __s < __p + __sz)
2775 __s += __n;
2776 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2777 }
2778 traits_type::move(__p + __pos, __s, __n);
2779 __sz += __n;
2780 __set_size(__sz);
2781 traits_type::assign(__p[__sz], value_type());
2782 }
2783 }
2784 else
2785 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2786 return *this;
2787}
2788
2789template <class _CharT, class _Traits, class _Allocator>
2790basic_string<_CharT, _Traits, _Allocator>&
2791basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2792{
2793 size_type __sz = size();
2794 if (__pos > __sz)
2795 this->__throw_out_of_range();
2796 if (__n)
2797 {
2798 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002799 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800 if (__cap - __sz >= __n)
2801 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002802 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803 size_type __n_move = __sz - __pos;
2804 if (__n_move != 0)
2805 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2806 }
2807 else
2808 {
2809 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002810 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 }
2812 traits_type::assign(__p + __pos, __n, __c);
2813 __sz += __n;
2814 __set_size(__sz);
2815 traits_type::assign(__p[__sz], value_type());
2816 }
2817 return *this;
2818}
2819
2820template <class _CharT, class _Traits, class _Allocator>
2821template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002822__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002824 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002825 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002826>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2828{
Louis Dionneba400782020-10-02 15:02:52 -04002829#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002830 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2831 "string::insert(iterator, range) called with an iterator not"
2832 " referring to this string");
2833#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002834 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002835 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836}
2837
2838template <class _CharT, class _Traits, class _Allocator>
2839template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002840__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002842 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002844>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2846{
Louis Dionneba400782020-10-02 15:02:52 -04002847#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002848 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2849 "string::insert(iterator, range) called with an iterator not"
2850 " referring to this string");
2851#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002853 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854 if (__n)
2855 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002856 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2857 !__addr_in_range(*__first))
2858 {
2859 size_type __sz = size();
2860 size_type __cap = capacity();
2861 value_type* __p;
2862 if (__cap - __sz >= __n)
2863 {
2864 __p = _VSTD::__to_address(__get_pointer());
2865 size_type __n_move = __sz - __ip;
2866 if (__n_move != 0)
2867 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2868 }
2869 else
2870 {
2871 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2872 __p = _VSTD::__to_address(__get_long_pointer());
2873 }
2874 __sz += __n;
2875 __set_size(__sz);
2876 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002877 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002878 traits_type::assign(*__p, *__first);
2879 }
2880 else
Marshall Clow958362f2016-09-05 01:54:30 +00002881 {
2882 const basic_string __temp(__first, __last, __alloc());
2883 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2884 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885 }
2886 return begin() + __ip;
2887}
2888
2889template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002890inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891basic_string<_CharT, _Traits, _Allocator>&
2892basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2893{
2894 return insert(__pos1, __str.data(), __str.size());
2895}
2896
2897template <class _CharT, class _Traits, class _Allocator>
2898basic_string<_CharT, _Traits, _Allocator>&
2899basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2900 size_type __pos2, size_type __n)
2901{
2902 size_type __str_sz = __str.size();
2903 if (__pos2 > __str_sz)
2904 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002905 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906}
2907
2908template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002909template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002910__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002911<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002912 __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 +00002913 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002914>
Marshall Clow82513342016-09-24 22:45:42 +00002915basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002916 size_type __pos2, size_type __n)
2917{
Marshall Clow82513342016-09-24 22:45:42 +00002918 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002919 size_type __str_sz = __sv.size();
2920 if (__pos2 > __str_sz)
2921 this->__throw_out_of_range();
2922 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2923}
2924
2925template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002926basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002927basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002929 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930 return insert(__pos, __s, traits_type::length(__s));
2931}
2932
2933template <class _CharT, class _Traits, class _Allocator>
2934typename basic_string<_CharT, _Traits, _Allocator>::iterator
2935basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2936{
2937 size_type __ip = static_cast<size_type>(__pos - begin());
2938 size_type __sz = size();
2939 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002940 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941 if (__cap == __sz)
2942 {
2943 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002944 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945 }
2946 else
2947 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002948 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949 size_type __n_move = __sz - __ip;
2950 if (__n_move != 0)
2951 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2952 }
2953 traits_type::assign(__p[__ip], __c);
2954 traits_type::assign(__p[++__sz], value_type());
2955 __set_size(__sz);
2956 return begin() + static_cast<difference_type>(__ip);
2957}
2958
2959template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002960inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961typename basic_string<_CharT, _Traits, _Allocator>::iterator
2962basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2963{
Louis Dionneba400782020-10-02 15:02:52 -04002964#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002965 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2966 "string::insert(iterator, n, value) called with an iterator not"
2967 " referring to this string");
2968#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 difference_type __p = __pos - begin();
2970 insert(static_cast<size_type>(__p), __n, __c);
2971 return begin() + __p;
2972}
2973
2974// replace
2975
2976template <class _CharT, class _Traits, class _Allocator>
2977basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002978basic_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 +00002979 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002981 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982 size_type __sz = size();
2983 if (__pos > __sz)
2984 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002985 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986 size_type __cap = capacity();
2987 if (__cap - __sz + __n1 >= __n2)
2988 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002989 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 if (__n1 != __n2)
2991 {
2992 size_type __n_move = __sz - __pos - __n1;
2993 if (__n_move != 0)
2994 {
2995 if (__n1 > __n2)
2996 {
2997 traits_type::move(__p + __pos, __s, __n2);
2998 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2999 goto __finish;
3000 }
3001 if (__p + __pos < __s && __s < __p + __sz)
3002 {
3003 if (__p + __pos + __n1 <= __s)
3004 __s += __n2 - __n1;
3005 else // __p + __pos < __s < __p + __pos + __n1
3006 {
3007 traits_type::move(__p + __pos, __s, __n1);
3008 __pos += __n1;
3009 __s += __n2;
3010 __n2 -= __n1;
3011 __n1 = 0;
3012 }
3013 }
3014 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3015 }
3016 }
3017 traits_type::move(__p + __pos, __s, __n2);
3018__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05003019// __sz += __n2 - __n1; in this and the below function below can cause unsigned
3020// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021 __sz += __n2 - __n1;
3022 __set_size(__sz);
3023 __invalidate_iterators_past(__sz);
3024 traits_type::assign(__p[__sz], value_type());
3025 }
3026 else
3027 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3028 return *this;
3029}
3030
3031template <class _CharT, class _Traits, class _Allocator>
3032basic_string<_CharT, _Traits, _Allocator>&
3033basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00003034 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035{
3036 size_type __sz = size();
3037 if (__pos > __sz)
3038 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003039 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003041 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042 if (__cap - __sz + __n1 >= __n2)
3043 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003044 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045 if (__n1 != __n2)
3046 {
3047 size_type __n_move = __sz - __pos - __n1;
3048 if (__n_move != 0)
3049 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3050 }
3051 }
3052 else
3053 {
3054 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003055 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056 }
3057 traits_type::assign(__p + __pos, __n2, __c);
3058 __sz += __n2 - __n1;
3059 __set_size(__sz);
3060 __invalidate_iterators_past(__sz);
3061 traits_type::assign(__p[__sz], value_type());
3062 return *this;
3063}
3064
3065template <class _CharT, class _Traits, class _Allocator>
3066template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003067__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003069 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003071>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003072basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 _InputIterator __j1, _InputIterator __j2)
3074{
Marshall Clow958362f2016-09-05 01:54:30 +00003075 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003076 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077}
3078
3079template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003080inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081basic_string<_CharT, _Traits, _Allocator>&
3082basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3083{
3084 return replace(__pos1, __n1, __str.data(), __str.size());
3085}
3086
3087template <class _CharT, class _Traits, class _Allocator>
3088basic_string<_CharT, _Traits, _Allocator>&
3089basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3090 size_type __pos2, size_type __n2)
3091{
3092 size_type __str_sz = __str.size();
3093 if (__pos2 > __str_sz)
3094 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003095 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096}
3097
3098template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003099template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003100__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003101<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003102 __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 +00003103 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003104>
Marshall Clow82513342016-09-24 22:45:42 +00003105basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003106 size_type __pos2, size_type __n2)
3107{
Marshall Clow82513342016-09-24 22:45:42 +00003108 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003109 size_type __str_sz = __sv.size();
3110 if (__pos2 > __str_sz)
3111 this->__throw_out_of_range();
3112 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3113}
3114
3115template <class _CharT, class _Traits, class _Allocator>
3116basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003117basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003119 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120 return replace(__pos, __n1, __s, traits_type::length(__s));
3121}
3122
3123template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003124inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003126basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127{
3128 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3129 __str.data(), __str.size());
3130}
3131
3132template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003133inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003135basic_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 +00003136{
3137 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3138}
3139
3140template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003141inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003143basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144{
3145 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3146}
3147
3148template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003149inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003151basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152{
3153 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3154}
3155
3156// erase
3157
Martijn Velsa81fc792020-02-26 13:25:43 -05003158// 'externally instantiated' erase() implementation, called when __n != npos.
3159// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003161void
3162basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3163 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 if (__n)
3166 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003167 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003168 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003169 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170 size_type __n_move = __sz - __pos - __n;
3171 if (__n_move != 0)
3172 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3173 __sz -= __n;
3174 __set_size(__sz);
3175 __invalidate_iterators_past(__sz);
3176 traits_type::assign(__p[__sz], value_type());
3177 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003178}
3179
3180template <class _CharT, class _Traits, class _Allocator>
3181basic_string<_CharT, _Traits, _Allocator>&
3182basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3183 size_type __n) {
3184 if (__pos > size()) this->__throw_out_of_range();
3185 if (__n == npos) {
3186 __erase_to_end(__pos);
3187 } else {
3188 __erase_external_with_move(__pos, __n);
3189 }
3190 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191}
3192
3193template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003194inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195typename basic_string<_CharT, _Traits, _Allocator>::iterator
3196basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3197{
Louis Dionneba400782020-10-02 15:02:52 -04003198#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003199 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3200 "string::erase(iterator) called with an iterator not"
3201 " referring to this string");
3202#endif
3203 _LIBCPP_ASSERT(__pos != end(),
3204 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205 iterator __b = begin();
3206 size_type __r = static_cast<size_type>(__pos - __b);
3207 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003208 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209}
3210
3211template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003212inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213typename basic_string<_CharT, _Traits, _Allocator>::iterator
3214basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3215{
Louis Dionneba400782020-10-02 15:02:52 -04003216#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003217 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3218 "string::erase(iterator, iterator) called with an iterator not"
3219 " referring to this string");
3220#endif
3221 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222 iterator __b = begin();
3223 size_type __r = static_cast<size_type>(__first - __b);
3224 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003225 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226}
3227
3228template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003229inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230void
3231basic_string<_CharT, _Traits, _Allocator>::pop_back()
3232{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003233 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234 size_type __sz;
3235 if (__is_long())
3236 {
3237 __sz = __get_long_size() - 1;
3238 __set_long_size(__sz);
3239 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3240 }
3241 else
3242 {
3243 __sz = __get_short_size() - 1;
3244 __set_short_size(__sz);
3245 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3246 }
3247 __invalidate_iterators_past(__sz);
3248}
3249
3250template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003251inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003253basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254{
3255 __invalidate_all_iterators();
3256 if (__is_long())
3257 {
3258 traits_type::assign(*__get_long_pointer(), value_type());
3259 __set_long_size(0);
3260 }
3261 else
3262 {
3263 traits_type::assign(*__get_short_pointer(), value_type());
3264 __set_short_size(0);
3265 }
3266}
3267
3268template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003269inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270void
3271basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3272{
3273 if (__is_long())
3274 {
3275 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3276 __set_long_size(__pos);
3277 }
3278 else
3279 {
3280 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3281 __set_short_size(__pos);
3282 }
3283 __invalidate_iterators_past(__pos);
3284}
3285
3286template <class _CharT, class _Traits, class _Allocator>
3287void
3288basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3289{
3290 size_type __sz = size();
3291 if (__n > __sz)
3292 append(__n - __sz, __c);
3293 else
3294 __erase_to_end(__n);
3295}
3296
3297template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003298inline void
3299basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3300{
3301 size_type __sz = size();
3302 if (__n > __sz) {
3303 __append_default_init(__n - __sz);
3304 } else
3305 __erase_to_end(__n);
3306}
3307
3308template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003309inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003311basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003313 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003314#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003315 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003317 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318#endif
3319}
3320
3321template <class _CharT, class _Traits, class _Allocator>
3322void
Marek Kurdejc9848142020-11-26 10:07:16 +01003323basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324{
Marek Kurdejc9848142020-11-26 10:07:16 +01003325 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003326 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003327
3328#if _LIBCPP_STD_VER > 17
3329 // Reserve never shrinks as of C++20.
3330 if (__requested_capacity <= capacity()) return;
3331#endif
3332
3333 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3334 __target_capacity = __recommend(__target_capacity);
3335 if (__target_capacity == capacity()) return;
3336
3337 __shrink_or_extend(__target_capacity);
3338}
3339
3340template <class _CharT, class _Traits, class _Allocator>
3341void
3342basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3343{
3344 size_type __target_capacity = __recommend(size());
3345 if (__target_capacity == capacity()) return;
3346
3347 __shrink_or_extend(__target_capacity);
3348}
3349
3350template <class _CharT, class _Traits, class _Allocator>
3351void
3352basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3353{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354 size_type __cap = capacity();
3355 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003356
3357 pointer __new_data, __p;
3358 bool __was_long, __now_long;
3359 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003361 __was_long = true;
3362 __now_long = false;
3363 __new_data = __get_short_pointer();
3364 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003366 else
3367 {
3368 if (__target_capacity > __cap)
3369 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3370 else
3371 {
3372 #ifndef _LIBCPP_NO_EXCEPTIONS
3373 try
3374 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003375 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003376 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3377 #ifndef _LIBCPP_NO_EXCEPTIONS
3378 }
3379 catch (...)
3380 {
3381 return;
3382 }
3383 #else // _LIBCPP_NO_EXCEPTIONS
3384 if (__new_data == nullptr)
3385 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003386 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003387 }
3388 __now_long = true;
3389 __was_long = __is_long();
3390 __p = __get_pointer();
3391 }
3392 traits_type::copy(_VSTD::__to_address(__new_data),
3393 _VSTD::__to_address(__p), size()+1);
3394 if (__was_long)
3395 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3396 if (__now_long)
3397 {
3398 __set_long_cap(__target_capacity+1);
3399 __set_long_size(__sz);
3400 __set_long_pointer(__new_data);
3401 }
3402 else
3403 __set_short_size(__sz);
3404 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405}
3406
3407template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003408inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003410basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003412 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413 return *(data() + __pos);
3414}
3415
3416template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003417inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003419basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003421 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003422 return *(__get_pointer() + __pos);
3423}
3424
3425template <class _CharT, class _Traits, class _Allocator>
3426typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3427basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3428{
3429 if (__n >= size())
3430 this->__throw_out_of_range();
3431 return (*this)[__n];
3432}
3433
3434template <class _CharT, class _Traits, class _Allocator>
3435typename basic_string<_CharT, _Traits, _Allocator>::reference
3436basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3437{
3438 if (__n >= size())
3439 this->__throw_out_of_range();
3440 return (*this)[__n];
3441}
3442
3443template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003444inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003446basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003448 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449 return *__get_pointer();
3450}
3451
3452template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003453inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003455basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003457 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458 return *data();
3459}
3460
3461template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003462inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003464basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003466 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003467 return *(__get_pointer() + size() - 1);
3468}
3469
3470template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003471inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003473basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003475 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476 return *(data() + size() - 1);
3477}
3478
3479template <class _CharT, class _Traits, class _Allocator>
3480typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003481basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482{
3483 size_type __sz = size();
3484 if (__pos > __sz)
3485 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003486 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487 traits_type::copy(__s, data() + __pos, __rlen);
3488 return __rlen;
3489}
3490
3491template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003492inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493basic_string<_CharT, _Traits, _Allocator>
3494basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3495{
3496 return basic_string(*this, __pos, __n, __alloc());
3497}
3498
3499template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003500inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501void
3502basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003503#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003504 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003505#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003506 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003507 __is_nothrow_swappable<allocator_type>::value)
3508#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509{
Louis Dionneba400782020-10-02 15:02:52 -04003510#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003511 if (!__is_long())
3512 __get_db()->__invalidate_all(this);
3513 if (!__str.__is_long())
3514 __get_db()->__invalidate_all(&__str);
3515 __get_db()->swap(this, &__str);
3516#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003517 _LIBCPP_ASSERT(
3518 __alloc_traits::propagate_on_container_swap::value ||
3519 __alloc_traits::is_always_equal::value ||
3520 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003521 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003522 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523}
3524
3525// find
3526
3527template <class _Traits>
3528struct _LIBCPP_HIDDEN __traits_eq
3529{
3530 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003531 _LIBCPP_INLINE_VISIBILITY
3532 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3533 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534};
3535
3536template<class _CharT, class _Traits, class _Allocator>
3537typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003538basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003539 size_type __pos,
3540 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003542 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003543 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003544 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545}
3546
3547template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003548inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003550basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3551 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003553 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003554 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555}
3556
3557template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003558template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003559__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003560<
3561 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3562 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003563>
Marshall Clowe46031a2018-07-02 18:41:15 +00003564basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003565 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003566{
Marshall Clowe46031a2018-07-02 18:41:15 +00003567 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003568 return __str_find<value_type, size_type, traits_type, npos>
3569 (data(), size(), __sv.data(), __pos, __sv.size());
3570}
3571
3572template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003573inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003574typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003575basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003576 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003578 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003579 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003580 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581}
3582
3583template<class _CharT, class _Traits, class _Allocator>
3584typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003585basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3586 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003588 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003589 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590}
3591
3592// rfind
3593
3594template<class _CharT, class _Traits, class _Allocator>
3595typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003596basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003597 size_type __pos,
3598 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003600 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003601 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003602 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603}
3604
3605template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003606inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003608basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3609 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003611 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003612 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613}
3614
3615template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003616template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003617__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003618<
3619 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3620 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003621>
Marshall Clowe46031a2018-07-02 18:41:15 +00003622basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003623 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003624{
Marshall Clowe46031a2018-07-02 18:41:15 +00003625 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003626 return __str_rfind<value_type, size_type, traits_type, npos>
3627 (data(), size(), __sv.data(), __pos, __sv.size());
3628}
3629
3630template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003631inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003632typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003633basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003634 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003636 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003637 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003638 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639}
3640
3641template<class _CharT, class _Traits, class _Allocator>
3642typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003643basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3644 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003646 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003647 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648}
3649
3650// find_first_of
3651
3652template<class _CharT, class _Traits, class _Allocator>
3653typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003654basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003655 size_type __pos,
3656 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003657{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003658 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003659 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003660 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661}
3662
3663template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003664inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003666basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3667 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003669 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003670 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671}
3672
3673template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003674template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003675__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003676<
3677 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3678 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003679>
Marshall Clowe46031a2018-07-02 18:41:15 +00003680basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003681 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003682{
Marshall Clowe46031a2018-07-02 18:41:15 +00003683 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003684 return __str_find_first_of<value_type, size_type, traits_type, npos>
3685 (data(), size(), __sv.data(), __pos, __sv.size());
3686}
3687
3688template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003689inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003690typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003691basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003692 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003694 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003695 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003696 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697}
3698
3699template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003700inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003702basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3703 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704{
3705 return find(__c, __pos);
3706}
3707
3708// find_last_of
3709
3710template<class _CharT, class _Traits, class _Allocator>
3711typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003712basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003713 size_type __pos,
3714 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003716 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003717 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003718 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719}
3720
3721template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003722inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003724basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3725 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003727 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003728 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729}
3730
3731template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003732template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003733__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003734<
3735 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3736 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003737>
Marshall Clowe46031a2018-07-02 18:41:15 +00003738basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003739 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003740{
Marshall Clowe46031a2018-07-02 18:41:15 +00003741 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003742 return __str_find_last_of<value_type, size_type, traits_type, npos>
3743 (data(), size(), __sv.data(), __pos, __sv.size());
3744}
3745
3746template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003747inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003748typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003749basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003750 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003752 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003753 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003754 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755}
3756
3757template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003758inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003760basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3761 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762{
3763 return rfind(__c, __pos);
3764}
3765
3766// find_first_not_of
3767
3768template<class _CharT, class _Traits, class _Allocator>
3769typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003770basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003771 size_type __pos,
3772 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003774 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003775 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003776 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777}
3778
3779template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003780inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003782basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3783 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003785 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003786 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787}
3788
3789template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003790template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003791__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003792<
3793 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3794 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003795>
Marshall Clowe46031a2018-07-02 18:41:15 +00003796basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003797 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003798{
Marshall Clowe46031a2018-07-02 18:41:15 +00003799 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003800 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3801 (data(), size(), __sv.data(), __pos, __sv.size());
3802}
3803
3804template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003805inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003806typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003807basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003808 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003810 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003811 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003812 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813}
3814
3815template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003816inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003818basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3819 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003821 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003822 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823}
3824
3825// find_last_not_of
3826
3827template<class _CharT, class _Traits, class _Allocator>
3828typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003829basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003830 size_type __pos,
3831 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003833 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003834 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003835 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836}
3837
3838template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003839inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003841basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3842 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003844 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003845 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846}
3847
3848template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003849template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003850__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003851<
3852 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3853 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003854>
Marshall Clowe46031a2018-07-02 18:41:15 +00003855basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003856 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003857{
Marshall Clowe46031a2018-07-02 18:41:15 +00003858 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3860 (data(), size(), __sv.data(), __pos, __sv.size());
3861}
3862
3863template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003864inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003865typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003866basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003867 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003868{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003869 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003870 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003871 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872}
3873
3874template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003875inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003877basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3878 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003880 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003881 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882}
3883
3884// compare
3885
3886template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003887template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003888__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003889<
3890 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3891 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003892>
zoecarver1997e0a2021-02-05 11:54:47 -08003893basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894{
Marshall Clowe46031a2018-07-02 18:41:15 +00003895 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003896 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003897 size_t __rhs_sz = __sv.size();
3898 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003899 _VSTD::min(__lhs_sz, __rhs_sz));
3900 if (__result != 0)
3901 return __result;
3902 if (__lhs_sz < __rhs_sz)
3903 return -1;
3904 if (__lhs_sz > __rhs_sz)
3905 return 1;
3906 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907}
3908
3909template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003910inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003912basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003914 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915}
3916
3917template <class _CharT, class _Traits, class _Allocator>
3918int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003919basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3920 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003921 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003922 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003923{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003924 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925 size_type __sz = size();
3926 if (__pos1 > __sz || __n2 == npos)
3927 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003928 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3929 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930 if (__r == 0)
3931 {
3932 if (__rlen < __n2)
3933 __r = -1;
3934 else if (__rlen > __n2)
3935 __r = 1;
3936 }
3937 return __r;
3938}
3939
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003940template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003941template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003942__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003943<
3944 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3945 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003946>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003947basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3948 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003949 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003950{
Marshall Clowe46031a2018-07-02 18:41:15 +00003951 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003952 return compare(__pos1, __n1, __sv.data(), __sv.size());
3953}
3954
3955template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003956inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003957int
3958basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3959 size_type __n1,
3960 const basic_string& __str) const
3961{
3962 return compare(__pos1, __n1, __str.data(), __str.size());
3963}
3964
3965template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003966template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003967__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003968<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003969 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3970 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003971 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003972>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003973basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3974 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003975 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003976 size_type __pos2,
3977 size_type __n2) const
3978{
Marshall Clow82513342016-09-24 22:45:42 +00003979 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003980 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3981}
3982
3983template <class _CharT, class _Traits, class _Allocator>
3984int
3985basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3986 size_type __n1,
3987 const basic_string& __str,
3988 size_type __pos2,
3989 size_type __n2) const
3990{
3991 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3992}
3993
3994template <class _CharT, class _Traits, class _Allocator>
3995int
3996basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3997{
3998 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3999 return compare(0, npos, __s, traits_type::length(__s));
4000}
4001
4002template <class _CharT, class _Traits, class _Allocator>
4003int
4004basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4005 size_type __n1,
4006 const value_type* __s) const
4007{
4008 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4009 return compare(__pos1, __n1, __s, traits_type::length(__s));
4010}
4011
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012// __invariants
4013
4014template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004015inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016bool
4017basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4018{
4019 if (size() > capacity())
4020 return false;
4021 if (capacity() < __min_cap - 1)
4022 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004023 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004025 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004026 return false;
4027 return true;
4028}
4029
Vedant Kumar55e007e2018-03-08 21:15:26 +00004030// __clear_and_shrink
4031
4032template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004033inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004034void
Marshall Clowe60a7182018-05-29 17:04:37 +00004035basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004036{
4037 clear();
4038 if(__is_long())
4039 {
4040 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4041 __set_long_cap(0);
4042 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004043 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004044 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004045}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004046
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047// operator==
4048
4049template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004050inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051bool
4052operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004053 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004054{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004055 size_t __lhs_sz = __lhs.size();
4056 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4057 __rhs.data(),
4058 __lhs_sz) == 0;
4059}
4060
4061template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004062inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004063bool
4064operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4065 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4066{
4067 size_t __lhs_sz = __lhs.size();
4068 if (__lhs_sz != __rhs.size())
4069 return false;
4070 const char* __lp = __lhs.data();
4071 const char* __rp = __rhs.data();
4072 if (__lhs.__is_long())
4073 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4074 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4075 if (*__lp != *__rp)
4076 return false;
4077 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078}
4079
4080template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004081inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004083operator==(const _CharT* __lhs,
4084 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004086 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4087 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4088 size_t __lhs_len = _Traits::length(__lhs);
4089 if (__lhs_len != __rhs.size()) return false;
4090 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091}
4092
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004094inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004096operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4097 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004099 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4100 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4101 size_t __rhs_len = _Traits::length(__rhs);
4102 if (__rhs_len != __lhs.size()) return false;
4103 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104}
4105
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004106template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004107inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108bool
4109operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004110 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111{
4112 return !(__lhs == __rhs);
4113}
4114
4115template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004118operator!=(const _CharT* __lhs,
4119 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120{
4121 return !(__lhs == __rhs);
4122}
4123
4124template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004125inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004127operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4128 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129{
4130 return !(__lhs == __rhs);
4131}
4132
4133// operator<
4134
4135template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004136inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137bool
4138operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004139 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004141 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142}
4143
4144template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004145inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004146bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004147operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4148 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004150 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151}
4152
4153template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004154inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004156operator< (const _CharT* __lhs,
4157 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158{
4159 return __rhs.compare(__lhs) > 0;
4160}
4161
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162// operator>
4163
4164template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004165inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166bool
4167operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004168 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169{
4170 return __rhs < __lhs;
4171}
4172
4173template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004174inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004175bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004176operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4177 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178{
4179 return __rhs < __lhs;
4180}
4181
4182template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004183inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004185operator> (const _CharT* __lhs,
4186 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187{
4188 return __rhs < __lhs;
4189}
4190
4191// operator<=
4192
4193template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004194inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195bool
4196operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004197 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198{
4199 return !(__rhs < __lhs);
4200}
4201
4202template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004203inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004205operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4206 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207{
4208 return !(__rhs < __lhs);
4209}
4210
4211template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004212inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004214operator<=(const _CharT* __lhs,
4215 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004216{
4217 return !(__rhs < __lhs);
4218}
4219
4220// operator>=
4221
4222template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004223inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224bool
4225operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004226 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227{
4228 return !(__lhs < __rhs);
4229}
4230
4231template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004232inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004234operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4235 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004236{
4237 return !(__lhs < __rhs);
4238}
4239
4240template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004241inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004243operator>=(const _CharT* __lhs,
4244 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245{
4246 return !(__lhs < __rhs);
4247}
4248
4249// operator +
4250
4251template<class _CharT, class _Traits, class _Allocator>
4252basic_string<_CharT, _Traits, _Allocator>
4253operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4254 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4255{
4256 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4257 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4258 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4259 __r.__init(__lhs.data(), __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+(const _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 __lhs_sz = _Traits::length(__lhs);
4270 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4271 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4272 __r.append(__rhs.data(), __rhs_sz);
4273 return __r;
4274}
4275
4276template<class _CharT, class _Traits, class _Allocator>
4277basic_string<_CharT, _Traits, _Allocator>
4278operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4279{
4280 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4281 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4282 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4283 __r.append(__rhs.data(), __rhs_sz);
4284 return __r;
4285}
4286
4287template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004288inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289basic_string<_CharT, _Traits, _Allocator>
4290operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _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 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4295 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4296 __r.append(__rhs, __rhs_sz);
4297 return __r;
4298}
4299
4300template<class _CharT, class _Traits, class _Allocator>
4301basic_string<_CharT, _Traits, _Allocator>
4302operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4303{
4304 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4305 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4306 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4307 __r.push_back(__rhs);
4308 return __r;
4309}
4310
Eric Fiselierfc92be82017-04-19 00:28:44 +00004311#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004312
4313template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004314inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315basic_string<_CharT, _Traits, _Allocator>
4316operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4317{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004318 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004319}
4320
4321template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004322inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323basic_string<_CharT, _Traits, _Allocator>
4324operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4325{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004326 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327}
4328
4329template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004330inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331basic_string<_CharT, _Traits, _Allocator>
4332operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4333{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004334 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335}
4336
4337template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004338inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339basic_string<_CharT, _Traits, _Allocator>
4340operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4341{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004342 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343}
4344
4345template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347basic_string<_CharT, _Traits, _Allocator>
4348operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4349{
4350 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004351 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352}
4353
4354template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004356basic_string<_CharT, _Traits, _Allocator>
4357operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4358{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004359 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360}
4361
4362template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364basic_string<_CharT, _Traits, _Allocator>
4365operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4366{
4367 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004368 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369}
4370
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004371#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372
4373// swap
4374
4375template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004376inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004378swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004379 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4380 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004381{
4382 __lhs.swap(__rhs);
4383}
4384
Bruce Mitchener170d8972020-11-24 12:53:53 -05004385_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4386_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4387_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4388_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4389_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004390
Bruce Mitchener170d8972020-11-24 12:53:53 -05004391_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4392_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4393_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004394
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004395_LIBCPP_FUNC_VIS string to_string(int __val);
4396_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4397_LIBCPP_FUNC_VIS string to_string(long __val);
4398_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4399_LIBCPP_FUNC_VIS string to_string(long long __val);
4400_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4401_LIBCPP_FUNC_VIS string to_string(float __val);
4402_LIBCPP_FUNC_VIS string to_string(double __val);
4403_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004404
Louis Dionne89258142021-08-23 15:32:36 -04004405#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004406_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4407_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4408_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4409_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4410_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004411
Bruce Mitchener170d8972020-11-24 12:53:53 -05004412_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4413_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4414_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004415
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004416_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4417_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4418_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4419_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4420_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4421_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4422_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4423_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4424_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004425#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004426
Howard Hinnantc51e1022010-05-11 19:42:16 +00004427template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004428_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004429const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4430 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004431
Marshall Clow851b9ec2019-05-20 21:56:51 +00004432template <class _CharT, class _Allocator>
4433struct _LIBCPP_TEMPLATE_VIS
4434 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4435 : public unary_function<
4436 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004437{
4438 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004439 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4440 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441};
4442
Howard Hinnantc51e1022010-05-11 19:42:16 +00004443
Howard Hinnantdc095972011-07-18 15:51:59 +00004444template<class _CharT, class _Traits, class _Allocator>
4445basic_ostream<_CharT, _Traits>&
4446operator<<(basic_ostream<_CharT, _Traits>& __os,
4447 const basic_string<_CharT, _Traits, _Allocator>& __str);
4448
4449template<class _CharT, class _Traits, class _Allocator>
4450basic_istream<_CharT, _Traits>&
4451operator>>(basic_istream<_CharT, _Traits>& __is,
4452 basic_string<_CharT, _Traits, _Allocator>& __str);
4453
4454template<class _CharT, class _Traits, class _Allocator>
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
Howard Hinnantdc095972011-07-18 15:51:59 +00004465template<class _CharT, class _Traits, class _Allocator>
4466inline _LIBCPP_INLINE_VISIBILITY
4467basic_istream<_CharT, _Traits>&
4468getline(basic_istream<_CharT, _Traits>&& __is,
4469 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4470
4471template<class _CharT, class _Traits, class _Allocator>
4472inline _LIBCPP_INLINE_VISIBILITY
4473basic_istream<_CharT, _Traits>&
4474getline(basic_istream<_CharT, _Traits>&& __is,
4475 basic_string<_CharT, _Traits, _Allocator>& __str);
4476
Marshall Clow29b53f22018-12-14 18:49:35 +00004477#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004478template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004479inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004480 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4481 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4482 auto __old_size = __str.size();
4483 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4484 return __old_size - __str.size();
4485}
Marshall Clow29b53f22018-12-14 18:49:35 +00004486
Marek Kurdeja98b1412020-05-02 13:58:03 +02004487template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004488inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004489 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4490 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4491 _Predicate __pred) {
4492 auto __old_size = __str.size();
4493 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4494 __str.end());
4495 return __old_size - __str.size();
4496}
Marshall Clow29b53f22018-12-14 18:49:35 +00004497#endif
4498
Louis Dionneba400782020-10-02 15:02:52 -04004499#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004500
4501template<class _CharT, class _Traits, class _Allocator>
4502bool
4503basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4504{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004505 return this->data() <= _VSTD::__to_address(__i->base()) &&
4506 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004507}
4508
4509template<class _CharT, class _Traits, class _Allocator>
4510bool
4511basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4512{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004513 return this->data() < _VSTD::__to_address(__i->base()) &&
4514 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004515}
4516
4517template<class _CharT, class _Traits, class _Allocator>
4518bool
4519basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4520{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004521 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004522 return this->data() <= __p && __p <= this->data() + this->size();
4523}
4524
4525template<class _CharT, class _Traits, class _Allocator>
4526bool
4527basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4528{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004529 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004530 return this->data() <= __p && __p < this->data() + this->size();
4531}
4532
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004533#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004534
Louis Dionne173f29e2019-05-29 16:01:36 +00004535#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004536// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004537inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004538{
4539 inline namespace string_literals
4540 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004541 inline _LIBCPP_INLINE_VISIBILITY
4542 basic_string<char> operator "" s( const char *__str, size_t __len )
4543 {
4544 return basic_string<char> (__str, __len);
4545 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004546
Louis Dionne89258142021-08-23 15:32:36 -04004547#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004548 inline _LIBCPP_INLINE_VISIBILITY
4549 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4550 {
4551 return basic_string<wchar_t> (__str, __len);
4552 }
Louis Dionne89258142021-08-23 15:32:36 -04004553#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004554
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004555#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004556 inline _LIBCPP_INLINE_VISIBILITY
4557 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4558 {
4559 return basic_string<char8_t> (__str, __len);
4560 }
4561#endif
4562
Howard Hinnant5c167562013-08-07 19:39:48 +00004563 inline _LIBCPP_INLINE_VISIBILITY
4564 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4565 {
4566 return basic_string<char16_t> (__str, __len);
4567 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004568
Howard Hinnant5c167562013-08-07 19:39:48 +00004569 inline _LIBCPP_INLINE_VISIBILITY
4570 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4571 {
4572 return basic_string<char32_t> (__str, __len);
4573 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004574 }
4575}
4576#endif
4577
Howard Hinnantc51e1022010-05-11 19:42:16 +00004578_LIBCPP_END_NAMESPACE_STD
4579
Eric Fiselierf4433a32017-05-31 22:07:49 +00004580_LIBCPP_POP_MACROS
4581
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004582#endif // _LIBCPP_STRING