blob: db29f21bdd599c159bc2c1884ac3fa52ab720a78 [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());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
114 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000115 basic_string(InputIterator begin, InputIterator end,
116 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
118 basic_string(const basic_string&, const Allocator&);
119 basic_string(basic_string&&, const Allocator&);
120
121 ~basic_string();
122
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000123 operator basic_string_view<charT, traits>() const noexcept;
124
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000126 template <class T>
127 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000128 basic_string& operator=(basic_string&& str)
129 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000130 allocator_type::propagate_on_container_move_assignment::value ||
131 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000132 basic_string& operator=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133 basic_string& operator=(value_type c);
134 basic_string& operator=(initializer_list<value_type>);
135
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000136 iterator begin() noexcept;
137 const_iterator begin() const noexcept;
138 iterator end() noexcept;
139 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000141 reverse_iterator rbegin() noexcept;
142 const_reverse_iterator rbegin() const noexcept;
143 reverse_iterator rend() noexcept;
144 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000145
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000146 const_iterator cbegin() const noexcept;
147 const_iterator cend() const noexcept;
148 const_reverse_iterator crbegin() const noexcept;
149 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000151 size_type size() const noexcept;
152 size_type length() const noexcept;
153 size_type max_size() const noexcept;
154 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155
156 void resize(size_type n, value_type c);
157 void resize(size_type n);
158
Marek Kurdejc9848142020-11-26 10:07:16 +0100159 void reserve(size_type res_arg);
160 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000162 void clear() noexcept;
163 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164
165 const_reference operator[](size_type pos) const;
166 reference operator[](size_type pos);
167
168 const_reference at(size_type n) const;
169 reference at(size_type n);
170
171 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000172 template <class T>
173 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000174 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175 basic_string& operator+=(value_type c);
176 basic_string& operator+=(initializer_list<value_type>);
177
178 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000179 template <class T>
180 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000181 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000182 template <class T>
183 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000184 basic_string& append(const value_type* s, size_type n);
185 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000187 template<class InputIterator>
188 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189 basic_string& append(initializer_list<value_type>);
190
191 void push_back(value_type c);
192 void pop_back();
193 reference front();
194 const_reference front() const;
195 reference back();
196 const_reference back() const;
197
198 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000199 template <class T>
200 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000201 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000202 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000203 template <class T>
204 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000205 basic_string& assign(const value_type* s, size_type n);
206 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000208 template<class InputIterator>
209 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210 basic_string& assign(initializer_list<value_type>);
211
212 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000213 template <class T>
214 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000215 basic_string& insert(size_type pos1, const basic_string& str,
216 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000217 template <class T>
218 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000219 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000220 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 basic_string& insert(size_type pos, size_type n, value_type c);
222 iterator insert(const_iterator p, value_type c);
223 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000224 template<class InputIterator>
225 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 iterator insert(const_iterator p, initializer_list<value_type>);
227
228 basic_string& erase(size_type pos = 0, size_type n = npos);
229 iterator erase(const_iterator position);
230 iterator erase(const_iterator first, const_iterator last);
231
232 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000233 template <class T>
234 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000235 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000236 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000237 template <class T>
238 basic_string& replace(size_type pos1, size_type n1, const T& t,
239 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000240 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
241 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000243 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000244 template <class T>
245 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000246 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
247 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000249 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000250 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
251 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252
Howard Hinnantd17880b2013-06-28 16:59:19 +0000253 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 basic_string substr(size_type pos = 0, size_type n = npos) const;
255
Howard Hinnant3e276872011-06-03 18:40:47 +0000256 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000257 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
258 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259
Howard Hinnantd17880b2013-06-28 16:59:19 +0000260 const value_type* c_str() const noexcept;
261 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000262 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000264 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000266 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000267 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800268 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 +0000269 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
270 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000273 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000274 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800275 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 +0000276 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
277 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000279
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000280 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000281 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800282 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 +0000283 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
284 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000287 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000288 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800289 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 +0000290 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
291 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000294 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000295 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800296 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 +0000297 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
298 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000301 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000302 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800303 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 +0000304 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
305 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000308 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000309 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800310 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000312 template <class T>
313 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000314 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000315 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000316 template <class T>
317 int compare(size_type pos1, size_type n1, const T& t,
318 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000319 int compare(const value_type* s) const noexcept;
320 int compare(size_type pos1, size_type n1, const value_type* s) const;
321 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322
Marek Kurdej24b4c512021-01-07 12:29:04 +0100323 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
324 bool starts_with(charT c) const noexcept; // C++20
325 bool starts_with(const charT* s) const; // C++20
326 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
327 bool ends_with(charT c) const noexcept; // C++20
328 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000329
Wim Leflere023c3542021-01-19 14:33:30 -0500330 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
331 constexpr bool contains(charT c) const noexcept; // C++2b
332 constexpr bool contains(const charT* s) const; // C++2b
333
Howard Hinnantc51e1022010-05-11 19:42:16 +0000334 bool __invariants() const;
335};
336
Marshall Clowa0563332018-02-08 06:34:03 +0000337template<class InputIterator,
338 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
339basic_string(InputIterator, InputIterator, Allocator = Allocator())
340 -> basic_string<typename iterator_traits<InputIterator>::value_type,
341 char_traits<typename iterator_traits<InputIterator>::value_type>,
342 Allocator>; // C++17
343
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344template<class charT, class traits, class Allocator>
345basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000346operator+(const basic_string<charT, traits, Allocator>& lhs,
347 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348
349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
351operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
352
353template<class charT, class traits, class Allocator>
354basic_string<charT, traits, Allocator>
355operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
356
357template<class charT, class traits, class Allocator>
358basic_string<charT, traits, Allocator>
359operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
360
361template<class charT, class traits, class Allocator>
362basic_string<charT, traits, Allocator>
363operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
364
365template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000366bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000367 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368
369template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000370bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000373bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000375template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000376bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000377 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378
379template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000380bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000383bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000386bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000387 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388
389template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000390bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000393bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394
395template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000396bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000397 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398
399template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000400bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000403bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000406bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000407 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408
409template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000410bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
412template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000413bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414
415template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000416bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000417 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418
419template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000420bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000423bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424
425template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000426void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000427 basic_string<charT, traits, Allocator>& rhs)
428 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
431basic_istream<charT, traits>&
432operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
433
434template<class charT, class traits, class Allocator>
435basic_ostream<charT, traits>&
436operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
437
438template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000439basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000440getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
441 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442
443template<class charT, class traits, class Allocator>
444basic_istream<charT, traits>&
445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
446
Marshall Clow29b53f22018-12-14 18:49:35 +0000447template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200448typename basic_string<charT, traits, Allocator>::size_type
449erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000450template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200451typename basic_string<charT, traits, Allocator>::size_type
452erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000453
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454typedef basic_string<char> string;
455typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100456typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000457typedef basic_string<char16_t> u16string;
458typedef basic_string<char32_t> u32string;
459
Bruce Mitchener170d8972020-11-24 12:53:53 -0500460int stoi (const string& str, size_t* idx = nullptr, int base = 10);
461long stol (const string& str, size_t* idx = nullptr, int base = 10);
462unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
463long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
464unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000465
Bruce Mitchener170d8972020-11-24 12:53:53 -0500466float stof (const string& str, size_t* idx = nullptr);
467double stod (const string& str, size_t* idx = nullptr);
468long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000469
470string to_string(int val);
471string to_string(unsigned val);
472string to_string(long val);
473string to_string(unsigned long val);
474string to_string(long long val);
475string to_string(unsigned long long val);
476string to_string(float val);
477string to_string(double val);
478string to_string(long double val);
479
Bruce Mitchener170d8972020-11-24 12:53:53 -0500480int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
481long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
482unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
483long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
484unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000485
Bruce Mitchener170d8972020-11-24 12:53:53 -0500486float stof (const wstring& str, size_t* idx = nullptr);
487double stod (const wstring& str, size_t* idx = nullptr);
488long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000489
490wstring to_wstring(int val);
491wstring to_wstring(unsigned val);
492wstring to_wstring(long val);
493wstring to_wstring(unsigned long val);
494wstring to_wstring(long long val);
495wstring to_wstring(unsigned long long val);
496wstring to_wstring(float val);
497wstring to_wstring(double val);
498wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499
500template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100501template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502template <> struct hash<u16string>;
503template <> struct hash<u32string>;
504template <> struct hash<wstring>;
505
Marshall Clowcba751f2013-07-23 17:05:24 +0000506basic_string<char> operator "" s( const char *str, size_t len ); // C++14
507basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100508basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000509basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
510basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
511
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512} // std
513
514*/
515
516#include <__config>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400517#include <compare>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000518#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519#include <iosfwd>
520#include <cstring>
Howard Hinnant155c2af2010-05-24 17:49:41 +0000521#include <cstdio> // For EOF.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522#include <cwchar>
523#include <algorithm>
524#include <iterator>
525#include <utility>
526#include <memory>
527#include <stdexcept>
528#include <type_traits>
529#include <initializer_list>
530#include <__functional_base>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000531#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
533#include <cstdint>
534#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000535
Eric Fiselier14b6de92014-08-10 23:53:08 +0000536#include <__debug>
537
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000538#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000540#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541
Eric Fiselierf4433a32017-05-31 22:07:49 +0000542_LIBCPP_PUSH_MACROS
543#include <__undef_macros>
544
545
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546_LIBCPP_BEGIN_NAMESPACE_STD
547
548// fpos
549
550template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000551class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552{
553private:
554 _StateT __st_;
555 streamoff __off_;
556public:
557 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
558
559 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
560
561 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
562 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
563
564 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
565 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
566 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
567 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
568};
569
570template <class _StateT>
571inline _LIBCPP_INLINE_VISIBILITY
572streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
573 {return streamoff(__x) - streamoff(__y);}
574
575template <class _StateT>
576inline _LIBCPP_INLINE_VISIBILITY
577bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
578 {return streamoff(__x) == streamoff(__y);}
579
580template <class _StateT>
581inline _LIBCPP_INLINE_VISIBILITY
582bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
583 {return streamoff(__x) != streamoff(__y);}
584
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585// basic_string
586
587template<class _CharT, class _Traits, class _Allocator>
588basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000589operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
590 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591
592template<class _CharT, class _Traits, class _Allocator>
593basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000594operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
596template<class _CharT, class _Traits, class _Allocator>
597basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000598operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
600template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000601inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000603operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604
605template<class _CharT, class _Traits, class _Allocator>
606basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000607operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608
Shoaib Meenaiea363712017-07-29 02:54:41 +0000609_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
610
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000612class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613{
614protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000615 _LIBCPP_NORETURN void __throw_length_error() const;
616 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617};
618
619template <bool __b>
620void
621__basic_string_common<__b>::__throw_length_error() const
622{
Marshall Clow8fea1612016-08-25 15:09:01 +0000623 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624}
625
626template <bool __b>
627void
628__basic_string_common<__b>::__throw_out_of_range() const
629{
Marshall Clow8fea1612016-08-25 15:09:01 +0000630 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631}
632
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000633_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634
Marshall Clow039b2f02016-01-13 21:54:34 +0000635template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400636struct __string_is_trivial_iterator : public false_type {};
637
638template <class _Tp>
639struct __string_is_trivial_iterator<_Tp*>
640 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000641
Louis Dionne173f29e2019-05-29 16:01:36 +0000642template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400643struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
644 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000645
Marshall Clow82513342016-09-24 22:45:42 +0000646template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500647struct __can_be_converted_to_string_view : public _BoolConstant<
648 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
649 !is_convertible<const _Tp&, const _CharT*>::value
650 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000651
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000652#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000653
654template <class _CharT, size_t = sizeof(_CharT)>
655struct __padding
656{
657 unsigned char __xx[sizeof(_CharT)-1];
658};
659
660template <class _CharT>
661struct __padding<_CharT, 1>
662{
663};
664
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400665#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000666
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400667#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800668typedef basic_string<char8_t> u8string;
669#endif
670
671#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
672typedef basic_string<char16_t> u16string;
673typedef basic_string<char32_t> u32string;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400674#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Richard Smith256954d2020-11-11 17:12:18 -0800675
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000676template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800677class
678 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400679#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800680 _LIBCPP_PREFERRED_NAME(u8string)
681#endif
682#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
683 _LIBCPP_PREFERRED_NAME(u16string)
684 _LIBCPP_PREFERRED_NAME(u32string)
685#endif
686 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687 : private __basic_string_common<true>
688{
689public:
690 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000691 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000693 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000695 typedef allocator_traits<allocator_type> __alloc_traits;
696 typedef typename __alloc_traits::size_type size_type;
697 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000698 typedef value_type& reference;
699 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000700 typedef typename __alloc_traits::pointer pointer;
701 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702
Marshall Clow79f33542018-03-21 00:36:05 +0000703 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
704 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
705 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
706 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000707 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000708 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000709 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000710
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 typedef __wrap_iter<pointer> iterator;
712 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000713 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
714 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715
716private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000717
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000718#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000719
720 struct __long
721 {
722 pointer __data_;
723 size_type __size_;
724 size_type __cap_;
725 };
726
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000727#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000728 static const size_type __short_mask = 0x01;
729 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000730#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000731 static const size_type __short_mask = 0x80;
732 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400733#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000734
735 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
736 (sizeof(__long) - 1)/sizeof(value_type) : 2};
737
738 struct __short
739 {
740 value_type __data_[__min_cap];
741 struct
742 : __padding<value_type>
743 {
744 unsigned char __size_;
745 };
746 };
747
748#else
749
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 struct __long
751 {
752 size_type __cap_;
753 size_type __size_;
754 pointer __data_;
755 };
756
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000757#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000758 static const size_type __short_mask = 0x80;
759 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000760#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000761 static const size_type __short_mask = 0x01;
762 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400763#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
766 (sizeof(__long) - 1)/sizeof(value_type) : 2};
767
768 struct __short
769 {
770 union
771 {
772 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000773 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 };
775 value_type __data_[__min_cap];
776 };
777
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400778#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000779
Howard Hinnant8ea98242013-08-23 17:37:05 +0000780 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781
Howard Hinnant8ea98242013-08-23 17:37:05 +0000782 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783
784 struct __raw
785 {
786 size_type __words[__n_words];
787 };
788
789 struct __rep
790 {
791 union
792 {
793 __long __l;
794 __short __s;
795 __raw __r;
796 };
797 };
798
799 __compressed_pair<__rep, allocator_type> __r_;
800
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200802 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803 static const size_type npos = -1;
804
Howard Hinnant3e276872011-06-03 18:40:47 +0000805 _LIBCPP_INLINE_VISIBILITY basic_string()
806 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000807
808 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
809#if _LIBCPP_STD_VER <= 14
810 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
811#else
812 _NOEXCEPT;
813#endif
814
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815 basic_string(const basic_string& __str);
816 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000817
Eric Fiselierfc92be82017-04-19 00:28:44 +0000818#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000820 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000821#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000822 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000823#else
824 _NOEXCEPT;
825#endif
826
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400829#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000830
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500831 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500833 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000834 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
835 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400836# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000837 __get_db()->__insert_c(this);
838# endif
839 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000840
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500841 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000842 _LIBCPP_INLINE_VISIBILITY
843 basic_string(const _CharT* __s, const _Allocator& __a);
844
Howard Hinnantcf823322010-12-17 14:46:43 +0000845 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000846 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000847 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000849 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000850 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000851
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500852 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000853 _LIBCPP_INLINE_VISIBILITY
854 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
855
Marshall Clow83445802016-04-07 18:13:41 +0000856 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000857 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000858 _LIBCPP_INLINE_VISIBILITY
859 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000860 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000861
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500862 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000863 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000864 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500865 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000866
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500867 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
868 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000869 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
870 explicit basic_string(const _Tp& __t);
871
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500872 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000873 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
874 explicit basic_string(const _Tp& __t, const allocator_type& __a);
875
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500876 template<class _InputIterator, class = _EnableIf<__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);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500879 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000882#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000883 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000884 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000885 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000886 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400887#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000889 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000891 _LIBCPP_INLINE_VISIBILITY
892 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
893
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000894 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000895
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500896 template <class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000897 basic_string& operator=(const _Tp& __t)
898 {__self_view __sv = __t; return assign(__sv);}
899
Eric Fiselierfc92be82017-04-19 00:28:44 +0000900#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000902 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000903 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000904 _LIBCPP_INLINE_VISIBILITY
905 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000907 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500996 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001014 _EnableIf<
1015 __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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001024 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001040 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001053 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001076 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001093 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001105 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001113 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001129 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001139 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001154 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001162 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001185 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001194 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001208 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001223 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001265 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001281 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001297 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001314 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001331 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001348 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001365 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001374 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001387 _EnableIf
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
1398 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1399 bool starts_with(__self_view __sv) const _NOEXCEPT
1400 { return __self_view(data(), size()).starts_with(__sv); }
1401
1402 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1403 bool starts_with(value_type __c) const _NOEXCEPT
1404 { return !empty() && _Traits::eq(front(), __c); }
1405
1406 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1407 bool starts_with(const value_type* __s) const _NOEXCEPT
1408 { return starts_with(__self_view(__s)); }
1409
1410 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1411 bool ends_with(__self_view __sv) const _NOEXCEPT
1412 { return __self_view(data(), size()).ends_with( __sv); }
1413
1414 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1415 bool ends_with(value_type __c) const _NOEXCEPT
1416 { return !empty() && _Traits::eq(back(), __c); }
1417
1418 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1419 bool ends_with(const value_type* __s) const _NOEXCEPT
1420 { 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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001583 _EnableIf
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
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001591 _EnableIf
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
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707 friend basic_string operator+<>(const basic_string&, const basic_string&);
1708 friend basic_string operator+<>(const value_type*, const basic_string&);
1709 friend basic_string operator+<>(value_type, const basic_string&);
1710 friend basic_string operator+<>(const basic_string&, const value_type*);
1711 friend basic_string operator+<>(const basic_string&, value_type);
1712};
1713
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001714// These declarations must appear before any functions are implicitly used
1715// so that they have the correct visibility specifier.
1716#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1717_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1718_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1719#else
1720_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1721_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1722#endif
1723
1724
Marshall Clowa0563332018-02-08 06:34:03 +00001725#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1726template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001727 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001728 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001729 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1730 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001731 >
1732basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1733 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001734
1735template<class _CharT,
1736 class _Traits,
1737 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001738 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001739 >
1740explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1741 -> basic_string<_CharT, _Traits, _Allocator>;
1742
1743template<class _CharT,
1744 class _Traits,
1745 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001746 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001747 class _Sz = typename allocator_traits<_Allocator>::size_type
1748 >
1749basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1750 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001751#endif
1752
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001754inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755void
1756basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1757{
Louis Dionneba400782020-10-02 15:02:52 -04001758#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001759 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001760#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761}
1762
1763template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001764inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001766basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767{
Louis Dionneba400782020-10-02 15:02:52 -04001768#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001769 __c_node* __c = __get_db()->__find_c_and_lock(this);
1770 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001772 const_pointer __new_last = __get_pointer() + __pos;
1773 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001775 --__p;
1776 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1777 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001779 (*__p)->__c_ = nullptr;
1780 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001781 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001784 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001786#else
1787 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001788#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789}
1790
1791template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001792inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001793basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001794 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001795 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796{
Louis Dionneba400782020-10-02 15:02:52 -04001797#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001798 __get_db()->__insert_c(this);
1799#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 __zero();
1801}
1802
1803template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001804inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001806#if _LIBCPP_STD_VER <= 14
1807 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1808#else
1809 _NOEXCEPT
1810#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001811: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812{
Louis Dionneba400782020-10-02 15:02:52 -04001813#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001814 __get_db()->__insert_c(this);
1815#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816 __zero();
1817}
1818
1819template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001820void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1821 size_type __sz,
1822 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823{
1824 if (__reserve > max_size())
1825 this->__throw_length_error();
1826 pointer __p;
1827 if (__reserve < __min_cap)
1828 {
1829 __set_short_size(__sz);
1830 __p = __get_short_pointer();
1831 }
1832 else
1833 {
1834 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001835 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 __set_long_pointer(__p);
1837 __set_long_cap(__cap+1);
1838 __set_long_size(__sz);
1839 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001840 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841 traits_type::assign(__p[__sz], value_type());
1842}
1843
1844template <class _CharT, class _Traits, class _Allocator>
1845void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001846basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847{
1848 if (__sz > max_size())
1849 this->__throw_length_error();
1850 pointer __p;
1851 if (__sz < __min_cap)
1852 {
1853 __set_short_size(__sz);
1854 __p = __get_short_pointer();
1855 }
1856 else
1857 {
1858 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001859 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860 __set_long_pointer(__p);
1861 __set_long_cap(__cap+1);
1862 __set_long_size(__sz);
1863 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001864 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 traits_type::assign(__p[__sz], value_type());
1866}
1867
1868template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001869template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001870basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001871 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001873 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001875#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001876 __get_db()->__insert_c(this);
1877#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878}
1879
1880template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001881inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001882basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001883 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001885 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001887#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001888 __get_db()->__insert_c(this);
1889#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890}
1891
1892template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001893inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001894basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001895 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001897 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001899#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001900 __get_db()->__insert_c(this);
1901#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902}
1903
1904template <class _CharT, class _Traits, class _Allocator>
1905basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001906 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907{
1908 if (!__str.__is_long())
1909 __r_.first().__r = __str.__r_.first().__r;
1910 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001911 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1912 __str.__get_long_size());
1913
Louis Dionneba400782020-10-02 15:02:52 -04001914#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001915 __get_db()->__insert_c(this);
1916#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917}
1918
1919template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001920basic_string<_CharT, _Traits, _Allocator>::basic_string(
1921 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001922 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923{
1924 if (!__str.__is_long())
1925 __r_.first().__r = __str.__r_.first().__r;
1926 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001927 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1928 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001929#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001930 __get_db()->__insert_c(this);
1931#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932}
1933
Martijn Vels5e7c9752020-03-04 17:52:46 -05001934template <class _CharT, class _Traits, class _Allocator>
1935void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1936 const value_type* __s, size_type __sz) {
1937 pointer __p;
1938 if (__sz < __min_cap) {
1939 __p = __get_short_pointer();
1940 __set_short_size(__sz);
1941 } else {
1942 if (__sz > max_size())
1943 this->__throw_length_error();
1944 size_t __cap = __recommend(__sz);
1945 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1946 __set_long_pointer(__p);
1947 __set_long_cap(__cap + 1);
1948 __set_long_size(__sz);
1949 }
1950 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1951}
1952
Eric Fiselierfc92be82017-04-19 00:28:44 +00001953#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954
1955template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001956inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001957basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001958#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001959 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001960#else
1961 _NOEXCEPT
1962#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001963 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964{
1965 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001966#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001967 __get_db()->__insert_c(this);
1968 if (__is_long())
1969 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970#endif
1971}
1972
1973template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001974inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001976 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977{
Marshall Clowd2d24692014-07-17 15:32:20 +00001978 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001979 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001980 else
1981 {
1982 __r_.first().__r = __str.__r_.first().__r;
1983 __str.__zero();
1984 }
Louis Dionneba400782020-10-02 15:02:52 -04001985#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001986 __get_db()->__insert_c(this);
1987 if (__is_long())
1988 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989#endif
1990}
1991
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001992#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993
1994template <class _CharT, class _Traits, class _Allocator>
1995void
1996basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1997{
1998 if (__n > max_size())
1999 this->__throw_length_error();
2000 pointer __p;
2001 if (__n < __min_cap)
2002 {
2003 __set_short_size(__n);
2004 __p = __get_short_pointer();
2005 }
2006 else
2007 {
2008 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002009 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010 __set_long_pointer(__p);
2011 __set_long_cap(__cap+1);
2012 __set_long_size(__n);
2013 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002014 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015 traits_type::assign(__p[__n], value_type());
2016}
2017
2018template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002019inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002020basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002021 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022{
2023 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002024#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002025 __get_db()->__insert_c(this);
2026#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027}
2028
2029template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002030template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002031basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002032 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033{
2034 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002035#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002036 __get_db()->__insert_c(this);
2037#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038}
2039
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002041basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2042 size_type __pos, size_type __n,
2043 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002044 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045{
2046 size_type __str_sz = __str.size();
2047 if (__pos > __str_sz)
2048 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002049 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002050#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002051 __get_db()->__insert_c(this);
2052#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053}
2054
2055template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002056inline
Marshall Clow83445802016-04-07 18:13:41 +00002057basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002058 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002059 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002060{
2061 size_type __str_sz = __str.size();
2062 if (__pos > __str_sz)
2063 this->__throw_out_of_range();
2064 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002065#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002066 __get_db()->__insert_c(this);
2067#endif
2068}
2069
2070template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002071template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002072basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002073 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002074 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002075{
Marshall Clowe46031a2018-07-02 18:41:15 +00002076 __self_view __sv0 = __t;
2077 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002078 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002079#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002080 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002081#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002082}
2083
2084template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002085template <class _Tp, class>
2086basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002087 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002088{
Marshall Clowe46031a2018-07-02 18:41:15 +00002089 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002090 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002091#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002092 __get_db()->__insert_c(this);
2093#endif
2094}
2095
2096template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002097template <class _Tp, class>
2098basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002099 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002100{
Marshall Clowe46031a2018-07-02 18:41:15 +00002101 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002102 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002103#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002104 __get_db()->__insert_c(this);
2105#endif
2106}
2107
2108template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002110_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002112 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2113>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2115{
2116 __zero();
2117#ifndef _LIBCPP_NO_EXCEPTIONS
2118 try
2119 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002120#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 for (; __first != __last; ++__first)
2122 push_back(*__first);
2123#ifndef _LIBCPP_NO_EXCEPTIONS
2124 }
2125 catch (...)
2126 {
2127 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002128 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129 throw;
2130 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002131#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132}
2133
2134template <class _CharT, class _Traits, class _Allocator>
2135template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002136_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002138 __is_cpp17_forward_iterator<_ForwardIterator>::value
2139>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2141{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002142 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 if (__sz > max_size())
2144 this->__throw_length_error();
2145 pointer __p;
2146 if (__sz < __min_cap)
2147 {
2148 __set_short_size(__sz);
2149 __p = __get_short_pointer();
2150 }
2151 else
2152 {
2153 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002154 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155 __set_long_pointer(__p);
2156 __set_long_cap(__cap+1);
2157 __set_long_size(__sz);
2158 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002159
2160#ifndef _LIBCPP_NO_EXCEPTIONS
2161 try
2162 {
2163#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002164 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165 traits_type::assign(*__p, *__first);
2166 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002167#ifndef _LIBCPP_NO_EXCEPTIONS
2168 }
2169 catch (...)
2170 {
2171 if (__is_long())
2172 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2173 throw;
2174 }
2175#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176}
2177
2178template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002179template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002180inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002182 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183{
2184 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002185#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002186 __get_db()->__insert_c(this);
2187#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188}
2189
2190template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002191template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2194 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002195 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196{
2197 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002198#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002199 __get_db()->__insert_c(this);
2200#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201}
2202
Eric Fiselierfc92be82017-04-19 00:28:44 +00002203#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002204
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002206inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002207basic_string<_CharT, _Traits, _Allocator>::basic_string(
2208 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002209 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210{
2211 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002212#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002213 __get_db()->__insert_c(this);
2214#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215}
2216
2217template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002218inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002219
Eric Fiselier812882b2017-02-17 01:17:10 +00002220basic_string<_CharT, _Traits, _Allocator>::basic_string(
2221 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002222 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223{
2224 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002225#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002226 __get_db()->__insert_c(this);
2227#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228}
2229
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002230#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002231
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2234{
Louis Dionneba400782020-10-02 15:02:52 -04002235#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002236 __get_db()->__erase_c(this);
2237#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002239 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240}
2241
2242template <class _CharT, class _Traits, class _Allocator>
2243void
2244basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2245 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002246 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 +00002247{
2248 size_type __ms = max_size();
2249 if (__delta_cap > __ms - __old_cap - 1)
2250 this->__throw_length_error();
2251 pointer __old_p = __get_pointer();
2252 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002253 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002255 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256 __invalidate_all_iterators();
2257 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002258 traits_type::copy(_VSTD::__to_address(__p),
2259 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002261 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2263 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002264 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2265 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002267 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 __set_long_pointer(__p);
2269 __set_long_cap(__cap+1);
2270 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2271 __set_long_size(__old_sz);
2272 traits_type::assign(__p[__old_sz], value_type());
2273}
2274
2275template <class _CharT, class _Traits, class _Allocator>
2276void
2277basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2278 size_type __n_copy, size_type __n_del, size_type __n_add)
2279{
2280 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002281 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282 this->__throw_length_error();
2283 pointer __old_p = __get_pointer();
2284 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002285 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002287 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 __invalidate_all_iterators();
2289 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002290 traits_type::copy(_VSTD::__to_address(__p),
2291 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2293 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002294 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2295 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002296 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002298 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299 __set_long_pointer(__p);
2300 __set_long_cap(__cap+1);
2301}
2302
2303// assign
2304
2305template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002306template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002307basic_string<_CharT, _Traits, _Allocator>&
2308basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002309 const value_type* __s, size_type __n) {
2310 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2311 if (__n < __cap) {
2312 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2313 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2314 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2315 traits_type::assign(__p[__n], value_type());
2316 __invalidate_iterators_past(__n);
2317 } else {
2318 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2319 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2320 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002321 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002322}
2323
2324template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002326basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2327 const value_type* __s, size_type __n) {
2328 size_type __cap = capacity();
2329 if (__cap >= __n) {
2330 value_type* __p = _VSTD::__to_address(__get_pointer());
2331 traits_type::move(__p, __s, __n);
2332 traits_type::assign(__p[__n], value_type());
2333 __set_size(__n);
2334 __invalidate_iterators_past(__n);
2335 } else {
2336 size_type __sz = size();
2337 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2338 }
2339 return *this;
2340}
2341
2342template <class _CharT, class _Traits, class _Allocator>
2343basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002344basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002346 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002347 return (_LIBCPP_BUILTIN_CONSTANT_P(__n) && __n < __min_cap)
2348 ? __assign_short(__s, __n)
2349 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350}
2351
2352template <class _CharT, class _Traits, class _Allocator>
2353basic_string<_CharT, _Traits, _Allocator>&
2354basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2355{
2356 size_type __cap = capacity();
2357 if (__cap < __n)
2358 {
2359 size_type __sz = size();
2360 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2361 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002362 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363 traits_type::assign(__p, __n, __c);
2364 traits_type::assign(__p[__n], value_type());
2365 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002366 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002367 return *this;
2368}
2369
2370template <class _CharT, class _Traits, class _Allocator>
2371basic_string<_CharT, _Traits, _Allocator>&
2372basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2373{
2374 pointer __p;
2375 if (__is_long())
2376 {
2377 __p = __get_long_pointer();
2378 __set_long_size(1);
2379 }
2380 else
2381 {
2382 __p = __get_short_pointer();
2383 __set_short_size(1);
2384 }
2385 traits_type::assign(*__p, __c);
2386 traits_type::assign(*++__p, value_type());
2387 __invalidate_iterators_past(1);
2388 return *this;
2389}
2390
2391template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002392basic_string<_CharT, _Traits, _Allocator>&
2393basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2394{
Martijn Vels596e3de2020-02-26 15:55:49 -05002395 if (this != &__str) {
2396 __copy_assign_alloc(__str);
2397 if (!__is_long()) {
2398 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002399 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002400 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002401 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002402 }
2403 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002404 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002405 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002406 }
2407 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002408}
2409
Eric Fiselierfc92be82017-04-19 00:28:44 +00002410#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002411
2412template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002413inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002414void
2415basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002416 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002417{
2418 if (__alloc() != __str.__alloc())
2419 assign(__str);
2420 else
2421 __move_assign(__str, true_type());
2422}
2423
2424template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002425inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002426void
2427basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002428#if _LIBCPP_STD_VER > 14
2429 _NOEXCEPT
2430#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002431 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002432#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002433{
marshall2ed41622019-11-27 07:13:00 -08002434 if (__is_long()) {
2435 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2436 __get_long_cap());
2437#if _LIBCPP_STD_VER <= 14
2438 if (!is_nothrow_move_assignable<allocator_type>::value) {
2439 __set_short_size(0);
2440 traits_type::assign(__get_short_pointer()[0], value_type());
2441 }
2442#endif
2443 }
2444 __move_assign_alloc(__str);
2445 __r_.first() = __str.__r_.first();
2446 __str.__set_short_size(0);
2447 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002448}
2449
2450template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002451inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002452basic_string<_CharT, _Traits, _Allocator>&
2453basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002454 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002455{
2456 __move_assign(__str, integral_constant<bool,
2457 __alloc_traits::propagate_on_container_move_assignment::value>());
2458 return *this;
2459}
2460
2461#endif
2462
2463template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002465_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002467 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002469>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2471{
Marshall Clow958362f2016-09-05 01:54:30 +00002472 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002473 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002474 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475}
2476
2477template <class _CharT, class _Traits, class _Allocator>
2478template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002479_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002481 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002483>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2485{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002487 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2488 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2489
2490 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2491 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002493 if (__cap < __n)
2494 {
2495 size_type __sz = size();
2496 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2497 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002498 pointer __p = __get_pointer();
2499 for (; __first != __last; ++__first, ++__p)
2500 traits_type::assign(*__p, *__first);
2501 traits_type::assign(*__p, value_type());
2502 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002503 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504 }
2505 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002506 {
2507 const basic_string __temp(__first, __last, __alloc());
2508 assign(__temp.data(), __temp.size());
2509 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510 return *this;
2511}
2512
2513template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514basic_string<_CharT, _Traits, _Allocator>&
2515basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2516{
2517 size_type __sz = __str.size();
2518 if (__pos > __sz)
2519 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002520 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521}
2522
2523template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002524template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002525_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002526<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002527 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2528 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002529 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002530>
Marshall Clow82513342016-09-24 22:45:42 +00002531basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002532{
Marshall Clow82513342016-09-24 22:45:42 +00002533 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002534 size_type __sz = __sv.size();
2535 if (__pos > __sz)
2536 this->__throw_out_of_range();
2537 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2538}
2539
2540
2541template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002542basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002543basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2544 return __assign_external(__s, traits_type::length(__s));
2545}
2546
2547template <class _CharT, class _Traits, class _Allocator>
2548basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002549basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002551 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002552 return _LIBCPP_BUILTIN_CONSTANT_P(*__s)
2553 ? (traits_type::length(__s) < __min_cap
2554 ? __assign_short(__s, traits_type::length(__s))
2555 : __assign_external(__s, traits_type::length(__s)))
2556 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558// append
2559
2560template <class _CharT, class _Traits, class _Allocator>
2561basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002562basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002564 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565 size_type __cap = capacity();
2566 size_type __sz = size();
2567 if (__cap - __sz >= __n)
2568 {
2569 if (__n)
2570 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002571 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572 traits_type::copy(__p + __sz, __s, __n);
2573 __sz += __n;
2574 __set_size(__sz);
2575 traits_type::assign(__p[__sz], value_type());
2576 }
2577 }
2578 else
2579 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2580 return *this;
2581}
2582
2583template <class _CharT, class _Traits, class _Allocator>
2584basic_string<_CharT, _Traits, _Allocator>&
2585basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2586{
2587 if (__n)
2588 {
2589 size_type __cap = capacity();
2590 size_type __sz = size();
2591 if (__cap - __sz < __n)
2592 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2593 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002594 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595 __sz += __n;
2596 __set_size(__sz);
2597 traits_type::assign(__p[__sz], value_type());
2598 }
2599 return *this;
2600}
2601
2602template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002603inline void
2604basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2605{
2606 if (__n)
2607 {
2608 size_type __cap = capacity();
2609 size_type __sz = size();
2610 if (__cap - __sz < __n)
2611 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2612 pointer __p = __get_pointer();
2613 __sz += __n;
2614 __set_size(__sz);
2615 traits_type::assign(__p[__sz], value_type());
2616 }
2617}
2618
2619template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620void
2621basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2622{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002623 bool __is_short = !__is_long();
2624 size_type __cap;
2625 size_type __sz;
2626 if (__is_short)
2627 {
2628 __cap = __min_cap - 1;
2629 __sz = __get_short_size();
2630 }
2631 else
2632 {
2633 __cap = __get_long_cap() - 1;
2634 __sz = __get_long_size();
2635 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002637 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002638 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002639 __is_short = !__is_long();
2640 }
2641 pointer __p;
2642 if (__is_short)
2643 {
2644 __p = __get_short_pointer() + __sz;
2645 __set_short_size(__sz+1);
2646 }
2647 else
2648 {
2649 __p = __get_long_pointer() + __sz;
2650 __set_long_size(__sz+1);
2651 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652 traits_type::assign(*__p, __c);
2653 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654}
2655
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656template <class _CharT, class _Traits, class _Allocator>
2657template<class _ForwardIterator>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002658_EnableIf
2659<
2660 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2661 basic_string<_CharT, _Traits, _Allocator>&
2662>
2663basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002664 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665{
2666 size_type __sz = size();
2667 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002668 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669 if (__n)
2670 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002671 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2672 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002673 {
2674 if (__cap - __sz < __n)
2675 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2676 pointer __p = __get_pointer() + __sz;
2677 for (; __first != __last; ++__p, ++__first)
2678 traits_type::assign(*__p, *__first);
2679 traits_type::assign(*__p, value_type());
2680 __set_size(__sz + __n);
2681 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002682 else
2683 {
2684 const basic_string __temp(__first, __last, __alloc());
2685 append(__temp.data(), __temp.size());
2686 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 }
2688 return *this;
2689}
2690
2691template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002692inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693basic_string<_CharT, _Traits, _Allocator>&
2694basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2695{
2696 return append(__str.data(), __str.size());
2697}
2698
2699template <class _CharT, class _Traits, class _Allocator>
2700basic_string<_CharT, _Traits, _Allocator>&
2701basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2702{
2703 size_type __sz = __str.size();
2704 if (__pos > __sz)
2705 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002706 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707}
2708
2709template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002710template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002711 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002712 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002713 __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 +00002714 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002715 >
Marshall Clow82513342016-09-24 22:45:42 +00002716basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002717{
Marshall Clow82513342016-09-24 22:45:42 +00002718 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002719 size_type __sz = __sv.size();
2720 if (__pos > __sz)
2721 this->__throw_out_of_range();
2722 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2723}
2724
2725template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002727basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002729 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730 return append(__s, traits_type::length(__s));
2731}
2732
2733// insert
2734
2735template <class _CharT, class _Traits, class _Allocator>
2736basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002737basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002739 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740 size_type __sz = size();
2741 if (__pos > __sz)
2742 this->__throw_out_of_range();
2743 size_type __cap = capacity();
2744 if (__cap - __sz >= __n)
2745 {
2746 if (__n)
2747 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002748 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749 size_type __n_move = __sz - __pos;
2750 if (__n_move != 0)
2751 {
2752 if (__p + __pos <= __s && __s < __p + __sz)
2753 __s += __n;
2754 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2755 }
2756 traits_type::move(__p + __pos, __s, __n);
2757 __sz += __n;
2758 __set_size(__sz);
2759 traits_type::assign(__p[__sz], value_type());
2760 }
2761 }
2762 else
2763 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2764 return *this;
2765}
2766
2767template <class _CharT, class _Traits, class _Allocator>
2768basic_string<_CharT, _Traits, _Allocator>&
2769basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2770{
2771 size_type __sz = size();
2772 if (__pos > __sz)
2773 this->__throw_out_of_range();
2774 if (__n)
2775 {
2776 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002777 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 if (__cap - __sz >= __n)
2779 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002780 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 size_type __n_move = __sz - __pos;
2782 if (__n_move != 0)
2783 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2784 }
2785 else
2786 {
2787 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002788 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789 }
2790 traits_type::assign(__p + __pos, __n, __c);
2791 __sz += __n;
2792 __set_size(__sz);
2793 traits_type::assign(__p[__sz], value_type());
2794 }
2795 return *this;
2796}
2797
2798template <class _CharT, class _Traits, class _Allocator>
2799template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002800_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002802 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002803 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002804>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2806{
Louis Dionneba400782020-10-02 15:02:52 -04002807#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002808 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2809 "string::insert(iterator, range) called with an iterator not"
2810 " referring to this string");
2811#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002812 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002813 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814}
2815
2816template <class _CharT, class _Traits, class _Allocator>
2817template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002818_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002820 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002822>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2824{
Louis Dionneba400782020-10-02 15:02:52 -04002825#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002826 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2827 "string::insert(iterator, range) called with an iterator not"
2828 " referring to this string");
2829#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002831 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 if (__n)
2833 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002834 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2835 !__addr_in_range(*__first))
2836 {
2837 size_type __sz = size();
2838 size_type __cap = capacity();
2839 value_type* __p;
2840 if (__cap - __sz >= __n)
2841 {
2842 __p = _VSTD::__to_address(__get_pointer());
2843 size_type __n_move = __sz - __ip;
2844 if (__n_move != 0)
2845 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2846 }
2847 else
2848 {
2849 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2850 __p = _VSTD::__to_address(__get_long_pointer());
2851 }
2852 __sz += __n;
2853 __set_size(__sz);
2854 traits_type::assign(__p[__sz], value_type());
2855 for (__p += __ip; __first != __last; ++__p, ++__first)
2856 traits_type::assign(*__p, *__first);
2857 }
2858 else
Marshall Clow958362f2016-09-05 01:54:30 +00002859 {
2860 const basic_string __temp(__first, __last, __alloc());
2861 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2862 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 }
2864 return begin() + __ip;
2865}
2866
2867template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002868inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869basic_string<_CharT, _Traits, _Allocator>&
2870basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2871{
2872 return insert(__pos1, __str.data(), __str.size());
2873}
2874
2875template <class _CharT, class _Traits, class _Allocator>
2876basic_string<_CharT, _Traits, _Allocator>&
2877basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2878 size_type __pos2, size_type __n)
2879{
2880 size_type __str_sz = __str.size();
2881 if (__pos2 > __str_sz)
2882 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002883 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884}
2885
2886template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002887template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002888_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002889<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002890 __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 +00002891 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002892>
Marshall Clow82513342016-09-24 22:45:42 +00002893basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002894 size_type __pos2, size_type __n)
2895{
Marshall Clow82513342016-09-24 22:45:42 +00002896 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002897 size_type __str_sz = __sv.size();
2898 if (__pos2 > __str_sz)
2899 this->__throw_out_of_range();
2900 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2901}
2902
2903template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002905basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002907 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 return insert(__pos, __s, traits_type::length(__s));
2909}
2910
2911template <class _CharT, class _Traits, class _Allocator>
2912typename basic_string<_CharT, _Traits, _Allocator>::iterator
2913basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2914{
2915 size_type __ip = static_cast<size_type>(__pos - begin());
2916 size_type __sz = size();
2917 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002918 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919 if (__cap == __sz)
2920 {
2921 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002922 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 }
2924 else
2925 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002926 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 size_type __n_move = __sz - __ip;
2928 if (__n_move != 0)
2929 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2930 }
2931 traits_type::assign(__p[__ip], __c);
2932 traits_type::assign(__p[++__sz], value_type());
2933 __set_size(__sz);
2934 return begin() + static_cast<difference_type>(__ip);
2935}
2936
2937template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002938inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939typename basic_string<_CharT, _Traits, _Allocator>::iterator
2940basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2941{
Louis Dionneba400782020-10-02 15:02:52 -04002942#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002943 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2944 "string::insert(iterator, n, value) called with an iterator not"
2945 " referring to this string");
2946#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 difference_type __p = __pos - begin();
2948 insert(static_cast<size_type>(__p), __n, __c);
2949 return begin() + __p;
2950}
2951
2952// replace
2953
2954template <class _CharT, class _Traits, class _Allocator>
2955basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002956basic_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 +00002957 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002959 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960 size_type __sz = size();
2961 if (__pos > __sz)
2962 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002963 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 size_type __cap = capacity();
2965 if (__cap - __sz + __n1 >= __n2)
2966 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002967 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968 if (__n1 != __n2)
2969 {
2970 size_type __n_move = __sz - __pos - __n1;
2971 if (__n_move != 0)
2972 {
2973 if (__n1 > __n2)
2974 {
2975 traits_type::move(__p + __pos, __s, __n2);
2976 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2977 goto __finish;
2978 }
2979 if (__p + __pos < __s && __s < __p + __sz)
2980 {
2981 if (__p + __pos + __n1 <= __s)
2982 __s += __n2 - __n1;
2983 else // __p + __pos < __s < __p + __pos + __n1
2984 {
2985 traits_type::move(__p + __pos, __s, __n1);
2986 __pos += __n1;
2987 __s += __n2;
2988 __n2 -= __n1;
2989 __n1 = 0;
2990 }
2991 }
2992 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2993 }
2994 }
2995 traits_type::move(__p + __pos, __s, __n2);
2996__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05002997// __sz += __n2 - __n1; in this and the below function below can cause unsigned
2998// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999 __sz += __n2 - __n1;
3000 __set_size(__sz);
3001 __invalidate_iterators_past(__sz);
3002 traits_type::assign(__p[__sz], value_type());
3003 }
3004 else
3005 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3006 return *this;
3007}
3008
3009template <class _CharT, class _Traits, class _Allocator>
3010basic_string<_CharT, _Traits, _Allocator>&
3011basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00003012 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013{
3014 size_type __sz = size();
3015 if (__pos > __sz)
3016 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003017 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003019 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003020 if (__cap - __sz + __n1 >= __n2)
3021 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003022 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023 if (__n1 != __n2)
3024 {
3025 size_type __n_move = __sz - __pos - __n1;
3026 if (__n_move != 0)
3027 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3028 }
3029 }
3030 else
3031 {
3032 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003033 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034 }
3035 traits_type::assign(__p + __pos, __n2, __c);
3036 __sz += __n2 - __n1;
3037 __set_size(__sz);
3038 __invalidate_iterators_past(__sz);
3039 traits_type::assign(__p[__sz], value_type());
3040 return *this;
3041}
3042
3043template <class _CharT, class _Traits, class _Allocator>
3044template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003045_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003047 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003049>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003050basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051 _InputIterator __j1, _InputIterator __j2)
3052{
Marshall Clow958362f2016-09-05 01:54:30 +00003053 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003054 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055}
3056
3057template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003058inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059basic_string<_CharT, _Traits, _Allocator>&
3060basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3061{
3062 return replace(__pos1, __n1, __str.data(), __str.size());
3063}
3064
3065template <class _CharT, class _Traits, class _Allocator>
3066basic_string<_CharT, _Traits, _Allocator>&
3067basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3068 size_type __pos2, size_type __n2)
3069{
3070 size_type __str_sz = __str.size();
3071 if (__pos2 > __str_sz)
3072 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003073 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074}
3075
3076template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003077template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003078_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003079<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003080 __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 +00003081 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003082>
Marshall Clow82513342016-09-24 22:45:42 +00003083basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003084 size_type __pos2, size_type __n2)
3085{
Marshall Clow82513342016-09-24 22:45:42 +00003086 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003087 size_type __str_sz = __sv.size();
3088 if (__pos2 > __str_sz)
3089 this->__throw_out_of_range();
3090 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3091}
3092
3093template <class _CharT, class _Traits, class _Allocator>
3094basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003095basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003097 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003098 return replace(__pos, __n1, __s, traits_type::length(__s));
3099}
3100
3101template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003102inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003104basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105{
3106 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3107 __str.data(), __str.size());
3108}
3109
3110template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003111inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003113basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114{
3115 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3116}
3117
3118template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003119inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003121basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122{
3123 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3124}
3125
3126template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003127inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003129basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130{
3131 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3132}
3133
3134// erase
3135
Martijn Velsa81fc792020-02-26 13:25:43 -05003136// 'externally instantiated' erase() implementation, called when __n != npos.
3137// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003139void
3140basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3141 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143 if (__n)
3144 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003145 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003146 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003147 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148 size_type __n_move = __sz - __pos - __n;
3149 if (__n_move != 0)
3150 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3151 __sz -= __n;
3152 __set_size(__sz);
3153 __invalidate_iterators_past(__sz);
3154 traits_type::assign(__p[__sz], value_type());
3155 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003156}
3157
3158template <class _CharT, class _Traits, class _Allocator>
3159basic_string<_CharT, _Traits, _Allocator>&
3160basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3161 size_type __n) {
3162 if (__pos > size()) this->__throw_out_of_range();
3163 if (__n == npos) {
3164 __erase_to_end(__pos);
3165 } else {
3166 __erase_external_with_move(__pos, __n);
3167 }
3168 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169}
3170
3171template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003172inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173typename basic_string<_CharT, _Traits, _Allocator>::iterator
3174basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3175{
Louis Dionneba400782020-10-02 15:02:52 -04003176#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003177 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3178 "string::erase(iterator) called with an iterator not"
3179 " referring to this string");
3180#endif
3181 _LIBCPP_ASSERT(__pos != end(),
3182 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183 iterator __b = begin();
3184 size_type __r = static_cast<size_type>(__pos - __b);
3185 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003186 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187}
3188
3189template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003190inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191typename basic_string<_CharT, _Traits, _Allocator>::iterator
3192basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3193{
Louis Dionneba400782020-10-02 15:02:52 -04003194#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003195 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3196 "string::erase(iterator, iterator) called with an iterator not"
3197 " referring to this string");
3198#endif
3199 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 iterator __b = begin();
3201 size_type __r = static_cast<size_type>(__first - __b);
3202 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003203 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204}
3205
3206template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003207inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208void
3209basic_string<_CharT, _Traits, _Allocator>::pop_back()
3210{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003211 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212 size_type __sz;
3213 if (__is_long())
3214 {
3215 __sz = __get_long_size() - 1;
3216 __set_long_size(__sz);
3217 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3218 }
3219 else
3220 {
3221 __sz = __get_short_size() - 1;
3222 __set_short_size(__sz);
3223 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3224 }
3225 __invalidate_iterators_past(__sz);
3226}
3227
3228template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003229inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003231basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232{
3233 __invalidate_all_iterators();
3234 if (__is_long())
3235 {
3236 traits_type::assign(*__get_long_pointer(), value_type());
3237 __set_long_size(0);
3238 }
3239 else
3240 {
3241 traits_type::assign(*__get_short_pointer(), value_type());
3242 __set_short_size(0);
3243 }
3244}
3245
3246template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003247inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248void
3249basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3250{
3251 if (__is_long())
3252 {
3253 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3254 __set_long_size(__pos);
3255 }
3256 else
3257 {
3258 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3259 __set_short_size(__pos);
3260 }
3261 __invalidate_iterators_past(__pos);
3262}
3263
3264template <class _CharT, class _Traits, class _Allocator>
3265void
3266basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3267{
3268 size_type __sz = size();
3269 if (__n > __sz)
3270 append(__n - __sz, __c);
3271 else
3272 __erase_to_end(__n);
3273}
3274
3275template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003276inline void
3277basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3278{
3279 size_type __sz = size();
3280 if (__n > __sz) {
3281 __append_default_init(__n - __sz);
3282 } else
3283 __erase_to_end(__n);
3284}
3285
3286template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003287inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003289basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003291 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003292#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003293 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003295 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296#endif
3297}
3298
3299template <class _CharT, class _Traits, class _Allocator>
3300void
Marek Kurdejc9848142020-11-26 10:07:16 +01003301basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302{
Marek Kurdejc9848142020-11-26 10:07:16 +01003303 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003305
3306#if _LIBCPP_STD_VER > 17
3307 // Reserve never shrinks as of C++20.
3308 if (__requested_capacity <= capacity()) return;
3309#endif
3310
3311 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3312 __target_capacity = __recommend(__target_capacity);
3313 if (__target_capacity == capacity()) return;
3314
3315 __shrink_or_extend(__target_capacity);
3316}
3317
3318template <class _CharT, class _Traits, class _Allocator>
3319void
3320basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3321{
3322 size_type __target_capacity = __recommend(size());
3323 if (__target_capacity == capacity()) return;
3324
3325 __shrink_or_extend(__target_capacity);
3326}
3327
3328template <class _CharT, class _Traits, class _Allocator>
3329void
3330basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3331{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332 size_type __cap = capacity();
3333 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003334
3335 pointer __new_data, __p;
3336 bool __was_long, __now_long;
3337 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003339 __was_long = true;
3340 __now_long = false;
3341 __new_data = __get_short_pointer();
3342 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003344 else
3345 {
3346 if (__target_capacity > __cap)
3347 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3348 else
3349 {
3350 #ifndef _LIBCPP_NO_EXCEPTIONS
3351 try
3352 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003353 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003354 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3355 #ifndef _LIBCPP_NO_EXCEPTIONS
3356 }
3357 catch (...)
3358 {
3359 return;
3360 }
3361 #else // _LIBCPP_NO_EXCEPTIONS
3362 if (__new_data == nullptr)
3363 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003364 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003365 }
3366 __now_long = true;
3367 __was_long = __is_long();
3368 __p = __get_pointer();
3369 }
3370 traits_type::copy(_VSTD::__to_address(__new_data),
3371 _VSTD::__to_address(__p), size()+1);
3372 if (__was_long)
3373 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3374 if (__now_long)
3375 {
3376 __set_long_cap(__target_capacity+1);
3377 __set_long_size(__sz);
3378 __set_long_pointer(__new_data);
3379 }
3380 else
3381 __set_short_size(__sz);
3382 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383}
3384
3385template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003386inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003388basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003390 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391 return *(data() + __pos);
3392}
3393
3394template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003395inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003397basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003399 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400 return *(__get_pointer() + __pos);
3401}
3402
3403template <class _CharT, class _Traits, class _Allocator>
3404typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3405basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3406{
3407 if (__n >= size())
3408 this->__throw_out_of_range();
3409 return (*this)[__n];
3410}
3411
3412template <class _CharT, class _Traits, class _Allocator>
3413typename basic_string<_CharT, _Traits, _Allocator>::reference
3414basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3415{
3416 if (__n >= size())
3417 this->__throw_out_of_range();
3418 return (*this)[__n];
3419}
3420
3421template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003422inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003424basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003426 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427 return *__get_pointer();
3428}
3429
3430template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003431inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003433basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003435 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436 return *data();
3437}
3438
3439template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003440inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003442basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003444 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445 return *(__get_pointer() + size() - 1);
3446}
3447
3448template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003449inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003451basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003453 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454 return *(data() + size() - 1);
3455}
3456
3457template <class _CharT, class _Traits, class _Allocator>
3458typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003459basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460{
3461 size_type __sz = size();
3462 if (__pos > __sz)
3463 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003464 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465 traits_type::copy(__s, data() + __pos, __rlen);
3466 return __rlen;
3467}
3468
3469template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003470inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003471basic_string<_CharT, _Traits, _Allocator>
3472basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3473{
3474 return basic_string(*this, __pos, __n, __alloc());
3475}
3476
3477template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003478inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479void
3480basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003481#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003482 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003483#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003484 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003485 __is_nothrow_swappable<allocator_type>::value)
3486#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487{
Louis Dionneba400782020-10-02 15:02:52 -04003488#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003489 if (!__is_long())
3490 __get_db()->__invalidate_all(this);
3491 if (!__str.__is_long())
3492 __get_db()->__invalidate_all(&__str);
3493 __get_db()->swap(this, &__str);
3494#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003495 _LIBCPP_ASSERT(
3496 __alloc_traits::propagate_on_container_swap::value ||
3497 __alloc_traits::is_always_equal::value ||
3498 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003499 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003500 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501}
3502
3503// find
3504
3505template <class _Traits>
3506struct _LIBCPP_HIDDEN __traits_eq
3507{
3508 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003509 _LIBCPP_INLINE_VISIBILITY
3510 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3511 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512};
3513
3514template<class _CharT, class _Traits, class _Allocator>
3515typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003516basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003517 size_type __pos,
3518 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003520 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003521 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003522 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523}
3524
3525template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003526inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003528basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3529 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003531 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003532 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533}
3534
3535template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003536template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003537_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003538<
3539 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3540 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003541>
Marshall Clowe46031a2018-07-02 18:41:15 +00003542basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003543 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003544{
Marshall Clowe46031a2018-07-02 18:41:15 +00003545 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003546 return __str_find<value_type, size_type, traits_type, npos>
3547 (data(), size(), __sv.data(), __pos, __sv.size());
3548}
3549
3550template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003551inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003552typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003553basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003554 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003556 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003557 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003558 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559}
3560
3561template<class _CharT, class _Traits, class _Allocator>
3562typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003563basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3564 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003566 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003567 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568}
3569
3570// rfind
3571
3572template<class _CharT, class _Traits, class _Allocator>
3573typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003574basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003575 size_type __pos,
3576 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003578 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003579 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003580 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581}
3582
3583template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003584inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003586basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3587 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003589 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003590 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591}
3592
3593template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003594template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003595_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003596<
3597 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3598 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003599>
Marshall Clowe46031a2018-07-02 18:41:15 +00003600basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003601 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003602{
Marshall Clowe46031a2018-07-02 18:41:15 +00003603 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003604 return __str_rfind<value_type, size_type, traits_type, npos>
3605 (data(), size(), __sv.data(), __pos, __sv.size());
3606}
3607
3608template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003609inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003610typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003611basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003612 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003614 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003615 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003616 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617}
3618
3619template<class _CharT, class _Traits, class _Allocator>
3620typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003621basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3622 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003624 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003625 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626}
3627
3628// find_first_of
3629
3630template<class _CharT, class _Traits, class _Allocator>
3631typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003632basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003633 size_type __pos,
3634 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003636 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003637 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003638 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639}
3640
3641template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003642inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003644basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3645 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003647 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003648 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649}
3650
3651template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003652template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003653_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003654<
3655 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3656 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003657>
Marshall Clowe46031a2018-07-02 18:41:15 +00003658basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003659 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003660{
Marshall Clowe46031a2018-07-02 18:41:15 +00003661 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003662 return __str_find_first_of<value_type, size_type, traits_type, npos>
3663 (data(), size(), __sv.data(), __pos, __sv.size());
3664}
3665
3666template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003667inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003668typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003669basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003670 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003672 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003673 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003674 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675}
3676
3677template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003678inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003680basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3681 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682{
3683 return find(__c, __pos);
3684}
3685
3686// find_last_of
3687
3688template<class _CharT, class _Traits, class _Allocator>
3689typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003690basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003691 size_type __pos,
3692 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003694 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003695 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003696 (data(), size(), __s, __pos, __n);
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_last_of(const basic_string& __str,
3703 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003705 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003706 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707}
3708
3709template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003710template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003711_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003712<
3713 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3714 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003715>
Marshall Clowe46031a2018-07-02 18:41:15 +00003716basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003717 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003718{
Marshall Clowe46031a2018-07-02 18:41:15 +00003719 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003720 return __str_find_last_of<value_type, size_type, traits_type, npos>
3721 (data(), size(), __sv.data(), __pos, __sv.size());
3722}
3723
3724template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003725inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003726typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003727basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003728 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003730 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003731 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003732 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733}
3734
3735template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003736inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003738basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3739 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740{
3741 return rfind(__c, __pos);
3742}
3743
3744// find_first_not_of
3745
3746template<class _CharT, class _Traits, class _Allocator>
3747typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003748basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003749 size_type __pos,
3750 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003752 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003753 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003754 (data(), size(), __s, __pos, __n);
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_first_not_of(const basic_string& __str,
3761 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003763 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003764 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765}
3766
3767template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003768template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003769_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003770<
3771 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3772 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003773>
Marshall Clowe46031a2018-07-02 18:41:15 +00003774basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003775 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003776{
Marshall Clowe46031a2018-07-02 18:41:15 +00003777 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003778 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3779 (data(), size(), __sv.data(), __pos, __sv.size());
3780}
3781
3782template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003783inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003784typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003785basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003786 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003788 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003789 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003790 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791}
3792
3793template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003794inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003796basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3797 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003799 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003800 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801}
3802
3803// find_last_not_of
3804
3805template<class _CharT, class _Traits, class _Allocator>
3806typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003807basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003808 size_type __pos,
3809 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003811 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003812 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003813 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814}
3815
3816template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003817inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003819basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3820 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003822 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003823 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824}
3825
3826template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003827template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003828_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003829<
3830 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3831 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003832>
Marshall Clowe46031a2018-07-02 18:41:15 +00003833basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003834 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003835{
Marshall Clowe46031a2018-07-02 18:41:15 +00003836 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003837 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3838 (data(), size(), __sv.data(), __pos, __sv.size());
3839}
3840
3841template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003842inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003843typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003844basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003845 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003847 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003848 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003849 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003850}
3851
3852template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003853inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003855basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3856 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003858 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003859 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860}
3861
3862// compare
3863
3864template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003865template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003866_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003867<
3868 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3869 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003870>
zoecarver1997e0a2021-02-05 11:54:47 -08003871basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872{
Marshall Clowe46031a2018-07-02 18:41:15 +00003873 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003874 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003875 size_t __rhs_sz = __sv.size();
3876 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003877 _VSTD::min(__lhs_sz, __rhs_sz));
3878 if (__result != 0)
3879 return __result;
3880 if (__lhs_sz < __rhs_sz)
3881 return -1;
3882 if (__lhs_sz > __rhs_sz)
3883 return 1;
3884 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003885}
3886
3887template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003888inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003890basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003891{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003892 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893}
3894
3895template <class _CharT, class _Traits, class _Allocator>
3896int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003897basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3898 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003899 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003900 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003902 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003903 size_type __sz = size();
3904 if (__pos1 > __sz || __n2 == npos)
3905 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003906 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3907 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908 if (__r == 0)
3909 {
3910 if (__rlen < __n2)
3911 __r = -1;
3912 else if (__rlen > __n2)
3913 __r = 1;
3914 }
3915 return __r;
3916}
3917
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003918template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003919template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003920_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003921<
3922 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3923 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003924>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003925basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3926 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003927 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003928{
Marshall Clowe46031a2018-07-02 18:41:15 +00003929 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003930 return compare(__pos1, __n1, __sv.data(), __sv.size());
3931}
3932
3933template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003934inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003935int
3936basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3937 size_type __n1,
3938 const basic_string& __str) const
3939{
3940 return compare(__pos1, __n1, __str.data(), __str.size());
3941}
3942
3943template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003944template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003945_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003946<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003947 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3948 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003949 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003950>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003951basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3952 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003953 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003954 size_type __pos2,
3955 size_type __n2) const
3956{
Marshall Clow82513342016-09-24 22:45:42 +00003957 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003958 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3959}
3960
3961template <class _CharT, class _Traits, class _Allocator>
3962int
3963basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3964 size_type __n1,
3965 const basic_string& __str,
3966 size_type __pos2,
3967 size_type __n2) const
3968{
3969 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3970}
3971
3972template <class _CharT, class _Traits, class _Allocator>
3973int
3974basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3975{
3976 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3977 return compare(0, npos, __s, traits_type::length(__s));
3978}
3979
3980template <class _CharT, class _Traits, class _Allocator>
3981int
3982basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3983 size_type __n1,
3984 const value_type* __s) const
3985{
3986 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3987 return compare(__pos1, __n1, __s, traits_type::length(__s));
3988}
3989
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990// __invariants
3991
3992template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003993inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003994bool
3995basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3996{
3997 if (size() > capacity())
3998 return false;
3999 if (capacity() < __min_cap - 1)
4000 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004001 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004003 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004 return false;
4005 return true;
4006}
4007
Vedant Kumar55e007e2018-03-08 21:15:26 +00004008// __clear_and_shrink
4009
4010template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004011inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004012void
Marshall Clowe60a7182018-05-29 17:04:37 +00004013basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004014{
4015 clear();
4016 if(__is_long())
4017 {
4018 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4019 __set_long_cap(0);
4020 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004021 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004022 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004023}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004024
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025// operator==
4026
4027template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004028inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029bool
4030operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004031 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004032{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004033 size_t __lhs_sz = __lhs.size();
4034 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4035 __rhs.data(),
4036 __lhs_sz) == 0;
4037}
4038
4039template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004041bool
4042operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4043 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4044{
4045 size_t __lhs_sz = __lhs.size();
4046 if (__lhs_sz != __rhs.size())
4047 return false;
4048 const char* __lp = __lhs.data();
4049 const char* __rp = __rhs.data();
4050 if (__lhs.__is_long())
4051 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4052 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4053 if (*__lp != *__rp)
4054 return false;
4055 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056}
4057
4058template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004059inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004061operator==(const _CharT* __lhs,
4062 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004064 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4065 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4066 size_t __lhs_len = _Traits::length(__lhs);
4067 if (__lhs_len != __rhs.size()) return false;
4068 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069}
4070
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004074operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4075 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004077 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4078 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4079 size_t __rhs_len = _Traits::length(__rhs);
4080 if (__rhs_len != __lhs.size()) return false;
4081 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082}
4083
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004084template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086bool
4087operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004088 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089{
4090 return !(__lhs == __rhs);
4091}
4092
4093template<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 _CharT* __lhs,
4097 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098{
4099 return !(__lhs == __rhs);
4100}
4101
4102template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004103inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004105operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4106 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107{
4108 return !(__lhs == __rhs);
4109}
4110
4111// operator<
4112
4113template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004114inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115bool
4116operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004117 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004119 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120}
4121
4122template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004123inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004125operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4126 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004128 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129}
4130
4131template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004132inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004134operator< (const _CharT* __lhs,
4135 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136{
4137 return __rhs.compare(__lhs) > 0;
4138}
4139
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140// operator>
4141
4142template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004143inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144bool
4145operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004146 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147{
4148 return __rhs < __lhs;
4149}
4150
4151template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004152inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004154operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4155 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156{
4157 return __rhs < __lhs;
4158}
4159
4160template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004161inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004163operator> (const _CharT* __lhs,
4164 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165{
4166 return __rhs < __lhs;
4167}
4168
4169// operator<=
4170
4171template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004172inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173bool
4174operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004175 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176{
4177 return !(__rhs < __lhs);
4178}
4179
4180template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004181inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004183operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4184 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185{
4186 return !(__rhs < __lhs);
4187}
4188
4189template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004190inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004192operator<=(const _CharT* __lhs,
4193 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194{
4195 return !(__rhs < __lhs);
4196}
4197
4198// operator>=
4199
4200template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004201inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202bool
4203operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004204 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205{
4206 return !(__lhs < __rhs);
4207}
4208
4209template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004210inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004212operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4213 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214{
4215 return !(__lhs < __rhs);
4216}
4217
4218template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004219inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004221operator>=(const _CharT* __lhs,
4222 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223{
4224 return !(__lhs < __rhs);
4225}
4226
4227// operator +
4228
4229template<class _CharT, class _Traits, class _Allocator>
4230basic_string<_CharT, _Traits, _Allocator>
4231operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4232 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4233{
4234 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4235 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4236 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4237 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4238 __r.append(__rhs.data(), __rhs_sz);
4239 return __r;
4240}
4241
4242template<class _CharT, class _Traits, class _Allocator>
4243basic_string<_CharT, _Traits, _Allocator>
4244operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4245{
4246 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4247 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4248 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4249 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4250 __r.append(__rhs.data(), __rhs_sz);
4251 return __r;
4252}
4253
4254template<class _CharT, class _Traits, class _Allocator>
4255basic_string<_CharT, _Traits, _Allocator>
4256operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4257{
4258 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4259 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4260 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4261 __r.append(__rhs.data(), __rhs_sz);
4262 return __r;
4263}
4264
4265template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004266inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267basic_string<_CharT, _Traits, _Allocator>
4268operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4269{
4270 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4271 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4272 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4273 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4274 __r.append(__rhs, __rhs_sz);
4275 return __r;
4276}
4277
4278template<class _CharT, class _Traits, class _Allocator>
4279basic_string<_CharT, _Traits, _Allocator>
4280operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4281{
4282 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4283 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4284 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4285 __r.push_back(__rhs);
4286 return __r;
4287}
4288
Eric Fiselierfc92be82017-04-19 00:28:44 +00004289#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290
4291template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004292inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293basic_string<_CharT, _Traits, _Allocator>
4294operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4295{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004296 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297}
4298
4299template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004300inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004301basic_string<_CharT, _Traits, _Allocator>
4302operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4303{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004304 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305}
4306
4307template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004308inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004309basic_string<_CharT, _Traits, _Allocator>
4310operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4311{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004312 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313}
4314
4315template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004316inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004317basic_string<_CharT, _Traits, _Allocator>
4318operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4319{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004320 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321}
4322
4323template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004324inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325basic_string<_CharT, _Traits, _Allocator>
4326operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4327{
4328 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004329 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330}
4331
4332template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004333inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334basic_string<_CharT, _Traits, _Allocator>
4335operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4336{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004337 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338}
4339
4340template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342basic_string<_CharT, _Traits, _Allocator>
4343operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4344{
4345 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004346 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347}
4348
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004349#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350
4351// swap
4352
4353template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004354inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004356swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004357 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4358 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359{
4360 __lhs.swap(__rhs);
4361}
4362
Bruce Mitchener170d8972020-11-24 12:53:53 -05004363_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4364_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4365_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4366_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4367_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004368
Bruce Mitchener170d8972020-11-24 12:53:53 -05004369_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4370_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4371_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004372
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004373_LIBCPP_FUNC_VIS string to_string(int __val);
4374_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4375_LIBCPP_FUNC_VIS string to_string(long __val);
4376_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4377_LIBCPP_FUNC_VIS string to_string(long long __val);
4378_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4379_LIBCPP_FUNC_VIS string to_string(float __val);
4380_LIBCPP_FUNC_VIS string to_string(double __val);
4381_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004382
Bruce Mitchener170d8972020-11-24 12:53:53 -05004383_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4384_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4385_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4386_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4387_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004388
Bruce Mitchener170d8972020-11-24 12:53:53 -05004389_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4390_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4391_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004392
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004393_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4394_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4395_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4396_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4397_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4398_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4399_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4400_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4401_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004402
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004404_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004405const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4406 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004407
Marshall Clow851b9ec2019-05-20 21:56:51 +00004408template <class _CharT, class _Allocator>
4409struct _LIBCPP_TEMPLATE_VIS
4410 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4411 : public unary_function<
4412 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413{
4414 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004415 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4416 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417};
4418
Howard Hinnantc51e1022010-05-11 19:42:16 +00004419
Howard Hinnantdc095972011-07-18 15:51:59 +00004420template<class _CharT, class _Traits, class _Allocator>
4421basic_ostream<_CharT, _Traits>&
4422operator<<(basic_ostream<_CharT, _Traits>& __os,
4423 const basic_string<_CharT, _Traits, _Allocator>& __str);
4424
4425template<class _CharT, class _Traits, class _Allocator>
4426basic_istream<_CharT, _Traits>&
4427operator>>(basic_istream<_CharT, _Traits>& __is,
4428 basic_string<_CharT, _Traits, _Allocator>& __str);
4429
4430template<class _CharT, class _Traits, class _Allocator>
4431basic_istream<_CharT, _Traits>&
4432getline(basic_istream<_CharT, _Traits>& __is,
4433 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4434
4435template<class _CharT, class _Traits, class _Allocator>
4436inline _LIBCPP_INLINE_VISIBILITY
4437basic_istream<_CharT, _Traits>&
4438getline(basic_istream<_CharT, _Traits>& __is,
4439 basic_string<_CharT, _Traits, _Allocator>& __str);
4440
Eric Fiselierfc92be82017-04-19 00:28:44 +00004441#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004442
4443template<class _CharT, class _Traits, class _Allocator>
4444inline _LIBCPP_INLINE_VISIBILITY
4445basic_istream<_CharT, _Traits>&
4446getline(basic_istream<_CharT, _Traits>&& __is,
4447 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4448
4449template<class _CharT, class _Traits, class _Allocator>
4450inline _LIBCPP_INLINE_VISIBILITY
4451basic_istream<_CharT, _Traits>&
4452getline(basic_istream<_CharT, _Traits>&& __is,
4453 basic_string<_CharT, _Traits, _Allocator>& __str);
4454
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004455#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004456
Marshall Clow29b53f22018-12-14 18:49:35 +00004457#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004458template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004459inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004460 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4461 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4462 auto __old_size = __str.size();
4463 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4464 return __old_size - __str.size();
4465}
Marshall Clow29b53f22018-12-14 18:49:35 +00004466
Marek Kurdeja98b1412020-05-02 13:58:03 +02004467template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004468inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004469 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4470 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4471 _Predicate __pred) {
4472 auto __old_size = __str.size();
4473 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4474 __str.end());
4475 return __old_size - __str.size();
4476}
Marshall Clow29b53f22018-12-14 18:49:35 +00004477#endif
4478
Louis Dionneba400782020-10-02 15:02:52 -04004479#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004480
4481template<class _CharT, class _Traits, class _Allocator>
4482bool
4483basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4484{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004485 return this->data() <= _VSTD::__to_address(__i->base()) &&
4486 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004487}
4488
4489template<class _CharT, class _Traits, class _Allocator>
4490bool
4491basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4492{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004493 return this->data() < _VSTD::__to_address(__i->base()) &&
4494 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004495}
4496
4497template<class _CharT, class _Traits, class _Allocator>
4498bool
4499basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4500{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004501 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004502 return this->data() <= __p && __p <= this->data() + this->size();
4503}
4504
4505template<class _CharT, class _Traits, class _Allocator>
4506bool
4507basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4508{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004509 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004510 return this->data() <= __p && __p < this->data() + this->size();
4511}
4512
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004513#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004514
Louis Dionne173f29e2019-05-29 16:01:36 +00004515#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004516// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004517inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004518{
4519 inline namespace string_literals
4520 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004521 inline _LIBCPP_INLINE_VISIBILITY
4522 basic_string<char> operator "" s( const char *__str, size_t __len )
4523 {
4524 return basic_string<char> (__str, __len);
4525 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004526
Howard Hinnant5c167562013-08-07 19:39:48 +00004527 inline _LIBCPP_INLINE_VISIBILITY
4528 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4529 {
4530 return basic_string<wchar_t> (__str, __len);
4531 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004532
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004533#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004534 inline _LIBCPP_INLINE_VISIBILITY
4535 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4536 {
4537 return basic_string<char8_t> (__str, __len);
4538 }
4539#endif
4540
Howard Hinnant5c167562013-08-07 19:39:48 +00004541 inline _LIBCPP_INLINE_VISIBILITY
4542 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4543 {
4544 return basic_string<char16_t> (__str, __len);
4545 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004546
Howard Hinnant5c167562013-08-07 19:39:48 +00004547 inline _LIBCPP_INLINE_VISIBILITY
4548 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4549 {
4550 return basic_string<char32_t> (__str, __len);
4551 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004552 }
4553}
4554#endif
4555
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556_LIBCPP_END_NAMESPACE_STD
4557
Eric Fiselierf4433a32017-05-31 22:07:49 +00004558_LIBCPP_POP_MACROS
4559
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004560#endif // _LIBCPP_STRING