blob: c5e0745250ee40e826f8070a3d6f94ebf1d3c307 [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'Dwyeref181602021-05-19 11:57:04 -0400517#include <__debug>
518#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400519#include <__iterator/wrap_iter.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520#include <algorithm>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400521#include <compare>
522#include <cstdio> // EOF
523#include <cstring>
524#include <cwchar>
525#include <initializer_list>
526#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528#include <memory>
529#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400530#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400532#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000533#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000534
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400535#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
536# include <cstdint>
537#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000538
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000539#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000541#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542
Eric Fiselierf4433a32017-05-31 22:07:49 +0000543_LIBCPP_PUSH_MACROS
544#include <__undef_macros>
545
546
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547_LIBCPP_BEGIN_NAMESPACE_STD
548
549// fpos
550
551template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000552class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553{
554private:
555 _StateT __st_;
556 streamoff __off_;
557public:
558 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
559
560 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
561
562 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
563 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
564
565 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
566 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
567 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
568 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
569};
570
571template <class _StateT>
572inline _LIBCPP_INLINE_VISIBILITY
573streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
574 {return streamoff(__x) - streamoff(__y);}
575
576template <class _StateT>
577inline _LIBCPP_INLINE_VISIBILITY
578bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
579 {return streamoff(__x) == streamoff(__y);}
580
581template <class _StateT>
582inline _LIBCPP_INLINE_VISIBILITY
583bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
584 {return streamoff(__x) != streamoff(__y);}
585
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586// basic_string
587
588template<class _CharT, class _Traits, class _Allocator>
589basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000590operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
591 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592
593template<class _CharT, class _Traits, class _Allocator>
594basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000595operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596
597template<class _CharT, class _Traits, class _Allocator>
598basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000599operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600
601template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000602inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000604operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605
606template<class _CharT, class _Traits, class _Allocator>
607basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000608operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609
Shoaib Meenaiea363712017-07-29 02:54:41 +0000610_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
611
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000613class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614{
615protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000616 _LIBCPP_NORETURN void __throw_length_error() const;
617 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618};
619
620template <bool __b>
621void
622__basic_string_common<__b>::__throw_length_error() const
623{
Marshall Clow8fea1612016-08-25 15:09:01 +0000624 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625}
626
627template <bool __b>
628void
629__basic_string_common<__b>::__throw_out_of_range() const
630{
Marshall Clow8fea1612016-08-25 15:09:01 +0000631 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632}
633
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000634_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635
Marshall Clow039b2f02016-01-13 21:54:34 +0000636template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400637struct __string_is_trivial_iterator : public false_type {};
638
639template <class _Tp>
640struct __string_is_trivial_iterator<_Tp*>
641 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000642
Louis Dionne173f29e2019-05-29 16:01:36 +0000643template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400644struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
645 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000646
Marshall Clow82513342016-09-24 22:45:42 +0000647template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500648struct __can_be_converted_to_string_view : public _BoolConstant<
649 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
650 !is_convertible<const _Tp&, const _CharT*>::value
651 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000652
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000653#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000654
655template <class _CharT, size_t = sizeof(_CharT)>
656struct __padding
657{
658 unsigned char __xx[sizeof(_CharT)-1];
659};
660
661template <class _CharT>
662struct __padding<_CharT, 1>
663{
664};
665
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400666#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000667
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400668#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800669typedef basic_string<char8_t> u8string;
670#endif
671
672#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
673typedef basic_string<char16_t> u16string;
674typedef basic_string<char32_t> u32string;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400675#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Richard Smith256954d2020-11-11 17:12:18 -0800676
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000677template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800678class
679 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400680#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800681 _LIBCPP_PREFERRED_NAME(u8string)
682#endif
683#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
684 _LIBCPP_PREFERRED_NAME(u16string)
685 _LIBCPP_PREFERRED_NAME(u32string)
686#endif
687 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 : private __basic_string_common<true>
689{
690public:
691 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000692 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000694 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000696 typedef allocator_traits<allocator_type> __alloc_traits;
697 typedef typename __alloc_traits::size_type size_type;
698 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000699 typedef value_type& reference;
700 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000701 typedef typename __alloc_traits::pointer pointer;
702 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000703
Marshall Clow79f33542018-03-21 00:36:05 +0000704 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
705 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
706 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
707 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000708 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000709 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000710 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000711
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 typedef __wrap_iter<pointer> iterator;
713 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000714 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
715 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716
717private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000718
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000719#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000720
721 struct __long
722 {
723 pointer __data_;
724 size_type __size_;
725 size_type __cap_;
726 };
727
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000728#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000729 static const size_type __short_mask = 0x01;
730 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000731#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000732 static const size_type __short_mask = 0x80;
733 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400734#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000735
736 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
737 (sizeof(__long) - 1)/sizeof(value_type) : 2};
738
739 struct __short
740 {
741 value_type __data_[__min_cap];
742 struct
743 : __padding<value_type>
744 {
745 unsigned char __size_;
746 };
747 };
748
749#else
750
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751 struct __long
752 {
753 size_type __cap_;
754 size_type __size_;
755 pointer __data_;
756 };
757
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000758#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000759 static const size_type __short_mask = 0x80;
760 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000761#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000762 static const size_type __short_mask = 0x01;
763 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400764#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
767 (sizeof(__long) - 1)/sizeof(value_type) : 2};
768
769 struct __short
770 {
771 union
772 {
773 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000774 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 };
776 value_type __data_[__min_cap];
777 };
778
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400779#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000780
Howard Hinnant8ea98242013-08-23 17:37:05 +0000781 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782
Howard Hinnant8ea98242013-08-23 17:37:05 +0000783 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784
785 struct __raw
786 {
787 size_type __words[__n_words];
788 };
789
790 struct __rep
791 {
792 union
793 {
794 __long __l;
795 __short __s;
796 __raw __r;
797 };
798 };
799
800 __compressed_pair<__rep, allocator_type> __r_;
801
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200803 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804 static const size_type npos = -1;
805
Howard Hinnant3e276872011-06-03 18:40:47 +0000806 _LIBCPP_INLINE_VISIBILITY basic_string()
807 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000808
809 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
810#if _LIBCPP_STD_VER <= 14
811 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
812#else
813 _NOEXCEPT;
814#endif
815
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816 basic_string(const basic_string& __str);
817 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000818
Eric Fiselierfc92be82017-04-19 00:28:44 +0000819#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000821 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000822#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000823 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000824#else
825 _NOEXCEPT;
826#endif
827
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400830#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000831
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500832 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000833 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500834 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000835 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
836 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400837# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000838 __get_db()->__insert_c(this);
839# endif
840 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000841
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500842 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000843 _LIBCPP_INLINE_VISIBILITY
844 basic_string(const _CharT* __s, const _Allocator& __a);
845
Howard Hinnantcf823322010-12-17 14:46:43 +0000846 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000847 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000848 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000849 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000850 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000851 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000852
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500853 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000854 _LIBCPP_INLINE_VISIBILITY
855 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
856
Marshall Clow83445802016-04-07 18:13:41 +0000857 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000858 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000859 _LIBCPP_INLINE_VISIBILITY
860 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000861 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000862
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500863 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 +0000864 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000865 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500866 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000867
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500868 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
869 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000870 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
871 explicit basic_string(const _Tp& __t);
872
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500873 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 +0000874 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
875 explicit basic_string(const _Tp& __t, const allocator_type& __a);
876
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500877 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500880 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000883#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000884 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000885 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000886 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000887 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400888#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000890 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000892 _LIBCPP_INLINE_VISIBILITY
893 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
894
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000895 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000896
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500897 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 +0000898 basic_string& operator=(const _Tp& __t)
899 {__self_view __sv = __t; return assign(__sv);}
900
Eric Fiselierfc92be82017-04-19 00:28:44 +0000901#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000903 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000904 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000905 _LIBCPP_INLINE_VISIBILITY
906 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000908 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910
Louis Dionneba400782020-10-02 15:02:52 -0400911#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000912 _LIBCPP_INLINE_VISIBILITY
913 iterator begin() _NOEXCEPT
914 {return iterator(this, __get_pointer());}
915 _LIBCPP_INLINE_VISIBILITY
916 const_iterator begin() const _NOEXCEPT
917 {return const_iterator(this, __get_pointer());}
918 _LIBCPP_INLINE_VISIBILITY
919 iterator end() _NOEXCEPT
920 {return iterator(this, __get_pointer() + size());}
921 _LIBCPP_INLINE_VISIBILITY
922 const_iterator end() const _NOEXCEPT
923 {return const_iterator(this, __get_pointer() + size());}
924#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000925 _LIBCPP_INLINE_VISIBILITY
926 iterator begin() _NOEXCEPT
927 {return iterator(__get_pointer());}
928 _LIBCPP_INLINE_VISIBILITY
929 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000930 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000931 _LIBCPP_INLINE_VISIBILITY
932 iterator end() _NOEXCEPT
933 {return iterator(__get_pointer() + size());}
934 _LIBCPP_INLINE_VISIBILITY
935 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000936 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400937#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000938 _LIBCPP_INLINE_VISIBILITY
939 reverse_iterator rbegin() _NOEXCEPT
940 {return reverse_iterator(end());}
941 _LIBCPP_INLINE_VISIBILITY
942 const_reverse_iterator rbegin() const _NOEXCEPT
943 {return const_reverse_iterator(end());}
944 _LIBCPP_INLINE_VISIBILITY
945 reverse_iterator rend() _NOEXCEPT
946 {return reverse_iterator(begin());}
947 _LIBCPP_INLINE_VISIBILITY
948 const_reverse_iterator rend() const _NOEXCEPT
949 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000951 _LIBCPP_INLINE_VISIBILITY
952 const_iterator cbegin() const _NOEXCEPT
953 {return begin();}
954 _LIBCPP_INLINE_VISIBILITY
955 const_iterator cend() const _NOEXCEPT
956 {return end();}
957 _LIBCPP_INLINE_VISIBILITY
958 const_reverse_iterator crbegin() const _NOEXCEPT
959 {return rbegin();}
960 _LIBCPP_INLINE_VISIBILITY
961 const_reverse_iterator crend() const _NOEXCEPT
962 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000964 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000966 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
967 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
968 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000969 {return (__is_long() ? __get_long_cap()
970 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971
972 void resize(size_type __n, value_type __c);
973 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
974
Marek Kurdejc9848142020-11-26 10:07:16 +0100975 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000976 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
977
Marek Kurdejc9848142020-11-26 10:07:16 +0100978 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
979 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000980 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100981 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000983 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000984 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
985 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000987 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
988 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989
990 const_reference at(size_type __n) const;
991 reference at(size_type __n);
992
993 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000994
995 template <class _Tp>
996 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500997 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +0000998 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500999 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1000 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001001 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001002 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001003 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001004 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001006#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001008#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009
Howard Hinnantcf823322010-12-17 14:46:43 +00001010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001012
1013 template <class _Tp>
1014 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001015 _EnableIf<
1016 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1017 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001018 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001019 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001020 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001021 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001022
Marshall Clow62953962016-10-03 23:40:48 +00001023 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001024 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001025 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001026 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001027 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1028 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001029 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001030 >
Marshall Clow82513342016-09-24 22:45:42 +00001031 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001032 basic_string& append(const value_type* __s, size_type __n);
1033 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001035
1036 _LIBCPP_INLINE_VISIBILITY
1037 void __append_default_init(size_type __n);
1038
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001040 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001041 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001043 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001045 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001046 _LIBCPP_INLINE_VISIBILITY
1047 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001048 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001049 append(__temp.data(), __temp.size());
1050 return *this;
1051 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001053 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001054 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001056 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001058 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001059 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001060 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001061
Eric Fiselierfc92be82017-04-19 00:28:44 +00001062#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001065#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066
1067 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001070 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1071 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1072 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1073 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074
Marshall Clowe46031a2018-07-02 18:41:15 +00001075 template <class _Tp>
1076 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001077 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001078 <
1079 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1080 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001081 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001082 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001083 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001084 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001085#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001086 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001087 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001088 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001089 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001090#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001091 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001092 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001093 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001094 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001095 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001096 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1097 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001098 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001099 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001100 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001101 basic_string& assign(const value_type* __s, size_type __n);
1102 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 basic_string& assign(size_type __n, value_type __c);
1104 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001105 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001108 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001110 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 assign(_InputIterator __first, _InputIterator __last);
1112 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001113 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001116 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001118 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001120#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001123#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124
Howard Hinnantcf823322010-12-17 14:46:43 +00001125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001127
1128 template <class _Tp>
1129 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001130 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001131 <
1132 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1133 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001134 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001135 insert(size_type __pos1, const _Tp& __t)
1136 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1137
Marshall Clow82513342016-09-24 22:45:42 +00001138 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001139 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001140 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001141 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001142 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001143 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001144 >
Marshall Clow82513342016-09-24 22:45:42 +00001145 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001146 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001147 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1148 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1150 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1153 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001154 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001155 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001157 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001159 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1161 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001162 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001163 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001165 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001167 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001169#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1172 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001173#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174
1175 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 iterator erase(const_iterator __first, const_iterator __last);
1180
Howard Hinnantcf823322010-12-17 14:46:43 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001183
1184 template <class _Tp>
1185 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001186 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001187 <
1188 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1189 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001190 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001191 replace(size_type __pos1, size_type __n1, const _Tp& __t) { __self_view __sv = __t; return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001192 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
Marshall Clow82513342016-09-24 22:45:42 +00001193 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001194 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001195 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001196 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001197 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001198 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001199 >
Marshall Clow82513342016-09-24 22:45:42 +00001200 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001201 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1202 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001205 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001206
1207 template <class _Tp>
1208 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001209 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001210 <
1211 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1212 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001213 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001214 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1215
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001217 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001219 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001221 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001223 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001224 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001226 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001228 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001229 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001230#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001232 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001234#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
Howard Hinnantd17880b2013-06-28 16:59:19 +00001236 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1239
Howard Hinnantcf823322010-12-17 14:46:43 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001241 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001242#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001243 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001244#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001245 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001246 __is_nothrow_swappable<allocator_type>::value);
1247#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248
Howard Hinnantcf823322010-12-17 14:46:43 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001250 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001251 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001252 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001253#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001254 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001255 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001256#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257
Howard Hinnantcf823322010-12-17 14:46:43 +00001258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001259 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260
Howard Hinnantcf823322010-12-17 14:46:43 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001262 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001263
1264 template <class _Tp>
1265 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001266 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001267 <
1268 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1269 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001270 >
zoecarver1997e0a2021-02-05 11:54:47 -08001271 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001272 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001274 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001275 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276
Howard Hinnantcf823322010-12-17 14:46:43 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001278 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001279
1280 template <class _Tp>
1281 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001282 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001283 <
1284 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1285 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001286 >
zoecarver1997e0a2021-02-05 11:54:47 -08001287 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001288 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001290 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001291 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292
Howard Hinnantcf823322010-12-17 14:46:43 +00001293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001294 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001295
1296 template <class _Tp>
1297 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001298 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001299 <
1300 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1301 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001302 >
zoecarver1997e0a2021-02-05 11:54:47 -08001303 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001304 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001306 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001308 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309
Howard Hinnantcf823322010-12-17 14:46:43 +00001310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001311 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001312
1313 template <class _Tp>
1314 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001315 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001316 <
1317 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1318 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001319 >
zoecarver1997e0a2021-02-05 11:54:47 -08001320 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001321 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001323 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001325 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326
Howard Hinnantcf823322010-12-17 14:46:43 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001328 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001329
1330 template <class _Tp>
1331 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001332 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001333 <
1334 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1335 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001336 >
zoecarver1997e0a2021-02-05 11:54:47 -08001337 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001338 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001340 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001341 _LIBCPP_INLINE_VISIBILITY
1342 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1343
1344 _LIBCPP_INLINE_VISIBILITY
1345 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001346
1347 template <class _Tp>
1348 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001349 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001350 <
1351 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1352 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001353 >
zoecarver1997e0a2021-02-05 11:54:47 -08001354 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001355 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001357 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001358 _LIBCPP_INLINE_VISIBILITY
1359 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1360
1361 _LIBCPP_INLINE_VISIBILITY
1362 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001363
1364 template <class _Tp>
1365 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001366 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001367 <
1368 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1369 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001370 >
zoecarver1997e0a2021-02-05 11:54:47 -08001371 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001372
1373 template <class _Tp>
1374 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001375 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001376 <
1377 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1378 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001379 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001380 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1381
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001384 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001385
Marshall Clow82513342016-09-24 22:45:42 +00001386 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001387 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001388 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001389 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001390 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001391 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001392 >
Marshall Clow82513342016-09-24 22:45:42 +00001393 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001394 int compare(const value_type* __s) const _NOEXCEPT;
1395 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1396 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397
Marshall Clow18c293b2017-12-04 20:11:38 +00001398#if _LIBCPP_STD_VER > 17
1399 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1400 bool starts_with(__self_view __sv) const _NOEXCEPT
1401 { return __self_view(data(), size()).starts_with(__sv); }
1402
1403 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1404 bool starts_with(value_type __c) const _NOEXCEPT
1405 { return !empty() && _Traits::eq(front(), __c); }
1406
1407 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1408 bool starts_with(const value_type* __s) const _NOEXCEPT
1409 { return starts_with(__self_view(__s)); }
1410
1411 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1412 bool ends_with(__self_view __sv) const _NOEXCEPT
1413 { return __self_view(data(), size()).ends_with( __sv); }
1414
1415 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1416 bool ends_with(value_type __c) const _NOEXCEPT
1417 { return !empty() && _Traits::eq(back(), __c); }
1418
1419 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1420 bool ends_with(const value_type* __s) const _NOEXCEPT
1421 { return ends_with(__self_view(__s)); }
1422#endif
1423
Wim Leflere023c3542021-01-19 14:33:30 -05001424#if _LIBCPP_STD_VER > 20
1425 constexpr _LIBCPP_INLINE_VISIBILITY
1426 bool contains(__self_view __sv) const noexcept
1427 { return __self_view(data(), size()).contains(__sv); }
1428
1429 constexpr _LIBCPP_INLINE_VISIBILITY
1430 bool contains(value_type __c) const noexcept
1431 { return __self_view(data(), size()).contains(__c); }
1432
1433 constexpr _LIBCPP_INLINE_VISIBILITY
1434 bool contains(const value_type* __s) const
1435 { return __self_view(data(), size()).contains(__s); }
1436#endif
1437
Howard Hinnantcf823322010-12-17 14:46:43 +00001438 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001439
Marshall Clowe60a7182018-05-29 17:04:37 +00001440 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001441
Marek Kurdejc9848142020-11-26 10:07:16 +01001442 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1443
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001444 _LIBCPP_INLINE_VISIBILITY
1445 bool __is_long() const _NOEXCEPT
1446 {return bool(__r_.first().__s.__size_ & __short_mask);}
1447
Louis Dionneba400782020-10-02 15:02:52 -04001448#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001449
1450 bool __dereferenceable(const const_iterator* __i) const;
1451 bool __decrementable(const const_iterator* __i) const;
1452 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1453 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1454
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001455#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001456
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001458 _LIBCPP_INLINE_VISIBILITY
1459 allocator_type& __alloc() _NOEXCEPT
1460 {return __r_.second();}
1461 _LIBCPP_INLINE_VISIBILITY
1462 const allocator_type& __alloc() const _NOEXCEPT
1463 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001465#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001466
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001468 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001469# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001471# else
1472 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1473# endif
1474
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001475 _LIBCPP_INLINE_VISIBILITY
1476 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001477# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001479# else
1480 {return __r_.first().__s.__size_;}
1481# endif
1482
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001483#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001484
1485 _LIBCPP_INLINE_VISIBILITY
1486 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001487# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001488 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1489# else
1490 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1491# endif
1492
1493 _LIBCPP_INLINE_VISIBILITY
1494 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001495# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001496 {return __r_.first().__s.__size_;}
1497# else
1498 {return __r_.first().__s.__size_ >> 1;}
1499# endif
1500
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001501#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001502
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001503 _LIBCPP_INLINE_VISIBILITY
1504 void __set_long_size(size_type __s) _NOEXCEPT
1505 {__r_.first().__l.__size_ = __s;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 size_type __get_long_size() const _NOEXCEPT
1508 {return __r_.first().__l.__size_;}
1509 _LIBCPP_INLINE_VISIBILITY
1510 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1512
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001513 _LIBCPP_INLINE_VISIBILITY
1514 void __set_long_cap(size_type __s) _NOEXCEPT
1515 {__r_.first().__l.__cap_ = __long_mask | __s;}
1516 _LIBCPP_INLINE_VISIBILITY
1517 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001518 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001520 _LIBCPP_INLINE_VISIBILITY
1521 void __set_long_pointer(pointer __p) _NOEXCEPT
1522 {__r_.first().__l.__data_ = __p;}
1523 _LIBCPP_INLINE_VISIBILITY
1524 pointer __get_long_pointer() _NOEXCEPT
1525 {return __r_.first().__l.__data_;}
1526 _LIBCPP_INLINE_VISIBILITY
1527 const_pointer __get_long_pointer() const _NOEXCEPT
1528 {return __r_.first().__l.__data_;}
1529 _LIBCPP_INLINE_VISIBILITY
1530 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001531 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 _LIBCPP_INLINE_VISIBILITY
1533 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001534 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001535 _LIBCPP_INLINE_VISIBILITY
1536 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001538 _LIBCPP_INLINE_VISIBILITY
1539 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1541
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001542 _LIBCPP_INLINE_VISIBILITY
1543 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544 {
1545 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1546 for (unsigned __i = 0; __i < __n_words; ++__i)
1547 __a[__i] = 0;
1548 }
1549
1550 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001552 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001553 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001555 static _LIBCPP_INLINE_VISIBILITY
1556 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001557 {
1558 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1559 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1560 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1561 if (__guess == __min_cap) ++__guess;
1562 return __guess;
1563 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001565 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001566 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001567 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001568 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001569 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001571
Martijn Vels5e7c9752020-03-04 17:52:46 -05001572 // Slow path for the (inlined) copy constructor for 'long' strings.
1573 // Always externally instantiated and not inlined.
1574 // Requires that __s is zero terminated.
1575 // The main reason for this function to exist is because for unstable, we
1576 // want to allow inlining of the copy constructor. However, we don't want
1577 // to call the __init() functions as those are marked as inline which may
1578 // result in over-aggressive inlining by the compiler, where our aim is
1579 // to only inline the fast path code directly in the ctor.
1580 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1581
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001583 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001584 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001586 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1587 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 __init(_InputIterator __first, _InputIterator __last);
1589
1590 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001591 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001592 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001594 __is_cpp17_forward_iterator<_ForwardIterator>::value
1595 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 __init(_ForwardIterator __first, _ForwardIterator __last);
1597
1598 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001599 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1601 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001602 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603
Martijn Vels596e3de2020-02-26 15:55:49 -05001604 // __assign_no_alias is invoked for assignment operations where we
1605 // have proof that the input does not alias the current instance.
1606 // For example, operator=(basic_string) performs a 'self' check.
1607 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001608 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001609
Howard Hinnantcf823322010-12-17 14:46:43 +00001610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 void __erase_to_end(size_type __pos);
1612
Martijn Velsa81fc792020-02-26 13:25:43 -05001613 // __erase_external_with_move is invoked for erase() invocations where
1614 // `n ~= npos`, likely requiring memory moves on the string data.
1615 void __erase_external_with_move(size_type __pos, size_type __n);
1616
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001617 _LIBCPP_INLINE_VISIBILITY
1618 void __copy_assign_alloc(const basic_string& __str)
1619 {__copy_assign_alloc(__str, integral_constant<bool,
1620 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1621
1622 _LIBCPP_INLINE_VISIBILITY
1623 void __copy_assign_alloc(const basic_string& __str, true_type)
1624 {
Marshall Clowf258c202017-01-31 03:40:52 +00001625 if (__alloc() == __str.__alloc())
1626 __alloc() = __str.__alloc();
1627 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001628 {
Marshall Clowf258c202017-01-31 03:40:52 +00001629 if (!__str.__is_long())
1630 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001631 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001632 __alloc() = __str.__alloc();
1633 }
1634 else
1635 {
1636 allocator_type __a = __str.__alloc();
1637 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001638 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001639 __alloc() = _VSTD::move(__a);
1640 __set_long_pointer(__p);
1641 __set_long_cap(__str.__get_long_cap());
1642 __set_long_size(__str.size());
1643 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001644 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001645 }
1646
1647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001648 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001649 {}
1650
Eric Fiselierfc92be82017-04-19 00:28:44 +00001651#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001652 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001653 void __move_assign(basic_string& __str, false_type)
1654 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001656 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001657#if _LIBCPP_STD_VER > 14
1658 _NOEXCEPT;
1659#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001660 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001661#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001662#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001663
1664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001665 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001666 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001667 _NOEXCEPT_(
1668 !__alloc_traits::propagate_on_container_move_assignment::value ||
1669 is_nothrow_move_assignable<allocator_type>::value)
1670 {__move_assign_alloc(__str, integral_constant<bool,
1671 __alloc_traits::propagate_on_container_move_assignment::value>());}
1672
1673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001674 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001675 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1676 {
1677 __alloc() = _VSTD::move(__c.__alloc());
1678 }
1679
1680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001681 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001682 _NOEXCEPT
1683 {}
1684
Martijn Velsda7d94f2020-06-19 14:24:03 -04001685 basic_string& __assign_external(const value_type* __s);
1686 basic_string& __assign_external(const value_type* __s, size_type __n);
1687
1688 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1689 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1690 pointer __p = __is_long()
1691 ? (__set_long_size(__n), __get_long_pointer())
1692 : (__set_short_size(__n), __get_short_pointer());
1693 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1694 traits_type::assign(__p[__n], value_type());
1695 return *this;
1696 }
1697
Howard Hinnantcf823322010-12-17 14:46:43 +00001698 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1699 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001701 template<class _Tp>
1702 _LIBCPP_INLINE_VISIBILITY
1703 bool __addr_in_range(_Tp&& __t) const {
1704 const volatile void *__p = _VSTD::addressof(__t);
1705 return data() <= __p && __p <= data() + size();
1706 }
1707
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 friend basic_string operator+<>(const basic_string&, const basic_string&);
1709 friend basic_string operator+<>(const value_type*, const basic_string&);
1710 friend basic_string operator+<>(value_type, const basic_string&);
1711 friend basic_string operator+<>(const basic_string&, const value_type*);
1712 friend basic_string operator+<>(const basic_string&, value_type);
1713};
1714
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001715// These declarations must appear before any functions are implicitly used
1716// so that they have the correct visibility specifier.
1717#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1718_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1719_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1720#else
1721_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1722_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1723#endif
1724
1725
Marshall Clowa0563332018-02-08 06:34:03 +00001726#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1727template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001728 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001729 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001730 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1731 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001732 >
1733basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1734 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001735
1736template<class _CharT,
1737 class _Traits,
1738 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001739 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001740 >
1741explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1742 -> basic_string<_CharT, _Traits, _Allocator>;
1743
1744template<class _CharT,
1745 class _Traits,
1746 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001747 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001748 class _Sz = typename allocator_traits<_Allocator>::size_type
1749 >
1750basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1751 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001752#endif
1753
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001755inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756void
1757basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1758{
Louis Dionneba400782020-10-02 15:02:52 -04001759#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001760 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001761#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762}
1763
1764template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001765inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001767basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001768{
Louis Dionneba400782020-10-02 15:02:52 -04001769#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001770 __c_node* __c = __get_db()->__find_c_and_lock(this);
1771 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001773 const_pointer __new_last = __get_pointer() + __pos;
1774 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001776 --__p;
1777 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1778 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001780 (*__p)->__c_ = nullptr;
1781 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001782 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001785 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001787#else
1788 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001789#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790}
1791
1792template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001793inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001794basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001795 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001796 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797{
Louis Dionneba400782020-10-02 15:02:52 -04001798#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001799 __get_db()->__insert_c(this);
1800#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 __zero();
1802}
1803
1804template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001805inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001807#if _LIBCPP_STD_VER <= 14
1808 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1809#else
1810 _NOEXCEPT
1811#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001812: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813{
Louis Dionneba400782020-10-02 15:02:52 -04001814#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001815 __get_db()->__insert_c(this);
1816#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 __zero();
1818}
1819
1820template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001821void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1822 size_type __sz,
1823 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824{
1825 if (__reserve > max_size())
1826 this->__throw_length_error();
1827 pointer __p;
1828 if (__reserve < __min_cap)
1829 {
1830 __set_short_size(__sz);
1831 __p = __get_short_pointer();
1832 }
1833 else
1834 {
1835 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001836 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 __set_long_pointer(__p);
1838 __set_long_cap(__cap+1);
1839 __set_long_size(__sz);
1840 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001841 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842 traits_type::assign(__p[__sz], value_type());
1843}
1844
1845template <class _CharT, class _Traits, class _Allocator>
1846void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001847basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848{
1849 if (__sz > max_size())
1850 this->__throw_length_error();
1851 pointer __p;
1852 if (__sz < __min_cap)
1853 {
1854 __set_short_size(__sz);
1855 __p = __get_short_pointer();
1856 }
1857 else
1858 {
1859 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001860 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 __set_long_pointer(__p);
1862 __set_long_cap(__cap+1);
1863 __set_long_size(__sz);
1864 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001865 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 traits_type::assign(__p[__sz], value_type());
1867}
1868
1869template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001870template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001871basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001872 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001874 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001876#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001877 __get_db()->__insert_c(this);
1878#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879}
1880
1881template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001882inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001883basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001884 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001886 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001888#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001889 __get_db()->__insert_c(this);
1890#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891}
1892
1893template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001894inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001895basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001896 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001898 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001900#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001901 __get_db()->__insert_c(this);
1902#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903}
1904
1905template <class _CharT, class _Traits, class _Allocator>
1906basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001907 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908{
1909 if (!__str.__is_long())
1910 __r_.first().__r = __str.__r_.first().__r;
1911 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001912 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1913 __str.__get_long_size());
1914
Louis Dionneba400782020-10-02 15:02:52 -04001915#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001916 __get_db()->__insert_c(this);
1917#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918}
1919
1920template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001921basic_string<_CharT, _Traits, _Allocator>::basic_string(
1922 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001923 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924{
1925 if (!__str.__is_long())
1926 __r_.first().__r = __str.__r_.first().__r;
1927 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001928 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1929 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001930#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001931 __get_db()->__insert_c(this);
1932#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933}
1934
Martijn Vels5e7c9752020-03-04 17:52:46 -05001935template <class _CharT, class _Traits, class _Allocator>
1936void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1937 const value_type* __s, size_type __sz) {
1938 pointer __p;
1939 if (__sz < __min_cap) {
1940 __p = __get_short_pointer();
1941 __set_short_size(__sz);
1942 } else {
1943 if (__sz > max_size())
1944 this->__throw_length_error();
1945 size_t __cap = __recommend(__sz);
1946 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1947 __set_long_pointer(__p);
1948 __set_long_cap(__cap + 1);
1949 __set_long_size(__sz);
1950 }
1951 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1952}
1953
Eric Fiselierfc92be82017-04-19 00:28:44 +00001954#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955
1956template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001957inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001958basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001959#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001960 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001961#else
1962 _NOEXCEPT
1963#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001964 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965{
1966 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001967#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001968 __get_db()->__insert_c(this);
1969 if (__is_long())
1970 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971#endif
1972}
1973
1974template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001975inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001977 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978{
Marshall Clowd2d24692014-07-17 15:32:20 +00001979 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001980 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001981 else
1982 {
1983 __r_.first().__r = __str.__r_.first().__r;
1984 __str.__zero();
1985 }
Louis Dionneba400782020-10-02 15:02:52 -04001986#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001987 __get_db()->__insert_c(this);
1988 if (__is_long())
1989 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990#endif
1991}
1992
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001993#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994
1995template <class _CharT, class _Traits, class _Allocator>
1996void
1997basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1998{
1999 if (__n > max_size())
2000 this->__throw_length_error();
2001 pointer __p;
2002 if (__n < __min_cap)
2003 {
2004 __set_short_size(__n);
2005 __p = __get_short_pointer();
2006 }
2007 else
2008 {
2009 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002010 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011 __set_long_pointer(__p);
2012 __set_long_cap(__cap+1);
2013 __set_long_size(__n);
2014 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002015 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016 traits_type::assign(__p[__n], value_type());
2017}
2018
2019template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002020inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002021basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002022 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002025#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002026 __get_db()->__insert_c(this);
2027#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028}
2029
2030template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002031template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002032basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002033 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034{
2035 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002036#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002037 __get_db()->__insert_c(this);
2038#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039}
2040
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002042basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2043 size_type __pos, size_type __n,
2044 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002045 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046{
2047 size_type __str_sz = __str.size();
2048 if (__pos > __str_sz)
2049 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002050 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002051#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002052 __get_db()->__insert_c(this);
2053#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054}
2055
2056template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002057inline
Marshall Clow83445802016-04-07 18:13:41 +00002058basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002059 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002060 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002061{
2062 size_type __str_sz = __str.size();
2063 if (__pos > __str_sz)
2064 this->__throw_out_of_range();
2065 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002066#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002067 __get_db()->__insert_c(this);
2068#endif
2069}
2070
2071template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002072template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002073basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002074 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002075 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002076{
Marshall Clowe46031a2018-07-02 18:41:15 +00002077 __self_view __sv0 = __t;
2078 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002079 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002080#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002081 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002082#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002083}
2084
2085template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002086template <class _Tp, class>
2087basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002088 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002089{
Marshall Clowe46031a2018-07-02 18:41:15 +00002090 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002091 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002092#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002093 __get_db()->__insert_c(this);
2094#endif
2095}
2096
2097template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002098template <class _Tp, class>
2099basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002100 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002101{
Marshall Clowe46031a2018-07-02 18:41:15 +00002102 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002103 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002104#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002105 __get_db()->__insert_c(this);
2106#endif
2107}
2108
2109template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002111_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002113 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2114>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2116{
2117 __zero();
2118#ifndef _LIBCPP_NO_EXCEPTIONS
2119 try
2120 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002121#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122 for (; __first != __last; ++__first)
2123 push_back(*__first);
2124#ifndef _LIBCPP_NO_EXCEPTIONS
2125 }
2126 catch (...)
2127 {
2128 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002129 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130 throw;
2131 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002132#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133}
2134
2135template <class _CharT, class _Traits, class _Allocator>
2136template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002137_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002139 __is_cpp17_forward_iterator<_ForwardIterator>::value
2140>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2142{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002143 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144 if (__sz > max_size())
2145 this->__throw_length_error();
2146 pointer __p;
2147 if (__sz < __min_cap)
2148 {
2149 __set_short_size(__sz);
2150 __p = __get_short_pointer();
2151 }
2152 else
2153 {
2154 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002155 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156 __set_long_pointer(__p);
2157 __set_long_cap(__cap+1);
2158 __set_long_size(__sz);
2159 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002160
2161#ifndef _LIBCPP_NO_EXCEPTIONS
2162 try
2163 {
2164#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002165 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166 traits_type::assign(*__p, *__first);
2167 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002168#ifndef _LIBCPP_NO_EXCEPTIONS
2169 }
2170 catch (...)
2171 {
2172 if (__is_long())
2173 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2174 throw;
2175 }
2176#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177}
2178
2179template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002180template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002181inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002183 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184{
2185 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002186#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002187 __get_db()->__insert_c(this);
2188#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189}
2190
2191template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002192template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002193inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2195 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002196 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197{
2198 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002199#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002200 __get_db()->__insert_c(this);
2201#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202}
2203
Eric Fiselierfc92be82017-04-19 00:28:44 +00002204#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002205
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002207inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002208basic_string<_CharT, _Traits, _Allocator>::basic_string(
2209 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002210 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211{
2212 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002213#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002214 __get_db()->__insert_c(this);
2215#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216}
2217
2218template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002219inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002220
Eric Fiselier812882b2017-02-17 01:17:10 +00002221basic_string<_CharT, _Traits, _Allocator>::basic_string(
2222 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002223 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224{
2225 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002226#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002227 __get_db()->__insert_c(this);
2228#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229}
2230
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002231#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002232
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2235{
Louis Dionneba400782020-10-02 15:02:52 -04002236#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002237 __get_db()->__erase_c(this);
2238#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002240 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241}
2242
2243template <class _CharT, class _Traits, class _Allocator>
2244void
2245basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2246 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002247 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 +00002248{
2249 size_type __ms = max_size();
2250 if (__delta_cap > __ms - __old_cap - 1)
2251 this->__throw_length_error();
2252 pointer __old_p = __get_pointer();
2253 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002254 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002256 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257 __invalidate_all_iterators();
2258 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002259 traits_type::copy(_VSTD::__to_address(__p),
2260 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002262 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2264 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002265 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2266 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002268 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 __set_long_pointer(__p);
2270 __set_long_cap(__cap+1);
2271 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2272 __set_long_size(__old_sz);
2273 traits_type::assign(__p[__old_sz], value_type());
2274}
2275
2276template <class _CharT, class _Traits, class _Allocator>
2277void
2278basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2279 size_type __n_copy, size_type __n_del, size_type __n_add)
2280{
2281 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002282 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 this->__throw_length_error();
2284 pointer __old_p = __get_pointer();
2285 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002286 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002288 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289 __invalidate_all_iterators();
2290 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002291 traits_type::copy(_VSTD::__to_address(__p),
2292 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002293 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2294 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002295 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2296 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002297 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002299 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300 __set_long_pointer(__p);
2301 __set_long_cap(__cap+1);
2302}
2303
2304// assign
2305
2306template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002307template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002308basic_string<_CharT, _Traits, _Allocator>&
2309basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002310 const value_type* __s, size_type __n) {
2311 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2312 if (__n < __cap) {
2313 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2314 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2315 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2316 traits_type::assign(__p[__n], value_type());
2317 __invalidate_iterators_past(__n);
2318 } else {
2319 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2320 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2321 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002322 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002323}
2324
2325template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002327basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2328 const value_type* __s, size_type __n) {
2329 size_type __cap = capacity();
2330 if (__cap >= __n) {
2331 value_type* __p = _VSTD::__to_address(__get_pointer());
2332 traits_type::move(__p, __s, __n);
2333 traits_type::assign(__p[__n], value_type());
2334 __set_size(__n);
2335 __invalidate_iterators_past(__n);
2336 } else {
2337 size_type __sz = size();
2338 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2339 }
2340 return *this;
2341}
2342
2343template <class _CharT, class _Traits, class _Allocator>
2344basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002345basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002347 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002348 return (_LIBCPP_BUILTIN_CONSTANT_P(__n) && __n < __min_cap)
2349 ? __assign_short(__s, __n)
2350 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351}
2352
2353template <class _CharT, class _Traits, class _Allocator>
2354basic_string<_CharT, _Traits, _Allocator>&
2355basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2356{
2357 size_type __cap = capacity();
2358 if (__cap < __n)
2359 {
2360 size_type __sz = size();
2361 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2362 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002363 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364 traits_type::assign(__p, __n, __c);
2365 traits_type::assign(__p[__n], value_type());
2366 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002367 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368 return *this;
2369}
2370
2371template <class _CharT, class _Traits, class _Allocator>
2372basic_string<_CharT, _Traits, _Allocator>&
2373basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2374{
2375 pointer __p;
2376 if (__is_long())
2377 {
2378 __p = __get_long_pointer();
2379 __set_long_size(1);
2380 }
2381 else
2382 {
2383 __p = __get_short_pointer();
2384 __set_short_size(1);
2385 }
2386 traits_type::assign(*__p, __c);
2387 traits_type::assign(*++__p, value_type());
2388 __invalidate_iterators_past(1);
2389 return *this;
2390}
2391
2392template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002393basic_string<_CharT, _Traits, _Allocator>&
2394basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2395{
Martijn Vels596e3de2020-02-26 15:55:49 -05002396 if (this != &__str) {
2397 __copy_assign_alloc(__str);
2398 if (!__is_long()) {
2399 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002400 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002401 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002402 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002403 }
2404 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002405 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002406 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002407 }
2408 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002409}
2410
Eric Fiselierfc92be82017-04-19 00:28:44 +00002411#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002412
2413template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002414inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002415void
2416basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002417 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002418{
2419 if (__alloc() != __str.__alloc())
2420 assign(__str);
2421 else
2422 __move_assign(__str, true_type());
2423}
2424
2425template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002426inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002427void
2428basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002429#if _LIBCPP_STD_VER > 14
2430 _NOEXCEPT
2431#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002432 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002433#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002434{
marshall2ed41622019-11-27 07:13:00 -08002435 if (__is_long()) {
2436 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2437 __get_long_cap());
2438#if _LIBCPP_STD_VER <= 14
2439 if (!is_nothrow_move_assignable<allocator_type>::value) {
2440 __set_short_size(0);
2441 traits_type::assign(__get_short_pointer()[0], value_type());
2442 }
2443#endif
2444 }
2445 __move_assign_alloc(__str);
2446 __r_.first() = __str.__r_.first();
2447 __str.__set_short_size(0);
2448 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002449}
2450
2451template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002452inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002453basic_string<_CharT, _Traits, _Allocator>&
2454basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002455 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002456{
2457 __move_assign(__str, integral_constant<bool,
2458 __alloc_traits::propagate_on_container_move_assignment::value>());
2459 return *this;
2460}
2461
2462#endif
2463
2464template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002466_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002468 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002470>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2472{
Marshall Clow958362f2016-09-05 01:54:30 +00002473 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002474 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002475 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476}
2477
2478template <class _CharT, class _Traits, class _Allocator>
2479template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002480_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002482 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002484>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2486{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002488 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2489 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2490
2491 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2492 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002494 if (__cap < __n)
2495 {
2496 size_type __sz = size();
2497 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2498 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002499 pointer __p = __get_pointer();
2500 for (; __first != __last; ++__first, ++__p)
2501 traits_type::assign(*__p, *__first);
2502 traits_type::assign(*__p, value_type());
2503 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002504 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505 }
2506 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002507 {
2508 const basic_string __temp(__first, __last, __alloc());
2509 assign(__temp.data(), __temp.size());
2510 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511 return *this;
2512}
2513
2514template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515basic_string<_CharT, _Traits, _Allocator>&
2516basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2517{
2518 size_type __sz = __str.size();
2519 if (__pos > __sz)
2520 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002521 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522}
2523
2524template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002525template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002526_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002527<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002528 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2529 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002530 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002531>
Marshall Clow82513342016-09-24 22:45:42 +00002532basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002533{
Marshall Clow82513342016-09-24 22:45:42 +00002534 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002535 size_type __sz = __sv.size();
2536 if (__pos > __sz)
2537 this->__throw_out_of_range();
2538 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2539}
2540
2541
2542template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002544basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2545 return __assign_external(__s, traits_type::length(__s));
2546}
2547
2548template <class _CharT, class _Traits, class _Allocator>
2549basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002550basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002552 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002553 return _LIBCPP_BUILTIN_CONSTANT_P(*__s)
2554 ? (traits_type::length(__s) < __min_cap
2555 ? __assign_short(__s, traits_type::length(__s))
2556 : __assign_external(__s, traits_type::length(__s)))
2557 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559// append
2560
2561template <class _CharT, class _Traits, class _Allocator>
2562basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002563basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002565 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566 size_type __cap = capacity();
2567 size_type __sz = size();
2568 if (__cap - __sz >= __n)
2569 {
2570 if (__n)
2571 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002572 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573 traits_type::copy(__p + __sz, __s, __n);
2574 __sz += __n;
2575 __set_size(__sz);
2576 traits_type::assign(__p[__sz], value_type());
2577 }
2578 }
2579 else
2580 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2581 return *this;
2582}
2583
2584template <class _CharT, class _Traits, class _Allocator>
2585basic_string<_CharT, _Traits, _Allocator>&
2586basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2587{
2588 if (__n)
2589 {
2590 size_type __cap = capacity();
2591 size_type __sz = size();
2592 if (__cap - __sz < __n)
2593 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2594 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002595 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596 __sz += __n;
2597 __set_size(__sz);
2598 traits_type::assign(__p[__sz], value_type());
2599 }
2600 return *this;
2601}
2602
2603template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002604inline void
2605basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2606{
2607 if (__n)
2608 {
2609 size_type __cap = capacity();
2610 size_type __sz = size();
2611 if (__cap - __sz < __n)
2612 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2613 pointer __p = __get_pointer();
2614 __sz += __n;
2615 __set_size(__sz);
2616 traits_type::assign(__p[__sz], value_type());
2617 }
2618}
2619
2620template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621void
2622basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2623{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002624 bool __is_short = !__is_long();
2625 size_type __cap;
2626 size_type __sz;
2627 if (__is_short)
2628 {
2629 __cap = __min_cap - 1;
2630 __sz = __get_short_size();
2631 }
2632 else
2633 {
2634 __cap = __get_long_cap() - 1;
2635 __sz = __get_long_size();
2636 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002638 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002640 __is_short = !__is_long();
2641 }
2642 pointer __p;
2643 if (__is_short)
2644 {
2645 __p = __get_short_pointer() + __sz;
2646 __set_short_size(__sz+1);
2647 }
2648 else
2649 {
2650 __p = __get_long_pointer() + __sz;
2651 __set_long_size(__sz+1);
2652 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653 traits_type::assign(*__p, __c);
2654 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655}
2656
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657template <class _CharT, class _Traits, class _Allocator>
2658template<class _ForwardIterator>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002659_EnableIf
2660<
2661 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2662 basic_string<_CharT, _Traits, _Allocator>&
2663>
2664basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002665 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666{
2667 size_type __sz = size();
2668 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002669 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670 if (__n)
2671 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002672 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2673 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002674 {
2675 if (__cap - __sz < __n)
2676 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2677 pointer __p = __get_pointer() + __sz;
2678 for (; __first != __last; ++__p, ++__first)
2679 traits_type::assign(*__p, *__first);
2680 traits_type::assign(*__p, value_type());
2681 __set_size(__sz + __n);
2682 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002683 else
2684 {
2685 const basic_string __temp(__first, __last, __alloc());
2686 append(__temp.data(), __temp.size());
2687 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688 }
2689 return *this;
2690}
2691
2692template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002693inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694basic_string<_CharT, _Traits, _Allocator>&
2695basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2696{
2697 return append(__str.data(), __str.size());
2698}
2699
2700template <class _CharT, class _Traits, class _Allocator>
2701basic_string<_CharT, _Traits, _Allocator>&
2702basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2703{
2704 size_type __sz = __str.size();
2705 if (__pos > __sz)
2706 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002707 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708}
2709
2710template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002711template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002712 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002713 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002714 __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 +00002715 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002716 >
Marshall Clow82513342016-09-24 22:45:42 +00002717basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002718{
Marshall Clow82513342016-09-24 22:45:42 +00002719 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002720 size_type __sz = __sv.size();
2721 if (__pos > __sz)
2722 this->__throw_out_of_range();
2723 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2724}
2725
2726template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002728basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002730 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731 return append(__s, traits_type::length(__s));
2732}
2733
2734// insert
2735
2736template <class _CharT, class _Traits, class _Allocator>
2737basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002738basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002740 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 size_type __sz = size();
2742 if (__pos > __sz)
2743 this->__throw_out_of_range();
2744 size_type __cap = capacity();
2745 if (__cap - __sz >= __n)
2746 {
2747 if (__n)
2748 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002749 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 size_type __n_move = __sz - __pos;
2751 if (__n_move != 0)
2752 {
2753 if (__p + __pos <= __s && __s < __p + __sz)
2754 __s += __n;
2755 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2756 }
2757 traits_type::move(__p + __pos, __s, __n);
2758 __sz += __n;
2759 __set_size(__sz);
2760 traits_type::assign(__p[__sz], value_type());
2761 }
2762 }
2763 else
2764 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2765 return *this;
2766}
2767
2768template <class _CharT, class _Traits, class _Allocator>
2769basic_string<_CharT, _Traits, _Allocator>&
2770basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2771{
2772 size_type __sz = size();
2773 if (__pos > __sz)
2774 this->__throw_out_of_range();
2775 if (__n)
2776 {
2777 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002778 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 if (__cap - __sz >= __n)
2780 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002781 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 size_type __n_move = __sz - __pos;
2783 if (__n_move != 0)
2784 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2785 }
2786 else
2787 {
2788 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002789 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 }
2791 traits_type::assign(__p + __pos, __n, __c);
2792 __sz += __n;
2793 __set_size(__sz);
2794 traits_type::assign(__p[__sz], value_type());
2795 }
2796 return *this;
2797}
2798
2799template <class _CharT, class _Traits, class _Allocator>
2800template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002801_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002803 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002804 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002805>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2807{
Louis Dionneba400782020-10-02 15:02:52 -04002808#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002809 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2810 "string::insert(iterator, range) called with an iterator not"
2811 " referring to this string");
2812#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002813 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002814 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815}
2816
2817template <class _CharT, class _Traits, class _Allocator>
2818template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002819_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002821 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002823>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2825{
Louis Dionneba400782020-10-02 15:02:52 -04002826#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002827 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2828 "string::insert(iterator, range) called with an iterator not"
2829 " referring to this string");
2830#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002832 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833 if (__n)
2834 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002835 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2836 !__addr_in_range(*__first))
2837 {
2838 size_type __sz = size();
2839 size_type __cap = capacity();
2840 value_type* __p;
2841 if (__cap - __sz >= __n)
2842 {
2843 __p = _VSTD::__to_address(__get_pointer());
2844 size_type __n_move = __sz - __ip;
2845 if (__n_move != 0)
2846 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2847 }
2848 else
2849 {
2850 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2851 __p = _VSTD::__to_address(__get_long_pointer());
2852 }
2853 __sz += __n;
2854 __set_size(__sz);
2855 traits_type::assign(__p[__sz], value_type());
2856 for (__p += __ip; __first != __last; ++__p, ++__first)
2857 traits_type::assign(*__p, *__first);
2858 }
2859 else
Marshall Clow958362f2016-09-05 01:54:30 +00002860 {
2861 const basic_string __temp(__first, __last, __alloc());
2862 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2863 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864 }
2865 return begin() + __ip;
2866}
2867
2868template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002869inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870basic_string<_CharT, _Traits, _Allocator>&
2871basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2872{
2873 return insert(__pos1, __str.data(), __str.size());
2874}
2875
2876template <class _CharT, class _Traits, class _Allocator>
2877basic_string<_CharT, _Traits, _Allocator>&
2878basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2879 size_type __pos2, size_type __n)
2880{
2881 size_type __str_sz = __str.size();
2882 if (__pos2 > __str_sz)
2883 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002884 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885}
2886
2887template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002888template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002889_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002890<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002891 __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 +00002892 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002893>
Marshall Clow82513342016-09-24 22:45:42 +00002894basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002895 size_type __pos2, size_type __n)
2896{
Marshall Clow82513342016-09-24 22:45:42 +00002897 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002898 size_type __str_sz = __sv.size();
2899 if (__pos2 > __str_sz)
2900 this->__throw_out_of_range();
2901 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2902}
2903
2904template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002906basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002908 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 return insert(__pos, __s, traits_type::length(__s));
2910}
2911
2912template <class _CharT, class _Traits, class _Allocator>
2913typename basic_string<_CharT, _Traits, _Allocator>::iterator
2914basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2915{
2916 size_type __ip = static_cast<size_type>(__pos - begin());
2917 size_type __sz = size();
2918 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002919 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920 if (__cap == __sz)
2921 {
2922 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002923 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924 }
2925 else
2926 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002927 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928 size_type __n_move = __sz - __ip;
2929 if (__n_move != 0)
2930 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2931 }
2932 traits_type::assign(__p[__ip], __c);
2933 traits_type::assign(__p[++__sz], value_type());
2934 __set_size(__sz);
2935 return begin() + static_cast<difference_type>(__ip);
2936}
2937
2938template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002939inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940typename basic_string<_CharT, _Traits, _Allocator>::iterator
2941basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2942{
Louis Dionneba400782020-10-02 15:02:52 -04002943#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002944 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2945 "string::insert(iterator, n, value) called with an iterator not"
2946 " referring to this string");
2947#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948 difference_type __p = __pos - begin();
2949 insert(static_cast<size_type>(__p), __n, __c);
2950 return begin() + __p;
2951}
2952
2953// replace
2954
2955template <class _CharT, class _Traits, class _Allocator>
2956basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002957basic_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 +00002958 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002960 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961 size_type __sz = size();
2962 if (__pos > __sz)
2963 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002964 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965 size_type __cap = capacity();
2966 if (__cap - __sz + __n1 >= __n2)
2967 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002968 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 if (__n1 != __n2)
2970 {
2971 size_type __n_move = __sz - __pos - __n1;
2972 if (__n_move != 0)
2973 {
2974 if (__n1 > __n2)
2975 {
2976 traits_type::move(__p + __pos, __s, __n2);
2977 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2978 goto __finish;
2979 }
2980 if (__p + __pos < __s && __s < __p + __sz)
2981 {
2982 if (__p + __pos + __n1 <= __s)
2983 __s += __n2 - __n1;
2984 else // __p + __pos < __s < __p + __pos + __n1
2985 {
2986 traits_type::move(__p + __pos, __s, __n1);
2987 __pos += __n1;
2988 __s += __n2;
2989 __n2 -= __n1;
2990 __n1 = 0;
2991 }
2992 }
2993 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2994 }
2995 }
2996 traits_type::move(__p + __pos, __s, __n2);
2997__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05002998// __sz += __n2 - __n1; in this and the below function below can cause unsigned
2999// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 __sz += __n2 - __n1;
3001 __set_size(__sz);
3002 __invalidate_iterators_past(__sz);
3003 traits_type::assign(__p[__sz], value_type());
3004 }
3005 else
3006 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3007 return *this;
3008}
3009
3010template <class _CharT, class _Traits, class _Allocator>
3011basic_string<_CharT, _Traits, _Allocator>&
3012basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00003013 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014{
3015 size_type __sz = size();
3016 if (__pos > __sz)
3017 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003018 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003020 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021 if (__cap - __sz + __n1 >= __n2)
3022 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003023 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024 if (__n1 != __n2)
3025 {
3026 size_type __n_move = __sz - __pos - __n1;
3027 if (__n_move != 0)
3028 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3029 }
3030 }
3031 else
3032 {
3033 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003034 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 }
3036 traits_type::assign(__p + __pos, __n2, __c);
3037 __sz += __n2 - __n1;
3038 __set_size(__sz);
3039 __invalidate_iterators_past(__sz);
3040 traits_type::assign(__p[__sz], value_type());
3041 return *this;
3042}
3043
3044template <class _CharT, class _Traits, class _Allocator>
3045template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003046_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003048 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003050>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003051basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052 _InputIterator __j1, _InputIterator __j2)
3053{
Marshall Clow958362f2016-09-05 01:54:30 +00003054 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003055 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056}
3057
3058template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003059inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060basic_string<_CharT, _Traits, _Allocator>&
3061basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3062{
3063 return replace(__pos1, __n1, __str.data(), __str.size());
3064}
3065
3066template <class _CharT, class _Traits, class _Allocator>
3067basic_string<_CharT, _Traits, _Allocator>&
3068basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3069 size_type __pos2, size_type __n2)
3070{
3071 size_type __str_sz = __str.size();
3072 if (__pos2 > __str_sz)
3073 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003074 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075}
3076
3077template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003078template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003079_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003080<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003081 __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 +00003082 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003083>
Marshall Clow82513342016-09-24 22:45:42 +00003084basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003085 size_type __pos2, size_type __n2)
3086{
Marshall Clow82513342016-09-24 22:45:42 +00003087 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003088 size_type __str_sz = __sv.size();
3089 if (__pos2 > __str_sz)
3090 this->__throw_out_of_range();
3091 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3092}
3093
3094template <class _CharT, class _Traits, class _Allocator>
3095basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003096basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003098 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099 return replace(__pos, __n1, __s, traits_type::length(__s));
3100}
3101
3102template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003103inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003105basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106{
3107 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3108 __str.data(), __str.size());
3109}
3110
3111template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003112inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003114basic_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 +00003115{
3116 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3117}
3118
3119template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003120inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003122basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123{
3124 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3125}
3126
3127template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003128inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003130basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131{
3132 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3133}
3134
3135// erase
3136
Martijn Velsa81fc792020-02-26 13:25:43 -05003137// 'externally instantiated' erase() implementation, called when __n != npos.
3138// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003140void
3141basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3142 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144 if (__n)
3145 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003146 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003147 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003148 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 size_type __n_move = __sz - __pos - __n;
3150 if (__n_move != 0)
3151 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3152 __sz -= __n;
3153 __set_size(__sz);
3154 __invalidate_iterators_past(__sz);
3155 traits_type::assign(__p[__sz], value_type());
3156 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003157}
3158
3159template <class _CharT, class _Traits, class _Allocator>
3160basic_string<_CharT, _Traits, _Allocator>&
3161basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3162 size_type __n) {
3163 if (__pos > size()) this->__throw_out_of_range();
3164 if (__n == npos) {
3165 __erase_to_end(__pos);
3166 } else {
3167 __erase_external_with_move(__pos, __n);
3168 }
3169 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170}
3171
3172template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003173inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174typename basic_string<_CharT, _Traits, _Allocator>::iterator
3175basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3176{
Louis Dionneba400782020-10-02 15:02:52 -04003177#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003178 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3179 "string::erase(iterator) called with an iterator not"
3180 " referring to this string");
3181#endif
3182 _LIBCPP_ASSERT(__pos != end(),
3183 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184 iterator __b = begin();
3185 size_type __r = static_cast<size_type>(__pos - __b);
3186 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003187 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188}
3189
3190template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003191inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192typename basic_string<_CharT, _Traits, _Allocator>::iterator
3193basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3194{
Louis Dionneba400782020-10-02 15:02:52 -04003195#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003196 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3197 "string::erase(iterator, iterator) called with an iterator not"
3198 " referring to this string");
3199#endif
3200 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201 iterator __b = begin();
3202 size_type __r = static_cast<size_type>(__first - __b);
3203 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003204 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205}
3206
3207template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003208inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209void
3210basic_string<_CharT, _Traits, _Allocator>::pop_back()
3211{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003212 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213 size_type __sz;
3214 if (__is_long())
3215 {
3216 __sz = __get_long_size() - 1;
3217 __set_long_size(__sz);
3218 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3219 }
3220 else
3221 {
3222 __sz = __get_short_size() - 1;
3223 __set_short_size(__sz);
3224 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3225 }
3226 __invalidate_iterators_past(__sz);
3227}
3228
3229template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003230inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003232basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233{
3234 __invalidate_all_iterators();
3235 if (__is_long())
3236 {
3237 traits_type::assign(*__get_long_pointer(), value_type());
3238 __set_long_size(0);
3239 }
3240 else
3241 {
3242 traits_type::assign(*__get_short_pointer(), value_type());
3243 __set_short_size(0);
3244 }
3245}
3246
3247template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003248inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249void
3250basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3251{
3252 if (__is_long())
3253 {
3254 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3255 __set_long_size(__pos);
3256 }
3257 else
3258 {
3259 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3260 __set_short_size(__pos);
3261 }
3262 __invalidate_iterators_past(__pos);
3263}
3264
3265template <class _CharT, class _Traits, class _Allocator>
3266void
3267basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3268{
3269 size_type __sz = size();
3270 if (__n > __sz)
3271 append(__n - __sz, __c);
3272 else
3273 __erase_to_end(__n);
3274}
3275
3276template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003277inline void
3278basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3279{
3280 size_type __sz = size();
3281 if (__n > __sz) {
3282 __append_default_init(__n - __sz);
3283 } else
3284 __erase_to_end(__n);
3285}
3286
3287template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003288inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003290basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003292 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003293#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003294 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003296 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297#endif
3298}
3299
3300template <class _CharT, class _Traits, class _Allocator>
3301void
Marek Kurdejc9848142020-11-26 10:07:16 +01003302basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303{
Marek Kurdejc9848142020-11-26 10:07:16 +01003304 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003306
3307#if _LIBCPP_STD_VER > 17
3308 // Reserve never shrinks as of C++20.
3309 if (__requested_capacity <= capacity()) return;
3310#endif
3311
3312 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3313 __target_capacity = __recommend(__target_capacity);
3314 if (__target_capacity == capacity()) return;
3315
3316 __shrink_or_extend(__target_capacity);
3317}
3318
3319template <class _CharT, class _Traits, class _Allocator>
3320void
3321basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3322{
3323 size_type __target_capacity = __recommend(size());
3324 if (__target_capacity == capacity()) return;
3325
3326 __shrink_or_extend(__target_capacity);
3327}
3328
3329template <class _CharT, class _Traits, class _Allocator>
3330void
3331basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3332{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333 size_type __cap = capacity();
3334 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003335
3336 pointer __new_data, __p;
3337 bool __was_long, __now_long;
3338 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003340 __was_long = true;
3341 __now_long = false;
3342 __new_data = __get_short_pointer();
3343 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003344 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003345 else
3346 {
3347 if (__target_capacity > __cap)
3348 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3349 else
3350 {
3351 #ifndef _LIBCPP_NO_EXCEPTIONS
3352 try
3353 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003354 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003355 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3356 #ifndef _LIBCPP_NO_EXCEPTIONS
3357 }
3358 catch (...)
3359 {
3360 return;
3361 }
3362 #else // _LIBCPP_NO_EXCEPTIONS
3363 if (__new_data == nullptr)
3364 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003365 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003366 }
3367 __now_long = true;
3368 __was_long = __is_long();
3369 __p = __get_pointer();
3370 }
3371 traits_type::copy(_VSTD::__to_address(__new_data),
3372 _VSTD::__to_address(__p), size()+1);
3373 if (__was_long)
3374 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3375 if (__now_long)
3376 {
3377 __set_long_cap(__target_capacity+1);
3378 __set_long_size(__sz);
3379 __set_long_pointer(__new_data);
3380 }
3381 else
3382 __set_short_size(__sz);
3383 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384}
3385
3386template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003387inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003389basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003391 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392 return *(data() + __pos);
3393}
3394
3395template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003396inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003398basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003400 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401 return *(__get_pointer() + __pos);
3402}
3403
3404template <class _CharT, class _Traits, class _Allocator>
3405typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3406basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3407{
3408 if (__n >= size())
3409 this->__throw_out_of_range();
3410 return (*this)[__n];
3411}
3412
3413template <class _CharT, class _Traits, class _Allocator>
3414typename basic_string<_CharT, _Traits, _Allocator>::reference
3415basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3416{
3417 if (__n >= size())
3418 this->__throw_out_of_range();
3419 return (*this)[__n];
3420}
3421
3422template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003423inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003424typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003425basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003427 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428 return *__get_pointer();
3429}
3430
3431template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003432inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003433typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003434basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003436 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437 return *data();
3438}
3439
3440template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003441inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003443basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003445 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446 return *(__get_pointer() + size() - 1);
3447}
3448
3449template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003450inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003451typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003452basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003454 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455 return *(data() + size() - 1);
3456}
3457
3458template <class _CharT, class _Traits, class _Allocator>
3459typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003460basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003461{
3462 size_type __sz = size();
3463 if (__pos > __sz)
3464 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003465 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466 traits_type::copy(__s, data() + __pos, __rlen);
3467 return __rlen;
3468}
3469
3470template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003471inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472basic_string<_CharT, _Traits, _Allocator>
3473basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3474{
3475 return basic_string(*this, __pos, __n, __alloc());
3476}
3477
3478template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003479inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480void
3481basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003482#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003483 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003484#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003485 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003486 __is_nothrow_swappable<allocator_type>::value)
3487#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488{
Louis Dionneba400782020-10-02 15:02:52 -04003489#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003490 if (!__is_long())
3491 __get_db()->__invalidate_all(this);
3492 if (!__str.__is_long())
3493 __get_db()->__invalidate_all(&__str);
3494 __get_db()->swap(this, &__str);
3495#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003496 _LIBCPP_ASSERT(
3497 __alloc_traits::propagate_on_container_swap::value ||
3498 __alloc_traits::is_always_equal::value ||
3499 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003500 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003501 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502}
3503
3504// find
3505
3506template <class _Traits>
3507struct _LIBCPP_HIDDEN __traits_eq
3508{
3509 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003510 _LIBCPP_INLINE_VISIBILITY
3511 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3512 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513};
3514
3515template<class _CharT, class _Traits, class _Allocator>
3516typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003517basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003518 size_type __pos,
3519 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003521 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003522 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003523 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524}
3525
3526template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003527inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003529basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3530 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003532 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003533 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534}
3535
3536template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003537template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003538_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003539<
3540 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3541 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003542>
Marshall Clowe46031a2018-07-02 18:41:15 +00003543basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003544 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003545{
Marshall Clowe46031a2018-07-02 18:41:15 +00003546 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003547 return __str_find<value_type, size_type, traits_type, npos>
3548 (data(), size(), __sv.data(), __pos, __sv.size());
3549}
3550
3551template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003552inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003553typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003554basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003555 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003557 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003558 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003559 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003560}
3561
3562template<class _CharT, class _Traits, class _Allocator>
3563typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003564basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3565 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003567 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003568 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569}
3570
3571// rfind
3572
3573template<class _CharT, class _Traits, class _Allocator>
3574typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003575basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003576 size_type __pos,
3577 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003579 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003580 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003581 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582}
3583
3584template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003585inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003587basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3588 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003590 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003591 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592}
3593
3594template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003595template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003596_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003597<
3598 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3599 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003600>
Marshall Clowe46031a2018-07-02 18:41:15 +00003601basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003602 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003603{
Marshall Clowe46031a2018-07-02 18:41:15 +00003604 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003605 return __str_rfind<value_type, size_type, traits_type, npos>
3606 (data(), size(), __sv.data(), __pos, __sv.size());
3607}
3608
3609template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003610inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003611typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003612basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003613 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003615 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003616 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003617 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618}
3619
3620template<class _CharT, class _Traits, class _Allocator>
3621typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003622basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3623 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003625 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003626 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627}
3628
3629// find_first_of
3630
3631template<class _CharT, class _Traits, class _Allocator>
3632typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003633basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003634 size_type __pos,
3635 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003637 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003638 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003639 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003640}
3641
3642template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003643inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003645basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3646 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003648 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003649 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650}
3651
3652template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003653template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003654_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003655<
3656 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3657 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003658>
Marshall Clowe46031a2018-07-02 18:41:15 +00003659basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003660 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003661{
Marshall Clowe46031a2018-07-02 18:41:15 +00003662 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003663 return __str_find_first_of<value_type, size_type, traits_type, npos>
3664 (data(), size(), __sv.data(), __pos, __sv.size());
3665}
3666
3667template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003668inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003669typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003670basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003671 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003673 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003674 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003675 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003676}
3677
3678template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003679inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003681basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3682 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683{
3684 return find(__c, __pos);
3685}
3686
3687// find_last_of
3688
3689template<class _CharT, class _Traits, class _Allocator>
3690typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003691basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003692 size_type __pos,
3693 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003695 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003696 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003697 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003698}
3699
3700template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003701inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003702typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003703basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3704 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003706 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003707 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708}
3709
3710template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003711template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003712_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003713<
3714 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3715 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003716>
Marshall Clowe46031a2018-07-02 18:41:15 +00003717basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003718 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003719{
Marshall Clowe46031a2018-07-02 18:41:15 +00003720 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003721 return __str_find_last_of<value_type, size_type, traits_type, npos>
3722 (data(), size(), __sv.data(), __pos, __sv.size());
3723}
3724
3725template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003726inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003727typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003728basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003729 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003731 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003732 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003733 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734}
3735
3736template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003737inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003739basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3740 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003741{
3742 return rfind(__c, __pos);
3743}
3744
3745// find_first_not_of
3746
3747template<class _CharT, class _Traits, class _Allocator>
3748typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003749basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003750 size_type __pos,
3751 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003752{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003753 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003754 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003755 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756}
3757
3758template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003759inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003760typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003761basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3762 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003764 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003765 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766}
3767
3768template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003769template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003770_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003771<
3772 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3773 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003774>
Marshall Clowe46031a2018-07-02 18:41:15 +00003775basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003776 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003777{
Marshall Clowe46031a2018-07-02 18:41:15 +00003778 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003779 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3780 (data(), size(), __sv.data(), __pos, __sv.size());
3781}
3782
3783template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003784inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003785typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003786basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003787 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003789 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003790 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003791 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792}
3793
3794template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003795inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003797basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3798 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003800 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003801 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802}
3803
3804// find_last_not_of
3805
3806template<class _CharT, class _Traits, class _Allocator>
3807typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003808basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003809 size_type __pos,
3810 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003812 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003813 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003814 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815}
3816
3817template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003818inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003820basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3821 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003823 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003824 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825}
3826
3827template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003828template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003829_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003830<
3831 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3832 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003833>
Marshall Clowe46031a2018-07-02 18:41:15 +00003834basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003835 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003836{
Marshall Clowe46031a2018-07-02 18:41:15 +00003837 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003838 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3839 (data(), size(), __sv.data(), __pos, __sv.size());
3840}
3841
3842template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003843inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003844typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003845basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003846 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003848 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003849 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003850 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851}
3852
3853template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003854inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003856basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3857 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003860 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861}
3862
3863// compare
3864
3865template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003866template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003867_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003868<
3869 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3870 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003871>
zoecarver1997e0a2021-02-05 11:54:47 -08003872basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873{
Marshall Clowe46031a2018-07-02 18:41:15 +00003874 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003875 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003876 size_t __rhs_sz = __sv.size();
3877 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003878 _VSTD::min(__lhs_sz, __rhs_sz));
3879 if (__result != 0)
3880 return __result;
3881 if (__lhs_sz < __rhs_sz)
3882 return -1;
3883 if (__lhs_sz > __rhs_sz)
3884 return 1;
3885 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886}
3887
3888template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003889inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003890int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003891basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003893 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894}
3895
3896template <class _CharT, class _Traits, class _Allocator>
3897int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003898basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3899 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003900 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003901 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003903 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904 size_type __sz = size();
3905 if (__pos1 > __sz || __n2 == npos)
3906 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003907 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3908 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909 if (__r == 0)
3910 {
3911 if (__rlen < __n2)
3912 __r = -1;
3913 else if (__rlen > __n2)
3914 __r = 1;
3915 }
3916 return __r;
3917}
3918
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003919template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003920template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003921_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003922<
3923 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3924 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003925>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003926basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3927 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003928 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003929{
Marshall Clowe46031a2018-07-02 18:41:15 +00003930 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003931 return compare(__pos1, __n1, __sv.data(), __sv.size());
3932}
3933
3934template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003935inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003936int
3937basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3938 size_type __n1,
3939 const basic_string& __str) const
3940{
3941 return compare(__pos1, __n1, __str.data(), __str.size());
3942}
3943
3944template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003945template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003946_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003947<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003948 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3949 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003950 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003951>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003952basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3953 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003954 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003955 size_type __pos2,
3956 size_type __n2) const
3957{
Marshall Clow82513342016-09-24 22:45:42 +00003958 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003959 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3960}
3961
3962template <class _CharT, class _Traits, class _Allocator>
3963int
3964basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3965 size_type __n1,
3966 const basic_string& __str,
3967 size_type __pos2,
3968 size_type __n2) const
3969{
3970 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3971}
3972
3973template <class _CharT, class _Traits, class _Allocator>
3974int
3975basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3976{
3977 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3978 return compare(0, npos, __s, traits_type::length(__s));
3979}
3980
3981template <class _CharT, class _Traits, class _Allocator>
3982int
3983basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3984 size_type __n1,
3985 const value_type* __s) const
3986{
3987 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3988 return compare(__pos1, __n1, __s, traits_type::length(__s));
3989}
3990
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991// __invariants
3992
3993template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003994inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995bool
3996basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3997{
3998 if (size() > capacity())
3999 return false;
4000 if (capacity() < __min_cap - 1)
4001 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004002 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004004 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005 return false;
4006 return true;
4007}
4008
Vedant Kumar55e007e2018-03-08 21:15:26 +00004009// __clear_and_shrink
4010
4011template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004012inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004013void
Marshall Clowe60a7182018-05-29 17:04:37 +00004014basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004015{
4016 clear();
4017 if(__is_long())
4018 {
4019 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4020 __set_long_cap(0);
4021 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004022 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004023 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004024}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004025
Howard Hinnantc51e1022010-05-11 19:42:16 +00004026// operator==
4027
4028template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004029inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030bool
4031operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004032 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004034 size_t __lhs_sz = __lhs.size();
4035 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4036 __rhs.data(),
4037 __lhs_sz) == 0;
4038}
4039
4040template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004041inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004042bool
4043operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4044 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4045{
4046 size_t __lhs_sz = __lhs.size();
4047 if (__lhs_sz != __rhs.size())
4048 return false;
4049 const char* __lp = __lhs.data();
4050 const char* __rp = __rhs.data();
4051 if (__lhs.__is_long())
4052 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4053 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4054 if (*__lp != *__rp)
4055 return false;
4056 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057}
4058
4059template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004060inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004062operator==(const _CharT* __lhs,
4063 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004065 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4066 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4067 size_t __lhs_len = _Traits::length(__lhs);
4068 if (__lhs_len != __rhs.size()) return false;
4069 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070}
4071
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004073inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004075operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4076 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004078 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4079 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4080 size_t __rhs_len = _Traits::length(__rhs);
4081 if (__rhs_len != __lhs.size()) return false;
4082 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083}
4084
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004085template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004086inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087bool
4088operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004089 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090{
4091 return !(__lhs == __rhs);
4092}
4093
4094template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004095inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004097operator!=(const _CharT* __lhs,
4098 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099{
4100 return !(__lhs == __rhs);
4101}
4102
4103template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004104inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004106operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4107 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108{
4109 return !(__lhs == __rhs);
4110}
4111
4112// operator<
4113
4114template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004115inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116bool
4117operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004118 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004120 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121}
4122
4123template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004124inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004126operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4127 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004129 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130}
4131
4132template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004133inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004135operator< (const _CharT* __lhs,
4136 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137{
4138 return __rhs.compare(__lhs) > 0;
4139}
4140
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141// operator>
4142
4143template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004144inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145bool
4146operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004147 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004148{
4149 return __rhs < __lhs;
4150}
4151
4152template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004153inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004155operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4156 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157{
4158 return __rhs < __lhs;
4159}
4160
4161template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004162inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004163bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004164operator> (const _CharT* __lhs,
4165 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166{
4167 return __rhs < __lhs;
4168}
4169
4170// operator<=
4171
4172template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004173inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004174bool
4175operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004176 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177{
4178 return !(__rhs < __lhs);
4179}
4180
4181template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004182inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004184operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4185 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186{
4187 return !(__rhs < __lhs);
4188}
4189
4190template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004191inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004193operator<=(const _CharT* __lhs,
4194 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195{
4196 return !(__rhs < __lhs);
4197}
4198
4199// operator>=
4200
4201template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004202inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203bool
4204operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004205 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206{
4207 return !(__lhs < __rhs);
4208}
4209
4210template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004211inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004213operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4214 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215{
4216 return !(__lhs < __rhs);
4217}
4218
4219template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004222operator>=(const _CharT* __lhs,
4223 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224{
4225 return !(__lhs < __rhs);
4226}
4227
4228// operator +
4229
4230template<class _CharT, class _Traits, class _Allocator>
4231basic_string<_CharT, _Traits, _Allocator>
4232operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4233 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4234{
4235 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4236 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4237 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4238 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4239 __r.append(__rhs.data(), __rhs_sz);
4240 return __r;
4241}
4242
4243template<class _CharT, class _Traits, class _Allocator>
4244basic_string<_CharT, _Traits, _Allocator>
4245operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4246{
4247 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4248 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4249 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4250 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4251 __r.append(__rhs.data(), __rhs_sz);
4252 return __r;
4253}
4254
4255template<class _CharT, class _Traits, class _Allocator>
4256basic_string<_CharT, _Traits, _Allocator>
4257operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4258{
4259 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4260 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4261 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4262 __r.append(__rhs.data(), __rhs_sz);
4263 return __r;
4264}
4265
4266template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004267inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268basic_string<_CharT, _Traits, _Allocator>
4269operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4270{
4271 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4272 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4273 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4274 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4275 __r.append(__rhs, __rhs_sz);
4276 return __r;
4277}
4278
4279template<class _CharT, class _Traits, class _Allocator>
4280basic_string<_CharT, _Traits, _Allocator>
4281operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4282{
4283 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4284 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4285 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4286 __r.push_back(__rhs);
4287 return __r;
4288}
4289
Eric Fiselierfc92be82017-04-19 00:28:44 +00004290#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004291
4292template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004293inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294basic_string<_CharT, _Traits, _Allocator>
4295operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4296{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004297 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004298}
4299
4300template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302basic_string<_CharT, _Traits, _Allocator>
4303operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4304{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004305 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306}
4307
4308template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310basic_string<_CharT, _Traits, _Allocator>
4311operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4312{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004313 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314}
4315
4316template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004317inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318basic_string<_CharT, _Traits, _Allocator>
4319operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4320{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004321 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322}
4323
4324template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004325inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326basic_string<_CharT, _Traits, _Allocator>
4327operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4328{
4329 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004330 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331}
4332
4333template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004334inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335basic_string<_CharT, _Traits, _Allocator>
4336operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4337{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004338 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339}
4340
4341template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343basic_string<_CharT, _Traits, _Allocator>
4344operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4345{
4346 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004347 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348}
4349
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004350#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351
4352// swap
4353
4354template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004356void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004357swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004358 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4359 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360{
4361 __lhs.swap(__rhs);
4362}
4363
Bruce Mitchener170d8972020-11-24 12:53:53 -05004364_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4365_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4366_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4367_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4368_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004369
Bruce Mitchener170d8972020-11-24 12:53:53 -05004370_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4371_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4372_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004373
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004374_LIBCPP_FUNC_VIS string to_string(int __val);
4375_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4376_LIBCPP_FUNC_VIS string to_string(long __val);
4377_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4378_LIBCPP_FUNC_VIS string to_string(long long __val);
4379_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4380_LIBCPP_FUNC_VIS string to_string(float __val);
4381_LIBCPP_FUNC_VIS string to_string(double __val);
4382_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004383
Bruce Mitchener170d8972020-11-24 12:53:53 -05004384_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4385_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4386_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4387_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4388_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004389
Bruce Mitchener170d8972020-11-24 12:53:53 -05004390_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4391_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4392_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004393
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004394_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4395_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4396_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4397_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4398_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4399_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4400_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4401_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4402_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004403
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004405_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004406const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4407 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004408
Marshall Clow851b9ec2019-05-20 21:56:51 +00004409template <class _CharT, class _Allocator>
4410struct _LIBCPP_TEMPLATE_VIS
4411 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4412 : public unary_function<
4413 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004414{
4415 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004416 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4417 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004418};
4419
Howard Hinnantc51e1022010-05-11 19:42:16 +00004420
Howard Hinnantdc095972011-07-18 15:51:59 +00004421template<class _CharT, class _Traits, class _Allocator>
4422basic_ostream<_CharT, _Traits>&
4423operator<<(basic_ostream<_CharT, _Traits>& __os,
4424 const basic_string<_CharT, _Traits, _Allocator>& __str);
4425
4426template<class _CharT, class _Traits, class _Allocator>
4427basic_istream<_CharT, _Traits>&
4428operator>>(basic_istream<_CharT, _Traits>& __is,
4429 basic_string<_CharT, _Traits, _Allocator>& __str);
4430
4431template<class _CharT, class _Traits, class _Allocator>
4432basic_istream<_CharT, _Traits>&
4433getline(basic_istream<_CharT, _Traits>& __is,
4434 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4435
4436template<class _CharT, class _Traits, class _Allocator>
4437inline _LIBCPP_INLINE_VISIBILITY
4438basic_istream<_CharT, _Traits>&
4439getline(basic_istream<_CharT, _Traits>& __is,
4440 basic_string<_CharT, _Traits, _Allocator>& __str);
4441
Eric Fiselierfc92be82017-04-19 00:28:44 +00004442#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004443
4444template<class _CharT, class _Traits, class _Allocator>
4445inline _LIBCPP_INLINE_VISIBILITY
4446basic_istream<_CharT, _Traits>&
4447getline(basic_istream<_CharT, _Traits>&& __is,
4448 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4449
4450template<class _CharT, class _Traits, class _Allocator>
4451inline _LIBCPP_INLINE_VISIBILITY
4452basic_istream<_CharT, _Traits>&
4453getline(basic_istream<_CharT, _Traits>&& __is,
4454 basic_string<_CharT, _Traits, _Allocator>& __str);
4455
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004456#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004457
Marshall Clow29b53f22018-12-14 18:49:35 +00004458#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004459template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004460inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004461 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4462 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4463 auto __old_size = __str.size();
4464 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4465 return __old_size - __str.size();
4466}
Marshall Clow29b53f22018-12-14 18:49:35 +00004467
Marek Kurdeja98b1412020-05-02 13:58:03 +02004468template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004469inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004470 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4471 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4472 _Predicate __pred) {
4473 auto __old_size = __str.size();
4474 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4475 __str.end());
4476 return __old_size - __str.size();
4477}
Marshall Clow29b53f22018-12-14 18:49:35 +00004478#endif
4479
Louis Dionneba400782020-10-02 15:02:52 -04004480#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004481
4482template<class _CharT, class _Traits, class _Allocator>
4483bool
4484basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4485{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004486 return this->data() <= _VSTD::__to_address(__i->base()) &&
4487 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004488}
4489
4490template<class _CharT, class _Traits, class _Allocator>
4491bool
4492basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4493{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004494 return this->data() < _VSTD::__to_address(__i->base()) &&
4495 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004496}
4497
4498template<class _CharT, class _Traits, class _Allocator>
4499bool
4500basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4501{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004502 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004503 return this->data() <= __p && __p <= this->data() + this->size();
4504}
4505
4506template<class _CharT, class _Traits, class _Allocator>
4507bool
4508basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4509{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004510 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004511 return this->data() <= __p && __p < this->data() + this->size();
4512}
4513
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004514#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004515
Louis Dionne173f29e2019-05-29 16:01:36 +00004516#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004517// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004518inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004519{
4520 inline namespace string_literals
4521 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004522 inline _LIBCPP_INLINE_VISIBILITY
4523 basic_string<char> operator "" s( const char *__str, size_t __len )
4524 {
4525 return basic_string<char> (__str, __len);
4526 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004527
Howard Hinnant5c167562013-08-07 19:39:48 +00004528 inline _LIBCPP_INLINE_VISIBILITY
4529 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4530 {
4531 return basic_string<wchar_t> (__str, __len);
4532 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004533
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004534#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004535 inline _LIBCPP_INLINE_VISIBILITY
4536 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4537 {
4538 return basic_string<char8_t> (__str, __len);
4539 }
4540#endif
4541
Howard Hinnant5c167562013-08-07 19:39:48 +00004542 inline _LIBCPP_INLINE_VISIBILITY
4543 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4544 {
4545 return basic_string<char16_t> (__str, __len);
4546 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004547
Howard Hinnant5c167562013-08-07 19:39:48 +00004548 inline _LIBCPP_INLINE_VISIBILITY
4549 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4550 {
4551 return basic_string<char32_t> (__str, __len);
4552 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004553 }
4554}
4555#endif
4556
Howard Hinnantc51e1022010-05-11 19:42:16 +00004557_LIBCPP_END_NAMESPACE_STD
4558
Eric Fiselierf4433a32017-05-31 22:07:49 +00004559_LIBCPP_POP_MACROS
4560
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004561#endif // _LIBCPP_STRING