blob: ef606665e22c8626dbd5454d23af5c6835f56c0c [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>
265 size_type find(const T& t, size_type pos = 0) const; // C++17
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>
272 size_type rfind(const T& t, size_type pos = npos) const; // C++17
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>
279 size_type find_first_of(const T& t, size_type pos = 0) const; // C++17
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>
286 size_type find_last_of(const T& t, size_type pos = npos) const noexcept; // C++17
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>
293 size_type find_first_not_of(const T& t, size_type pos = 0) const; // C++17
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>
300 size_type find_last_not_of(const T& t, size_type pos = npos) const; // C++17
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>
307 int compare(const T& t) const noexcept; // C++17
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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327 bool __invariants() const;
328};
329
Marshall Clowa0563332018-02-08 06:34:03 +0000330template<class InputIterator,
331 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
332basic_string(InputIterator, InputIterator, Allocator = Allocator())
333 -> basic_string<typename iterator_traits<InputIterator>::value_type,
334 char_traits<typename iterator_traits<InputIterator>::value_type>,
335 Allocator>; // C++17
336
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337template<class charT, class traits, class Allocator>
338basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000339operator+(const basic_string<charT, traits, Allocator>& lhs,
340 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341
342template<class charT, class traits, class Allocator>
343basic_string<charT, traits, Allocator>
344operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
345
346template<class charT, class traits, class Allocator>
347basic_string<charT, traits, Allocator>
348operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
349
350template<class charT, class traits, class Allocator>
351basic_string<charT, traits, Allocator>
352operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
357
358template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000359bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000360 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000363bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000366bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000368template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000369bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000370 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000373bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374
375template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000376bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377
378template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000379bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000380 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000383bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000386bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387
388template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000389bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000390 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000393bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394
395template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000396bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397
398template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000399bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000400 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000403bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000406bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407
408template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000409bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000410 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
412template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000413bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414
415template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000416bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417
418template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000419void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000420 basic_string<charT, traits, Allocator>& rhs)
421 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422
423template<class charT, class traits, class Allocator>
424basic_istream<charT, traits>&
425operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
426
427template<class charT, class traits, class Allocator>
428basic_ostream<charT, traits>&
429operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
430
431template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000432basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000433getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
434 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435
436template<class charT, class traits, class Allocator>
437basic_istream<charT, traits>&
438getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
439
Marshall Clow29b53f22018-12-14 18:49:35 +0000440template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200441typename basic_string<charT, traits, Allocator>::size_type
442erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000443template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200444typename basic_string<charT, traits, Allocator>::size_type
445erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000446
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447typedef basic_string<char> string;
448typedef basic_string<wchar_t> wstring;
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000449typedef basic_string<char16_t> u16string;
450typedef basic_string<char32_t> u32string;
451
Bruce Mitchener170d8972020-11-24 12:53:53 -0500452int stoi (const string& str, size_t* idx = nullptr, int base = 10);
453long stol (const string& str, size_t* idx = nullptr, int base = 10);
454unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
455long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
456unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000457
Bruce Mitchener170d8972020-11-24 12:53:53 -0500458float stof (const string& str, size_t* idx = nullptr);
459double stod (const string& str, size_t* idx = nullptr);
460long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000461
462string to_string(int val);
463string to_string(unsigned val);
464string to_string(long val);
465string to_string(unsigned long val);
466string to_string(long long val);
467string to_string(unsigned long long val);
468string to_string(float val);
469string to_string(double val);
470string to_string(long double val);
471
Bruce Mitchener170d8972020-11-24 12:53:53 -0500472int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
473long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
474unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
475long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
476unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000477
Bruce Mitchener170d8972020-11-24 12:53:53 -0500478float stof (const wstring& str, size_t* idx = nullptr);
479double stod (const wstring& str, size_t* idx = nullptr);
480long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000481
482wstring to_wstring(int val);
483wstring to_wstring(unsigned val);
484wstring to_wstring(long val);
485wstring to_wstring(unsigned long val);
486wstring to_wstring(long long val);
487wstring to_wstring(unsigned long long val);
488wstring to_wstring(float val);
489wstring to_wstring(double val);
490wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000491
492template <> struct hash<string>;
493template <> struct hash<u16string>;
494template <> struct hash<u32string>;
495template <> struct hash<wstring>;
496
Marshall Clowcba751f2013-07-23 17:05:24 +0000497basic_string<char> operator "" s( const char *str, size_t len ); // C++14
498basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
499basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
500basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
501
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502} // std
503
504*/
505
506#include <__config>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000507#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000508#include <iosfwd>
509#include <cstring>
Howard Hinnant155c2af2010-05-24 17:49:41 +0000510#include <cstdio> // For EOF.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000511#include <cwchar>
512#include <algorithm>
513#include <iterator>
514#include <utility>
515#include <memory>
516#include <stdexcept>
517#include <type_traits>
518#include <initializer_list>
519#include <__functional_base>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000520#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
522#include <cstdint>
523#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000524
Eric Fiselier14b6de92014-08-10 23:53:08 +0000525#include <__debug>
526
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000527#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000529#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530
Eric Fiselierf4433a32017-05-31 22:07:49 +0000531_LIBCPP_PUSH_MACROS
532#include <__undef_macros>
533
534
Howard Hinnantc51e1022010-05-11 19:42:16 +0000535_LIBCPP_BEGIN_NAMESPACE_STD
536
537// fpos
538
539template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000540class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541{
542private:
543 _StateT __st_;
544 streamoff __off_;
545public:
546 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
547
548 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
549
550 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
551 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
552
553 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
554 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
555 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
556 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
557};
558
559template <class _StateT>
560inline _LIBCPP_INLINE_VISIBILITY
561streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
562 {return streamoff(__x) - streamoff(__y);}
563
564template <class _StateT>
565inline _LIBCPP_INLINE_VISIBILITY
566bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
567 {return streamoff(__x) == streamoff(__y);}
568
569template <class _StateT>
570inline _LIBCPP_INLINE_VISIBILITY
571bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
572 {return streamoff(__x) != streamoff(__y);}
573
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574// basic_string
575
576template<class _CharT, class _Traits, class _Allocator>
577basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000578operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
579 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580
581template<class _CharT, class _Traits, class _Allocator>
582basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000583operator+(const _CharT* __x, 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+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588
589template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000590inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000592operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593
594template<class _CharT, class _Traits, class _Allocator>
595basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000596operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597
Shoaib Meenaiea363712017-07-29 02:54:41 +0000598_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
599
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000601class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602{
603protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000604 _LIBCPP_NORETURN void __throw_length_error() const;
605 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606};
607
608template <bool __b>
609void
610__basic_string_common<__b>::__throw_length_error() const
611{
Marshall Clow8fea1612016-08-25 15:09:01 +0000612 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613}
614
615template <bool __b>
616void
617__basic_string_common<__b>::__throw_out_of_range() const
618{
Marshall Clow8fea1612016-08-25 15:09:01 +0000619 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620}
621
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000622_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623
Marshall Clow039b2f02016-01-13 21:54:34 +0000624#ifdef _LIBCPP_NO_EXCEPTIONS
625template <class _Iter>
626struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
627#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
628template <class _Iter>
629struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
630#else
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500631template <class _Iter, bool = __is_cpp17_forward_iterator<_Iter>::value>
Marshall Clow039b2f02016-01-13 21:54:34 +0000632struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
Louis Dionne173f29e2019-05-29 16:01:36 +0000633 noexcept(++(declval<_Iter&>())) &&
634 is_nothrow_assignable<_Iter&, _Iter>::value &&
635 noexcept(declval<_Iter>() == declval<_Iter>()) &&
Marshall Clow039b2f02016-01-13 21:54:34 +0000636 noexcept(*declval<_Iter>())
637)) {};
638
Louis Dionne173f29e2019-05-29 16:01:36 +0000639template <class _Iter>
Marshall Clow039b2f02016-01-13 21:54:34 +0000640struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
641#endif
642
643
644template <class _Iter>
645struct __libcpp_string_gets_noexcept_iterator
646 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
647
Marshall Clow82513342016-09-24 22:45:42 +0000648template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500649struct __can_be_converted_to_string_view : public _BoolConstant<
650 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
651 !is_convertible<const _Tp&, const _CharT*>::value
652 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000653
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000654#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000655
656template <class _CharT, size_t = sizeof(_CharT)>
657struct __padding
658{
659 unsigned char __xx[sizeof(_CharT)-1];
660};
661
662template <class _CharT>
663struct __padding<_CharT, 1>
664{
665};
666
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000667#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000668
Richard Smith256954d2020-11-11 17:12:18 -0800669#ifndef _LIBCPP_NO_HAS_CHAR8_T
670typedef basic_string<char8_t> u8string;
671#endif
672
673#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
674typedef basic_string<char16_t> u16string;
675typedef basic_string<char32_t> u32string;
676#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
677
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000678template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800679class
680 _LIBCPP_TEMPLATE_VIS
681#ifndef _LIBCPP_NO_HAS_CHAR8_T
682 _LIBCPP_PREFERRED_NAME(u8string)
683#endif
684#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
685 _LIBCPP_PREFERRED_NAME(u16string)
686 _LIBCPP_PREFERRED_NAME(u32string)
687#endif
688 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689 : private __basic_string_common<true>
690{
691public:
692 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000693 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000695 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000697 typedef allocator_traits<allocator_type> __alloc_traits;
698 typedef typename __alloc_traits::size_type size_type;
699 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000700 typedef value_type& reference;
701 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000702 typedef typename __alloc_traits::pointer pointer;
703 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704
Marshall Clow79f33542018-03-21 00:36:05 +0000705 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
706 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
707 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
708 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000709 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000710 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000711 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000712
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713 typedef __wrap_iter<pointer> iterator;
714 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000715 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
716 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000717
718private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000719
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000720#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000721
722 struct __long
723 {
724 pointer __data_;
725 size_type __size_;
726 size_type __cap_;
727 };
728
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000729#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000730 static const size_type __short_mask = 0x01;
731 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000732#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000733 static const size_type __short_mask = 0x80;
734 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant68bf1812013-04-30 21:44:48 +0000735#endif // _LIBCPP_BIG_ENDIAN
736
737 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
738 (sizeof(__long) - 1)/sizeof(value_type) : 2};
739
740 struct __short
741 {
742 value_type __data_[__min_cap];
743 struct
744 : __padding<value_type>
745 {
746 unsigned char __size_;
747 };
748 };
749
750#else
751
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 struct __long
753 {
754 size_type __cap_;
755 size_type __size_;
756 pointer __data_;
757 };
758
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000759#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000760 static const size_type __short_mask = 0x80;
761 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000762#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000763 static const size_type __short_mask = 0x01;
764 static const size_type __long_mask = 0x1ul;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000765#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
768 (sizeof(__long) - 1)/sizeof(value_type) : 2};
769
770 struct __short
771 {
772 union
773 {
774 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000775 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776 };
777 value_type __data_[__min_cap];
778 };
779
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000780#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000781
Howard Hinnant8ea98242013-08-23 17:37:05 +0000782 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783
Howard Hinnant8ea98242013-08-23 17:37:05 +0000784 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785
786 struct __raw
787 {
788 size_type __words[__n_words];
789 };
790
791 struct __rep
792 {
793 union
794 {
795 __long __l;
796 __short __s;
797 __raw __r;
798 };
799 };
800
801 __compressed_pair<__rep, allocator_type> __r_;
802
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803public:
Eric Fiselier2ae9e062020-01-16 15:00:34 -0500804 _LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805 static const size_type npos = -1;
806
Howard Hinnant3e276872011-06-03 18:40:47 +0000807 _LIBCPP_INLINE_VISIBILITY basic_string()
808 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000809
810 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
811#if _LIBCPP_STD_VER <= 14
812 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
813#else
814 _NOEXCEPT;
815#endif
816
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 basic_string(const basic_string& __str);
818 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000819
Eric Fiselierfc92be82017-04-19 00:28:44 +0000820#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000822 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000823#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000824 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000825#else
826 _NOEXCEPT;
827#endif
828
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000830 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000831#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000832
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500833 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000834 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500835 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000836 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
837 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400838# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000839 __get_db()->__insert_c(this);
840# endif
841 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000842
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500843 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000844 _LIBCPP_INLINE_VISIBILITY
845 basic_string(const _CharT* __s, const _Allocator& __a);
846
Howard Hinnantcf823322010-12-17 14:46:43 +0000847 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000849 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000850 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000851 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000852 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000853
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500854 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000855 _LIBCPP_INLINE_VISIBILITY
856 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
857
Marshall Clow83445802016-04-07 18:13:41 +0000858 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000859 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000860 _LIBCPP_INLINE_VISIBILITY
861 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000862 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000863
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500864 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 +0000865 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000866 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500867 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000868
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500869 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
870 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000871 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
872 explicit basic_string(const _Tp& __t);
873
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500874 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 +0000875 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
876 explicit basic_string(const _Tp& __t, const allocator_type& __a);
877
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500878 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500881 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000884#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000885 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000886 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000887 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000888 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000889#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000891 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000893 _LIBCPP_INLINE_VISIBILITY
894 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
895
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000896 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000897
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500898 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 +0000899 basic_string& operator=(const _Tp& __t)
900 {__self_view __sv = __t; return assign(__sv);}
901
Eric Fiselierfc92be82017-04-19 00:28:44 +0000902#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000904 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000905 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000906 _LIBCPP_INLINE_VISIBILITY
907 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000909 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911
Louis Dionneba400782020-10-02 15:02:52 -0400912#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000913 _LIBCPP_INLINE_VISIBILITY
914 iterator begin() _NOEXCEPT
915 {return iterator(this, __get_pointer());}
916 _LIBCPP_INLINE_VISIBILITY
917 const_iterator begin() const _NOEXCEPT
918 {return const_iterator(this, __get_pointer());}
919 _LIBCPP_INLINE_VISIBILITY
920 iterator end() _NOEXCEPT
921 {return iterator(this, __get_pointer() + size());}
922 _LIBCPP_INLINE_VISIBILITY
923 const_iterator end() const _NOEXCEPT
924 {return const_iterator(this, __get_pointer() + size());}
925#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000926 _LIBCPP_INLINE_VISIBILITY
927 iterator begin() _NOEXCEPT
928 {return iterator(__get_pointer());}
929 _LIBCPP_INLINE_VISIBILITY
930 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000931 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000932 _LIBCPP_INLINE_VISIBILITY
933 iterator end() _NOEXCEPT
934 {return iterator(__get_pointer() + size());}
935 _LIBCPP_INLINE_VISIBILITY
936 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000937 {return const_iterator(__get_pointer() + size());}
Louis Dionneba400782020-10-02 15:02:52 -0400938#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000939 _LIBCPP_INLINE_VISIBILITY
940 reverse_iterator rbegin() _NOEXCEPT
941 {return reverse_iterator(end());}
942 _LIBCPP_INLINE_VISIBILITY
943 const_reverse_iterator rbegin() const _NOEXCEPT
944 {return const_reverse_iterator(end());}
945 _LIBCPP_INLINE_VISIBILITY
946 reverse_iterator rend() _NOEXCEPT
947 {return reverse_iterator(begin());}
948 _LIBCPP_INLINE_VISIBILITY
949 const_reverse_iterator rend() const _NOEXCEPT
950 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000952 _LIBCPP_INLINE_VISIBILITY
953 const_iterator cbegin() const _NOEXCEPT
954 {return begin();}
955 _LIBCPP_INLINE_VISIBILITY
956 const_iterator cend() const _NOEXCEPT
957 {return end();}
958 _LIBCPP_INLINE_VISIBILITY
959 const_reverse_iterator crbegin() const _NOEXCEPT
960 {return rbegin();}
961 _LIBCPP_INLINE_VISIBILITY
962 const_reverse_iterator crend() const _NOEXCEPT
963 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000965 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000967 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
968 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
969 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000970 {return (__is_long() ? __get_long_cap()
971 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972
973 void resize(size_type __n, value_type __c);
974 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
975
Marek Kurdejc9848142020-11-26 10:07:16 +0100976 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000977 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
978
Marek Kurdejc9848142020-11-26 10:07:16 +0100979 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
980 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000981 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100982 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000984 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000985 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
986 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000988 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
989 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990
991 const_reference at(size_type __n) const;
992 reference at(size_type __n);
993
994 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000995
996 template <class _Tp>
997 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500998 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +0000999 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001000 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1001 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001002 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001003 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001004 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001005 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001007#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001009#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010
Howard Hinnantcf823322010-12-17 14:46:43 +00001011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001013
1014 template <class _Tp>
1015 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001016 _EnableIf<
1017 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1018 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001019 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001020 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001021 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001022 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001023
Marshall Clow62953962016-10-03 23:40:48 +00001024 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001025 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001027 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001028 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1029 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001030 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001031 >
Marshall Clow82513342016-09-24 22:45:42 +00001032 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001033 basic_string& append(const value_type* __s, size_type __n);
1034 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001036
1037 _LIBCPP_INLINE_VISIBILITY
1038 void __append_default_init(size_type __n);
1039
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001040 template <class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001041 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1042 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001044 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001045 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001047 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001048 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001050 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001051 _LIBCPP_INLINE_VISIBILITY
1052 append(_InputIterator __first, _InputIterator __last) {
1053 const basic_string __temp (__first, __last, __alloc());
1054 append(__temp.data(), __temp.size());
1055 return *this;
1056 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001058 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001059 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001061 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001062 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001064 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001065 _LIBCPP_INLINE_VISIBILITY
1066 append(_ForwardIterator __first, _ForwardIterator __last) {
1067 return __append_forward_unsafe(__first, __last);
1068 }
1069
Eric Fiselierfc92be82017-04-19 00:28:44 +00001070#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001073#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074
1075 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001078 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1079 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1080 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1081 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082
Marshall Clowe46031a2018-07-02 18:41:15 +00001083 template <class _Tp>
1084 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001085 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001086 <
1087 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1088 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001089 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001090 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001091 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001092 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001093#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001094 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001095 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001096 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001097 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001098#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001099 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001100 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001101 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001102 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001103 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001104 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1105 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001106 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001107 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001108 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001109 basic_string& assign(const value_type* __s, size_type __n);
1110 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 basic_string& assign(size_type __n, value_type __c);
1112 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001113 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001116 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001117 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001119 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 assign(_InputIterator __first, _InputIterator __last);
1121 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001122 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001123 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001125 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001126 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001128 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001130#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001133#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134
Howard Hinnantcf823322010-12-17 14:46:43 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001137
1138 template <class _Tp>
1139 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001140 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001141 <
1142 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1143 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001144 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001145 insert(size_type __pos1, const _Tp& __t)
1146 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1147
Marshall Clow82513342016-09-24 22:45:42 +00001148 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001149 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001150 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001151 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001152 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001153 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001154 >
Marshall Clow82513342016-09-24 22:45:42 +00001155 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001156 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001157 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1158 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1160 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1163 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001164 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001165 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001167 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001168 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001170 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1172 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001173 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001174 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001176 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001177 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001179 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001181#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1184 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001185#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186
1187 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 iterator erase(const_iterator __first, const_iterator __last);
1192
Howard Hinnantcf823322010-12-17 14:46:43 +00001193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001195
1196 template <class _Tp>
1197 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001198 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001199 <
1200 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1201 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001202 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001203 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 +00001204 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 +00001205 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001206 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001207 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001208 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001209 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001210 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001211 >
Marshall Clow82513342016-09-24 22:45:42 +00001212 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001213 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1214 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001217 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001218
1219 template <class _Tp>
1220 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001221 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001222 <
1223 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1224 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001225 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001226 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1227
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001229 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001231 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001233 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001235 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001236 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001238 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001240 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001241 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001242#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001244 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001246#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247
Howard Hinnantd17880b2013-06-28 16:59:19 +00001248 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1251
Howard Hinnantcf823322010-12-17 14:46:43 +00001252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001253 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001254#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001255 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001256#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001257 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001258 __is_nothrow_swappable<allocator_type>::value);
1259#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260
Howard Hinnantcf823322010-12-17 14:46:43 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001262 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001263 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001264 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001265#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001266 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001267 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001268#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269
Howard Hinnantcf823322010-12-17 14:46:43 +00001270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001271 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272
Howard Hinnantcf823322010-12-17 14:46:43 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001274 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001275
1276 template <class _Tp>
1277 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001278 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001279 <
1280 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1281 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001282 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001283 find(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001284 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001286 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001287 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288
Howard Hinnantcf823322010-12-17 14:46:43 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001290 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001291
1292 template <class _Tp>
1293 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001294 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001295 <
1296 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1297 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001298 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001299 rfind(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001302 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001303 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304
Howard Hinnantcf823322010-12-17 14:46:43 +00001305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001306 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001307
1308 template <class _Tp>
1309 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001310 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001311 <
1312 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1313 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001314 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001315 find_first_of(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001318 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001320 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321
Howard Hinnantcf823322010-12-17 14:46:43 +00001322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001323 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001324
1325 template <class _Tp>
1326 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001327 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001328 <
1329 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1330 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001331 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001332 find_last_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001335 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001337 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
Howard Hinnantcf823322010-12-17 14:46:43 +00001339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001340 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001341
1342 template <class _Tp>
1343 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001344 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001345 <
1346 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1347 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001348 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001349 find_first_not_of(const _Tp &__t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001350 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 +00001351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001352 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001353 _LIBCPP_INLINE_VISIBILITY
1354 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1355
1356 _LIBCPP_INLINE_VISIBILITY
1357 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001358
1359 template <class _Tp>
1360 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001361 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001362 <
1363 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1364 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001365 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001366 find_last_not_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001367 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 +00001368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001369 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001370 _LIBCPP_INLINE_VISIBILITY
1371 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1372
1373 _LIBCPP_INLINE_VISIBILITY
1374 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001375
1376 template <class _Tp>
1377 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001378 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001379 <
1380 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1381 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001382 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001383 compare(const _Tp &__t) const;
1384
1385 template <class _Tp>
1386 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001387 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001388 <
1389 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1390 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001391 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001392 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1393
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001396 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 +00001397
Marshall Clow82513342016-09-24 22:45:42 +00001398 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001399 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001400 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001401 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001402 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001403 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001404 >
Marshall Clow82513342016-09-24 22:45:42 +00001405 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 +00001406 int compare(const value_type* __s) const _NOEXCEPT;
1407 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1408 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001409
Marshall Clow18c293b2017-12-04 20:11:38 +00001410#if _LIBCPP_STD_VER > 17
1411 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1412 bool starts_with(__self_view __sv) const _NOEXCEPT
1413 { return __self_view(data(), size()).starts_with(__sv); }
1414
1415 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1416 bool starts_with(value_type __c) const _NOEXCEPT
1417 { return !empty() && _Traits::eq(front(), __c); }
1418
1419 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1420 bool starts_with(const value_type* __s) const _NOEXCEPT
1421 { return starts_with(__self_view(__s)); }
1422
1423 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1424 bool ends_with(__self_view __sv) const _NOEXCEPT
1425 { return __self_view(data(), size()).ends_with( __sv); }
1426
1427 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1428 bool ends_with(value_type __c) const _NOEXCEPT
1429 { return !empty() && _Traits::eq(back(), __c); }
1430
1431 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1432 bool ends_with(const value_type* __s) const _NOEXCEPT
1433 { return ends_with(__self_view(__s)); }
1434#endif
1435
Howard Hinnantcf823322010-12-17 14:46:43 +00001436 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001437
Marshall Clowe60a7182018-05-29 17:04:37 +00001438 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001439
Marek Kurdejc9848142020-11-26 10:07:16 +01001440 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1441
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001442 _LIBCPP_INLINE_VISIBILITY
1443 bool __is_long() const _NOEXCEPT
1444 {return bool(__r_.first().__s.__size_ & __short_mask);}
1445
Louis Dionneba400782020-10-02 15:02:52 -04001446#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001447
1448 bool __dereferenceable(const const_iterator* __i) const;
1449 bool __decrementable(const const_iterator* __i) const;
1450 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1451 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1452
Louis Dionneba400782020-10-02 15:02:52 -04001453#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001454
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001456 _LIBCPP_INLINE_VISIBILITY
1457 allocator_type& __alloc() _NOEXCEPT
1458 {return __r_.second();}
1459 _LIBCPP_INLINE_VISIBILITY
1460 const allocator_type& __alloc() const _NOEXCEPT
1461 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001463#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001464
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001466 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001467# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001469# else
1470 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1471# endif
1472
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001473 _LIBCPP_INLINE_VISIBILITY
1474 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001475# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001477# else
1478 {return __r_.first().__s.__size_;}
1479# endif
1480
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001481#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001482
1483 _LIBCPP_INLINE_VISIBILITY
1484 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001485# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001486 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1487# else
1488 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1489# endif
1490
1491 _LIBCPP_INLINE_VISIBILITY
1492 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001493# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001494 {return __r_.first().__s.__size_;}
1495# else
1496 {return __r_.first().__s.__size_ >> 1;}
1497# endif
1498
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001499#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001500
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001501 _LIBCPP_INLINE_VISIBILITY
1502 void __set_long_size(size_type __s) _NOEXCEPT
1503 {__r_.first().__l.__size_ = __s;}
1504 _LIBCPP_INLINE_VISIBILITY
1505 size_type __get_long_size() const _NOEXCEPT
1506 {return __r_.first().__l.__size_;}
1507 _LIBCPP_INLINE_VISIBILITY
1508 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1510
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001511 _LIBCPP_INLINE_VISIBILITY
1512 void __set_long_cap(size_type __s) _NOEXCEPT
1513 {__r_.first().__l.__cap_ = __long_mask | __s;}
1514 _LIBCPP_INLINE_VISIBILITY
1515 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001516 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001518 _LIBCPP_INLINE_VISIBILITY
1519 void __set_long_pointer(pointer __p) _NOEXCEPT
1520 {__r_.first().__l.__data_ = __p;}
1521 _LIBCPP_INLINE_VISIBILITY
1522 pointer __get_long_pointer() _NOEXCEPT
1523 {return __r_.first().__l.__data_;}
1524 _LIBCPP_INLINE_VISIBILITY
1525 const_pointer __get_long_pointer() const _NOEXCEPT
1526 {return __r_.first().__l.__data_;}
1527 _LIBCPP_INLINE_VISIBILITY
1528 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001529 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001530 _LIBCPP_INLINE_VISIBILITY
1531 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001532 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001533 _LIBCPP_INLINE_VISIBILITY
1534 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001536 _LIBCPP_INLINE_VISIBILITY
1537 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1539
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001540 _LIBCPP_INLINE_VISIBILITY
1541 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 {
1543 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1544 for (unsigned __i = 0; __i < __n_words; ++__i)
1545 __a[__i] = 0;
1546 }
1547
1548 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001550 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001551 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001553 static _LIBCPP_INLINE_VISIBILITY
1554 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001555 {
1556 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1557 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1558 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1559 if (__guess == __min_cap) ++__guess;
1560 return __guess;
1561 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001563 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001564 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001565 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001566 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001567 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001569
Martijn Vels5e7c9752020-03-04 17:52:46 -05001570 // Slow path for the (inlined) copy constructor for 'long' strings.
1571 // Always externally instantiated and not inlined.
1572 // Requires that __s is zero terminated.
1573 // The main reason for this function to exist is because for unstable, we
1574 // want to allow inlining of the copy constructor. However, we don't want
1575 // to call the __init() functions as those are marked as inline which may
1576 // result in over-aggressive inlining by the compiler, where our aim is
1577 // to only inline the fast path code directly in the ctor.
1578 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1579
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001581 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001582 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001584 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1585 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 __init(_InputIterator __first, _InputIterator __last);
1587
1588 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001589 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001590 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001592 __is_cpp17_forward_iterator<_ForwardIterator>::value
1593 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 __init(_ForwardIterator __first, _ForwardIterator __last);
1595
1596 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001597 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1599 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001600 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601
Martijn Vels596e3de2020-02-26 15:55:49 -05001602 // __assign_no_alias is invoked for assignment operations where we
1603 // have proof that the input does not alias the current instance.
1604 // For example, operator=(basic_string) performs a 'self' check.
1605 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001606 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001607
Howard Hinnantcf823322010-12-17 14:46:43 +00001608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609 void __erase_to_end(size_type __pos);
1610
Martijn Velsa81fc792020-02-26 13:25:43 -05001611 // __erase_external_with_move is invoked for erase() invocations where
1612 // `n ~= npos`, likely requiring memory moves on the string data.
1613 void __erase_external_with_move(size_type __pos, size_type __n);
1614
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001615 _LIBCPP_INLINE_VISIBILITY
1616 void __copy_assign_alloc(const basic_string& __str)
1617 {__copy_assign_alloc(__str, integral_constant<bool,
1618 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1619
1620 _LIBCPP_INLINE_VISIBILITY
1621 void __copy_assign_alloc(const basic_string& __str, true_type)
1622 {
Marshall Clowf258c202017-01-31 03:40:52 +00001623 if (__alloc() == __str.__alloc())
1624 __alloc() = __str.__alloc();
1625 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001626 {
Marshall Clowf258c202017-01-31 03:40:52 +00001627 if (!__str.__is_long())
1628 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001629 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001630 __alloc() = __str.__alloc();
1631 }
1632 else
1633 {
1634 allocator_type __a = __str.__alloc();
1635 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001636 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001637 __alloc() = _VSTD::move(__a);
1638 __set_long_pointer(__p);
1639 __set_long_cap(__str.__get_long_cap());
1640 __set_long_size(__str.size());
1641 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001642 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001643 }
1644
1645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001646 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001647 {}
1648
Eric Fiselierfc92be82017-04-19 00:28:44 +00001649#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001650 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001651 void __move_assign(basic_string& __str, false_type)
1652 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001654 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001655#if _LIBCPP_STD_VER > 14
1656 _NOEXCEPT;
1657#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001658 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001659#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001660#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001661
1662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001663 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001664 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001665 _NOEXCEPT_(
1666 !__alloc_traits::propagate_on_container_move_assignment::value ||
1667 is_nothrow_move_assignable<allocator_type>::value)
1668 {__move_assign_alloc(__str, integral_constant<bool,
1669 __alloc_traits::propagate_on_container_move_assignment::value>());}
1670
1671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001672 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001673 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1674 {
1675 __alloc() = _VSTD::move(__c.__alloc());
1676 }
1677
1678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001679 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001680 _NOEXCEPT
1681 {}
1682
Martijn Velsda7d94f2020-06-19 14:24:03 -04001683 basic_string& __assign_external(const value_type* __s);
1684 basic_string& __assign_external(const value_type* __s, size_type __n);
1685
1686 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1687 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1688 pointer __p = __is_long()
1689 ? (__set_long_size(__n), __get_long_pointer())
1690 : (__set_short_size(__n), __get_short_pointer());
1691 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1692 traits_type::assign(__p[__n], value_type());
1693 return *this;
1694 }
1695
Howard Hinnantcf823322010-12-17 14:46:43 +00001696 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1697 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698
1699 friend basic_string operator+<>(const basic_string&, const basic_string&);
1700 friend basic_string operator+<>(const value_type*, const basic_string&);
1701 friend basic_string operator+<>(value_type, const basic_string&);
1702 friend basic_string operator+<>(const basic_string&, const value_type*);
1703 friend basic_string operator+<>(const basic_string&, value_type);
1704};
1705
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001706// These declarations must appear before any functions are implicitly used
1707// so that they have the correct visibility specifier.
1708#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1709_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1710_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1711#else
1712_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1713_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1714#endif
1715
1716
Marshall Clowa0563332018-02-08 06:34:03 +00001717#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1718template<class _InputIterator,
1719 class _CharT = typename iterator_traits<_InputIterator>::value_type,
1720 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001721 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1722 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001723 >
1724basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1725 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001726
1727template<class _CharT,
1728 class _Traits,
1729 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001730 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001731 >
1732explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1733 -> basic_string<_CharT, _Traits, _Allocator>;
1734
1735template<class _CharT,
1736 class _Traits,
1737 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001738 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001739 class _Sz = typename allocator_traits<_Allocator>::size_type
1740 >
1741basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1742 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001743#endif
1744
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001746inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747void
1748basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1749{
Louis Dionneba400782020-10-02 15:02:52 -04001750#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001751 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001752#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753}
1754
1755template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001756inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757void
Howard Hinnant28b24882011-12-01 20:21:04 +00001758basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Louis Dionneba400782020-10-02 15:02:52 -04001759#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant28b24882011-12-01 20:21:04 +00001760 __pos
1761#endif
1762 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763{
Louis Dionneba400782020-10-02 15:02:52 -04001764#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001765 __c_node* __c = __get_db()->__find_c_and_lock(this);
1766 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001768 const_pointer __new_last = __get_pointer() + __pos;
1769 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001771 --__p;
1772 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1773 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001775 (*__p)->__c_ = nullptr;
1776 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001777 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001780 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 }
Louis Dionneba400782020-10-02 15:02:52 -04001782#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783}
1784
1785template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001786inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001787basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001788 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001789 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790{
Louis Dionneba400782020-10-02 15:02:52 -04001791#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001792 __get_db()->__insert_c(this);
1793#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 __zero();
1795}
1796
1797template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001798inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001800#if _LIBCPP_STD_VER <= 14
1801 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1802#else
1803 _NOEXCEPT
1804#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001805: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806{
Louis Dionneba400782020-10-02 15:02:52 -04001807#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001808 __get_db()->__insert_c(this);
1809#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810 __zero();
1811}
1812
1813template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001814void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1815 size_type __sz,
1816 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817{
1818 if (__reserve > max_size())
1819 this->__throw_length_error();
1820 pointer __p;
1821 if (__reserve < __min_cap)
1822 {
1823 __set_short_size(__sz);
1824 __p = __get_short_pointer();
1825 }
1826 else
1827 {
1828 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001829 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830 __set_long_pointer(__p);
1831 __set_long_cap(__cap+1);
1832 __set_long_size(__sz);
1833 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001834 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835 traits_type::assign(__p[__sz], value_type());
1836}
1837
1838template <class _CharT, class _Traits, class _Allocator>
1839void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001840basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841{
1842 if (__sz > max_size())
1843 this->__throw_length_error();
1844 pointer __p;
1845 if (__sz < __min_cap)
1846 {
1847 __set_short_size(__sz);
1848 __p = __get_short_pointer();
1849 }
1850 else
1851 {
1852 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001853 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854 __set_long_pointer(__p);
1855 __set_long_cap(__cap+1);
1856 __set_long_size(__sz);
1857 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001858 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 traits_type::assign(__p[__sz], value_type());
1860}
1861
1862template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001863template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001864basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001865 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001867 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001869#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001870 __get_db()->__insert_c(this);
1871#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872}
1873
1874template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001875inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001876basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001877 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001879 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001881#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001882 __get_db()->__insert_c(this);
1883#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884}
1885
1886template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001887inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001888basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001889 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001891 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001893#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001894 __get_db()->__insert_c(this);
1895#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896}
1897
1898template <class _CharT, class _Traits, class _Allocator>
1899basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001900 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901{
1902 if (!__str.__is_long())
1903 __r_.first().__r = __str.__r_.first().__r;
1904 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001905 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1906 __str.__get_long_size());
1907
Louis Dionneba400782020-10-02 15:02:52 -04001908#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001909 __get_db()->__insert_c(this);
1910#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911}
1912
1913template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001914basic_string<_CharT, _Traits, _Allocator>::basic_string(
1915 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001916 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917{
1918 if (!__str.__is_long())
1919 __r_.first().__r = __str.__r_.first().__r;
1920 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001921 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1922 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001923#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001924 __get_db()->__insert_c(this);
1925#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926}
1927
Martijn Vels5e7c9752020-03-04 17:52:46 -05001928template <class _CharT, class _Traits, class _Allocator>
1929void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1930 const value_type* __s, size_type __sz) {
1931 pointer __p;
1932 if (__sz < __min_cap) {
1933 __p = __get_short_pointer();
1934 __set_short_size(__sz);
1935 } else {
1936 if (__sz > max_size())
1937 this->__throw_length_error();
1938 size_t __cap = __recommend(__sz);
1939 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1940 __set_long_pointer(__p);
1941 __set_long_cap(__cap + 1);
1942 __set_long_size(__sz);
1943 }
1944 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1945}
1946
Eric Fiselierfc92be82017-04-19 00:28:44 +00001947#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948
1949template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001950inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001951basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001952#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001953 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001954#else
1955 _NOEXCEPT
1956#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001957 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958{
1959 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001960#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001961 __get_db()->__insert_c(this);
1962 if (__is_long())
1963 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964#endif
1965}
1966
1967template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001968inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001970 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971{
Marshall Clowd2d24692014-07-17 15:32:20 +00001972 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001973 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001974 else
1975 {
1976 __r_.first().__r = __str.__r_.first().__r;
1977 __str.__zero();
1978 }
Louis Dionneba400782020-10-02 15:02:52 -04001979#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001980 __get_db()->__insert_c(this);
1981 if (__is_long())
1982 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983#endif
1984}
1985
Eric Fiselierfc92be82017-04-19 00:28:44 +00001986#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987
1988template <class _CharT, class _Traits, class _Allocator>
1989void
1990basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1991{
1992 if (__n > max_size())
1993 this->__throw_length_error();
1994 pointer __p;
1995 if (__n < __min_cap)
1996 {
1997 __set_short_size(__n);
1998 __p = __get_short_pointer();
1999 }
2000 else
2001 {
2002 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002003 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004 __set_long_pointer(__p);
2005 __set_long_cap(__cap+1);
2006 __set_long_size(__n);
2007 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002008 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009 traits_type::assign(__p[__n], value_type());
2010}
2011
2012template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002013inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002014basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002015 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016{
2017 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002018#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002019 __get_db()->__insert_c(this);
2020#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021}
2022
2023template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002024template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002025basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002026 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027{
2028 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002029#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002030 __get_db()->__insert_c(this);
2031#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032}
2033
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002035basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2036 size_type __pos, size_type __n,
2037 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002038 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039{
2040 size_type __str_sz = __str.size();
2041 if (__pos > __str_sz)
2042 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002043 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002044#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002045 __get_db()->__insert_c(this);
2046#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047}
2048
2049template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002050inline
Marshall Clow83445802016-04-07 18:13:41 +00002051basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002052 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002053 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002054{
2055 size_type __str_sz = __str.size();
2056 if (__pos > __str_sz)
2057 this->__throw_out_of_range();
2058 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002059#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002060 __get_db()->__insert_c(this);
2061#endif
2062}
2063
2064template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002065template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002066basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002067 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002068 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002069{
Marshall Clowe46031a2018-07-02 18:41:15 +00002070 __self_view __sv0 = __t;
2071 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002072 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002073#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002074 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002075#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002076}
2077
2078template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002079template <class _Tp, class>
2080basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002081 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002082{
Marshall Clowe46031a2018-07-02 18:41:15 +00002083 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002084 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002085#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002086 __get_db()->__insert_c(this);
2087#endif
2088}
2089
2090template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002091template <class _Tp, class>
2092basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002093 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002094{
Marshall Clowe46031a2018-07-02 18:41:15 +00002095 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002096 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002097#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002098 __get_db()->__insert_c(this);
2099#endif
2100}
2101
2102template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002104_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002106 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2107>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2109{
2110 __zero();
2111#ifndef _LIBCPP_NO_EXCEPTIONS
2112 try
2113 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002114#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115 for (; __first != __last; ++__first)
2116 push_back(*__first);
2117#ifndef _LIBCPP_NO_EXCEPTIONS
2118 }
2119 catch (...)
2120 {
2121 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002122 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123 throw;
2124 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002125#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126}
2127
2128template <class _CharT, class _Traits, class _Allocator>
2129template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002130_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002132 __is_cpp17_forward_iterator<_ForwardIterator>::value
2133>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2135{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002136 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137 if (__sz > max_size())
2138 this->__throw_length_error();
2139 pointer __p;
2140 if (__sz < __min_cap)
2141 {
2142 __set_short_size(__sz);
2143 __p = __get_short_pointer();
2144 }
2145 else
2146 {
2147 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002148 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 __set_long_pointer(__p);
2150 __set_long_cap(__cap+1);
2151 __set_long_size(__sz);
2152 }
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002153 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154 traits_type::assign(*__p, *__first);
2155 traits_type::assign(*__p, value_type());
2156}
2157
2158template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002159template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002160inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002162 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163{
2164 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002165#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002166 __get_db()->__insert_c(this);
2167#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168}
2169
2170template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002171template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002172inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2174 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002175 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176{
2177 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002178#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002179 __get_db()->__insert_c(this);
2180#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181}
2182
Eric Fiselierfc92be82017-04-19 00:28:44 +00002183#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002184
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002186inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002187basic_string<_CharT, _Traits, _Allocator>::basic_string(
2188 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002189 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190{
2191 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002192#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002193 __get_db()->__insert_c(this);
2194#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195}
2196
2197template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002198inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002199
Eric Fiselier812882b2017-02-17 01:17:10 +00002200basic_string<_CharT, _Traits, _Allocator>::basic_string(
2201 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002202 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203{
2204 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002205#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002206 __get_db()->__insert_c(this);
2207#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208}
2209
Eric Fiselierfc92be82017-04-19 00:28:44 +00002210#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002211
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2214{
Louis Dionneba400782020-10-02 15:02:52 -04002215#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002216 __get_db()->__erase_c(this);
2217#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002219 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220}
2221
2222template <class _CharT, class _Traits, class _Allocator>
2223void
2224basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2225 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002226 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 +00002227{
2228 size_type __ms = max_size();
2229 if (__delta_cap > __ms - __old_cap - 1)
2230 this->__throw_length_error();
2231 pointer __old_p = __get_pointer();
2232 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002233 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002235 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236 __invalidate_all_iterators();
2237 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002238 traits_type::copy(_VSTD::__to_address(__p),
2239 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002241 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2243 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002244 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2245 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002247 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 __set_long_pointer(__p);
2249 __set_long_cap(__cap+1);
2250 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2251 __set_long_size(__old_sz);
2252 traits_type::assign(__p[__old_sz], value_type());
2253}
2254
2255template <class _CharT, class _Traits, class _Allocator>
2256void
2257basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2258 size_type __n_copy, size_type __n_del, size_type __n_add)
2259{
2260 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002261 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 this->__throw_length_error();
2263 pointer __old_p = __get_pointer();
2264 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002265 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002267 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 __invalidate_all_iterators();
2269 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002270 traits_type::copy(_VSTD::__to_address(__p),
2271 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2273 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002274 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2275 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002276 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002278 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279 __set_long_pointer(__p);
2280 __set_long_cap(__cap+1);
2281}
2282
2283// assign
2284
2285template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002286template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002287basic_string<_CharT, _Traits, _Allocator>&
2288basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002289 const value_type* __s, size_type __n) {
2290 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2291 if (__n < __cap) {
2292 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2293 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2294 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2295 traits_type::assign(__p[__n], value_type());
2296 __invalidate_iterators_past(__n);
2297 } else {
2298 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2299 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2300 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002301 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002302}
2303
2304template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002306basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2307 const value_type* __s, size_type __n) {
2308 size_type __cap = capacity();
2309 if (__cap >= __n) {
2310 value_type* __p = _VSTD::__to_address(__get_pointer());
2311 traits_type::move(__p, __s, __n);
2312 traits_type::assign(__p[__n], value_type());
2313 __set_size(__n);
2314 __invalidate_iterators_past(__n);
2315 } else {
2316 size_type __sz = size();
2317 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2318 }
2319 return *this;
2320}
2321
2322template <class _CharT, class _Traits, class _Allocator>
2323basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002324basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002326 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002327 return (_LIBCPP_BUILTIN_CONSTANT_P(__n) && __n < __min_cap)
2328 ? __assign_short(__s, __n)
2329 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330}
2331
2332template <class _CharT, class _Traits, class _Allocator>
2333basic_string<_CharT, _Traits, _Allocator>&
2334basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2335{
2336 size_type __cap = capacity();
2337 if (__cap < __n)
2338 {
2339 size_type __sz = size();
2340 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2341 }
2342 else
2343 __invalidate_iterators_past(__n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002344 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345 traits_type::assign(__p, __n, __c);
2346 traits_type::assign(__p[__n], value_type());
2347 __set_size(__n);
2348 return *this;
2349}
2350
2351template <class _CharT, class _Traits, class _Allocator>
2352basic_string<_CharT, _Traits, _Allocator>&
2353basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2354{
2355 pointer __p;
2356 if (__is_long())
2357 {
2358 __p = __get_long_pointer();
2359 __set_long_size(1);
2360 }
2361 else
2362 {
2363 __p = __get_short_pointer();
2364 __set_short_size(1);
2365 }
2366 traits_type::assign(*__p, __c);
2367 traits_type::assign(*++__p, value_type());
2368 __invalidate_iterators_past(1);
2369 return *this;
2370}
2371
2372template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002373basic_string<_CharT, _Traits, _Allocator>&
2374basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2375{
Martijn Vels596e3de2020-02-26 15:55:49 -05002376 if (this != &__str) {
2377 __copy_assign_alloc(__str);
2378 if (!__is_long()) {
2379 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002380 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002381 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002382 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002383 }
2384 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002385 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002386 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002387 }
2388 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002389}
2390
Eric Fiselierfc92be82017-04-19 00:28:44 +00002391#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002392
2393template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002394inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002395void
2396basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002397 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002398{
2399 if (__alloc() != __str.__alloc())
2400 assign(__str);
2401 else
2402 __move_assign(__str, true_type());
2403}
2404
2405template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002406inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002407void
2408basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002409#if _LIBCPP_STD_VER > 14
2410 _NOEXCEPT
2411#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002412 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002413#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002414{
marshall2ed41622019-11-27 07:13:00 -08002415 if (__is_long()) {
2416 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2417 __get_long_cap());
2418#if _LIBCPP_STD_VER <= 14
2419 if (!is_nothrow_move_assignable<allocator_type>::value) {
2420 __set_short_size(0);
2421 traits_type::assign(__get_short_pointer()[0], value_type());
2422 }
2423#endif
2424 }
2425 __move_assign_alloc(__str);
2426 __r_.first() = __str.__r_.first();
2427 __str.__set_short_size(0);
2428 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002429}
2430
2431template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002432inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002433basic_string<_CharT, _Traits, _Allocator>&
2434basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002435 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002436{
2437 __move_assign(__str, integral_constant<bool,
2438 __alloc_traits::propagate_on_container_move_assignment::value>());
2439 return *this;
2440}
2441
2442#endif
2443
2444template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002446_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002448 __is_exactly_cpp17_input_iterator <_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002449 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002451>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2453{
Marshall Clow958362f2016-09-05 01:54:30 +00002454 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002455 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002456 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457}
2458
2459template <class _CharT, class _Traits, class _Allocator>
2460template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002461_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002463 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002464 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002466>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2468{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002469 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 size_type __cap = capacity();
2471 if (__cap < __n)
2472 {
2473 size_type __sz = size();
2474 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2475 }
2476 else
2477 __invalidate_iterators_past(__n);
2478 pointer __p = __get_pointer();
2479 for (; __first != __last; ++__first, ++__p)
2480 traits_type::assign(*__p, *__first);
2481 traits_type::assign(*__p, value_type());
2482 __set_size(__n);
2483 return *this;
2484}
2485
2486template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487basic_string<_CharT, _Traits, _Allocator>&
2488basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2489{
2490 size_type __sz = __str.size();
2491 if (__pos > __sz)
2492 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002493 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494}
2495
2496template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002497template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002498_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002499<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002500 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2501 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002502 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002503>
Marshall Clow82513342016-09-24 22:45:42 +00002504basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002505{
Marshall Clow82513342016-09-24 22:45:42 +00002506 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002507 size_type __sz = __sv.size();
2508 if (__pos > __sz)
2509 this->__throw_out_of_range();
2510 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2511}
2512
2513
2514template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002516basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2517 return __assign_external(__s, traits_type::length(__s));
2518}
2519
2520template <class _CharT, class _Traits, class _Allocator>
2521basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002522basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002524 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002525 return _LIBCPP_BUILTIN_CONSTANT_P(*__s)
2526 ? (traits_type::length(__s) < __min_cap
2527 ? __assign_short(__s, traits_type::length(__s))
2528 : __assign_external(__s, traits_type::length(__s)))
2529 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531// append
2532
2533template <class _CharT, class _Traits, class _Allocator>
2534basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002535basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002537 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538 size_type __cap = capacity();
2539 size_type __sz = size();
2540 if (__cap - __sz >= __n)
2541 {
2542 if (__n)
2543 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002544 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545 traits_type::copy(__p + __sz, __s, __n);
2546 __sz += __n;
2547 __set_size(__sz);
2548 traits_type::assign(__p[__sz], value_type());
2549 }
2550 }
2551 else
2552 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2553 return *this;
2554}
2555
2556template <class _CharT, class _Traits, class _Allocator>
2557basic_string<_CharT, _Traits, _Allocator>&
2558basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2559{
2560 if (__n)
2561 {
2562 size_type __cap = capacity();
2563 size_type __sz = size();
2564 if (__cap - __sz < __n)
2565 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2566 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002567 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568 __sz += __n;
2569 __set_size(__sz);
2570 traits_type::assign(__p[__sz], value_type());
2571 }
2572 return *this;
2573}
2574
2575template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002576inline void
2577basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2578{
2579 if (__n)
2580 {
2581 size_type __cap = capacity();
2582 size_type __sz = size();
2583 if (__cap - __sz < __n)
2584 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2585 pointer __p = __get_pointer();
2586 __sz += __n;
2587 __set_size(__sz);
2588 traits_type::assign(__p[__sz], value_type());
2589 }
2590}
2591
2592template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593void
2594basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2595{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002596 bool __is_short = !__is_long();
2597 size_type __cap;
2598 size_type __sz;
2599 if (__is_short)
2600 {
2601 __cap = __min_cap - 1;
2602 __sz = __get_short_size();
2603 }
2604 else
2605 {
2606 __cap = __get_long_cap() - 1;
2607 __sz = __get_long_size();
2608 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002610 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002612 __is_short = !__is_long();
2613 }
2614 pointer __p;
2615 if (__is_short)
2616 {
2617 __p = __get_short_pointer() + __sz;
2618 __set_short_size(__sz+1);
2619 }
2620 else
2621 {
2622 __p = __get_long_pointer() + __sz;
2623 __set_long_size(__sz+1);
2624 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625 traits_type::assign(*__p, __c);
2626 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627}
2628
Marshall Clow6ebbf662016-09-07 03:32:06 +00002629template <class _Tp>
Marshall Clow958362f2016-09-05 01:54:30 +00002630bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2631{
2632 return __first <= __p && __p < __last;
2633}
2634
Marshall Clow6ebbf662016-09-07 03:32:06 +00002635template <class _Tp1, class _Tp2>
Eric Fiselier6003c772016-12-23 23:37:52 +00002636bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clow6ebbf662016-09-07 03:32:06 +00002637{
2638 return false;
2639}
2640
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641template <class _CharT, class _Traits, class _Allocator>
2642template<class _ForwardIterator>
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002643basic_string<_CharT, _Traits, _Allocator>&
2644basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2645 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646{
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002647 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002648 "function requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649 size_type __sz = size();
2650 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002651 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652 if (__n)
2653 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002654 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2655 _CharRef __tmp_ref = *__first;
2656 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002657 {
2658 const basic_string __temp (__first, __last, __alloc());
2659 append(__temp.data(), __temp.size());
2660 }
Louis Dionne173f29e2019-05-29 16:01:36 +00002661 else
Marshall Clow958362f2016-09-05 01:54:30 +00002662 {
2663 if (__cap - __sz < __n)
2664 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2665 pointer __p = __get_pointer() + __sz;
2666 for (; __first != __last; ++__p, ++__first)
2667 traits_type::assign(*__p, *__first);
2668 traits_type::assign(*__p, value_type());
2669 __set_size(__sz + __n);
2670 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671 }
2672 return *this;
2673}
2674
2675template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002676inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677basic_string<_CharT, _Traits, _Allocator>&
2678basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2679{
2680 return append(__str.data(), __str.size());
2681}
2682
2683template <class _CharT, class _Traits, class _Allocator>
2684basic_string<_CharT, _Traits, _Allocator>&
2685basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2686{
2687 size_type __sz = __str.size();
2688 if (__pos > __sz)
2689 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002690 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691}
2692
2693template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002694template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002695 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002696 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002697 __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 +00002698 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002699 >
Marshall Clow82513342016-09-24 22:45:42 +00002700basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002701{
Marshall Clow82513342016-09-24 22:45:42 +00002702 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002703 size_type __sz = __sv.size();
2704 if (__pos > __sz)
2705 this->__throw_out_of_range();
2706 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2707}
2708
2709template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002711basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002713 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714 return append(__s, traits_type::length(__s));
2715}
2716
2717// insert
2718
2719template <class _CharT, class _Traits, class _Allocator>
2720basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002721basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002723 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724 size_type __sz = size();
2725 if (__pos > __sz)
2726 this->__throw_out_of_range();
2727 size_type __cap = capacity();
2728 if (__cap - __sz >= __n)
2729 {
2730 if (__n)
2731 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002732 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 size_type __n_move = __sz - __pos;
2734 if (__n_move != 0)
2735 {
2736 if (__p + __pos <= __s && __s < __p + __sz)
2737 __s += __n;
2738 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2739 }
2740 traits_type::move(__p + __pos, __s, __n);
2741 __sz += __n;
2742 __set_size(__sz);
2743 traits_type::assign(__p[__sz], value_type());
2744 }
2745 }
2746 else
2747 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2748 return *this;
2749}
2750
2751template <class _CharT, class _Traits, class _Allocator>
2752basic_string<_CharT, _Traits, _Allocator>&
2753basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2754{
2755 size_type __sz = size();
2756 if (__pos > __sz)
2757 this->__throw_out_of_range();
2758 if (__n)
2759 {
2760 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002761 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762 if (__cap - __sz >= __n)
2763 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002764 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765 size_type __n_move = __sz - __pos;
2766 if (__n_move != 0)
2767 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2768 }
2769 else
2770 {
2771 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002772 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773 }
2774 traits_type::assign(__p + __pos, __n, __c);
2775 __sz += __n;
2776 __set_size(__sz);
2777 traits_type::assign(__p[__sz], value_type());
2778 }
2779 return *this;
2780}
2781
2782template <class _CharT, class _Traits, class _Allocator>
2783template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002784_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002786 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002787 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2788 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002789>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2791{
Louis Dionneba400782020-10-02 15:02:52 -04002792#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002793 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2794 "string::insert(iterator, range) called with an iterator not"
2795 " referring to this string");
2796#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002797 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002798 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799}
2800
2801template <class _CharT, class _Traits, class _Allocator>
2802template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002803_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002805 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002806 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002808>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2810{
Louis Dionneba400782020-10-02 15:02:52 -04002811#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002812 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2813 "string::insert(iterator, range) called with an iterator not"
2814 " referring to this string");
2815#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002817 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818 if (__n)
2819 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002820 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2821 _CharRef __tmp_char = *__first;
2822 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002823 {
2824 const basic_string __temp(__first, __last, __alloc());
2825 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2826 }
2827
2828 size_type __sz = size();
2829 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002830 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 if (__cap - __sz >= __n)
2832 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002833 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 size_type __n_move = __sz - __ip;
2835 if (__n_move != 0)
2836 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2837 }
2838 else
2839 {
2840 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002841 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842 }
2843 __sz += __n;
2844 __set_size(__sz);
2845 traits_type::assign(__p[__sz], value_type());
2846 for (__p += __ip; __first != __last; ++__p, ++__first)
2847 traits_type::assign(*__p, *__first);
2848 }
2849 return begin() + __ip;
2850}
2851
2852template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002853inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854basic_string<_CharT, _Traits, _Allocator>&
2855basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2856{
2857 return insert(__pos1, __str.data(), __str.size());
2858}
2859
2860template <class _CharT, class _Traits, class _Allocator>
2861basic_string<_CharT, _Traits, _Allocator>&
2862basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2863 size_type __pos2, size_type __n)
2864{
2865 size_type __str_sz = __str.size();
2866 if (__pos2 > __str_sz)
2867 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002868 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869}
2870
2871template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002872template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002873_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002874<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002875 __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 +00002876 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002877>
Marshall Clow82513342016-09-24 22:45:42 +00002878basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002879 size_type __pos2, size_type __n)
2880{
Marshall Clow82513342016-09-24 22:45:42 +00002881 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002882 size_type __str_sz = __sv.size();
2883 if (__pos2 > __str_sz)
2884 this->__throw_out_of_range();
2885 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2886}
2887
2888template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002890basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002892 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 return insert(__pos, __s, traits_type::length(__s));
2894}
2895
2896template <class _CharT, class _Traits, class _Allocator>
2897typename basic_string<_CharT, _Traits, _Allocator>::iterator
2898basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2899{
2900 size_type __ip = static_cast<size_type>(__pos - begin());
2901 size_type __sz = size();
2902 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002903 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904 if (__cap == __sz)
2905 {
2906 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002907 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 }
2909 else
2910 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002911 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 size_type __n_move = __sz - __ip;
2913 if (__n_move != 0)
2914 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2915 }
2916 traits_type::assign(__p[__ip], __c);
2917 traits_type::assign(__p[++__sz], value_type());
2918 __set_size(__sz);
2919 return begin() + static_cast<difference_type>(__ip);
2920}
2921
2922template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002923inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924typename basic_string<_CharT, _Traits, _Allocator>::iterator
2925basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2926{
Louis Dionneba400782020-10-02 15:02:52 -04002927#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002928 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2929 "string::insert(iterator, n, value) called with an iterator not"
2930 " referring to this string");
2931#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 difference_type __p = __pos - begin();
2933 insert(static_cast<size_type>(__p), __n, __c);
2934 return begin() + __p;
2935}
2936
2937// replace
2938
2939template <class _CharT, class _Traits, class _Allocator>
2940basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002941basic_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 +00002942 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002944 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945 size_type __sz = size();
2946 if (__pos > __sz)
2947 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002948 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949 size_type __cap = capacity();
2950 if (__cap - __sz + __n1 >= __n2)
2951 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002952 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953 if (__n1 != __n2)
2954 {
2955 size_type __n_move = __sz - __pos - __n1;
2956 if (__n_move != 0)
2957 {
2958 if (__n1 > __n2)
2959 {
2960 traits_type::move(__p + __pos, __s, __n2);
2961 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2962 goto __finish;
2963 }
2964 if (__p + __pos < __s && __s < __p + __sz)
2965 {
2966 if (__p + __pos + __n1 <= __s)
2967 __s += __n2 - __n1;
2968 else // __p + __pos < __s < __p + __pos + __n1
2969 {
2970 traits_type::move(__p + __pos, __s, __n1);
2971 __pos += __n1;
2972 __s += __n2;
2973 __n2 -= __n1;
2974 __n1 = 0;
2975 }
2976 }
2977 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2978 }
2979 }
2980 traits_type::move(__p + __pos, __s, __n2);
2981__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05002982// __sz += __n2 - __n1; in this and the below function below can cause unsigned
2983// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984 __sz += __n2 - __n1;
2985 __set_size(__sz);
2986 __invalidate_iterators_past(__sz);
2987 traits_type::assign(__p[__sz], value_type());
2988 }
2989 else
2990 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2991 return *this;
2992}
2993
2994template <class _CharT, class _Traits, class _Allocator>
2995basic_string<_CharT, _Traits, _Allocator>&
2996basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00002997 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998{
2999 size_type __sz = size();
3000 if (__pos > __sz)
3001 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003002 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003003 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003004 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005 if (__cap - __sz + __n1 >= __n2)
3006 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003007 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003008 if (__n1 != __n2)
3009 {
3010 size_type __n_move = __sz - __pos - __n1;
3011 if (__n_move != 0)
3012 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3013 }
3014 }
3015 else
3016 {
3017 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003018 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 }
3020 traits_type::assign(__p + __pos, __n2, __c);
3021 __sz += __n2 - __n1;
3022 __set_size(__sz);
3023 __invalidate_iterators_past(__sz);
3024 traits_type::assign(__p[__sz], value_type());
3025 return *this;
3026}
3027
3028template <class _CharT, class _Traits, class _Allocator>
3029template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003030_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00003031<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003032 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003034>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003035basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036 _InputIterator __j1, _InputIterator __j2)
3037{
Marshall Clow958362f2016-09-05 01:54:30 +00003038 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003039 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040}
3041
3042template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003043inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003044basic_string<_CharT, _Traits, _Allocator>&
3045basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3046{
3047 return replace(__pos1, __n1, __str.data(), __str.size());
3048}
3049
3050template <class _CharT, class _Traits, class _Allocator>
3051basic_string<_CharT, _Traits, _Allocator>&
3052basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3053 size_type __pos2, size_type __n2)
3054{
3055 size_type __str_sz = __str.size();
3056 if (__pos2 > __str_sz)
3057 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003058 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059}
3060
3061template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003062template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003063_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003064<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003065 __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 +00003066 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003067>
Marshall Clow82513342016-09-24 22:45:42 +00003068basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003069 size_type __pos2, size_type __n2)
3070{
Marshall Clow82513342016-09-24 22:45:42 +00003071 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003072 size_type __str_sz = __sv.size();
3073 if (__pos2 > __str_sz)
3074 this->__throw_out_of_range();
3075 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3076}
3077
3078template <class _CharT, class _Traits, class _Allocator>
3079basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003080basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003082 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083 return replace(__pos, __n1, __s, traits_type::length(__s));
3084}
3085
3086template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003087inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003089basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090{
3091 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3092 __str.data(), __str.size());
3093}
3094
3095template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003096inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003098basic_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 +00003099{
3100 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3101}
3102
3103template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003104inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003106basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107{
3108 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3109}
3110
3111template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003112inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003114basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115{
3116 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3117}
3118
3119// erase
3120
Martijn Velsa81fc792020-02-26 13:25:43 -05003121// 'externally instantiated' erase() implementation, called when __n != npos.
3122// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003124void
3125basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3126 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 if (__n)
3129 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003130 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003131 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003132 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 size_type __n_move = __sz - __pos - __n;
3134 if (__n_move != 0)
3135 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3136 __sz -= __n;
3137 __set_size(__sz);
3138 __invalidate_iterators_past(__sz);
3139 traits_type::assign(__p[__sz], value_type());
3140 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003141}
3142
3143template <class _CharT, class _Traits, class _Allocator>
3144basic_string<_CharT, _Traits, _Allocator>&
3145basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3146 size_type __n) {
3147 if (__pos > size()) this->__throw_out_of_range();
3148 if (__n == npos) {
3149 __erase_to_end(__pos);
3150 } else {
3151 __erase_external_with_move(__pos, __n);
3152 }
3153 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154}
3155
3156template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003157inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158typename basic_string<_CharT, _Traits, _Allocator>::iterator
3159basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3160{
Louis Dionneba400782020-10-02 15:02:52 -04003161#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003162 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3163 "string::erase(iterator) called with an iterator not"
3164 " referring to this string");
3165#endif
3166 _LIBCPP_ASSERT(__pos != end(),
3167 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168 iterator __b = begin();
3169 size_type __r = static_cast<size_type>(__pos - __b);
3170 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003171 return __b + static_cast<difference_type>(__r);
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 __first, const_iterator __last)
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(&__first) == this,
3181 "string::erase(iterator, iterator) called with an iterator not"
3182 " referring to this string");
3183#endif
3184 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185 iterator __b = begin();
3186 size_type __r = static_cast<size_type>(__first - __b);
3187 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003188 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189}
3190
3191template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193void
3194basic_string<_CharT, _Traits, _Allocator>::pop_back()
3195{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003196 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197 size_type __sz;
3198 if (__is_long())
3199 {
3200 __sz = __get_long_size() - 1;
3201 __set_long_size(__sz);
3202 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3203 }
3204 else
3205 {
3206 __sz = __get_short_size() - 1;
3207 __set_short_size(__sz);
3208 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3209 }
3210 __invalidate_iterators_past(__sz);
3211}
3212
3213template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003214inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003216basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217{
3218 __invalidate_all_iterators();
3219 if (__is_long())
3220 {
3221 traits_type::assign(*__get_long_pointer(), value_type());
3222 __set_long_size(0);
3223 }
3224 else
3225 {
3226 traits_type::assign(*__get_short_pointer(), value_type());
3227 __set_short_size(0);
3228 }
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
3234basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3235{
3236 if (__is_long())
3237 {
3238 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3239 __set_long_size(__pos);
3240 }
3241 else
3242 {
3243 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3244 __set_short_size(__pos);
3245 }
3246 __invalidate_iterators_past(__pos);
3247}
3248
3249template <class _CharT, class _Traits, class _Allocator>
3250void
3251basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3252{
3253 size_type __sz = size();
3254 if (__n > __sz)
3255 append(__n - __sz, __c);
3256 else
3257 __erase_to_end(__n);
3258}
3259
3260template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003261inline void
3262basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3263{
3264 size_type __sz = size();
3265 if (__n > __sz) {
3266 __append_default_init(__n - __sz);
3267 } else
3268 __erase_to_end(__n);
3269}
3270
3271template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003272inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003274basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003276 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003277#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003278 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003280 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281#endif
3282}
3283
3284template <class _CharT, class _Traits, class _Allocator>
3285void
Marek Kurdejc9848142020-11-26 10:07:16 +01003286basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003287{
Marek Kurdejc9848142020-11-26 10:07:16 +01003288 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003290
3291#if _LIBCPP_STD_VER > 17
3292 // Reserve never shrinks as of C++20.
3293 if (__requested_capacity <= capacity()) return;
3294#endif
3295
3296 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3297 __target_capacity = __recommend(__target_capacity);
3298 if (__target_capacity == capacity()) return;
3299
3300 __shrink_or_extend(__target_capacity);
3301}
3302
3303template <class _CharT, class _Traits, class _Allocator>
3304void
3305basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3306{
3307 size_type __target_capacity = __recommend(size());
3308 if (__target_capacity == capacity()) return;
3309
3310 __shrink_or_extend(__target_capacity);
3311}
3312
3313template <class _CharT, class _Traits, class _Allocator>
3314void
3315basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3316{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003317 size_type __cap = capacity();
3318 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003319
3320 pointer __new_data, __p;
3321 bool __was_long, __now_long;
3322 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003324 __was_long = true;
3325 __now_long = false;
3326 __new_data = __get_short_pointer();
3327 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003329 else
3330 {
3331 if (__target_capacity > __cap)
3332 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3333 else
3334 {
3335 #ifndef _LIBCPP_NO_EXCEPTIONS
3336 try
3337 {
3338 #endif // _LIBCPP_NO_EXCEPTIONS
3339 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3340 #ifndef _LIBCPP_NO_EXCEPTIONS
3341 }
3342 catch (...)
3343 {
3344 return;
3345 }
3346 #else // _LIBCPP_NO_EXCEPTIONS
3347 if (__new_data == nullptr)
3348 return;
3349 #endif // _LIBCPP_NO_EXCEPTIONS
3350 }
3351 __now_long = true;
3352 __was_long = __is_long();
3353 __p = __get_pointer();
3354 }
3355 traits_type::copy(_VSTD::__to_address(__new_data),
3356 _VSTD::__to_address(__p), size()+1);
3357 if (__was_long)
3358 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3359 if (__now_long)
3360 {
3361 __set_long_cap(__target_capacity+1);
3362 __set_long_size(__sz);
3363 __set_long_pointer(__new_data);
3364 }
3365 else
3366 __set_short_size(__sz);
3367 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368}
3369
3370template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003371inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003373basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003375 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376 return *(data() + __pos);
3377}
3378
3379template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003380inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003381typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003382basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003384 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385 return *(__get_pointer() + __pos);
3386}
3387
3388template <class _CharT, class _Traits, class _Allocator>
3389typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3390basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3391{
3392 if (__n >= size())
3393 this->__throw_out_of_range();
3394 return (*this)[__n];
3395}
3396
3397template <class _CharT, class _Traits, class _Allocator>
3398typename basic_string<_CharT, _Traits, _Allocator>::reference
3399basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3400{
3401 if (__n >= size())
3402 this->__throw_out_of_range();
3403 return (*this)[__n];
3404}
3405
3406template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003407inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003409basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003411 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412 return *__get_pointer();
3413}
3414
3415template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003416inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003418basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003419{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003420 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003421 return *data();
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>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003429 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430 return *(__get_pointer() + size() - 1);
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>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003438 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003439 return *(data() + size() - 1);
3440}
3441
3442template <class _CharT, class _Traits, class _Allocator>
3443typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003444basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445{
3446 size_type __sz = size();
3447 if (__pos > __sz)
3448 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003449 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450 traits_type::copy(__s, data() + __pos, __rlen);
3451 return __rlen;
3452}
3453
3454template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003455inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456basic_string<_CharT, _Traits, _Allocator>
3457basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3458{
3459 return basic_string(*this, __pos, __n, __alloc());
3460}
3461
3462template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003463inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464void
3465basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003466#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003467 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003468#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003469 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003470 __is_nothrow_swappable<allocator_type>::value)
3471#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472{
Louis Dionneba400782020-10-02 15:02:52 -04003473#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003474 if (!__is_long())
3475 __get_db()->__invalidate_all(this);
3476 if (!__str.__is_long())
3477 __get_db()->__invalidate_all(&__str);
3478 __get_db()->swap(this, &__str);
3479#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003480 _LIBCPP_ASSERT(
3481 __alloc_traits::propagate_on_container_swap::value ||
3482 __alloc_traits::is_always_equal::value ||
3483 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003484 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003485 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486}
3487
3488// find
3489
3490template <class _Traits>
3491struct _LIBCPP_HIDDEN __traits_eq
3492{
3493 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003494 _LIBCPP_INLINE_VISIBILITY
3495 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3496 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497};
3498
3499template<class _CharT, class _Traits, class _Allocator>
3500typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003501basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003502 size_type __pos,
3503 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003505 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003506 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003507 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508}
3509
3510template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003511inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003513basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3514 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003516 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003517 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518}
3519
3520template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003521template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003522_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003523<
3524 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3525 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003526>
Marshall Clowe46031a2018-07-02 18:41:15 +00003527basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
3528 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003529{
Marshall Clowe46031a2018-07-02 18:41:15 +00003530 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003531 return __str_find<value_type, size_type, traits_type, npos>
3532 (data(), size(), __sv.data(), __pos, __sv.size());
3533}
3534
3535template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003536inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003537typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003538basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003539 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003541 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003542 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003543 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544}
3545
3546template<class _CharT, class _Traits, class _Allocator>
3547typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003548basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3549 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003551 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003552 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553}
3554
3555// rfind
3556
3557template<class _CharT, class _Traits, class _Allocator>
3558typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003559basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003560 size_type __pos,
3561 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003563 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003564 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003565 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566}
3567
3568template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003569inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003571basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3572 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003574 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003575 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576}
3577
3578template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003579template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003580_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003581<
3582 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3583 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003584>
Marshall Clowe46031a2018-07-02 18:41:15 +00003585basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
3586 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003587{
Marshall Clowe46031a2018-07-02 18:41:15 +00003588 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003589 return __str_rfind<value_type, size_type, traits_type, npos>
3590 (data(), size(), __sv.data(), __pos, __sv.size());
3591}
3592
3593template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003594inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003595typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003596basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003597 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003599 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003600 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003601 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602}
3603
3604template<class _CharT, class _Traits, class _Allocator>
3605typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003606basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3607 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003609 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003610 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611}
3612
3613// find_first_of
3614
3615template<class _CharT, class _Traits, class _Allocator>
3616typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003617basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003618 size_type __pos,
3619 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003621 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003622 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003623 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624}
3625
3626template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003627inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003629basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3630 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003632 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003633 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634}
3635
3636template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003637template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003638_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003639<
3640 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3641 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003642>
Marshall Clowe46031a2018-07-02 18:41:15 +00003643basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
3644 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003645{
Marshall Clowe46031a2018-07-02 18:41:15 +00003646 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003647 return __str_find_first_of<value_type, size_type, traits_type, npos>
3648 (data(), size(), __sv.data(), __pos, __sv.size());
3649}
3650
3651template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003652inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003653typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003654basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003655 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003657 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003658 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003659 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660}
3661
3662template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003663inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003665basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3666 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667{
3668 return find(__c, __pos);
3669}
3670
3671// find_last_of
3672
3673template<class _CharT, class _Traits, class _Allocator>
3674typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003675basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003676 size_type __pos,
3677 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003679 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003680 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003681 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682}
3683
3684template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003685inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003687basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3688 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003690 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003691 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692}
3693
3694template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003695template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003696_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003697<
3698 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3699 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003700>
Marshall Clowe46031a2018-07-02 18:41:15 +00003701basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
3702 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003703{
Marshall Clowe46031a2018-07-02 18:41:15 +00003704 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003705 return __str_find_last_of<value_type, size_type, traits_type, npos>
3706 (data(), size(), __sv.data(), __pos, __sv.size());
3707}
3708
3709template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003710inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003711typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003712basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003713 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003715 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003716 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003717 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718}
3719
3720template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003721inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003723basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3724 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725{
3726 return rfind(__c, __pos);
3727}
3728
3729// find_first_not_of
3730
3731template<class _CharT, class _Traits, class _Allocator>
3732typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003733basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003734 size_type __pos,
3735 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003737 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003738 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003739 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740}
3741
3742template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003743inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003745basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3746 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003748 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003749 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750}
3751
3752template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003753template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003754_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003755<
3756 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3757 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003758>
Marshall Clowe46031a2018-07-02 18:41:15 +00003759basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
3760 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003761{
Marshall Clowe46031a2018-07-02 18:41:15 +00003762 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003763 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3764 (data(), size(), __sv.data(), __pos, __sv.size());
3765}
3766
3767template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003768inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003769typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003770basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003771 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003773 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003774 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003775 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776}
3777
3778template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003779inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003781basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3782 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003784 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003785 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786}
3787
3788// find_last_not_of
3789
3790template<class _CharT, class _Traits, class _Allocator>
3791typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003792basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003793 size_type __pos,
3794 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003796 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003797 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003798 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799}
3800
3801template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003802inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003804basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3805 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003807 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003808 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809}
3810
3811template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003812template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003813_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003814<
3815 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3816 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003817>
Marshall Clowe46031a2018-07-02 18:41:15 +00003818basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
3819 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003820{
Marshall Clowe46031a2018-07-02 18:41:15 +00003821 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003822 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3823 (data(), size(), __sv.data(), __pos, __sv.size());
3824}
3825
3826template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003827inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003828typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003829basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003830 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003831{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003832 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003833 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003834 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835}
3836
3837template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003838inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003840basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3841 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003843 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003844 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845}
3846
3847// compare
3848
3849template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003850template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003851_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003852<
3853 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3854 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003855>
Marshall Clowe46031a2018-07-02 18:41:15 +00003856basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857{
Marshall Clowe46031a2018-07-02 18:41:15 +00003858 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003859 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003860 size_t __rhs_sz = __sv.size();
3861 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003862 _VSTD::min(__lhs_sz, __rhs_sz));
3863 if (__result != 0)
3864 return __result;
3865 if (__lhs_sz < __rhs_sz)
3866 return -1;
3867 if (__lhs_sz > __rhs_sz)
3868 return 1;
3869 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870}
3871
3872template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003873inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003875basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003877 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878}
3879
3880template <class _CharT, class _Traits, class _Allocator>
3881int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003882basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3883 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003884 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003885 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003887 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003888 size_type __sz = size();
3889 if (__pos1 > __sz || __n2 == npos)
3890 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003891 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3892 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893 if (__r == 0)
3894 {
3895 if (__rlen < __n2)
3896 __r = -1;
3897 else if (__rlen > __n2)
3898 __r = 1;
3899 }
3900 return __r;
3901}
3902
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003903template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003904template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003905_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003906<
3907 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3908 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003909>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003910basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3911 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003912 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003913{
Marshall Clowe46031a2018-07-02 18:41:15 +00003914 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003915 return compare(__pos1, __n1, __sv.data(), __sv.size());
3916}
3917
3918template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003919inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003920int
3921basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3922 size_type __n1,
3923 const basic_string& __str) const
3924{
3925 return compare(__pos1, __n1, __str.data(), __str.size());
3926}
3927
3928template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003929template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003930_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003931<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003932 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3933 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003934 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003935>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003936basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3937 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003938 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003939 size_type __pos2,
3940 size_type __n2) const
3941{
Marshall Clow82513342016-09-24 22:45:42 +00003942 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003943 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3944}
3945
3946template <class _CharT, class _Traits, class _Allocator>
3947int
3948basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3949 size_type __n1,
3950 const basic_string& __str,
3951 size_type __pos2,
3952 size_type __n2) const
3953{
3954 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3955}
3956
3957template <class _CharT, class _Traits, class _Allocator>
3958int
3959basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3960{
3961 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3962 return compare(0, npos, __s, traits_type::length(__s));
3963}
3964
3965template <class _CharT, class _Traits, class _Allocator>
3966int
3967basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3968 size_type __n1,
3969 const value_type* __s) const
3970{
3971 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3972 return compare(__pos1, __n1, __s, traits_type::length(__s));
3973}
3974
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975// __invariants
3976
3977template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003978inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979bool
3980basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3981{
3982 if (size() > capacity())
3983 return false;
3984 if (capacity() < __min_cap - 1)
3985 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003986 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003988 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989 return false;
3990 return true;
3991}
3992
Vedant Kumar55e007e2018-03-08 21:15:26 +00003993// __clear_and_shrink
3994
3995template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003996inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003997void
Marshall Clowe60a7182018-05-29 17:04:37 +00003998basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003999{
4000 clear();
4001 if(__is_long())
4002 {
4003 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4004 __set_long_cap(0);
4005 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004006 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004007 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004008}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004009
Howard Hinnantc51e1022010-05-11 19:42:16 +00004010// operator==
4011
4012template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004013inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014bool
4015operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004016 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004018 size_t __lhs_sz = __lhs.size();
4019 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4020 __rhs.data(),
4021 __lhs_sz) == 0;
4022}
4023
4024template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004025inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004026bool
4027operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4028 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4029{
4030 size_t __lhs_sz = __lhs.size();
4031 if (__lhs_sz != __rhs.size())
4032 return false;
4033 const char* __lp = __lhs.data();
4034 const char* __rp = __rhs.data();
4035 if (__lhs.__is_long())
4036 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4037 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4038 if (*__lp != *__rp)
4039 return false;
4040 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041}
4042
4043template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004044inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004046operator==(const _CharT* __lhs,
4047 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004049 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4050 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4051 size_t __lhs_len = _Traits::length(__lhs);
4052 if (__lhs_len != __rhs.size()) return false;
4053 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004054}
4055
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004057inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004059operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4060 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004062 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4063 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4064 size_t __rhs_len = _Traits::length(__rhs);
4065 if (__rhs_len != __lhs.size()) return false;
4066 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067}
4068
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004069template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004070inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071bool
4072operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004073 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074{
4075 return !(__lhs == __rhs);
4076}
4077
4078template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004079inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004081operator!=(const _CharT* __lhs,
4082 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083{
4084 return !(__lhs == __rhs);
4085}
4086
4087template<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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004090operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4091 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092{
4093 return !(__lhs == __rhs);
4094}
4095
4096// operator<
4097
4098template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004099inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100bool
4101operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004102 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004104 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105}
4106
4107template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004108inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004110operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4111 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004113 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114}
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004119operator< (const _CharT* __lhs,
4120 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121{
4122 return __rhs.compare(__lhs) > 0;
4123}
4124
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125// operator>
4126
4127template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004128inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129bool
4130operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004131 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132{
4133 return __rhs < __lhs;
4134}
4135
4136template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004137inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004139operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4140 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141{
4142 return __rhs < __lhs;
4143}
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004148operator> (const _CharT* __lhs,
4149 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150{
4151 return __rhs < __lhs;
4152}
4153
4154// operator<=
4155
4156template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004157inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158bool
4159operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004160 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161{
4162 return !(__rhs < __lhs);
4163}
4164
4165template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004166inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004168operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4169 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004170{
4171 return !(__rhs < __lhs);
4172}
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004177operator<=(const _CharT* __lhs,
4178 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179{
4180 return !(__rhs < __lhs);
4181}
4182
4183// operator>=
4184
4185template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004186inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187bool
4188operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004189 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190{
4191 return !(__lhs < __rhs);
4192}
4193
4194template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004195inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004197operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4198 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004199{
4200 return !(__lhs < __rhs);
4201}
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004206operator>=(const _CharT* __lhs,
4207 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208{
4209 return !(__lhs < __rhs);
4210}
4211
4212// operator +
4213
4214template<class _CharT, class _Traits, class _Allocator>
4215basic_string<_CharT, _Traits, _Allocator>
4216operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4217 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4218{
4219 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4220 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4221 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4222 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4223 __r.append(__rhs.data(), __rhs_sz);
4224 return __r;
4225}
4226
4227template<class _CharT, class _Traits, class _Allocator>
4228basic_string<_CharT, _Traits, _Allocator>
4229operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4230{
4231 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4232 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4233 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4234 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4235 __r.append(__rhs.data(), __rhs_sz);
4236 return __r;
4237}
4238
4239template<class _CharT, class _Traits, class _Allocator>
4240basic_string<_CharT, _Traits, _Allocator>
4241operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4242{
4243 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4244 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4245 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4246 __r.append(__rhs.data(), __rhs_sz);
4247 return __r;
4248}
4249
4250template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004251inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252basic_string<_CharT, _Traits, _Allocator>
4253operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4254{
4255 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4256 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4257 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4258 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4259 __r.append(__rhs, __rhs_sz);
4260 return __r;
4261}
4262
4263template<class _CharT, class _Traits, class _Allocator>
4264basic_string<_CharT, _Traits, _Allocator>
4265operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4266{
4267 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4268 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4269 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4270 __r.push_back(__rhs);
4271 return __r;
4272}
4273
Eric Fiselierfc92be82017-04-19 00:28:44 +00004274#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275
4276template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004277inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278basic_string<_CharT, _Traits, _Allocator>
4279operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4280{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004281 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282}
4283
4284template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004285inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286basic_string<_CharT, _Traits, _Allocator>
4287operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4288{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004289 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290}
4291
4292template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004293inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294basic_string<_CharT, _Traits, _Allocator>
4295operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4296{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004297 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004298}
4299
4300template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302basic_string<_CharT, _Traits, _Allocator>
4303operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4304{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004305 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306}
4307
4308template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310basic_string<_CharT, _Traits, _Allocator>
4311operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4312{
4313 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004314 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315}
4316
4317template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004318inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004319basic_string<_CharT, _Traits, _Allocator>
4320operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4321{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004322 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323}
4324
4325template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004326inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327basic_string<_CharT, _Traits, _Allocator>
4328operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4329{
4330 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004331 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004332}
4333
Eric Fiselierfc92be82017-04-19 00:28:44 +00004334#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335
4336// swap
4337
4338template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004339inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004341swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004342 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4343 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344{
4345 __lhs.swap(__rhs);
4346}
4347
Bruce Mitchener170d8972020-11-24 12:53:53 -05004348_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4349_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4350_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4351_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4352_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004353
Bruce Mitchener170d8972020-11-24 12:53:53 -05004354_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4355_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4356_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004357
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004358_LIBCPP_FUNC_VIS string to_string(int __val);
4359_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4360_LIBCPP_FUNC_VIS string to_string(long __val);
4361_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4362_LIBCPP_FUNC_VIS string to_string(long long __val);
4363_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4364_LIBCPP_FUNC_VIS string to_string(float __val);
4365_LIBCPP_FUNC_VIS string to_string(double __val);
4366_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004367
Bruce Mitchener170d8972020-11-24 12:53:53 -05004368_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4369_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4370_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4371_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4372_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004373
Bruce Mitchener170d8972020-11-24 12:53:53 -05004374_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4375_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4376_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004377
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004378_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4379_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4380_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4381_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4382_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4383_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4384_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4385_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4386_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004387
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004389_LIBCPP_FUNC_VIS
4390const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4391 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004392
Marshall Clow851b9ec2019-05-20 21:56:51 +00004393template <class _CharT, class _Allocator>
4394struct _LIBCPP_TEMPLATE_VIS
4395 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4396 : public unary_function<
4397 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004398{
4399 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004400 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4401 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004402};
4403
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404
Howard Hinnantdc095972011-07-18 15:51:59 +00004405template<class _CharT, class _Traits, class _Allocator>
4406basic_ostream<_CharT, _Traits>&
4407operator<<(basic_ostream<_CharT, _Traits>& __os,
4408 const basic_string<_CharT, _Traits, _Allocator>& __str);
4409
4410template<class _CharT, class _Traits, class _Allocator>
4411basic_istream<_CharT, _Traits>&
4412operator>>(basic_istream<_CharT, _Traits>& __is,
4413 basic_string<_CharT, _Traits, _Allocator>& __str);
4414
4415template<class _CharT, class _Traits, class _Allocator>
4416basic_istream<_CharT, _Traits>&
4417getline(basic_istream<_CharT, _Traits>& __is,
4418 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4419
4420template<class _CharT, class _Traits, class _Allocator>
4421inline _LIBCPP_INLINE_VISIBILITY
4422basic_istream<_CharT, _Traits>&
4423getline(basic_istream<_CharT, _Traits>& __is,
4424 basic_string<_CharT, _Traits, _Allocator>& __str);
4425
Eric Fiselierfc92be82017-04-19 00:28:44 +00004426#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004427
4428template<class _CharT, class _Traits, class _Allocator>
4429inline _LIBCPP_INLINE_VISIBILITY
4430basic_istream<_CharT, _Traits>&
4431getline(basic_istream<_CharT, _Traits>&& __is,
4432 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4433
4434template<class _CharT, class _Traits, class _Allocator>
4435inline _LIBCPP_INLINE_VISIBILITY
4436basic_istream<_CharT, _Traits>&
4437getline(basic_istream<_CharT, _Traits>&& __is,
4438 basic_string<_CharT, _Traits, _Allocator>& __str);
4439
Eric Fiselierfc92be82017-04-19 00:28:44 +00004440#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004441
Marshall Clow29b53f22018-12-14 18:49:35 +00004442#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004443template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004444inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004445 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4446 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4447 auto __old_size = __str.size();
4448 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4449 return __old_size - __str.size();
4450}
Marshall Clow29b53f22018-12-14 18:49:35 +00004451
Marek Kurdeja98b1412020-05-02 13:58:03 +02004452template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004453inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004454 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4455 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4456 _Predicate __pred) {
4457 auto __old_size = __str.size();
4458 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4459 __str.end());
4460 return __old_size - __str.size();
4461}
Marshall Clow29b53f22018-12-14 18:49:35 +00004462#endif
4463
Louis Dionneba400782020-10-02 15:02:52 -04004464#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004465
4466template<class _CharT, class _Traits, class _Allocator>
4467bool
4468basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4469{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004470 return this->data() <= _VSTD::__to_address(__i->base()) &&
4471 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004472}
4473
4474template<class _CharT, class _Traits, class _Allocator>
4475bool
4476basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4477{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004478 return this->data() < _VSTD::__to_address(__i->base()) &&
4479 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004480}
4481
4482template<class _CharT, class _Traits, class _Allocator>
4483bool
4484basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4485{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004486 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004487 return this->data() <= __p && __p <= this->data() + this->size();
4488}
4489
4490template<class _CharT, class _Traits, class _Allocator>
4491bool
4492basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4493{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004494 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004495 return this->data() <= __p && __p < this->data() + this->size();
4496}
4497
Louis Dionneba400782020-10-02 15:02:52 -04004498#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004499
Louis Dionne173f29e2019-05-29 16:01:36 +00004500#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004501// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004502inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004503{
4504 inline namespace string_literals
4505 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004506 inline _LIBCPP_INLINE_VISIBILITY
4507 basic_string<char> operator "" s( const char *__str, size_t __len )
4508 {
4509 return basic_string<char> (__str, __len);
4510 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004511
Howard Hinnant5c167562013-08-07 19:39:48 +00004512 inline _LIBCPP_INLINE_VISIBILITY
4513 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4514 {
4515 return basic_string<wchar_t> (__str, __len);
4516 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004517
Marshall Clow8732fed2018-12-11 04:35:44 +00004518#ifndef _LIBCPP_NO_HAS_CHAR8_T
4519 inline _LIBCPP_INLINE_VISIBILITY
4520 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4521 {
4522 return basic_string<char8_t> (__str, __len);
4523 }
4524#endif
4525
Howard Hinnant5c167562013-08-07 19:39:48 +00004526 inline _LIBCPP_INLINE_VISIBILITY
4527 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4528 {
4529 return basic_string<char16_t> (__str, __len);
4530 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004531
Howard Hinnant5c167562013-08-07 19:39:48 +00004532 inline _LIBCPP_INLINE_VISIBILITY
4533 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4534 {
4535 return basic_string<char32_t> (__str, __len);
4536 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004537 }
4538}
4539#endif
4540
Howard Hinnantc51e1022010-05-11 19:42:16 +00004541_LIBCPP_END_NAMESPACE_STD
4542
Eric Fiselierf4433a32017-05-31 22:07:49 +00004543_LIBCPP_POP_MACROS
4544
Howard Hinnantc51e1022010-05-11 19:42:16 +00004545#endif // _LIBCPP_STRING