blob: 19bd22b3acf5cc080886151ceea391fb1e5530dc [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>;
72
Howard Hinnant3b6579a2010-08-22 00:02:43 +000073template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000074class basic_string
75{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076public:
77// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000078 typedef traits traits_type;
79 typedef typename traits_type::char_type value_type;
80 typedef Allocator allocator_type;
81 typedef typename allocator_type::size_type size_type;
82 typedef typename allocator_type::difference_type difference_type;
83 typedef typename allocator_type::reference reference;
84 typedef typename allocator_type::const_reference const_reference;
85 typedef typename allocator_type::pointer pointer;
86 typedef typename allocator_type::const_pointer const_pointer;
87 typedef implementation-defined iterator;
88 typedef implementation-defined const_iterator;
89 typedef std::reverse_iterator<iterator> reverse_iterator;
90 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
91
92 static const size_type npos = -1;
93
Howard Hinnant3e276872011-06-03 18:40:47 +000094 basic_string()
95 noexcept(is_nothrow_default_constructible<allocator_type>::value);
96 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +000097 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +000098 basic_string(basic_string&& str)
99 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000100 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000101 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000102 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000103 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000104 template<class T>
105 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000106 template <class T>
107 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000108 basic_string(const value_type* s, const allocator_type& a = allocator_type());
109 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
111 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000112 basic_string(InputIterator begin, InputIterator end,
113 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
115 basic_string(const basic_string&, const Allocator&);
116 basic_string(basic_string&&, const Allocator&);
117
118 ~basic_string();
119
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000120 operator basic_string_view<charT, traits>() const noexcept;
121
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000123 template <class T>
124 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000125 basic_string& operator=(basic_string&& str)
126 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000127 allocator_type::propagate_on_container_move_assignment::value ||
128 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000129 basic_string& operator=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130 basic_string& operator=(value_type c);
131 basic_string& operator=(initializer_list<value_type>);
132
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000133 iterator begin() noexcept;
134 const_iterator begin() const noexcept;
135 iterator end() noexcept;
136 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 reverse_iterator rbegin() noexcept;
139 const_reverse_iterator rbegin() const noexcept;
140 reverse_iterator rend() noexcept;
141 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 const_iterator cbegin() const noexcept;
144 const_iterator cend() const noexcept;
145 const_reverse_iterator crbegin() const noexcept;
146 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 size_type size() const noexcept;
149 size_type length() const noexcept;
150 size_type max_size() const noexcept;
151 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
153 void resize(size_type n, value_type c);
154 void resize(size_type n);
155
Marek Kurdejc9848142020-11-26 10:07:16 +0100156 void reserve(size_type res_arg);
157 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000159 void clear() noexcept;
160 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161
162 const_reference operator[](size_type pos) const;
163 reference operator[](size_type pos);
164
165 const_reference at(size_type n) const;
166 reference at(size_type n);
167
168 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000169 template <class T>
170 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000171 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172 basic_string& operator+=(value_type c);
173 basic_string& operator+=(initializer_list<value_type>);
174
175 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000176 template <class T>
177 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000178 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000179 template <class T>
180 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000181 basic_string& append(const value_type* s, size_type n);
182 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000184 template<class InputIterator>
185 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186 basic_string& append(initializer_list<value_type>);
187
188 void push_back(value_type c);
189 void pop_back();
190 reference front();
191 const_reference front() const;
192 reference back();
193 const_reference back() const;
194
195 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000196 template <class T>
197 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000198 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000199 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000200 template <class T>
201 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000202 basic_string& assign(const value_type* s, size_type n);
203 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000205 template<class InputIterator>
206 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 basic_string& assign(initializer_list<value_type>);
208
209 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000210 template <class T>
211 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000212 basic_string& insert(size_type pos1, const basic_string& str,
213 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000214 template <class T>
215 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000216 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000217 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218 basic_string& insert(size_type pos, size_type n, value_type c);
219 iterator insert(const_iterator p, value_type c);
220 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000221 template<class InputIterator>
222 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 iterator insert(const_iterator p, initializer_list<value_type>);
224
225 basic_string& erase(size_type pos = 0, size_type n = npos);
226 iterator erase(const_iterator position);
227 iterator erase(const_iterator first, const_iterator last);
228
229 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000230 template <class T>
231 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000232 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000233 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000234 template <class T>
235 basic_string& replace(size_type pos1, size_type n1, const T& t,
236 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000237 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
238 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000240 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000241 template <class T>
242 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000243 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
244 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000245 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000246 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000247 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
248 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000249
Howard Hinnantd17880b2013-06-28 16:59:19 +0000250 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251 basic_string substr(size_type pos = 0, size_type n = npos) const;
252
Howard Hinnant3e276872011-06-03 18:40:47 +0000253 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000254 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
255 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256
Howard Hinnantd17880b2013-06-28 16:59:19 +0000257 const value_type* c_str() const noexcept;
258 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000259 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000261 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000263 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000264 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800265 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 +0000266 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
267 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000268 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000270 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000271 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800272 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 +0000273 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
274 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000275 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000277 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000278 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800279 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 +0000280 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
281 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000282 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000284 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000285 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800286 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 +0000287 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
288 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000289 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000291 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000292 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800293 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 +0000294 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
295 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000296 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000298 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000299 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800300 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 +0000301 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
302 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000303 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000305 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000306 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800307 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000309 template <class T>
310 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000311 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000312 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000313 template <class T>
314 int compare(size_type pos1, size_type n1, const T& t,
315 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000316 int compare(const value_type* s) const noexcept;
317 int compare(size_type pos1, size_type n1, const value_type* s) const;
318 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319
Marek Kurdej24b4c512021-01-07 12:29:04 +0100320 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
321 bool starts_with(charT c) const noexcept; // C++20
322 bool starts_with(const charT* s) const; // C++20
323 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
324 bool ends_with(charT c) const noexcept; // C++20
325 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000326
Wim Leflere023c3542021-01-19 14:33:30 -0500327 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
328 constexpr bool contains(charT c) const noexcept; // C++2b
329 constexpr bool contains(const charT* s) const; // C++2b
330
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331 bool __invariants() const;
332};
333
Marshall Clowa0563332018-02-08 06:34:03 +0000334template<class InputIterator,
335 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
336basic_string(InputIterator, InputIterator, Allocator = Allocator())
337 -> basic_string<typename iterator_traits<InputIterator>::value_type,
338 char_traits<typename iterator_traits<InputIterator>::value_type>,
339 Allocator>; // C++17
340
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341template<class charT, class traits, class Allocator>
342basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000343operator+(const basic_string<charT, traits, Allocator>& lhs,
344 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345
346template<class charT, class traits, class Allocator>
347basic_string<charT, traits, Allocator>
348operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
349
350template<class charT, class traits, class Allocator>
351basic_string<charT, traits, Allocator>
352operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
361
362template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000363bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000364 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365
366template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000367bool operator==(const charT* lhs, 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 basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000372template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000373bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000374 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375
376template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000377bool operator!=(const charT* lhs, 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 basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000383bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000384 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385
386template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000387bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* 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 charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000393bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000394 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395
396template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000397bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* 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 charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000403bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000404 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405
406template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000407bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* 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 charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
412template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000413bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000414 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415
416template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000417bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* 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 charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000423void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000424 basic_string<charT, traits, Allocator>& rhs)
425 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
428basic_istream<charT, traits>&
429operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
430
431template<class charT, class traits, class Allocator>
432basic_ostream<charT, traits>&
433operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
434
435template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000436basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000437getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
438 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439
440template<class charT, class traits, class Allocator>
441basic_istream<charT, traits>&
442getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
443
Marshall Clow29b53f22018-12-14 18:49:35 +0000444template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200445typename basic_string<charT, traits, Allocator>::size_type
446erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000447template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200448typename basic_string<charT, traits, Allocator>::size_type
449erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000450
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451typedef basic_string<char> string;
452typedef basic_string<wchar_t> wstring;
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000453typedef basic_string<char16_t> u16string;
454typedef basic_string<char32_t> u32string;
455
Bruce Mitchener170d8972020-11-24 12:53:53 -0500456int stoi (const string& str, size_t* idx = nullptr, int base = 10);
457long stol (const string& str, size_t* idx = nullptr, int base = 10);
458unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
459long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
460unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000461
Bruce Mitchener170d8972020-11-24 12:53:53 -0500462float stof (const string& str, size_t* idx = nullptr);
463double stod (const string& str, size_t* idx = nullptr);
464long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000465
466string to_string(int val);
467string to_string(unsigned val);
468string to_string(long val);
469string to_string(unsigned long val);
470string to_string(long long val);
471string to_string(unsigned long long val);
472string to_string(float val);
473string to_string(double val);
474string to_string(long double val);
475
Bruce Mitchener170d8972020-11-24 12:53:53 -0500476int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
477long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
478unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
479long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
480unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000481
Bruce Mitchener170d8972020-11-24 12:53:53 -0500482float stof (const wstring& str, size_t* idx = nullptr);
483double stod (const wstring& str, size_t* idx = nullptr);
484long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000485
486wstring to_wstring(int val);
487wstring to_wstring(unsigned val);
488wstring to_wstring(long val);
489wstring to_wstring(unsigned long val);
490wstring to_wstring(long long val);
491wstring to_wstring(unsigned long long val);
492wstring to_wstring(float val);
493wstring to_wstring(double val);
494wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000495
496template <> struct hash<string>;
497template <> struct hash<u16string>;
498template <> struct hash<u32string>;
499template <> struct hash<wstring>;
500
Marshall Clowcba751f2013-07-23 17:05:24 +0000501basic_string<char> operator "" s( const char *str, size_t len ); // C++14
502basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
503basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
504basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
505
Howard Hinnantc51e1022010-05-11 19:42:16 +0000506} // std
507
508*/
509
510#include <__config>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000511#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512#include <iosfwd>
513#include <cstring>
Howard Hinnant155c2af2010-05-24 17:49:41 +0000514#include <cstdio> // For EOF.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515#include <cwchar>
516#include <algorithm>
517#include <iterator>
518#include <utility>
519#include <memory>
520#include <stdexcept>
521#include <type_traits>
522#include <initializer_list>
523#include <__functional_base>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000524#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
526#include <cstdint>
527#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000528
Eric Fiselier14b6de92014-08-10 23:53:08 +0000529#include <__debug>
530
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000531#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000533#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534
Eric Fiselierf4433a32017-05-31 22:07:49 +0000535_LIBCPP_PUSH_MACROS
536#include <__undef_macros>
537
538
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539_LIBCPP_BEGIN_NAMESPACE_STD
540
541// fpos
542
543template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000544class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545{
546private:
547 _StateT __st_;
548 streamoff __off_;
549public:
550 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
551
552 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
553
554 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
555 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
556
557 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
558 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
559 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
560 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
561};
562
563template <class _StateT>
564inline _LIBCPP_INLINE_VISIBILITY
565streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
566 {return streamoff(__x) - streamoff(__y);}
567
568template <class _StateT>
569inline _LIBCPP_INLINE_VISIBILITY
570bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
571 {return streamoff(__x) == streamoff(__y);}
572
573template <class _StateT>
574inline _LIBCPP_INLINE_VISIBILITY
575bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
576 {return streamoff(__x) != streamoff(__y);}
577
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578// basic_string
579
580template<class _CharT, class _Traits, class _Allocator>
581basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000582operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
583 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584
585template<class _CharT, class _Traits, class _Allocator>
586basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000587operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588
589template<class _CharT, class _Traits, class _Allocator>
590basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000591operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592
593template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000594inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000596operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597
598template<class _CharT, class _Traits, class _Allocator>
599basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000600operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601
Shoaib Meenaiea363712017-07-29 02:54:41 +0000602_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
603
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000605class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606{
607protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000608 _LIBCPP_NORETURN void __throw_length_error() const;
609 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610};
611
612template <bool __b>
613void
614__basic_string_common<__b>::__throw_length_error() const
615{
Marshall Clow8fea1612016-08-25 15:09:01 +0000616 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617}
618
619template <bool __b>
620void
621__basic_string_common<__b>::__throw_out_of_range() const
622{
Marshall Clow8fea1612016-08-25 15:09:01 +0000623 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624}
625
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000626_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627
Marshall Clow039b2f02016-01-13 21:54:34 +0000628#ifdef _LIBCPP_NO_EXCEPTIONS
629template <class _Iter>
630struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
631#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
632template <class _Iter>
633struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
634#else
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500635template <class _Iter, bool = __is_cpp17_forward_iterator<_Iter>::value>
Marshall Clow039b2f02016-01-13 21:54:34 +0000636struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
Louis Dionne173f29e2019-05-29 16:01:36 +0000637 noexcept(++(declval<_Iter&>())) &&
638 is_nothrow_assignable<_Iter&, _Iter>::value &&
639 noexcept(declval<_Iter>() == declval<_Iter>()) &&
Marshall Clow039b2f02016-01-13 21:54:34 +0000640 noexcept(*declval<_Iter>())
641)) {};
642
Louis Dionne173f29e2019-05-29 16:01:36 +0000643template <class _Iter>
Marshall Clow039b2f02016-01-13 21:54:34 +0000644struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
645#endif
646
647
648template <class _Iter>
649struct __libcpp_string_gets_noexcept_iterator
650 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
651
Marshall Clow82513342016-09-24 22:45:42 +0000652template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500653struct __can_be_converted_to_string_view : public _BoolConstant<
654 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
655 !is_convertible<const _Tp&, const _CharT*>::value
656 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000657
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000658#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000659
660template <class _CharT, size_t = sizeof(_CharT)>
661struct __padding
662{
663 unsigned char __xx[sizeof(_CharT)-1];
664};
665
666template <class _CharT>
667struct __padding<_CharT, 1>
668{
669};
670
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000671#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000672
Richard Smith256954d2020-11-11 17:12:18 -0800673#ifndef _LIBCPP_NO_HAS_CHAR8_T
674typedef basic_string<char8_t> u8string;
675#endif
676
677#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
678typedef basic_string<char16_t> u16string;
679typedef basic_string<char32_t> u32string;
680#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
681
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000682template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800683class
684 _LIBCPP_TEMPLATE_VIS
685#ifndef _LIBCPP_NO_HAS_CHAR8_T
686 _LIBCPP_PREFERRED_NAME(u8string)
687#endif
688#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
689 _LIBCPP_PREFERRED_NAME(u16string)
690 _LIBCPP_PREFERRED_NAME(u32string)
691#endif
692 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693 : private __basic_string_common<true>
694{
695public:
696 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000697 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000699 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000701 typedef allocator_traits<allocator_type> __alloc_traits;
702 typedef typename __alloc_traits::size_type size_type;
703 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000704 typedef value_type& reference;
705 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000706 typedef typename __alloc_traits::pointer pointer;
707 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708
Marshall Clow79f33542018-03-21 00:36:05 +0000709 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
710 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
711 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
712 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000713 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000714 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000715 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000716
Howard Hinnantc51e1022010-05-11 19:42:16 +0000717 typedef __wrap_iter<pointer> iterator;
718 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000719 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
720 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721
722private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000723
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000724#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000725
726 struct __long
727 {
728 pointer __data_;
729 size_type __size_;
730 size_type __cap_;
731 };
732
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000733#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000734 static const size_type __short_mask = 0x01;
735 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000736#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000737 static const size_type __short_mask = 0x80;
738 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant68bf1812013-04-30 21:44:48 +0000739#endif // _LIBCPP_BIG_ENDIAN
740
741 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
742 (sizeof(__long) - 1)/sizeof(value_type) : 2};
743
744 struct __short
745 {
746 value_type __data_[__min_cap];
747 struct
748 : __padding<value_type>
749 {
750 unsigned char __size_;
751 };
752 };
753
754#else
755
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756 struct __long
757 {
758 size_type __cap_;
759 size_type __size_;
760 pointer __data_;
761 };
762
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000763#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000764 static const size_type __short_mask = 0x80;
765 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000766#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000767 static const size_type __short_mask = 0x01;
768 static const size_type __long_mask = 0x1ul;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000769#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
772 (sizeof(__long) - 1)/sizeof(value_type) : 2};
773
774 struct __short
775 {
776 union
777 {
778 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000779 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780 };
781 value_type __data_[__min_cap];
782 };
783
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000784#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000785
Howard Hinnant8ea98242013-08-23 17:37:05 +0000786 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787
Howard Hinnant8ea98242013-08-23 17:37:05 +0000788 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789
790 struct __raw
791 {
792 size_type __words[__n_words];
793 };
794
795 struct __rep
796 {
797 union
798 {
799 __long __l;
800 __short __s;
801 __raw __r;
802 };
803 };
804
805 __compressed_pair<__rep, allocator_type> __r_;
806
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200808 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809 static const size_type npos = -1;
810
Howard Hinnant3e276872011-06-03 18:40:47 +0000811 _LIBCPP_INLINE_VISIBILITY basic_string()
812 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000813
814 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
815#if _LIBCPP_STD_VER <= 14
816 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
817#else
818 _NOEXCEPT;
819#endif
820
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 basic_string(const basic_string& __str);
822 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000823
Eric Fiselierfc92be82017-04-19 00:28:44 +0000824#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000826 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000827#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000828 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000829#else
830 _NOEXCEPT;
831#endif
832
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000835#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000836
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500837 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000838 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500839 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000840 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
841 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400842# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000843 __get_db()->__insert_c(this);
844# endif
845 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000846
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500847 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000848 _LIBCPP_INLINE_VISIBILITY
849 basic_string(const _CharT* __s, const _Allocator& __a);
850
Howard Hinnantcf823322010-12-17 14:46:43 +0000851 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000852 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000853 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000854 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000855 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000856 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000857
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500858 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000859 _LIBCPP_INLINE_VISIBILITY
860 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
861
Marshall Clow83445802016-04-07 18:13:41 +0000862 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000863 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000864 _LIBCPP_INLINE_VISIBILITY
865 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000866 const _Allocator& __a = _Allocator());
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 && !__is_same_uncvref<_Tp, basic_string>::value> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000869 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000870 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500871 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000872
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500873 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
874 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000875 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
876 explicit basic_string(const _Tp& __t);
877
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500878 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 +0000879 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
880 explicit basic_string(const _Tp& __t, const allocator_type& __a);
881
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500882 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500885 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000888#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000889 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000890 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000891 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000892 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000893#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000895 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000897 _LIBCPP_INLINE_VISIBILITY
898 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
899
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000900 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000901
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500902 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 +0000903 basic_string& operator=(const _Tp& __t)
904 {__self_view __sv = __t; return assign(__sv);}
905
Eric Fiselierfc92be82017-04-19 00:28:44 +0000906#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000908 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000909 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000910 _LIBCPP_INLINE_VISIBILITY
911 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000913 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915
Louis Dionneba400782020-10-02 15:02:52 -0400916#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000917 _LIBCPP_INLINE_VISIBILITY
918 iterator begin() _NOEXCEPT
919 {return iterator(this, __get_pointer());}
920 _LIBCPP_INLINE_VISIBILITY
921 const_iterator begin() const _NOEXCEPT
922 {return const_iterator(this, __get_pointer());}
923 _LIBCPP_INLINE_VISIBILITY
924 iterator end() _NOEXCEPT
925 {return iterator(this, __get_pointer() + size());}
926 _LIBCPP_INLINE_VISIBILITY
927 const_iterator end() const _NOEXCEPT
928 {return const_iterator(this, __get_pointer() + size());}
929#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000930 _LIBCPP_INLINE_VISIBILITY
931 iterator begin() _NOEXCEPT
932 {return iterator(__get_pointer());}
933 _LIBCPP_INLINE_VISIBILITY
934 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000935 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000936 _LIBCPP_INLINE_VISIBILITY
937 iterator end() _NOEXCEPT
938 {return iterator(__get_pointer() + size());}
939 _LIBCPP_INLINE_VISIBILITY
940 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000941 {return const_iterator(__get_pointer() + size());}
Louis Dionneba400782020-10-02 15:02:52 -0400942#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000943 _LIBCPP_INLINE_VISIBILITY
944 reverse_iterator rbegin() _NOEXCEPT
945 {return reverse_iterator(end());}
946 _LIBCPP_INLINE_VISIBILITY
947 const_reverse_iterator rbegin() const _NOEXCEPT
948 {return const_reverse_iterator(end());}
949 _LIBCPP_INLINE_VISIBILITY
950 reverse_iterator rend() _NOEXCEPT
951 {return reverse_iterator(begin());}
952 _LIBCPP_INLINE_VISIBILITY
953 const_reverse_iterator rend() const _NOEXCEPT
954 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000956 _LIBCPP_INLINE_VISIBILITY
957 const_iterator cbegin() const _NOEXCEPT
958 {return begin();}
959 _LIBCPP_INLINE_VISIBILITY
960 const_iterator cend() const _NOEXCEPT
961 {return end();}
962 _LIBCPP_INLINE_VISIBILITY
963 const_reverse_iterator crbegin() const _NOEXCEPT
964 {return rbegin();}
965 _LIBCPP_INLINE_VISIBILITY
966 const_reverse_iterator crend() const _NOEXCEPT
967 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000969 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000971 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
972 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
973 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000974 {return (__is_long() ? __get_long_cap()
975 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976
977 void resize(size_type __n, value_type __c);
978 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
979
Marek Kurdejc9848142020-11-26 10:07:16 +0100980 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000981 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
982
Marek Kurdejc9848142020-11-26 10:07:16 +0100983 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
984 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000985 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100986 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000988 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000989 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
990 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000992 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
993 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994
995 const_reference at(size_type __n) const;
996 reference at(size_type __n);
997
998 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000999
1000 template <class _Tp>
1001 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001002 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001003 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001004 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1005 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001006 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001007 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001008 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001009 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001011#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001013#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014
Howard Hinnantcf823322010-12-17 14:46:43 +00001015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001017
1018 template <class _Tp>
1019 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001020 _EnableIf<
1021 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1022 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001023 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001024 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001025 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001026 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001027
Marshall Clow62953962016-10-03 23:40:48 +00001028 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001029 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001030 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001031 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001032 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1033 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001034 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001035 >
Marshall Clow82513342016-09-24 22:45:42 +00001036 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001037 basic_string& append(const value_type* __s, size_type __n);
1038 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001040
1041 _LIBCPP_INLINE_VISIBILITY
1042 void __append_default_init(size_type __n);
1043
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001044 template <class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001045 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1046 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001048 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001049 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001051 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001052 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001054 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001055 _LIBCPP_INLINE_VISIBILITY
1056 append(_InputIterator __first, _InputIterator __last) {
1057 const basic_string __temp (__first, __last, __alloc());
1058 append(__temp.data(), __temp.size());
1059 return *this;
1060 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001062 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001063 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001065 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001066 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001068 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001069 _LIBCPP_INLINE_VISIBILITY
1070 append(_ForwardIterator __first, _ForwardIterator __last) {
1071 return __append_forward_unsafe(__first, __last);
1072 }
1073
Eric Fiselierfc92be82017-04-19 00:28:44 +00001074#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001077#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078
1079 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001080 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001082 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1083 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1084 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1085 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086
Marshall Clowe46031a2018-07-02 18:41:15 +00001087 template <class _Tp>
1088 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001089 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001090 <
1091 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1092 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001093 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001094 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001095 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001096 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001097#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001098 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001099 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001100 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001101 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001102#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001103 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001104 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001105 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001107 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001108 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1109 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001110 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001111 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001112 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001113 basic_string& assign(const value_type* __s, size_type __n);
1114 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 basic_string& assign(size_type __n, value_type __c);
1116 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001117 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001118 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001120 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001121 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001123 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 assign(_InputIterator __first, _InputIterator __last);
1125 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001126 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001127 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001129 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001130 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001132 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001134#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001137#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138
Howard Hinnantcf823322010-12-17 14:46:43 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001141
1142 template <class _Tp>
1143 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001144 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001145 <
1146 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1147 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001148 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001149 insert(size_type __pos1, const _Tp& __t)
1150 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1151
Marshall Clow82513342016-09-24 22:45:42 +00001152 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001153 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001154 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001155 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001156 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001157 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001158 >
Marshall Clow82513342016-09-24 22:45:42 +00001159 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001160 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001161 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1162 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1164 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1167 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001168 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001169 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001171 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001172 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001174 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1176 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001177 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001178 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001180 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001181 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001183 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001185#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1188 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001189#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190
1191 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195 iterator erase(const_iterator __first, const_iterator __last);
1196
Howard Hinnantcf823322010-12-17 14:46:43 +00001197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001199
1200 template <class _Tp>
1201 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001202 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001203 <
1204 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1205 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001206 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001207 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 +00001208 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 +00001209 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001210 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001211 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001212 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001213 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001214 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001215 >
Marshall Clow82513342016-09-24 22:45:42 +00001216 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001217 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1218 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
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, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001222
1223 template <class _Tp>
1224 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001225 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001226 <
1227 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1228 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001229 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001230 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1231
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001233 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001235 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001237 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001239 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001240 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001242 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001244 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001245 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001246#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001248 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001250#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251
Howard Hinnantd17880b2013-06-28 16:59:19 +00001252 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1255
Howard Hinnantcf823322010-12-17 14:46:43 +00001256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001257 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001258#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001259 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001260#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001261 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001262 __is_nothrow_swappable<allocator_type>::value);
1263#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264
Howard Hinnantcf823322010-12-17 14:46:43 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001266 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001267 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001268 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001269#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001270 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001271 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001272#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273
Howard Hinnantcf823322010-12-17 14:46:43 +00001274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001275 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
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 find(const basic_string& __str, size_type __pos = 0) 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 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001288 size_type find(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 find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001291 size_type find(value_type __c, size_type __pos = 0) 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 rfind(const basic_string& __str, size_type __pos = npos) 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 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001304 size_type rfind(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 rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001307 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308
Howard Hinnantcf823322010-12-17 14:46:43 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001310 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001311
1312 template <class _Tp>
1313 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001314 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001315 <
1316 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1317 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001318 >
zoecarver1997e0a2021-02-05 11:54:47 -08001319 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001320 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001322 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001324 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
Howard Hinnantcf823322010-12-17 14:46:43 +00001326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001327 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001328
1329 template <class _Tp>
1330 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001331 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001332 <
1333 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1334 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001335 >
zoecarver1997e0a2021-02-05 11:54:47 -08001336 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001337 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001339 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001341 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342
Howard Hinnantcf823322010-12-17 14:46:43 +00001343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001344 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001345
1346 template <class _Tp>
1347 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001348 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001349 <
1350 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1351 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001352 >
zoecarver1997e0a2021-02-05 11:54:47 -08001353 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001354 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 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001356 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001357 _LIBCPP_INLINE_VISIBILITY
1358 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1359
1360 _LIBCPP_INLINE_VISIBILITY
1361 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001362
1363 template <class _Tp>
1364 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001365 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001366 <
1367 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1368 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001369 >
zoecarver1997e0a2021-02-05 11:54:47 -08001370 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001371 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 +00001372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001373 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001374 _LIBCPP_INLINE_VISIBILITY
1375 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1376
1377 _LIBCPP_INLINE_VISIBILITY
1378 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001379
1380 template <class _Tp>
1381 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001382 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001383 <
1384 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1385 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001386 >
zoecarver1997e0a2021-02-05 11:54:47 -08001387 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001388
1389 template <class _Tp>
1390 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001391 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001392 <
1393 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1394 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001395 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001396 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1397
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001400 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 +00001401
Marshall Clow82513342016-09-24 22:45:42 +00001402 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001403 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001404 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001405 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001406 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001407 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001408 >
Marshall Clow82513342016-09-24 22:45:42 +00001409 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 +00001410 int compare(const value_type* __s) const _NOEXCEPT;
1411 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1412 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413
Marshall Clow18c293b2017-12-04 20:11:38 +00001414#if _LIBCPP_STD_VER > 17
1415 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1416 bool starts_with(__self_view __sv) const _NOEXCEPT
1417 { return __self_view(data(), size()).starts_with(__sv); }
1418
1419 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1420 bool starts_with(value_type __c) const _NOEXCEPT
1421 { return !empty() && _Traits::eq(front(), __c); }
1422
1423 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1424 bool starts_with(const value_type* __s) const _NOEXCEPT
1425 { return starts_with(__self_view(__s)); }
1426
1427 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1428 bool ends_with(__self_view __sv) const _NOEXCEPT
1429 { return __self_view(data(), size()).ends_with( __sv); }
1430
1431 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1432 bool ends_with(value_type __c) const _NOEXCEPT
1433 { return !empty() && _Traits::eq(back(), __c); }
1434
1435 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1436 bool ends_with(const value_type* __s) const _NOEXCEPT
1437 { return ends_with(__self_view(__s)); }
1438#endif
1439
Wim Leflere023c3542021-01-19 14:33:30 -05001440#if _LIBCPP_STD_VER > 20
1441 constexpr _LIBCPP_INLINE_VISIBILITY
1442 bool contains(__self_view __sv) const noexcept
1443 { return __self_view(data(), size()).contains(__sv); }
1444
1445 constexpr _LIBCPP_INLINE_VISIBILITY
1446 bool contains(value_type __c) const noexcept
1447 { return __self_view(data(), size()).contains(__c); }
1448
1449 constexpr _LIBCPP_INLINE_VISIBILITY
1450 bool contains(const value_type* __s) const
1451 { return __self_view(data(), size()).contains(__s); }
1452#endif
1453
Howard Hinnantcf823322010-12-17 14:46:43 +00001454 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001455
Marshall Clowe60a7182018-05-29 17:04:37 +00001456 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001457
Marek Kurdejc9848142020-11-26 10:07:16 +01001458 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1459
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001460 _LIBCPP_INLINE_VISIBILITY
1461 bool __is_long() const _NOEXCEPT
1462 {return bool(__r_.first().__s.__size_ & __short_mask);}
1463
Louis Dionneba400782020-10-02 15:02:52 -04001464#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001465
1466 bool __dereferenceable(const const_iterator* __i) const;
1467 bool __decrementable(const const_iterator* __i) const;
1468 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1469 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1470
Louis Dionneba400782020-10-02 15:02:52 -04001471#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001472
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001474 _LIBCPP_INLINE_VISIBILITY
1475 allocator_type& __alloc() _NOEXCEPT
1476 {return __r_.second();}
1477 _LIBCPP_INLINE_VISIBILITY
1478 const allocator_type& __alloc() const _NOEXCEPT
1479 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001481#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001482
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001484 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001485# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001487# else
1488 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1489# endif
1490
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001491 _LIBCPP_INLINE_VISIBILITY
1492 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001493# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001495# else
1496 {return __r_.first().__s.__size_;}
1497# endif
1498
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001499#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001500
1501 _LIBCPP_INLINE_VISIBILITY
1502 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001503# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001504 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1505# else
1506 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1507# endif
1508
1509 _LIBCPP_INLINE_VISIBILITY
1510 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001511# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001512 {return __r_.first().__s.__size_;}
1513# else
1514 {return __r_.first().__s.__size_ >> 1;}
1515# endif
1516
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001517#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __set_long_size(size_type __s) _NOEXCEPT
1521 {__r_.first().__l.__size_ = __s;}
1522 _LIBCPP_INLINE_VISIBILITY
1523 size_type __get_long_size() const _NOEXCEPT
1524 {return __r_.first().__l.__size_;}
1525 _LIBCPP_INLINE_VISIBILITY
1526 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1528
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001529 _LIBCPP_INLINE_VISIBILITY
1530 void __set_long_cap(size_type __s) _NOEXCEPT
1531 {__r_.first().__l.__cap_ = __long_mask | __s;}
1532 _LIBCPP_INLINE_VISIBILITY
1533 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001534 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001536 _LIBCPP_INLINE_VISIBILITY
1537 void __set_long_pointer(pointer __p) _NOEXCEPT
1538 {__r_.first().__l.__data_ = __p;}
1539 _LIBCPP_INLINE_VISIBILITY
1540 pointer __get_long_pointer() _NOEXCEPT
1541 {return __r_.first().__l.__data_;}
1542 _LIBCPP_INLINE_VISIBILITY
1543 const_pointer __get_long_pointer() const _NOEXCEPT
1544 {return __r_.first().__l.__data_;}
1545 _LIBCPP_INLINE_VISIBILITY
1546 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001547 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001548 _LIBCPP_INLINE_VISIBILITY
1549 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001550 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001551 _LIBCPP_INLINE_VISIBILITY
1552 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001554 _LIBCPP_INLINE_VISIBILITY
1555 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1557
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001558 _LIBCPP_INLINE_VISIBILITY
1559 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 {
1561 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1562 for (unsigned __i = 0; __i < __n_words; ++__i)
1563 __a[__i] = 0;
1564 }
1565
1566 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001568 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001569 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001571 static _LIBCPP_INLINE_VISIBILITY
1572 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001573 {
1574 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1575 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1576 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1577 if (__guess == __min_cap) ++__guess;
1578 return __guess;
1579 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001581 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001582 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001583 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001584 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001585 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001587
Martijn Vels5e7c9752020-03-04 17:52:46 -05001588 // Slow path for the (inlined) copy constructor for 'long' strings.
1589 // Always externally instantiated and not inlined.
1590 // Requires that __s is zero terminated.
1591 // The main reason for this function to exist is because for unstable, we
1592 // want to allow inlining of the copy constructor. However, we don't want
1593 // to call the __init() functions as those are marked as inline which may
1594 // result in over-aggressive inlining by the compiler, where our aim is
1595 // to only inline the fast path code directly in the ctor.
1596 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1597
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001599 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001600 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001602 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1603 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 __init(_InputIterator __first, _InputIterator __last);
1605
1606 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001607 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001608 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001610 __is_cpp17_forward_iterator<_ForwardIterator>::value
1611 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 __init(_ForwardIterator __first, _ForwardIterator __last);
1613
1614 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001615 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1617 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001618 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619
Martijn Vels596e3de2020-02-26 15:55:49 -05001620 // __assign_no_alias is invoked for assignment operations where we
1621 // have proof that the input does not alias the current instance.
1622 // For example, operator=(basic_string) performs a 'self' check.
1623 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001624 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001625
Howard Hinnantcf823322010-12-17 14:46:43 +00001626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627 void __erase_to_end(size_type __pos);
1628
Martijn Velsa81fc792020-02-26 13:25:43 -05001629 // __erase_external_with_move is invoked for erase() invocations where
1630 // `n ~= npos`, likely requiring memory moves on the string data.
1631 void __erase_external_with_move(size_type __pos, size_type __n);
1632
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001633 _LIBCPP_INLINE_VISIBILITY
1634 void __copy_assign_alloc(const basic_string& __str)
1635 {__copy_assign_alloc(__str, integral_constant<bool,
1636 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1637
1638 _LIBCPP_INLINE_VISIBILITY
1639 void __copy_assign_alloc(const basic_string& __str, true_type)
1640 {
Marshall Clowf258c202017-01-31 03:40:52 +00001641 if (__alloc() == __str.__alloc())
1642 __alloc() = __str.__alloc();
1643 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001644 {
Marshall Clowf258c202017-01-31 03:40:52 +00001645 if (!__str.__is_long())
1646 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001647 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001648 __alloc() = __str.__alloc();
1649 }
1650 else
1651 {
1652 allocator_type __a = __str.__alloc();
1653 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001654 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001655 __alloc() = _VSTD::move(__a);
1656 __set_long_pointer(__p);
1657 __set_long_cap(__str.__get_long_cap());
1658 __set_long_size(__str.size());
1659 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001660 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001661 }
1662
1663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001664 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001665 {}
1666
Eric Fiselierfc92be82017-04-19 00:28:44 +00001667#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001668 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001669 void __move_assign(basic_string& __str, false_type)
1670 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001672 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001673#if _LIBCPP_STD_VER > 14
1674 _NOEXCEPT;
1675#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001676 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001677#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001678#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001679
1680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001681 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001682 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001683 _NOEXCEPT_(
1684 !__alloc_traits::propagate_on_container_move_assignment::value ||
1685 is_nothrow_move_assignable<allocator_type>::value)
1686 {__move_assign_alloc(__str, integral_constant<bool,
1687 __alloc_traits::propagate_on_container_move_assignment::value>());}
1688
1689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001690 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001691 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1692 {
1693 __alloc() = _VSTD::move(__c.__alloc());
1694 }
1695
1696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001697 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001698 _NOEXCEPT
1699 {}
1700
Martijn Velsda7d94f2020-06-19 14:24:03 -04001701 basic_string& __assign_external(const value_type* __s);
1702 basic_string& __assign_external(const value_type* __s, size_type __n);
1703
1704 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1705 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1706 pointer __p = __is_long()
1707 ? (__set_long_size(__n), __get_long_pointer())
1708 : (__set_short_size(__n), __get_short_pointer());
1709 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1710 traits_type::assign(__p[__n], value_type());
1711 return *this;
1712 }
1713
Howard Hinnantcf823322010-12-17 14:46:43 +00001714 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1715 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716
1717 friend basic_string operator+<>(const basic_string&, const basic_string&);
1718 friend basic_string operator+<>(const value_type*, const basic_string&);
1719 friend basic_string operator+<>(value_type, const basic_string&);
1720 friend basic_string operator+<>(const basic_string&, const value_type*);
1721 friend basic_string operator+<>(const basic_string&, value_type);
1722};
1723
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001724// These declarations must appear before any functions are implicitly used
1725// so that they have the correct visibility specifier.
1726#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1727_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1728_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1729#else
1730_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1731_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1732#endif
1733
1734
Marshall Clowa0563332018-02-08 06:34:03 +00001735#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1736template<class _InputIterator,
1737 class _CharT = typename iterator_traits<_InputIterator>::value_type,
1738 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001739 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1740 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001741 >
1742basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1743 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001744
1745template<class _CharT,
1746 class _Traits,
1747 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001748 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001749 >
1750explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1751 -> basic_string<_CharT, _Traits, _Allocator>;
1752
1753template<class _CharT,
1754 class _Traits,
1755 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001756 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001757 class _Sz = typename allocator_traits<_Allocator>::size_type
1758 >
1759basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1760 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001761#endif
1762
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001764inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765void
1766basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1767{
Louis Dionneba400782020-10-02 15:02:52 -04001768#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001769 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001770#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771}
1772
1773template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001774inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775void
Howard Hinnant28b24882011-12-01 20:21:04 +00001776basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Louis Dionneba400782020-10-02 15:02:52 -04001777#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant28b24882011-12-01 20:21:04 +00001778 __pos
1779#endif
1780 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781{
Louis Dionneba400782020-10-02 15:02:52 -04001782#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001783 __c_node* __c = __get_db()->__find_c_and_lock(this);
1784 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001786 const_pointer __new_last = __get_pointer() + __pos;
1787 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001789 --__p;
1790 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1791 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001793 (*__p)->__c_ = nullptr;
1794 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001795 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001798 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 }
Louis Dionneba400782020-10-02 15:02:52 -04001800#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801}
1802
1803template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001804inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001805basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001806 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001807 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808{
Louis Dionneba400782020-10-02 15:02:52 -04001809#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001810 __get_db()->__insert_c(this);
1811#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 __zero();
1813}
1814
1815template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001816inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001818#if _LIBCPP_STD_VER <= 14
1819 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1820#else
1821 _NOEXCEPT
1822#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001823: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824{
Louis Dionneba400782020-10-02 15:02:52 -04001825#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001826 __get_db()->__insert_c(this);
1827#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 __zero();
1829}
1830
1831template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001832void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1833 size_type __sz,
1834 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835{
1836 if (__reserve > max_size())
1837 this->__throw_length_error();
1838 pointer __p;
1839 if (__reserve < __min_cap)
1840 {
1841 __set_short_size(__sz);
1842 __p = __get_short_pointer();
1843 }
1844 else
1845 {
1846 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001847 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848 __set_long_pointer(__p);
1849 __set_long_cap(__cap+1);
1850 __set_long_size(__sz);
1851 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001852 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 traits_type::assign(__p[__sz], value_type());
1854}
1855
1856template <class _CharT, class _Traits, class _Allocator>
1857void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001858basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859{
1860 if (__sz > max_size())
1861 this->__throw_length_error();
1862 pointer __p;
1863 if (__sz < __min_cap)
1864 {
1865 __set_short_size(__sz);
1866 __p = __get_short_pointer();
1867 }
1868 else
1869 {
1870 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001871 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872 __set_long_pointer(__p);
1873 __set_long_cap(__cap+1);
1874 __set_long_size(__sz);
1875 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001876 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 traits_type::assign(__p[__sz], value_type());
1878}
1879
1880template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001881template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001882basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001883 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001885 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001887#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001888 __get_db()->__insert_c(this);
1889#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890}
1891
1892template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001893inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001894basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001895 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001897 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001899#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001900 __get_db()->__insert_c(this);
1901#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902}
1903
1904template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001905inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001906basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001907 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001909 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001911#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001912 __get_db()->__insert_c(this);
1913#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914}
1915
1916template <class _CharT, class _Traits, class _Allocator>
1917basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001918 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919{
1920 if (!__str.__is_long())
1921 __r_.first().__r = __str.__r_.first().__r;
1922 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001923 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1924 __str.__get_long_size());
1925
Louis Dionneba400782020-10-02 15:02:52 -04001926#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001927 __get_db()->__insert_c(this);
1928#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929}
1930
1931template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001932basic_string<_CharT, _Traits, _Allocator>::basic_string(
1933 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001934 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935{
1936 if (!__str.__is_long())
1937 __r_.first().__r = __str.__r_.first().__r;
1938 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001939 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1940 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001941#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001942 __get_db()->__insert_c(this);
1943#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944}
1945
Martijn Vels5e7c9752020-03-04 17:52:46 -05001946template <class _CharT, class _Traits, class _Allocator>
1947void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1948 const value_type* __s, size_type __sz) {
1949 pointer __p;
1950 if (__sz < __min_cap) {
1951 __p = __get_short_pointer();
1952 __set_short_size(__sz);
1953 } else {
1954 if (__sz > max_size())
1955 this->__throw_length_error();
1956 size_t __cap = __recommend(__sz);
1957 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1958 __set_long_pointer(__p);
1959 __set_long_cap(__cap + 1);
1960 __set_long_size(__sz);
1961 }
1962 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1963}
1964
Eric Fiselierfc92be82017-04-19 00:28:44 +00001965#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966
1967template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001968inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001969basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001970#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001971 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001972#else
1973 _NOEXCEPT
1974#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001975 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976{
1977 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001978#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001979 __get_db()->__insert_c(this);
1980 if (__is_long())
1981 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982#endif
1983}
1984
1985template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001986inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001988 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989{
Marshall Clowd2d24692014-07-17 15:32:20 +00001990 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001991 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001992 else
1993 {
1994 __r_.first().__r = __str.__r_.first().__r;
1995 __str.__zero();
1996 }
Louis Dionneba400782020-10-02 15:02:52 -04001997#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001998 __get_db()->__insert_c(this);
1999 if (__is_long())
2000 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001#endif
2002}
2003
Eric Fiselierfc92be82017-04-19 00:28:44 +00002004#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005
2006template <class _CharT, class _Traits, class _Allocator>
2007void
2008basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2009{
2010 if (__n > max_size())
2011 this->__throw_length_error();
2012 pointer __p;
2013 if (__n < __min_cap)
2014 {
2015 __set_short_size(__n);
2016 __p = __get_short_pointer();
2017 }
2018 else
2019 {
2020 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002021 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022 __set_long_pointer(__p);
2023 __set_long_cap(__cap+1);
2024 __set_long_size(__n);
2025 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002026 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027 traits_type::assign(__p[__n], value_type());
2028}
2029
2030template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002031inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002032basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002033 : __r_(__default_init_tag(), __default_init_tag())
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
2041template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002042template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002043basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002044 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045{
2046 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002047#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002048 __get_db()->__insert_c(this);
2049#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002050}
2051
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002053basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2054 size_type __pos, size_type __n,
2055 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002056 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057{
2058 size_type __str_sz = __str.size();
2059 if (__pos > __str_sz)
2060 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002061 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002062#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002063 __get_db()->__insert_c(this);
2064#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065}
2066
2067template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002068inline
Marshall Clow83445802016-04-07 18:13:41 +00002069basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002070 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002071 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002072{
2073 size_type __str_sz = __str.size();
2074 if (__pos > __str_sz)
2075 this->__throw_out_of_range();
2076 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002077#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002078 __get_db()->__insert_c(this);
2079#endif
2080}
2081
2082template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002083template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002084basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002085 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002086 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002087{
Marshall Clowe46031a2018-07-02 18:41:15 +00002088 __self_view __sv0 = __t;
2089 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002090 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002091#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002092 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002093#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002094}
2095
2096template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002097template <class _Tp, class>
2098basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002099 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002100{
Marshall Clowe46031a2018-07-02 18:41:15 +00002101 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002102 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002103#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002104 __get_db()->__insert_c(this);
2105#endif
2106}
2107
2108template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002109template <class _Tp, class>
2110basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002111 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002112{
Marshall Clowe46031a2018-07-02 18:41:15 +00002113 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002114 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002115#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002116 __get_db()->__insert_c(this);
2117#endif
2118}
2119
2120template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002122_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002124 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2125>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2127{
2128 __zero();
2129#ifndef _LIBCPP_NO_EXCEPTIONS
2130 try
2131 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002132#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 for (; __first != __last; ++__first)
2134 push_back(*__first);
2135#ifndef _LIBCPP_NO_EXCEPTIONS
2136 }
2137 catch (...)
2138 {
2139 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002140 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141 throw;
2142 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002143#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144}
2145
2146template <class _CharT, class _Traits, class _Allocator>
2147template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002148_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002150 __is_cpp17_forward_iterator<_ForwardIterator>::value
2151>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2153{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002154 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155 if (__sz > max_size())
2156 this->__throw_length_error();
2157 pointer __p;
2158 if (__sz < __min_cap)
2159 {
2160 __set_short_size(__sz);
2161 __p = __get_short_pointer();
2162 }
2163 else
2164 {
2165 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002166 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 __set_long_pointer(__p);
2168 __set_long_cap(__cap+1);
2169 __set_long_size(__sz);
2170 }
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002171 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 traits_type::assign(*__p, *__first);
2173 traits_type::assign(*__p, value_type());
2174}
2175
2176template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002177template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002178inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002180 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181{
2182 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002183#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002184 __get_db()->__insert_c(this);
2185#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186}
2187
2188template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002189template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002190inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2192 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002193 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194{
2195 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002196#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002197 __get_db()->__insert_c(this);
2198#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199}
2200
Eric Fiselierfc92be82017-04-19 00:28:44 +00002201#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002202
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002204inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002205basic_string<_CharT, _Traits, _Allocator>::basic_string(
2206 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002207 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208{
2209 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002210#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002211 __get_db()->__insert_c(this);
2212#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213}
2214
2215template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002216inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002217
Eric Fiselier812882b2017-02-17 01:17:10 +00002218basic_string<_CharT, _Traits, _Allocator>::basic_string(
2219 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002220 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221{
2222 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002223#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002224 __get_db()->__insert_c(this);
2225#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226}
2227
Eric Fiselierfc92be82017-04-19 00:28:44 +00002228#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002229
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2232{
Louis Dionneba400782020-10-02 15:02:52 -04002233#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002234 __get_db()->__erase_c(this);
2235#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002237 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238}
2239
2240template <class _CharT, class _Traits, class _Allocator>
2241void
2242basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2243 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002244 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 +00002245{
2246 size_type __ms = max_size();
2247 if (__delta_cap > __ms - __old_cap - 1)
2248 this->__throw_length_error();
2249 pointer __old_p = __get_pointer();
2250 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002251 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002253 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 __invalidate_all_iterators();
2255 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002256 traits_type::copy(_VSTD::__to_address(__p),
2257 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002259 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2261 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002262 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2263 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002265 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 __set_long_pointer(__p);
2267 __set_long_cap(__cap+1);
2268 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2269 __set_long_size(__old_sz);
2270 traits_type::assign(__p[__old_sz], value_type());
2271}
2272
2273template <class _CharT, class _Traits, class _Allocator>
2274void
2275basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2276 size_type __n_copy, size_type __n_del, size_type __n_add)
2277{
2278 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002279 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280 this->__throw_length_error();
2281 pointer __old_p = __get_pointer();
2282 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002283 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002285 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286 __invalidate_all_iterators();
2287 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002288 traits_type::copy(_VSTD::__to_address(__p),
2289 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2291 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002292 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2293 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002294 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002296 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 __set_long_pointer(__p);
2298 __set_long_cap(__cap+1);
2299}
2300
2301// assign
2302
2303template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002304template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002305basic_string<_CharT, _Traits, _Allocator>&
2306basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002307 const value_type* __s, size_type __n) {
2308 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2309 if (__n < __cap) {
2310 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2311 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2312 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2313 traits_type::assign(__p[__n], value_type());
2314 __invalidate_iterators_past(__n);
2315 } else {
2316 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2317 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2318 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002319 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002320}
2321
2322template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002324basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2325 const value_type* __s, size_type __n) {
2326 size_type __cap = capacity();
2327 if (__cap >= __n) {
2328 value_type* __p = _VSTD::__to_address(__get_pointer());
2329 traits_type::move(__p, __s, __n);
2330 traits_type::assign(__p[__n], value_type());
2331 __set_size(__n);
2332 __invalidate_iterators_past(__n);
2333 } else {
2334 size_type __sz = size();
2335 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2336 }
2337 return *this;
2338}
2339
2340template <class _CharT, class _Traits, class _Allocator>
2341basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002342basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002344 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002345 return (_LIBCPP_BUILTIN_CONSTANT_P(__n) && __n < __min_cap)
2346 ? __assign_short(__s, __n)
2347 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348}
2349
2350template <class _CharT, class _Traits, class _Allocator>
2351basic_string<_CharT, _Traits, _Allocator>&
2352basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2353{
2354 size_type __cap = capacity();
2355 if (__cap < __n)
2356 {
2357 size_type __sz = size();
2358 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2359 }
2360 else
2361 __invalidate_iterators_past(__n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002362 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363 traits_type::assign(__p, __n, __c);
2364 traits_type::assign(__p[__n], value_type());
2365 __set_size(__n);
2366 return *this;
2367}
2368
2369template <class _CharT, class _Traits, class _Allocator>
2370basic_string<_CharT, _Traits, _Allocator>&
2371basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2372{
2373 pointer __p;
2374 if (__is_long())
2375 {
2376 __p = __get_long_pointer();
2377 __set_long_size(1);
2378 }
2379 else
2380 {
2381 __p = __get_short_pointer();
2382 __set_short_size(1);
2383 }
2384 traits_type::assign(*__p, __c);
2385 traits_type::assign(*++__p, value_type());
2386 __invalidate_iterators_past(1);
2387 return *this;
2388}
2389
2390template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002391basic_string<_CharT, _Traits, _Allocator>&
2392basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2393{
Martijn Vels596e3de2020-02-26 15:55:49 -05002394 if (this != &__str) {
2395 __copy_assign_alloc(__str);
2396 if (!__is_long()) {
2397 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002398 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002399 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002400 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002401 }
2402 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002403 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002404 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002405 }
2406 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002407}
2408
Eric Fiselierfc92be82017-04-19 00:28:44 +00002409#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002410
2411template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002412inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002413void
2414basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002415 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002416{
2417 if (__alloc() != __str.__alloc())
2418 assign(__str);
2419 else
2420 __move_assign(__str, true_type());
2421}
2422
2423template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002424inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002425void
2426basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002427#if _LIBCPP_STD_VER > 14
2428 _NOEXCEPT
2429#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002430 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002431#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002432{
marshall2ed41622019-11-27 07:13:00 -08002433 if (__is_long()) {
2434 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2435 __get_long_cap());
2436#if _LIBCPP_STD_VER <= 14
2437 if (!is_nothrow_move_assignable<allocator_type>::value) {
2438 __set_short_size(0);
2439 traits_type::assign(__get_short_pointer()[0], value_type());
2440 }
2441#endif
2442 }
2443 __move_assign_alloc(__str);
2444 __r_.first() = __str.__r_.first();
2445 __str.__set_short_size(0);
2446 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002447}
2448
2449template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002450inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002451basic_string<_CharT, _Traits, _Allocator>&
2452basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002453 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002454{
2455 __move_assign(__str, integral_constant<bool,
2456 __alloc_traits::propagate_on_container_move_assignment::value>());
2457 return *this;
2458}
2459
2460#endif
2461
2462template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002464_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002466 __is_exactly_cpp17_input_iterator <_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002467 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002469>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2471{
Marshall Clow958362f2016-09-05 01:54:30 +00002472 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002473 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002474 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475}
2476
2477template <class _CharT, class _Traits, class _Allocator>
2478template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002479_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002481 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002482 && __libcpp_string_gets_noexcept_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 Hinnantb1ad5a82011-06-30 21:18:19 +00002487 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488 size_type __cap = capacity();
2489 if (__cap < __n)
2490 {
2491 size_type __sz = size();
2492 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2493 }
2494 else
2495 __invalidate_iterators_past(__n);
2496 pointer __p = __get_pointer();
2497 for (; __first != __last; ++__first, ++__p)
2498 traits_type::assign(*__p, *__first);
2499 traits_type::assign(*__p, value_type());
2500 __set_size(__n);
2501 return *this;
2502}
2503
2504template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505basic_string<_CharT, _Traits, _Allocator>&
2506basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2507{
2508 size_type __sz = __str.size();
2509 if (__pos > __sz)
2510 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002511 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512}
2513
2514template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002515template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002516_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002517<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002518 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2519 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002520 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002521>
Marshall Clow82513342016-09-24 22:45:42 +00002522basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002523{
Marshall Clow82513342016-09-24 22:45:42 +00002524 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002525 size_type __sz = __sv.size();
2526 if (__pos > __sz)
2527 this->__throw_out_of_range();
2528 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2529}
2530
2531
2532template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002534basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2535 return __assign_external(__s, traits_type::length(__s));
2536}
2537
2538template <class _CharT, class _Traits, class _Allocator>
2539basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002540basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002542 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002543 return _LIBCPP_BUILTIN_CONSTANT_P(*__s)
2544 ? (traits_type::length(__s) < __min_cap
2545 ? __assign_short(__s, traits_type::length(__s))
2546 : __assign_external(__s, traits_type::length(__s)))
2547 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549// append
2550
2551template <class _CharT, class _Traits, class _Allocator>
2552basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002553basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002555 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556 size_type __cap = capacity();
2557 size_type __sz = size();
2558 if (__cap - __sz >= __n)
2559 {
2560 if (__n)
2561 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002562 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563 traits_type::copy(__p + __sz, __s, __n);
2564 __sz += __n;
2565 __set_size(__sz);
2566 traits_type::assign(__p[__sz], value_type());
2567 }
2568 }
2569 else
2570 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2571 return *this;
2572}
2573
2574template <class _CharT, class _Traits, class _Allocator>
2575basic_string<_CharT, _Traits, _Allocator>&
2576basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2577{
2578 if (__n)
2579 {
2580 size_type __cap = capacity();
2581 size_type __sz = size();
2582 if (__cap - __sz < __n)
2583 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2584 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002585 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586 __sz += __n;
2587 __set_size(__sz);
2588 traits_type::assign(__p[__sz], value_type());
2589 }
2590 return *this;
2591}
2592
2593template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002594inline void
2595basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2596{
2597 if (__n)
2598 {
2599 size_type __cap = capacity();
2600 size_type __sz = size();
2601 if (__cap - __sz < __n)
2602 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2603 pointer __p = __get_pointer();
2604 __sz += __n;
2605 __set_size(__sz);
2606 traits_type::assign(__p[__sz], value_type());
2607 }
2608}
2609
2610template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611void
2612basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2613{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002614 bool __is_short = !__is_long();
2615 size_type __cap;
2616 size_type __sz;
2617 if (__is_short)
2618 {
2619 __cap = __min_cap - 1;
2620 __sz = __get_short_size();
2621 }
2622 else
2623 {
2624 __cap = __get_long_cap() - 1;
2625 __sz = __get_long_size();
2626 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002628 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002630 __is_short = !__is_long();
2631 }
2632 pointer __p;
2633 if (__is_short)
2634 {
2635 __p = __get_short_pointer() + __sz;
2636 __set_short_size(__sz+1);
2637 }
2638 else
2639 {
2640 __p = __get_long_pointer() + __sz;
2641 __set_long_size(__sz+1);
2642 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 traits_type::assign(*__p, __c);
2644 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645}
2646
Marshall Clow6ebbf662016-09-07 03:32:06 +00002647template <class _Tp>
Marshall Clow958362f2016-09-05 01:54:30 +00002648bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2649{
2650 return __first <= __p && __p < __last;
2651}
2652
Marshall Clow6ebbf662016-09-07 03:32:06 +00002653template <class _Tp1, class _Tp2>
Eric Fiselier6003c772016-12-23 23:37:52 +00002654bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clow6ebbf662016-09-07 03:32:06 +00002655{
2656 return false;
2657}
2658
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659template <class _CharT, class _Traits, class _Allocator>
2660template<class _ForwardIterator>
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002661basic_string<_CharT, _Traits, _Allocator>&
2662basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2663 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664{
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002665 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002666 "function requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667 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 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002672 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2673 _CharRef __tmp_ref = *__first;
2674 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002675 {
2676 const basic_string __temp (__first, __last, __alloc());
2677 append(__temp.data(), __temp.size());
2678 }
Louis Dionne173f29e2019-05-29 16:01:36 +00002679 else
Marshall Clow958362f2016-09-05 01:54:30 +00002680 {
2681 if (__cap - __sz < __n)
2682 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2683 pointer __p = __get_pointer() + __sz;
2684 for (; __first != __last; ++__p, ++__first)
2685 traits_type::assign(*__p, *__first);
2686 traits_type::assign(*__p, value_type());
2687 __set_size(__sz + __n);
2688 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 }
2690 return *this;
2691}
2692
2693template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002694inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695basic_string<_CharT, _Traits, _Allocator>&
2696basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2697{
2698 return append(__str.data(), __str.size());
2699}
2700
2701template <class _CharT, class _Traits, class _Allocator>
2702basic_string<_CharT, _Traits, _Allocator>&
2703basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2704{
2705 size_type __sz = __str.size();
2706 if (__pos > __sz)
2707 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002708 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709}
2710
2711template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002712template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002713 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002714 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002715 __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 +00002716 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002717 >
Marshall Clow82513342016-09-24 22:45:42 +00002718basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002719{
Marshall Clow82513342016-09-24 22:45:42 +00002720 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002721 size_type __sz = __sv.size();
2722 if (__pos > __sz)
2723 this->__throw_out_of_range();
2724 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2725}
2726
2727template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002729basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002731 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732 return append(__s, traits_type::length(__s));
2733}
2734
2735// insert
2736
2737template <class _CharT, class _Traits, class _Allocator>
2738basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002739basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002741 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 size_type __sz = size();
2743 if (__pos > __sz)
2744 this->__throw_out_of_range();
2745 size_type __cap = capacity();
2746 if (__cap - __sz >= __n)
2747 {
2748 if (__n)
2749 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002750 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751 size_type __n_move = __sz - __pos;
2752 if (__n_move != 0)
2753 {
2754 if (__p + __pos <= __s && __s < __p + __sz)
2755 __s += __n;
2756 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2757 }
2758 traits_type::move(__p + __pos, __s, __n);
2759 __sz += __n;
2760 __set_size(__sz);
2761 traits_type::assign(__p[__sz], value_type());
2762 }
2763 }
2764 else
2765 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2766 return *this;
2767}
2768
2769template <class _CharT, class _Traits, class _Allocator>
2770basic_string<_CharT, _Traits, _Allocator>&
2771basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2772{
2773 size_type __sz = size();
2774 if (__pos > __sz)
2775 this->__throw_out_of_range();
2776 if (__n)
2777 {
2778 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002779 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780 if (__cap - __sz >= __n)
2781 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002782 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 size_type __n_move = __sz - __pos;
2784 if (__n_move != 0)
2785 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2786 }
2787 else
2788 {
2789 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002790 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 }
2792 traits_type::assign(__p + __pos, __n, __c);
2793 __sz += __n;
2794 __set_size(__sz);
2795 traits_type::assign(__p[__sz], value_type());
2796 }
2797 return *this;
2798}
2799
2800template <class _CharT, class _Traits, class _Allocator>
2801template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002802_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002804 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002805 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2806 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002807>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2809{
Louis Dionneba400782020-10-02 15:02:52 -04002810#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002811 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2812 "string::insert(iterator, range) called with an iterator not"
2813 " referring to this string");
2814#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002815 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002816 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817}
2818
2819template <class _CharT, class _Traits, class _Allocator>
2820template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002821_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002823 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002824 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002826>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2828{
Louis Dionneba400782020-10-02 15:02:52 -04002829#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002830 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2831 "string::insert(iterator, range) called with an iterator not"
2832 " referring to this string");
2833#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002835 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 if (__n)
2837 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002838 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2839 _CharRef __tmp_char = *__first;
2840 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002841 {
2842 const basic_string __temp(__first, __last, __alloc());
2843 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2844 }
2845
2846 size_type __sz = size();
2847 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002848 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 if (__cap - __sz >= __n)
2850 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002851 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852 size_type __n_move = __sz - __ip;
2853 if (__n_move != 0)
2854 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2855 }
2856 else
2857 {
2858 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002859 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 }
2861 __sz += __n;
2862 __set_size(__sz);
2863 traits_type::assign(__p[__sz], value_type());
2864 for (__p += __ip; __first != __last; ++__p, ++__first)
2865 traits_type::assign(*__p, *__first);
2866 }
2867 return begin() + __ip;
2868}
2869
2870template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002871inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872basic_string<_CharT, _Traits, _Allocator>&
2873basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2874{
2875 return insert(__pos1, __str.data(), __str.size());
2876}
2877
2878template <class _CharT, class _Traits, class _Allocator>
2879basic_string<_CharT, _Traits, _Allocator>&
2880basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2881 size_type __pos2, size_type __n)
2882{
2883 size_type __str_sz = __str.size();
2884 if (__pos2 > __str_sz)
2885 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002886 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887}
2888
2889template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002890template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002891_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002892<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002893 __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 +00002894 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002895>
Marshall Clow82513342016-09-24 22:45:42 +00002896basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002897 size_type __pos2, size_type __n)
2898{
Marshall Clow82513342016-09-24 22:45:42 +00002899 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002900 size_type __str_sz = __sv.size();
2901 if (__pos2 > __str_sz)
2902 this->__throw_out_of_range();
2903 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2904}
2905
2906template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002908basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002910 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 return insert(__pos, __s, traits_type::length(__s));
2912}
2913
2914template <class _CharT, class _Traits, class _Allocator>
2915typename basic_string<_CharT, _Traits, _Allocator>::iterator
2916basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2917{
2918 size_type __ip = static_cast<size_type>(__pos - begin());
2919 size_type __sz = size();
2920 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002921 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922 if (__cap == __sz)
2923 {
2924 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002925 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002926 }
2927 else
2928 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002929 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930 size_type __n_move = __sz - __ip;
2931 if (__n_move != 0)
2932 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2933 }
2934 traits_type::assign(__p[__ip], __c);
2935 traits_type::assign(__p[++__sz], value_type());
2936 __set_size(__sz);
2937 return begin() + static_cast<difference_type>(__ip);
2938}
2939
2940template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002941inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942typename basic_string<_CharT, _Traits, _Allocator>::iterator
2943basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2944{
Louis Dionneba400782020-10-02 15:02:52 -04002945#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002946 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2947 "string::insert(iterator, n, value) called with an iterator not"
2948 " referring to this string");
2949#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950 difference_type __p = __pos - begin();
2951 insert(static_cast<size_type>(__p), __n, __c);
2952 return begin() + __p;
2953}
2954
2955// replace
2956
2957template <class _CharT, class _Traits, class _Allocator>
2958basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002959basic_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 +00002960 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002962 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 size_type __sz = size();
2964 if (__pos > __sz)
2965 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002966 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967 size_type __cap = capacity();
2968 if (__cap - __sz + __n1 >= __n2)
2969 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002970 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 if (__n1 != __n2)
2972 {
2973 size_type __n_move = __sz - __pos - __n1;
2974 if (__n_move != 0)
2975 {
2976 if (__n1 > __n2)
2977 {
2978 traits_type::move(__p + __pos, __s, __n2);
2979 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2980 goto __finish;
2981 }
2982 if (__p + __pos < __s && __s < __p + __sz)
2983 {
2984 if (__p + __pos + __n1 <= __s)
2985 __s += __n2 - __n1;
2986 else // __p + __pos < __s < __p + __pos + __n1
2987 {
2988 traits_type::move(__p + __pos, __s, __n1);
2989 __pos += __n1;
2990 __s += __n2;
2991 __n2 -= __n1;
2992 __n1 = 0;
2993 }
2994 }
2995 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2996 }
2997 }
2998 traits_type::move(__p + __pos, __s, __n2);
2999__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05003000// __sz += __n2 - __n1; in this and the below function below can cause unsigned
3001// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002 __sz += __n2 - __n1;
3003 __set_size(__sz);
3004 __invalidate_iterators_past(__sz);
3005 traits_type::assign(__p[__sz], value_type());
3006 }
3007 else
3008 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3009 return *this;
3010}
3011
3012template <class _CharT, class _Traits, class _Allocator>
3013basic_string<_CharT, _Traits, _Allocator>&
3014basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00003015 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003016{
3017 size_type __sz = size();
3018 if (__pos > __sz)
3019 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003020 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003022 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023 if (__cap - __sz + __n1 >= __n2)
3024 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003025 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003026 if (__n1 != __n2)
3027 {
3028 size_type __n_move = __sz - __pos - __n1;
3029 if (__n_move != 0)
3030 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3031 }
3032 }
3033 else
3034 {
3035 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003036 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037 }
3038 traits_type::assign(__p + __pos, __n2, __c);
3039 __sz += __n2 - __n1;
3040 __set_size(__sz);
3041 __invalidate_iterators_past(__sz);
3042 traits_type::assign(__p[__sz], value_type());
3043 return *this;
3044}
3045
3046template <class _CharT, class _Traits, class _Allocator>
3047template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003048_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003050 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003052>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003053basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054 _InputIterator __j1, _InputIterator __j2)
3055{
Marshall Clow958362f2016-09-05 01:54:30 +00003056 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003057 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058}
3059
3060template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003061inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062basic_string<_CharT, _Traits, _Allocator>&
3063basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3064{
3065 return replace(__pos1, __n1, __str.data(), __str.size());
3066}
3067
3068template <class _CharT, class _Traits, class _Allocator>
3069basic_string<_CharT, _Traits, _Allocator>&
3070basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3071 size_type __pos2, size_type __n2)
3072{
3073 size_type __str_sz = __str.size();
3074 if (__pos2 > __str_sz)
3075 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003076 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077}
3078
3079template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003080template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003081_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003082<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003083 __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 +00003084 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003085>
Marshall Clow82513342016-09-24 22:45:42 +00003086basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003087 size_type __pos2, size_type __n2)
3088{
Marshall Clow82513342016-09-24 22:45:42 +00003089 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003090 size_type __str_sz = __sv.size();
3091 if (__pos2 > __str_sz)
3092 this->__throw_out_of_range();
3093 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3094}
3095
3096template <class _CharT, class _Traits, class _Allocator>
3097basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003098basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003100 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 return replace(__pos, __n1, __s, traits_type::length(__s));
3102}
3103
3104template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003105inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003107basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108{
3109 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3110 __str.data(), __str.size());
3111}
3112
3113template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003114inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003116basic_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 +00003117{
3118 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3119}
3120
3121template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003122inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003124basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125{
3126 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3127}
3128
3129template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003130inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003132basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133{
3134 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3135}
3136
3137// erase
3138
Martijn Velsa81fc792020-02-26 13:25:43 -05003139// 'externally instantiated' erase() implementation, called when __n != npos.
3140// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003142void
3143basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3144 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 if (__n)
3147 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003148 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003149 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003150 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 size_type __n_move = __sz - __pos - __n;
3152 if (__n_move != 0)
3153 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3154 __sz -= __n;
3155 __set_size(__sz);
3156 __invalidate_iterators_past(__sz);
3157 traits_type::assign(__p[__sz], value_type());
3158 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003159}
3160
3161template <class _CharT, class _Traits, class _Allocator>
3162basic_string<_CharT, _Traits, _Allocator>&
3163basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3164 size_type __n) {
3165 if (__pos > size()) this->__throw_out_of_range();
3166 if (__n == npos) {
3167 __erase_to_end(__pos);
3168 } else {
3169 __erase_external_with_move(__pos, __n);
3170 }
3171 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172}
3173
3174template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003175inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176typename basic_string<_CharT, _Traits, _Allocator>::iterator
3177basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3178{
Louis Dionneba400782020-10-02 15:02:52 -04003179#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003180 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3181 "string::erase(iterator) called with an iterator not"
3182 " referring to this string");
3183#endif
3184 _LIBCPP_ASSERT(__pos != end(),
3185 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186 iterator __b = begin();
3187 size_type __r = static_cast<size_type>(__pos - __b);
3188 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003189 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190}
3191
3192template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003193inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194typename basic_string<_CharT, _Traits, _Allocator>::iterator
3195basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3196{
Louis Dionneba400782020-10-02 15:02:52 -04003197#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003198 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3199 "string::erase(iterator, iterator) called with an iterator not"
3200 " referring to this string");
3201#endif
3202 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203 iterator __b = begin();
3204 size_type __r = static_cast<size_type>(__first - __b);
3205 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003206 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207}
3208
3209template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003210inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211void
3212basic_string<_CharT, _Traits, _Allocator>::pop_back()
3213{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003214 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215 size_type __sz;
3216 if (__is_long())
3217 {
3218 __sz = __get_long_size() - 1;
3219 __set_long_size(__sz);
3220 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3221 }
3222 else
3223 {
3224 __sz = __get_short_size() - 1;
3225 __set_short_size(__sz);
3226 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3227 }
3228 __invalidate_iterators_past(__sz);
3229}
3230
3231template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003232inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003234basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235{
3236 __invalidate_all_iterators();
3237 if (__is_long())
3238 {
3239 traits_type::assign(*__get_long_pointer(), value_type());
3240 __set_long_size(0);
3241 }
3242 else
3243 {
3244 traits_type::assign(*__get_short_pointer(), value_type());
3245 __set_short_size(0);
3246 }
3247}
3248
3249template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003250inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251void
3252basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3253{
3254 if (__is_long())
3255 {
3256 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3257 __set_long_size(__pos);
3258 }
3259 else
3260 {
3261 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3262 __set_short_size(__pos);
3263 }
3264 __invalidate_iterators_past(__pos);
3265}
3266
3267template <class _CharT, class _Traits, class _Allocator>
3268void
3269basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3270{
3271 size_type __sz = size();
3272 if (__n > __sz)
3273 append(__n - __sz, __c);
3274 else
3275 __erase_to_end(__n);
3276}
3277
3278template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003279inline void
3280basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3281{
3282 size_type __sz = size();
3283 if (__n > __sz) {
3284 __append_default_init(__n - __sz);
3285 } else
3286 __erase_to_end(__n);
3287}
3288
3289template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003290inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003292basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003293{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003294 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003295#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003296 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003298 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299#endif
3300}
3301
3302template <class _CharT, class _Traits, class _Allocator>
3303void
Marek Kurdejc9848142020-11-26 10:07:16 +01003304basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305{
Marek Kurdejc9848142020-11-26 10:07:16 +01003306 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003308
3309#if _LIBCPP_STD_VER > 17
3310 // Reserve never shrinks as of C++20.
3311 if (__requested_capacity <= capacity()) return;
3312#endif
3313
3314 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3315 __target_capacity = __recommend(__target_capacity);
3316 if (__target_capacity == capacity()) return;
3317
3318 __shrink_or_extend(__target_capacity);
3319}
3320
3321template <class _CharT, class _Traits, class _Allocator>
3322void
3323basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3324{
3325 size_type __target_capacity = __recommend(size());
3326 if (__target_capacity == capacity()) return;
3327
3328 __shrink_or_extend(__target_capacity);
3329}
3330
3331template <class _CharT, class _Traits, class _Allocator>
3332void
3333basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3334{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003335 size_type __cap = capacity();
3336 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003337
3338 pointer __new_data, __p;
3339 bool __was_long, __now_long;
3340 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003342 __was_long = true;
3343 __now_long = false;
3344 __new_data = __get_short_pointer();
3345 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003347 else
3348 {
3349 if (__target_capacity > __cap)
3350 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3351 else
3352 {
3353 #ifndef _LIBCPP_NO_EXCEPTIONS
3354 try
3355 {
3356 #endif // _LIBCPP_NO_EXCEPTIONS
3357 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3358 #ifndef _LIBCPP_NO_EXCEPTIONS
3359 }
3360 catch (...)
3361 {
3362 return;
3363 }
3364 #else // _LIBCPP_NO_EXCEPTIONS
3365 if (__new_data == nullptr)
3366 return;
3367 #endif // _LIBCPP_NO_EXCEPTIONS
3368 }
3369 __now_long = true;
3370 __was_long = __is_long();
3371 __p = __get_pointer();
3372 }
3373 traits_type::copy(_VSTD::__to_address(__new_data),
3374 _VSTD::__to_address(__p), size()+1);
3375 if (__was_long)
3376 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3377 if (__now_long)
3378 {
3379 __set_long_cap(__target_capacity+1);
3380 __set_long_size(__sz);
3381 __set_long_pointer(__new_data);
3382 }
3383 else
3384 __set_short_size(__sz);
3385 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386}
3387
3388template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003389inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003391basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003393 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394 return *(data() + __pos);
3395}
3396
3397template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003398inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003400basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003402 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003403 return *(__get_pointer() + __pos);
3404}
3405
3406template <class _CharT, class _Traits, class _Allocator>
3407typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3408basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3409{
3410 if (__n >= size())
3411 this->__throw_out_of_range();
3412 return (*this)[__n];
3413}
3414
3415template <class _CharT, class _Traits, class _Allocator>
3416typename basic_string<_CharT, _Traits, _Allocator>::reference
3417basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3418{
3419 if (__n >= size())
3420 this->__throw_out_of_range();
3421 return (*this)[__n];
3422}
3423
3424template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003425inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003427basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003429 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430 return *__get_pointer();
3431}
3432
3433template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003434inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003436basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003438 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003439 return *data();
3440}
3441
3442template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003443inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003445basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003447 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448 return *(__get_pointer() + size() - 1);
3449}
3450
3451template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003452inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003454basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003456 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457 return *(data() + size() - 1);
3458}
3459
3460template <class _CharT, class _Traits, class _Allocator>
3461typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003462basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463{
3464 size_type __sz = size();
3465 if (__pos > __sz)
3466 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003467 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468 traits_type::copy(__s, data() + __pos, __rlen);
3469 return __rlen;
3470}
3471
3472template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003473inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474basic_string<_CharT, _Traits, _Allocator>
3475basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3476{
3477 return basic_string(*this, __pos, __n, __alloc());
3478}
3479
3480template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003481inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482void
3483basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003484#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003485 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003486#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003487 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003488 __is_nothrow_swappable<allocator_type>::value)
3489#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490{
Louis Dionneba400782020-10-02 15:02:52 -04003491#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003492 if (!__is_long())
3493 __get_db()->__invalidate_all(this);
3494 if (!__str.__is_long())
3495 __get_db()->__invalidate_all(&__str);
3496 __get_db()->swap(this, &__str);
3497#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003498 _LIBCPP_ASSERT(
3499 __alloc_traits::propagate_on_container_swap::value ||
3500 __alloc_traits::is_always_equal::value ||
3501 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003502 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003503 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504}
3505
3506// find
3507
3508template <class _Traits>
3509struct _LIBCPP_HIDDEN __traits_eq
3510{
3511 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003512 _LIBCPP_INLINE_VISIBILITY
3513 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3514 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515};
3516
3517template<class _CharT, class _Traits, class _Allocator>
3518typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003519basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003520 size_type __pos,
3521 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003523 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003524 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003525 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526}
3527
3528template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003529inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003531basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3532 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003534 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003535 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536}
3537
3538template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003539template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003540_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003541<
3542 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3543 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003544>
Marshall Clowe46031a2018-07-02 18:41:15 +00003545basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003546 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003547{
Marshall Clowe46031a2018-07-02 18:41:15 +00003548 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003549 return __str_find<value_type, size_type, traits_type, npos>
3550 (data(), size(), __sv.data(), __pos, __sv.size());
3551}
3552
3553template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003554inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003555typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003556basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003557 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003559 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003560 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003561 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562}
3563
3564template<class _CharT, class _Traits, class _Allocator>
3565typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003566basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3567 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003569 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003570 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571}
3572
3573// rfind
3574
3575template<class _CharT, class _Traits, class _Allocator>
3576typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003577basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003578 size_type __pos,
3579 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003581 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003582 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003583 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584}
3585
3586template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003587inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003589basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3590 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003592 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003593 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594}
3595
3596template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003597template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003598_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003599<
3600 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3601 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003602>
Marshall Clowe46031a2018-07-02 18:41:15 +00003603basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003604 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003605{
Marshall Clowe46031a2018-07-02 18:41:15 +00003606 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003607 return __str_rfind<value_type, size_type, traits_type, npos>
3608 (data(), size(), __sv.data(), __pos, __sv.size());
3609}
3610
3611template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003612inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003613typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003614basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003615 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003617 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003618 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003619 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620}
3621
3622template<class _CharT, class _Traits, class _Allocator>
3623typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003624basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3625 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003627 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003628 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629}
3630
3631// find_first_of
3632
3633template<class _CharT, class _Traits, class _Allocator>
3634typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003635basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003636 size_type __pos,
3637 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003639 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003640 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003641 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642}
3643
3644template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003645inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003647basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3648 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003650 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003651 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652}
3653
3654template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003655template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003656_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003657<
3658 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3659 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003660>
Marshall Clowe46031a2018-07-02 18:41:15 +00003661basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003662 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003663{
Marshall Clowe46031a2018-07-02 18:41:15 +00003664 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003665 return __str_find_first_of<value_type, size_type, traits_type, npos>
3666 (data(), size(), __sv.data(), __pos, __sv.size());
3667}
3668
3669template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003670inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003671typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003672basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003673 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003675 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003676 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003677 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678}
3679
3680template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003681inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003683basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3684 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685{
3686 return find(__c, __pos);
3687}
3688
3689// find_last_of
3690
3691template<class _CharT, class _Traits, class _Allocator>
3692typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003693basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003694 size_type __pos,
3695 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003697 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003698 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003699 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700}
3701
3702template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003703inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003705basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3706 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003708 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003709 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003710}
3711
3712template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003713template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003714_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003715<
3716 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3717 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003718>
Marshall Clowe46031a2018-07-02 18:41:15 +00003719basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003720 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003721{
Marshall Clowe46031a2018-07-02 18:41:15 +00003722 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003723 return __str_find_last_of<value_type, size_type, traits_type, npos>
3724 (data(), size(), __sv.data(), __pos, __sv.size());
3725}
3726
3727template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003728inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003729typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003730basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003731 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003733 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003734 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003735 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736}
3737
3738template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003739inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003741basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3742 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743{
3744 return rfind(__c, __pos);
3745}
3746
3747// find_first_not_of
3748
3749template<class _CharT, class _Traits, class _Allocator>
3750typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003751basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003752 size_type __pos,
3753 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003755 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003756 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003757 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758}
3759
3760template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003761inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003763basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3764 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003766 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003767 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768}
3769
3770template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003771template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003772_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003773<
3774 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3775 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003776>
Marshall Clowe46031a2018-07-02 18:41:15 +00003777basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003778 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003779{
Marshall Clowe46031a2018-07-02 18:41:15 +00003780 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003781 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3782 (data(), size(), __sv.data(), __pos, __sv.size());
3783}
3784
3785template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003786inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003787typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003788basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003789 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003791 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003792 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003793 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794}
3795
3796template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003797inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003799basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3800 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003802 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003803 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804}
3805
3806// find_last_not_of
3807
3808template<class _CharT, class _Traits, class _Allocator>
3809typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003810basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003811 size_type __pos,
3812 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003814 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003815 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003816 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817}
3818
3819template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003820inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003822basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3823 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003825 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003826 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827}
3828
3829template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003830template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003831_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003832<
3833 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3834 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003835>
Marshall Clowe46031a2018-07-02 18:41:15 +00003836basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003837 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003838{
Marshall Clowe46031a2018-07-02 18:41:15 +00003839 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003840 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3841 (data(), size(), __sv.data(), __pos, __sv.size());
3842}
3843
3844template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003845inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003846typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003847basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003848 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003850 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003851 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003852 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853}
3854
3855template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003856inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003858basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3859 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003861 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003862 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863}
3864
3865// compare
3866
3867template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003868template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003869_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003870<
3871 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3872 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003873>
zoecarver1997e0a2021-02-05 11:54:47 -08003874basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875{
Marshall Clowe46031a2018-07-02 18:41:15 +00003876 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003877 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003878 size_t __rhs_sz = __sv.size();
3879 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003880 _VSTD::min(__lhs_sz, __rhs_sz));
3881 if (__result != 0)
3882 return __result;
3883 if (__lhs_sz < __rhs_sz)
3884 return -1;
3885 if (__lhs_sz > __rhs_sz)
3886 return 1;
3887 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003888}
3889
3890template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003891inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003893basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003895 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003896}
3897
3898template <class _CharT, class _Traits, class _Allocator>
3899int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003900basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3901 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003902 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003903 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003905 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003906 size_type __sz = size();
3907 if (__pos1 > __sz || __n2 == npos)
3908 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003909 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3910 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911 if (__r == 0)
3912 {
3913 if (__rlen < __n2)
3914 __r = -1;
3915 else if (__rlen > __n2)
3916 __r = 1;
3917 }
3918 return __r;
3919}
3920
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003921template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003922template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003923_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003924<
3925 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3926 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003927>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003928basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3929 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003930 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003931{
Marshall Clowe46031a2018-07-02 18:41:15 +00003932 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003933 return compare(__pos1, __n1, __sv.data(), __sv.size());
3934}
3935
3936template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003937inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003938int
3939basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3940 size_type __n1,
3941 const basic_string& __str) const
3942{
3943 return compare(__pos1, __n1, __str.data(), __str.size());
3944}
3945
3946template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003947template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003948_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003949<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003950 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3951 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003952 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003953>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003954basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3955 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003956 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003957 size_type __pos2,
3958 size_type __n2) const
3959{
Marshall Clow82513342016-09-24 22:45:42 +00003960 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003961 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3962}
3963
3964template <class _CharT, class _Traits, class _Allocator>
3965int
3966basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3967 size_type __n1,
3968 const basic_string& __str,
3969 size_type __pos2,
3970 size_type __n2) const
3971{
3972 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3973}
3974
3975template <class _CharT, class _Traits, class _Allocator>
3976int
3977basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3978{
3979 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3980 return compare(0, npos, __s, traits_type::length(__s));
3981}
3982
3983template <class _CharT, class _Traits, class _Allocator>
3984int
3985basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3986 size_type __n1,
3987 const value_type* __s) const
3988{
3989 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3990 return compare(__pos1, __n1, __s, traits_type::length(__s));
3991}
3992
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993// __invariants
3994
3995template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003996inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997bool
3998basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3999{
4000 if (size() > capacity())
4001 return false;
4002 if (capacity() < __min_cap - 1)
4003 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004004 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004006 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007 return false;
4008 return true;
4009}
4010
Vedant Kumar55e007e2018-03-08 21:15:26 +00004011// __clear_and_shrink
4012
4013template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004014inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004015void
Marshall Clowe60a7182018-05-29 17:04:37 +00004016basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004017{
4018 clear();
4019 if(__is_long())
4020 {
4021 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4022 __set_long_cap(0);
4023 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004024 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004025 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004026}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004027
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028// operator==
4029
4030template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004031inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004032bool
4033operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004034 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004036 size_t __lhs_sz = __lhs.size();
4037 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4038 __rhs.data(),
4039 __lhs_sz) == 0;
4040}
4041
4042template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004044bool
4045operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4046 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4047{
4048 size_t __lhs_sz = __lhs.size();
4049 if (__lhs_sz != __rhs.size())
4050 return false;
4051 const char* __lp = __lhs.data();
4052 const char* __rp = __rhs.data();
4053 if (__lhs.__is_long())
4054 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4055 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4056 if (*__lp != *__rp)
4057 return false;
4058 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059}
4060
4061template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004062inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004064operator==(const _CharT* __lhs,
4065 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004067 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4068 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4069 size_t __lhs_len = _Traits::length(__lhs);
4070 if (__lhs_len != __rhs.size()) return false;
4071 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072}
4073
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004075inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004077operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4078 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004080 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4081 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4082 size_t __rhs_len = _Traits::length(__rhs);
4083 if (__rhs_len != __lhs.size()) return false;
4084 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085}
4086
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004087template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089bool
4090operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004091 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092{
4093 return !(__lhs == __rhs);
4094}
4095
4096template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004097inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004099operator!=(const _CharT* __lhs,
4100 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101{
4102 return !(__lhs == __rhs);
4103}
4104
4105template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004106inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004108operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4109 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110{
4111 return !(__lhs == __rhs);
4112}
4113
4114// operator<
4115
4116template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004117inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118bool
4119operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004120 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004122 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123}
4124
4125template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004126inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004128operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4129 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004131 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132}
4133
4134template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004135inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004137operator< (const _CharT* __lhs,
4138 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139{
4140 return __rhs.compare(__lhs) > 0;
4141}
4142
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143// operator>
4144
4145template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004146inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147bool
4148operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004149 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150{
4151 return __rhs < __lhs;
4152}
4153
4154template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004155inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004157operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4158 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159{
4160 return __rhs < __lhs;
4161}
4162
4163template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004164inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004166operator> (const _CharT* __lhs,
4167 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168{
4169 return __rhs < __lhs;
4170}
4171
4172// operator<=
4173
4174template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004175inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176bool
4177operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004178 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179{
4180 return !(__rhs < __lhs);
4181}
4182
4183template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004184inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004186operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4187 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188{
4189 return !(__rhs < __lhs);
4190}
4191
4192template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004193inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004195operator<=(const _CharT* __lhs,
4196 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197{
4198 return !(__rhs < __lhs);
4199}
4200
4201// operator>=
4202
4203template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205bool
4206operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004207 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208{
4209 return !(__lhs < __rhs);
4210}
4211
4212template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004213inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004215operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4216 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217{
4218 return !(__lhs < __rhs);
4219}
4220
4221template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004222inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004224operator>=(const _CharT* __lhs,
4225 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226{
4227 return !(__lhs < __rhs);
4228}
4229
4230// operator +
4231
4232template<class _CharT, class _Traits, class _Allocator>
4233basic_string<_CharT, _Traits, _Allocator>
4234operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4235 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4236{
4237 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4238 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4239 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4240 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4241 __r.append(__rhs.data(), __rhs_sz);
4242 return __r;
4243}
4244
4245template<class _CharT, class _Traits, class _Allocator>
4246basic_string<_CharT, _Traits, _Allocator>
4247operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4248{
4249 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4250 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4251 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4252 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4253 __r.append(__rhs.data(), __rhs_sz);
4254 return __r;
4255}
4256
4257template<class _CharT, class _Traits, class _Allocator>
4258basic_string<_CharT, _Traits, _Allocator>
4259operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4260{
4261 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4262 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4263 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4264 __r.append(__rhs.data(), __rhs_sz);
4265 return __r;
4266}
4267
4268template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004269inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270basic_string<_CharT, _Traits, _Allocator>
4271operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4272{
4273 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4274 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4275 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4276 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4277 __r.append(__rhs, __rhs_sz);
4278 return __r;
4279}
4280
4281template<class _CharT, class _Traits, class _Allocator>
4282basic_string<_CharT, _Traits, _Allocator>
4283operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4284{
4285 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4286 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4287 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4288 __r.push_back(__rhs);
4289 return __r;
4290}
4291
Eric Fiselierfc92be82017-04-19 00:28:44 +00004292#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293
4294template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296basic_string<_CharT, _Traits, _Allocator>
4297operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4298{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004299 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004300}
4301
4302template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304basic_string<_CharT, _Traits, _Allocator>
4305operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4306{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004307 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308}
4309
4310template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004312basic_string<_CharT, _Traits, _Allocator>
4313operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4314{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004315 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004316}
4317
4318template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004319inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004320basic_string<_CharT, _Traits, _Allocator>
4321operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4322{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004323 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324}
4325
4326template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004327inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004328basic_string<_CharT, _Traits, _Allocator>
4329operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4330{
4331 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004332 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333}
4334
4335template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004336inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004337basic_string<_CharT, _Traits, _Allocator>
4338operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4339{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004340 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341}
4342
4343template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004344inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345basic_string<_CharT, _Traits, _Allocator>
4346operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4347{
4348 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004349 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350}
4351
Eric Fiselierfc92be82017-04-19 00:28:44 +00004352#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004353
4354// swap
4355
4356template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004357inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004359swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004360 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4361 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004362{
4363 __lhs.swap(__rhs);
4364}
4365
Bruce Mitchener170d8972020-11-24 12:53:53 -05004366_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4367_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4368_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4369_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4370_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004371
Bruce Mitchener170d8972020-11-24 12:53:53 -05004372_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4373_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4374_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004375
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004376_LIBCPP_FUNC_VIS string to_string(int __val);
4377_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4378_LIBCPP_FUNC_VIS string to_string(long __val);
4379_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4380_LIBCPP_FUNC_VIS string to_string(long long __val);
4381_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4382_LIBCPP_FUNC_VIS string to_string(float __val);
4383_LIBCPP_FUNC_VIS string to_string(double __val);
4384_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004385
Bruce Mitchener170d8972020-11-24 12:53:53 -05004386_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4387_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4388_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4389_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4390_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004391
Bruce Mitchener170d8972020-11-24 12:53:53 -05004392_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4393_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4394_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004395
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004396_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4397_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4398_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4399_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4400_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4401_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4402_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4403_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4404_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004405
Howard Hinnantc51e1022010-05-11 19:42:16 +00004406template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004407_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004408const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4409 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004410
Marshall Clow851b9ec2019-05-20 21:56:51 +00004411template <class _CharT, class _Allocator>
4412struct _LIBCPP_TEMPLATE_VIS
4413 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4414 : public unary_function<
4415 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416{
4417 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004418 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4419 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004420};
4421
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422
Howard Hinnantdc095972011-07-18 15:51:59 +00004423template<class _CharT, class _Traits, class _Allocator>
4424basic_ostream<_CharT, _Traits>&
4425operator<<(basic_ostream<_CharT, _Traits>& __os,
4426 const basic_string<_CharT, _Traits, _Allocator>& __str);
4427
4428template<class _CharT, class _Traits, class _Allocator>
4429basic_istream<_CharT, _Traits>&
4430operator>>(basic_istream<_CharT, _Traits>& __is,
4431 basic_string<_CharT, _Traits, _Allocator>& __str);
4432
4433template<class _CharT, class _Traits, class _Allocator>
4434basic_istream<_CharT, _Traits>&
4435getline(basic_istream<_CharT, _Traits>& __is,
4436 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4437
4438template<class _CharT, class _Traits, class _Allocator>
4439inline _LIBCPP_INLINE_VISIBILITY
4440basic_istream<_CharT, _Traits>&
4441getline(basic_istream<_CharT, _Traits>& __is,
4442 basic_string<_CharT, _Traits, _Allocator>& __str);
4443
Eric Fiselierfc92be82017-04-19 00:28:44 +00004444#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004445
4446template<class _CharT, class _Traits, class _Allocator>
4447inline _LIBCPP_INLINE_VISIBILITY
4448basic_istream<_CharT, _Traits>&
4449getline(basic_istream<_CharT, _Traits>&& __is,
4450 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4451
4452template<class _CharT, class _Traits, class _Allocator>
4453inline _LIBCPP_INLINE_VISIBILITY
4454basic_istream<_CharT, _Traits>&
4455getline(basic_istream<_CharT, _Traits>&& __is,
4456 basic_string<_CharT, _Traits, _Allocator>& __str);
4457
Eric Fiselierfc92be82017-04-19 00:28:44 +00004458#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004459
Marshall Clow29b53f22018-12-14 18:49:35 +00004460#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004461template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004462inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004463 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4464 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4465 auto __old_size = __str.size();
4466 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4467 return __old_size - __str.size();
4468}
Marshall Clow29b53f22018-12-14 18:49:35 +00004469
Marek Kurdeja98b1412020-05-02 13:58:03 +02004470template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004471inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004472 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4473 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4474 _Predicate __pred) {
4475 auto __old_size = __str.size();
4476 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4477 __str.end());
4478 return __old_size - __str.size();
4479}
Marshall Clow29b53f22018-12-14 18:49:35 +00004480#endif
4481
Louis Dionneba400782020-10-02 15:02:52 -04004482#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004483
4484template<class _CharT, class _Traits, class _Allocator>
4485bool
4486basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4487{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004488 return this->data() <= _VSTD::__to_address(__i->base()) &&
4489 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004490}
4491
4492template<class _CharT, class _Traits, class _Allocator>
4493bool
4494basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4495{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004496 return this->data() < _VSTD::__to_address(__i->base()) &&
4497 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004498}
4499
4500template<class _CharT, class _Traits, class _Allocator>
4501bool
4502basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4503{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004504 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004505 return this->data() <= __p && __p <= this->data() + this->size();
4506}
4507
4508template<class _CharT, class _Traits, class _Allocator>
4509bool
4510basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4511{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004512 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004513 return this->data() <= __p && __p < this->data() + this->size();
4514}
4515
Louis Dionneba400782020-10-02 15:02:52 -04004516#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004517
Louis Dionne173f29e2019-05-29 16:01:36 +00004518#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004519// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004520inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004521{
4522 inline namespace string_literals
4523 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004524 inline _LIBCPP_INLINE_VISIBILITY
4525 basic_string<char> operator "" s( const char *__str, size_t __len )
4526 {
4527 return basic_string<char> (__str, __len);
4528 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004529
Howard Hinnant5c167562013-08-07 19:39:48 +00004530 inline _LIBCPP_INLINE_VISIBILITY
4531 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4532 {
4533 return basic_string<wchar_t> (__str, __len);
4534 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004535
Marshall Clow8732fed2018-12-11 04:35:44 +00004536#ifndef _LIBCPP_NO_HAS_CHAR8_T
4537 inline _LIBCPP_INLINE_VISIBILITY
4538 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4539 {
4540 return basic_string<char8_t> (__str, __len);
4541 }
4542#endif
4543
Howard Hinnant5c167562013-08-07 19:39:48 +00004544 inline _LIBCPP_INLINE_VISIBILITY
4545 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4546 {
4547 return basic_string<char16_t> (__str, __len);
4548 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004549
Howard Hinnant5c167562013-08-07 19:39:48 +00004550 inline _LIBCPP_INLINE_VISIBILITY
4551 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4552 {
4553 return basic_string<char32_t> (__str, __len);
4554 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004555 }
4556}
4557#endif
4558
Howard Hinnantc51e1022010-05-11 19:42:16 +00004559_LIBCPP_END_NAMESPACE_STD
4560
Eric Fiselierf4433a32017-05-31 22:07:49 +00004561_LIBCPP_POP_MACROS
4562
Howard Hinnantc51e1022010-05-11 19:42:16 +00004563#endif // _LIBCPP_STRING