blob: c13cefc64e393cfab7be84bc260d4b645917f930 [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
156 void reserve(size_type res_arg = 0);
157 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000158 void clear() noexcept;
159 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160
161 const_reference operator[](size_type pos) const;
162 reference operator[](size_type pos);
163
164 const_reference at(size_type n) const;
165 reference at(size_type n);
166
167 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000168 template <class T>
169 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000170 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 basic_string& operator+=(value_type c);
172 basic_string& operator+=(initializer_list<value_type>);
173
174 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000175 template <class T>
176 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000177 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000178 template <class T>
179 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000180 basic_string& append(const value_type* s, size_type n);
181 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000183 template<class InputIterator>
184 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185 basic_string& append(initializer_list<value_type>);
186
187 void push_back(value_type c);
188 void pop_back();
189 reference front();
190 const_reference front() const;
191 reference back();
192 const_reference back() const;
193
194 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000195 template <class T>
196 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000197 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000198 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000199 template <class T>
200 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000201 basic_string& assign(const value_type* s, size_type n);
202 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000204 template<class InputIterator>
205 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 basic_string& assign(initializer_list<value_type>);
207
208 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000209 template <class T>
210 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000211 basic_string& insert(size_type pos1, const basic_string& str,
212 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000213 template <class T>
214 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000215 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000216 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217 basic_string& insert(size_type pos, size_type n, value_type c);
218 iterator insert(const_iterator p, value_type c);
219 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 template<class InputIterator>
221 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 iterator insert(const_iterator p, initializer_list<value_type>);
223
224 basic_string& erase(size_type pos = 0, size_type n = npos);
225 iterator erase(const_iterator position);
226 iterator erase(const_iterator first, const_iterator last);
227
228 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000229 template <class T>
230 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000231 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000232 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000233 template <class T>
234 basic_string& replace(size_type pos1, size_type n1, const T& t,
235 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000236 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
237 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000238 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000239 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000240 template <class T>
241 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000242 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
243 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000244 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000245 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000246 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
247 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248
Howard Hinnantd17880b2013-06-28 16:59:19 +0000249 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250 basic_string substr(size_type pos = 0, size_type n = npos) const;
251
Howard Hinnant3e276872011-06-03 18:40:47 +0000252 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000253 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
254 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255
Howard Hinnantd17880b2013-06-28 16:59:19 +0000256 const value_type* c_str() const noexcept;
257 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000258 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000260 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000262 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000263 template <class T>
264 size_type find(const T& t, size_type pos = 0) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
266 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000267 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000270 template <class T>
271 size_type rfind(const T& t, size_type pos = npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000272 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
273 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000274 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000277 template <class T>
278 size_type find_first_of(const T& t, size_type pos = 0) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000279 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
280 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000281 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000284 template <class T>
285 size_type find_last_of(const T& t, size_type pos = npos) const noexcept; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000286 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
287 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000288 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000291 template <class T>
292 size_type find_first_not_of(const T& t, size_type pos = 0) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000293 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
294 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000295 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000298 template <class T>
299 size_type find_last_not_of(const T& t, size_type pos = npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000300 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
301 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000302 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000305 template <class T>
306 int compare(const T& t) const noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000308 template <class T>
309 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000310 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000311 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000312 template <class T>
313 int compare(size_type pos1, size_type n1, const T& t,
314 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000315 int compare(const value_type* s) const noexcept;
316 int compare(size_type pos1, size_type n1, const value_type* s) const;
317 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318
Marshall Clow18c293b2017-12-04 20:11:38 +0000319 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
320 bool starts_with(charT c) const noexcept; // C++2a
321 bool starts_with(const charT* s) const; // C++2a
322 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
323 bool ends_with(charT c) const noexcept; // C++2a
324 bool ends_with(const charT* s) const; // C++2a
325
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 bool __invariants() const;
327};
328
Marshall Clowa0563332018-02-08 06:34:03 +0000329template<class InputIterator,
330 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
331basic_string(InputIterator, InputIterator, Allocator = Allocator())
332 -> basic_string<typename iterator_traits<InputIterator>::value_type,
333 char_traits<typename iterator_traits<InputIterator>::value_type>,
334 Allocator>; // C++17
335
Howard Hinnantc51e1022010-05-11 19:42:16 +0000336template<class charT, class traits, class Allocator>
337basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000338operator+(const basic_string<charT, traits, Allocator>& lhs,
339 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000340
341template<class charT, class traits, class Allocator>
342basic_string<charT, traits, Allocator>
343operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
344
345template<class charT, class traits, class Allocator>
346basic_string<charT, traits, Allocator>
347operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
348
349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
351operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
352
353template<class charT, class traits, class Allocator>
354basic_string<charT, traits, Allocator>
355operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
356
357template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000358bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000359 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
361template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000362bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
364template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000365bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000367template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000368bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000369 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370
371template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000378bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000379 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380
381template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000388bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000389 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390
391template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000398bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000399 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400
401template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000408bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000409 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410
411template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000418void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000419 basic_string<charT, traits, Allocator>& rhs)
420 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422template<class charT, class traits, class Allocator>
423basic_istream<charT, traits>&
424operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
425
426template<class charT, class traits, class Allocator>
427basic_ostream<charT, traits>&
428operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
429
430template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000431basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000432getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
433 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
Marshall Clow29b53f22018-12-14 18:49:35 +0000439template<class charT, class traits, class Allocator, class U>
440void erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
441template<class charT, class traits, class Allocator, class Predicate>
442void erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
443
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444typedef basic_string<char> string;
445typedef basic_string<wchar_t> wstring;
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000446typedef basic_string<char16_t> u16string;
447typedef basic_string<char32_t> u32string;
448
449int stoi (const string& str, size_t* idx = 0, int base = 10);
450long stol (const string& str, size_t* idx = 0, int base = 10);
451unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
452long long stoll (const string& str, size_t* idx = 0, int base = 10);
453unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
454
455float stof (const string& str, size_t* idx = 0);
456double stod (const string& str, size_t* idx = 0);
457long double stold(const string& str, size_t* idx = 0);
458
459string to_string(int val);
460string to_string(unsigned val);
461string to_string(long val);
462string to_string(unsigned long val);
463string to_string(long long val);
464string to_string(unsigned long long val);
465string to_string(float val);
466string to_string(double val);
467string to_string(long double val);
468
469int stoi (const wstring& str, size_t* idx = 0, int base = 10);
470long stol (const wstring& str, size_t* idx = 0, int base = 10);
471unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
472long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
473unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
474
475float stof (const wstring& str, size_t* idx = 0);
476double stod (const wstring& str, size_t* idx = 0);
477long double stold(const wstring& str, size_t* idx = 0);
478
479wstring to_wstring(int val);
480wstring to_wstring(unsigned val);
481wstring to_wstring(long val);
482wstring to_wstring(unsigned long val);
483wstring to_wstring(long long val);
484wstring to_wstring(unsigned long long val);
485wstring to_wstring(float val);
486wstring to_wstring(double val);
487wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488
489template <> struct hash<string>;
490template <> struct hash<u16string>;
491template <> struct hash<u32string>;
492template <> struct hash<wstring>;
493
Marshall Clowcba751f2013-07-23 17:05:24 +0000494basic_string<char> operator "" s( const char *str, size_t len ); // C++14
495basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
496basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
497basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
498
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499} // std
500
501*/
502
503#include <__config>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000504#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000505#include <iosfwd>
506#include <cstring>
Howard Hinnant155c2af2010-05-24 17:49:41 +0000507#include <cstdio> // For EOF.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000508#include <cwchar>
509#include <algorithm>
510#include <iterator>
511#include <utility>
512#include <memory>
513#include <stdexcept>
514#include <type_traits>
515#include <initializer_list>
516#include <__functional_base>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000517#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
519#include <cstdint>
520#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000521
Eric Fiselier14b6de92014-08-10 23:53:08 +0000522#include <__debug>
523
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000524#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000526#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527
Eric Fiselierf4433a32017-05-31 22:07:49 +0000528_LIBCPP_PUSH_MACROS
529#include <__undef_macros>
530
531
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532_LIBCPP_BEGIN_NAMESPACE_STD
533
534// fpos
535
536template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000537class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000538{
539private:
540 _StateT __st_;
541 streamoff __off_;
542public:
543 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
544
545 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
546
547 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
548 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
549
550 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
551 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
552 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
553 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
554};
555
556template <class _StateT>
557inline _LIBCPP_INLINE_VISIBILITY
558streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
559 {return streamoff(__x) - streamoff(__y);}
560
561template <class _StateT>
562inline _LIBCPP_INLINE_VISIBILITY
563bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
564 {return streamoff(__x) == streamoff(__y);}
565
566template <class _StateT>
567inline _LIBCPP_INLINE_VISIBILITY
568bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
569 {return streamoff(__x) != streamoff(__y);}
570
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571// basic_string
572
573template<class _CharT, class _Traits, class _Allocator>
574basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000575operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
576 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577
578template<class _CharT, class _Traits, class _Allocator>
579basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000580operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581
582template<class _CharT, class _Traits, class _Allocator>
583basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000584operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585
586template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000587inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000589operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590
591template<class _CharT, class _Traits, class _Allocator>
592basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000593operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594
Shoaib Meenaiea363712017-07-29 02:54:41 +0000595_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
596
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000598class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599{
600protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000601 _LIBCPP_NORETURN void __throw_length_error() const;
602 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603};
604
605template <bool __b>
606void
607__basic_string_common<__b>::__throw_length_error() const
608{
Marshall Clow8fea1612016-08-25 15:09:01 +0000609 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610}
611
612template <bool __b>
613void
614__basic_string_common<__b>::__throw_out_of_range() const
615{
Marshall Clow8fea1612016-08-25 15:09:01 +0000616 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617}
618
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000619_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620
Marshall Clow039b2f02016-01-13 21:54:34 +0000621#ifdef _LIBCPP_NO_EXCEPTIONS
622template <class _Iter>
623struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
624#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
625template <class _Iter>
626struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
627#else
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500628template <class _Iter, bool = __is_cpp17_forward_iterator<_Iter>::value>
Marshall Clow039b2f02016-01-13 21:54:34 +0000629struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
Louis Dionne173f29e2019-05-29 16:01:36 +0000630 noexcept(++(declval<_Iter&>())) &&
631 is_nothrow_assignable<_Iter&, _Iter>::value &&
632 noexcept(declval<_Iter>() == declval<_Iter>()) &&
Marshall Clow039b2f02016-01-13 21:54:34 +0000633 noexcept(*declval<_Iter>())
634)) {};
635
Louis Dionne173f29e2019-05-29 16:01:36 +0000636template <class _Iter>
Marshall Clow039b2f02016-01-13 21:54:34 +0000637struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
638#endif
639
640
641template <class _Iter>
642struct __libcpp_string_gets_noexcept_iterator
643 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
644
Marshall Clow82513342016-09-24 22:45:42 +0000645template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500646struct __can_be_converted_to_string_view : public _BoolConstant<
647 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
648 !is_convertible<const _Tp&, const _CharT*>::value
649 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000650
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000651#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000652
653template <class _CharT, size_t = sizeof(_CharT)>
654struct __padding
655{
656 unsigned char __xx[sizeof(_CharT)-1];
657};
658
659template <class _CharT>
660struct __padding<_CharT, 1>
661{
662};
663
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000664#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000665
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000666template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000667class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668 : private __basic_string_common<true>
669{
670public:
671 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000672 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000674 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000676 typedef allocator_traits<allocator_type> __alloc_traits;
677 typedef typename __alloc_traits::size_type size_type;
678 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000679 typedef value_type& reference;
680 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000681 typedef typename __alloc_traits::pointer pointer;
682 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000683
Marshall Clow79f33542018-03-21 00:36:05 +0000684 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
685 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
686 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
687 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000688 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000689 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000690 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000691
Howard Hinnant8ea98242013-08-23 17:37:05 +0000692#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693 typedef pointer iterator;
694 typedef const_pointer const_iterator;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000695#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 typedef __wrap_iter<pointer> iterator;
697 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000698#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000699 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
700 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701
702private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000703
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000704#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000705
706 struct __long
707 {
708 pointer __data_;
709 size_type __size_;
710 size_type __cap_;
711 };
712
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000713#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000714 static const size_type __short_mask = 0x01;
715 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000716#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000717 static const size_type __short_mask = 0x80;
718 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant68bf1812013-04-30 21:44:48 +0000719#endif // _LIBCPP_BIG_ENDIAN
720
721 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
722 (sizeof(__long) - 1)/sizeof(value_type) : 2};
723
724 struct __short
725 {
726 value_type __data_[__min_cap];
727 struct
728 : __padding<value_type>
729 {
730 unsigned char __size_;
731 };
732 };
733
734#else
735
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736 struct __long
737 {
738 size_type __cap_;
739 size_type __size_;
740 pointer __data_;
741 };
742
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000743#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000744 static const size_type __short_mask = 0x80;
745 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000746#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000747 static const size_type __short_mask = 0x01;
748 static const size_type __long_mask = 0x1ul;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000749#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
752 (sizeof(__long) - 1)/sizeof(value_type) : 2};
753
754 struct __short
755 {
756 union
757 {
758 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000759 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 };
761 value_type __data_[__min_cap];
762 };
763
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000764#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000765
Howard Hinnant8ea98242013-08-23 17:37:05 +0000766 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767
Howard Hinnant8ea98242013-08-23 17:37:05 +0000768 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769
770 struct __raw
771 {
772 size_type __words[__n_words];
773 };
774
775 struct __rep
776 {
777 union
778 {
779 __long __l;
780 __short __s;
781 __raw __r;
782 };
783 };
784
785 __compressed_pair<__rep, allocator_type> __r_;
786
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787public:
788 static const size_type npos = -1;
789
Howard Hinnant3e276872011-06-03 18:40:47 +0000790 _LIBCPP_INLINE_VISIBILITY basic_string()
791 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000792
793 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
794#if _LIBCPP_STD_VER <= 14
795 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
796#else
797 _NOEXCEPT;
798#endif
799
Howard Hinnantc51e1022010-05-11 19:42:16 +0000800 basic_string(const basic_string& __str);
801 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000802
Eric Fiselierfc92be82017-04-19 00:28:44 +0000803#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000805 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000806#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000807 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000808#else
809 _NOEXCEPT;
810#endif
811
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000814#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000815
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500816 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t>>
Eric Fiseliera8567862018-07-17 05:48:48 +0000817 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500818 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000819 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
820 __init(__s, traits_type::length(__s));
821# if _LIBCPP_DEBUG_LEVEL >= 2
822 __get_db()->__insert_c(this);
823# endif
824 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000825
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500826 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t>>
Marshall Clowe46031a2018-07-02 18:41:15 +0000827 _LIBCPP_INLINE_VISIBILITY
828 basic_string(const _CharT* __s, const _Allocator& __a);
829
Howard Hinnantcf823322010-12-17 14:46:43 +0000830 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000831 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000833 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000834 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000835 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000836
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500837 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t>>
Marshall Clowe46031a2018-07-02 18:41:15 +0000838 _LIBCPP_INLINE_VISIBILITY
839 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
840
Marshall Clow83445802016-04-07 18:13:41 +0000841 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000842 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000843 _LIBCPP_INLINE_VISIBILITY
844 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000845 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000846
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500847 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 +0000848 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000849 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500850 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000851
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500852 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
853 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000854 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
855 explicit basic_string(const _Tp& __t);
856
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500857 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 +0000858 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
859 explicit basic_string(const _Tp& __t, const allocator_type& __a);
860
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500861 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>>
Howard Hinnantcf823322010-12-17 14:46:43 +0000862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500864 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>>
Howard Hinnantcf823322010-12-17 14:46:43 +0000865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000867#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000868 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000869 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000870 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000871 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000872#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000874 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000876 _LIBCPP_INLINE_VISIBILITY
877 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
878
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000879 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000880
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500881 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 +0000882 basic_string& operator=(const _Tp& __t)
883 {__self_view __sv = __t; return assign(__sv);}
884
Eric Fiselierfc92be82017-04-19 00:28:44 +0000885#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000887 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000888 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000889 _LIBCPP_INLINE_VISIBILITY
890 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000892 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894
Howard Hinnant8ea98242013-08-23 17:37:05 +0000895#if _LIBCPP_DEBUG_LEVEL >= 2
896 _LIBCPP_INLINE_VISIBILITY
897 iterator begin() _NOEXCEPT
898 {return iterator(this, __get_pointer());}
899 _LIBCPP_INLINE_VISIBILITY
900 const_iterator begin() const _NOEXCEPT
901 {return const_iterator(this, __get_pointer());}
902 _LIBCPP_INLINE_VISIBILITY
903 iterator end() _NOEXCEPT
904 {return iterator(this, __get_pointer() + size());}
905 _LIBCPP_INLINE_VISIBILITY
906 const_iterator end() const _NOEXCEPT
907 {return const_iterator(this, __get_pointer() + size());}
908#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000909 _LIBCPP_INLINE_VISIBILITY
910 iterator begin() _NOEXCEPT
911 {return iterator(__get_pointer());}
912 _LIBCPP_INLINE_VISIBILITY
913 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000914 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000915 _LIBCPP_INLINE_VISIBILITY
916 iterator end() _NOEXCEPT
917 {return iterator(__get_pointer() + size());}
918 _LIBCPP_INLINE_VISIBILITY
919 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000920 {return const_iterator(__get_pointer() + size());}
Howard Hinnant8ea98242013-08-23 17:37:05 +0000921#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000922 _LIBCPP_INLINE_VISIBILITY
923 reverse_iterator rbegin() _NOEXCEPT
924 {return reverse_iterator(end());}
925 _LIBCPP_INLINE_VISIBILITY
926 const_reverse_iterator rbegin() const _NOEXCEPT
927 {return const_reverse_iterator(end());}
928 _LIBCPP_INLINE_VISIBILITY
929 reverse_iterator rend() _NOEXCEPT
930 {return reverse_iterator(begin());}
931 _LIBCPP_INLINE_VISIBILITY
932 const_reverse_iterator rend() const _NOEXCEPT
933 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000935 _LIBCPP_INLINE_VISIBILITY
936 const_iterator cbegin() const _NOEXCEPT
937 {return begin();}
938 _LIBCPP_INLINE_VISIBILITY
939 const_iterator cend() const _NOEXCEPT
940 {return end();}
941 _LIBCPP_INLINE_VISIBILITY
942 const_reverse_iterator crbegin() const _NOEXCEPT
943 {return rbegin();}
944 _LIBCPP_INLINE_VISIBILITY
945 const_reverse_iterator crend() const _NOEXCEPT
946 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000948 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000950 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
951 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
952 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000953 {return (__is_long() ? __get_long_cap()
954 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955
956 void resize(size_type __n, value_type __c);
957 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
958
Marshall Clow6ae12a82018-11-28 18:18:34 +0000959 void reserve(size_type __res_arg);
Eric Fiselier451d5582018-11-26 20:15:38 +0000960 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
961
Marshall Clow6ae12a82018-11-28 18:18:34 +0000962 _LIBCPP_INLINE_VISIBILITY
963 void reserve() _NOEXCEPT {reserve(0);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000965 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnantcf823322010-12-17 14:46:43 +0000966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000967 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000968 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
969 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000971 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
972 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973
974 const_reference at(size_type __n) const;
975 reference at(size_type __n);
976
977 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000978
979 template <class _Tp>
980 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500981 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +0000982 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500983 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
984 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000985 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500986 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000987 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000988 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000990#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000992#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993
Howard Hinnantcf823322010-12-17 14:46:43 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000996
997 template <class _Tp>
998 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500999 _EnableIf<
1000 __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 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001005 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001006
Marshall Clow62953962016-10-03 23:40:48 +00001007 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001008 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001009 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001010 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001011 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1012 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001013 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001014 >
Marshall Clow82513342016-09-24 22:45:42 +00001015 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001016 basic_string& append(const value_type* __s, size_type __n);
1017 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001019
1020 _LIBCPP_INLINE_VISIBILITY
1021 void __append_default_init(size_type __n);
1022
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001023 template <class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001024 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1025 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001027 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001028 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001030 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001031 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001033 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001034 _LIBCPP_INLINE_VISIBILITY
1035 append(_InputIterator __first, _InputIterator __last) {
1036 const basic_string __temp (__first, __last, __alloc());
1037 append(__temp.data(), __temp.size());
1038 return *this;
1039 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001041 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001042 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001044 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001045 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001047 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001048 _LIBCPP_INLINE_VISIBILITY
1049 append(_ForwardIterator __first, _ForwardIterator __last) {
1050 return __append_forward_unsafe(__first, __last);
1051 }
1052
Eric Fiselierfc92be82017-04-19 00:28:44 +00001053#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001056#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057
1058 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001061 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1062 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1063 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1064 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065
Marshall Clowe46031a2018-07-02 18:41:15 +00001066 template <class _Tp>
1067 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001068 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001069 <
1070 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1071 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001072 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001073 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001074 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001075 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001076#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001077 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001078 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001079 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001080 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001081#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001082 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001083 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001084 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001085 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001086 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001087 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1088 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001089 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001090 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001091 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001092 basic_string& assign(const value_type* __s, size_type __n);
1093 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094 basic_string& assign(size_type __n, value_type __c);
1095 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001096 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001097 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001099 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001100 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001102 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 assign(_InputIterator __first, _InputIterator __last);
1104 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001105 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001108 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001109 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001111 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001113#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001116#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117
Howard Hinnantcf823322010-12-17 14:46:43 +00001118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001120
1121 template <class _Tp>
1122 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001123 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001124 <
1125 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1126 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001127 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001128 insert(size_type __pos1, const _Tp& __t)
1129 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1130
Marshall Clow82513342016-09-24 22:45:42 +00001131 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001132 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001134 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001135 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001136 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001137 >
Marshall Clow82513342016-09-24 22:45:42 +00001138 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001139 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001140 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1141 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1143 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1146 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001147 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001148 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001150 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001151 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001153 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1155 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001156 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001157 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001159 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001160 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001162 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001164#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1167 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001168#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169
1170 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 iterator erase(const_iterator __first, const_iterator __last);
1175
Howard Hinnantcf823322010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001178
1179 template <class _Tp>
1180 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001181 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001182 <
1183 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1184 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001185 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001186 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 +00001187 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 +00001188 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001189 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001190 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001191 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001192 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001193 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001194 >
Marshall Clow82513342016-09-24 22:45:42 +00001195 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001196 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1197 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001200 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001201
1202 template <class _Tp>
1203 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001204 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001205 <
1206 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1207 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001208 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001209 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1210
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001212 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001214 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001216 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001218 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001219 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001221 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001223 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001224 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001225#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001227 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001229#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230
Howard Hinnantd17880b2013-06-28 16:59:19 +00001231 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1234
Howard Hinnantcf823322010-12-17 14:46:43 +00001235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001236 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001237#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001238 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001239#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001240 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001241 __is_nothrow_swappable<allocator_type>::value);
1242#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243
Howard Hinnantcf823322010-12-17 14:46:43 +00001244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001245 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001246 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001247 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001248#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001249 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001250 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001251#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252
Howard Hinnantcf823322010-12-17 14:46:43 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001254 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255
Howard Hinnantcf823322010-12-17 14:46:43 +00001256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001257 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001258
1259 template <class _Tp>
1260 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001261 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001262 <
1263 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1264 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001265 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001266 find(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001267 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001269 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001270 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
Howard Hinnantcf823322010-12-17 14:46:43 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001273 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001274
1275 template <class _Tp>
1276 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001277 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001278 <
1279 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1280 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001281 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001282 rfind(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001283 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001285 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001286 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
Howard Hinnantcf823322010-12-17 14:46:43 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001289 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001290
1291 template <class _Tp>
1292 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001293 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001294 <
1295 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1296 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001297 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001298 find_first_of(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001299 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001301 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001303 size_type find_first_of(value_type __c, size_type __pos = 0) 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_last_of(const basic_string& __str, size_type __pos = npos) 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_last_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 size_type find_last_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_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001320 size_type find_last_of(value_type __c, size_type __pos = npos) 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_first_not_of(const basic_string& __str, size_type __pos = 0) 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_first_not_of(const _Tp &__t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 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 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001335 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001336 _LIBCPP_INLINE_VISIBILITY
1337 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1338
1339 _LIBCPP_INLINE_VISIBILITY
1340 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) 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_last_not_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001350 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 +00001351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001352 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001353 _LIBCPP_INLINE_VISIBILITY
1354 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1355
1356 _LIBCPP_INLINE_VISIBILITY
1357 int compare(const basic_string& __str) 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 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001365 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001366 compare(const _Tp &__t) const;
1367
1368 template <class _Tp>
1369 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001370 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001371 <
1372 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1373 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001374 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001375 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1376
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001379 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 +00001380
Marshall Clow82513342016-09-24 22:45:42 +00001381 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001382 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001383 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001384 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001385 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001386 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001387 >
Marshall Clow82513342016-09-24 22:45:42 +00001388 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 +00001389 int compare(const value_type* __s) const _NOEXCEPT;
1390 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1391 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392
Marshall Clow18c293b2017-12-04 20:11:38 +00001393#if _LIBCPP_STD_VER > 17
1394 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1395 bool starts_with(__self_view __sv) const _NOEXCEPT
1396 { return __self_view(data(), size()).starts_with(__sv); }
1397
1398 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1399 bool starts_with(value_type __c) const _NOEXCEPT
1400 { return !empty() && _Traits::eq(front(), __c); }
1401
1402 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1403 bool starts_with(const value_type* __s) const _NOEXCEPT
1404 { return starts_with(__self_view(__s)); }
1405
1406 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1407 bool ends_with(__self_view __sv) const _NOEXCEPT
1408 { return __self_view(data(), size()).ends_with( __sv); }
1409
1410 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1411 bool ends_with(value_type __c) const _NOEXCEPT
1412 { return !empty() && _Traits::eq(back(), __c); }
1413
1414 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1415 bool ends_with(const value_type* __s) const _NOEXCEPT
1416 { return ends_with(__self_view(__s)); }
1417#endif
1418
Howard Hinnantcf823322010-12-17 14:46:43 +00001419 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001420
Marshall Clowe60a7182018-05-29 17:04:37 +00001421 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001422
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001423 _LIBCPP_INLINE_VISIBILITY
1424 bool __is_long() const _NOEXCEPT
1425 {return bool(__r_.first().__s.__size_ & __short_mask);}
1426
Howard Hinnant8ea98242013-08-23 17:37:05 +00001427#if _LIBCPP_DEBUG_LEVEL >= 2
1428
1429 bool __dereferenceable(const const_iterator* __i) const;
1430 bool __decrementable(const const_iterator* __i) const;
1431 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1432 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1433
1434#endif // _LIBCPP_DEBUG_LEVEL >= 2
1435
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001437 _LIBCPP_INLINE_VISIBILITY
1438 allocator_type& __alloc() _NOEXCEPT
1439 {return __r_.second();}
1440 _LIBCPP_INLINE_VISIBILITY
1441 const allocator_type& __alloc() const _NOEXCEPT
1442 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001444#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001445
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001447 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001448# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001450# else
1451 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1452# endif
1453
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001454 _LIBCPP_INLINE_VISIBILITY
1455 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001456# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001458# else
1459 {return __r_.first().__s.__size_;}
1460# endif
1461
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001462#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001463
1464 _LIBCPP_INLINE_VISIBILITY
1465 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001466# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001467 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1468# else
1469 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1470# endif
1471
1472 _LIBCPP_INLINE_VISIBILITY
1473 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001474# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001475 {return __r_.first().__s.__size_;}
1476# else
1477 {return __r_.first().__s.__size_ >> 1;}
1478# endif
1479
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001480#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001481
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001482 _LIBCPP_INLINE_VISIBILITY
1483 void __set_long_size(size_type __s) _NOEXCEPT
1484 {__r_.first().__l.__size_ = __s;}
1485 _LIBCPP_INLINE_VISIBILITY
1486 size_type __get_long_size() const _NOEXCEPT
1487 {return __r_.first().__l.__size_;}
1488 _LIBCPP_INLINE_VISIBILITY
1489 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1491
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001492 _LIBCPP_INLINE_VISIBILITY
1493 void __set_long_cap(size_type __s) _NOEXCEPT
1494 {__r_.first().__l.__cap_ = __long_mask | __s;}
1495 _LIBCPP_INLINE_VISIBILITY
1496 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001497 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001499 _LIBCPP_INLINE_VISIBILITY
1500 void __set_long_pointer(pointer __p) _NOEXCEPT
1501 {__r_.first().__l.__data_ = __p;}
1502 _LIBCPP_INLINE_VISIBILITY
1503 pointer __get_long_pointer() _NOEXCEPT
1504 {return __r_.first().__l.__data_;}
1505 _LIBCPP_INLINE_VISIBILITY
1506 const_pointer __get_long_pointer() const _NOEXCEPT
1507 {return __r_.first().__l.__data_;}
1508 _LIBCPP_INLINE_VISIBILITY
1509 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001510 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001511 _LIBCPP_INLINE_VISIBILITY
1512 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001513 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001514 _LIBCPP_INLINE_VISIBILITY
1515 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001517 _LIBCPP_INLINE_VISIBILITY
1518 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1520
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001521 _LIBCPP_INLINE_VISIBILITY
1522 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523 {
1524 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1525 for (unsigned __i = 0; __i < __n_words; ++__i)
1526 __a[__i] = 0;
1527 }
1528
1529 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001531 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001532 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001534 static _LIBCPP_INLINE_VISIBILITY
1535 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001536 {
1537 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1538 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1539 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1540 if (__guess == __min_cap) ++__guess;
1541 return __guess;
1542 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001544 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001545 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001546 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001547 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001548 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001550
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001552 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001553 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001555 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1556 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 __init(_InputIterator __first, _InputIterator __last);
1558
1559 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001560 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001561 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001563 __is_cpp17_forward_iterator<_ForwardIterator>::value
1564 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 __init(_ForwardIterator __first, _ForwardIterator __last);
1566
1567 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001568 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1570 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001571 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572
Howard Hinnantcf823322010-12-17 14:46:43 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 void __erase_to_end(size_type __pos);
1575
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001576 _LIBCPP_INLINE_VISIBILITY
1577 void __copy_assign_alloc(const basic_string& __str)
1578 {__copy_assign_alloc(__str, integral_constant<bool,
1579 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1580
1581 _LIBCPP_INLINE_VISIBILITY
1582 void __copy_assign_alloc(const basic_string& __str, true_type)
1583 {
Marshall Clowf258c202017-01-31 03:40:52 +00001584 if (__alloc() == __str.__alloc())
1585 __alloc() = __str.__alloc();
1586 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001587 {
Marshall Clowf258c202017-01-31 03:40:52 +00001588 if (!__str.__is_long())
1589 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001590 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001591 __alloc() = __str.__alloc();
1592 }
1593 else
1594 {
1595 allocator_type __a = __str.__alloc();
1596 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001597 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001598 __alloc() = _VSTD::move(__a);
1599 __set_long_pointer(__p);
1600 __set_long_cap(__str.__get_long_cap());
1601 __set_long_size(__str.size());
1602 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001603 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001604 }
1605
1606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001607 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001608 {}
1609
Eric Fiselierfc92be82017-04-19 00:28:44 +00001610#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001611 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001612 void __move_assign(basic_string& __str, false_type)
1613 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001615 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001616#if _LIBCPP_STD_VER > 14
1617 _NOEXCEPT;
1618#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001619 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001620#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001621#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001622
1623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001624 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001625 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001626 _NOEXCEPT_(
1627 !__alloc_traits::propagate_on_container_move_assignment::value ||
1628 is_nothrow_move_assignable<allocator_type>::value)
1629 {__move_assign_alloc(__str, integral_constant<bool,
1630 __alloc_traits::propagate_on_container_move_assignment::value>());}
1631
1632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001633 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001634 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1635 {
1636 __alloc() = _VSTD::move(__c.__alloc());
1637 }
1638
1639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001640 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001641 _NOEXCEPT
1642 {}
1643
Howard Hinnantcf823322010-12-17 14:46:43 +00001644 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1645 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646
1647 friend basic_string operator+<>(const basic_string&, const basic_string&);
1648 friend basic_string operator+<>(const value_type*, const basic_string&);
1649 friend basic_string operator+<>(value_type, const basic_string&);
1650 friend basic_string operator+<>(const basic_string&, const value_type*);
1651 friend basic_string operator+<>(const basic_string&, value_type);
1652};
1653
Marshall Clowa0563332018-02-08 06:34:03 +00001654#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1655template<class _InputIterator,
1656 class _CharT = typename iterator_traits<_InputIterator>::value_type,
1657 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001658 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1659 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001660 >
1661basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1662 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001663
1664template<class _CharT,
1665 class _Traits,
1666 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001667 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001668 >
1669explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1670 -> basic_string<_CharT, _Traits, _Allocator>;
1671
1672template<class _CharT,
1673 class _Traits,
1674 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001675 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001676 class _Sz = typename allocator_traits<_Allocator>::size_type
1677 >
1678basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1679 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001680#endif
1681
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001683inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684void
1685basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1686{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001687#if _LIBCPP_DEBUG_LEVEL >= 2
1688 __get_db()->__invalidate_all(this);
1689#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690}
1691
1692template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001693inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694void
Howard Hinnant28b24882011-12-01 20:21:04 +00001695basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant8ea98242013-08-23 17:37:05 +00001696#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant28b24882011-12-01 20:21:04 +00001697 __pos
1698#endif
1699 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001701#if _LIBCPP_DEBUG_LEVEL >= 2
1702 __c_node* __c = __get_db()->__find_c_and_lock(this);
1703 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001705 const_pointer __new_last = __get_pointer() + __pos;
1706 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001708 --__p;
1709 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1710 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001712 (*__p)->__c_ = nullptr;
1713 if (--__c->end_ != __p)
1714 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001717 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001719#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720}
1721
1722template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001723inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001724basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001725 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001726 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001728#if _LIBCPP_DEBUG_LEVEL >= 2
1729 __get_db()->__insert_c(this);
1730#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731 __zero();
1732}
1733
1734template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001735inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001737#if _LIBCPP_STD_VER <= 14
1738 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1739#else
1740 _NOEXCEPT
1741#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001742: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001744#if _LIBCPP_DEBUG_LEVEL >= 2
1745 __get_db()->__insert_c(this);
1746#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747 __zero();
1748}
1749
1750template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001751void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1752 size_type __sz,
1753 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754{
1755 if (__reserve > max_size())
1756 this->__throw_length_error();
1757 pointer __p;
1758 if (__reserve < __min_cap)
1759 {
1760 __set_short_size(__sz);
1761 __p = __get_short_pointer();
1762 }
1763 else
1764 {
1765 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001766 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767 __set_long_pointer(__p);
1768 __set_long_cap(__cap+1);
1769 __set_long_size(__sz);
1770 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001771 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772 traits_type::assign(__p[__sz], value_type());
1773}
1774
1775template <class _CharT, class _Traits, class _Allocator>
1776void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001777basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778{
1779 if (__sz > max_size())
1780 this->__throw_length_error();
1781 pointer __p;
1782 if (__sz < __min_cap)
1783 {
1784 __set_short_size(__sz);
1785 __p = __get_short_pointer();
1786 }
1787 else
1788 {
1789 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001790 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 __set_long_pointer(__p);
1792 __set_long_cap(__cap+1);
1793 __set_long_size(__sz);
1794 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001795 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 traits_type::assign(__p[__sz], value_type());
1797}
1798
1799template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001800template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001801basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001802 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001804 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 __init(__s, traits_type::length(__s));
Howard Hinnant8ea98242013-08-23 17:37:05 +00001806#if _LIBCPP_DEBUG_LEVEL >= 2
1807 __get_db()->__insert_c(this);
1808#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809}
1810
1811template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001812inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001813basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001814 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001816 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 __init(__s, __n);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001818#if _LIBCPP_DEBUG_LEVEL >= 2
1819 __get_db()->__insert_c(this);
1820#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821}
1822
1823template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001824inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001825basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001826 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001828 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 __init(__s, __n);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001830#if _LIBCPP_DEBUG_LEVEL >= 2
1831 __get_db()->__insert_c(this);
1832#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833}
1834
1835template <class _CharT, class _Traits, class _Allocator>
1836basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001837 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838{
1839 if (!__str.__is_long())
1840 __r_.first().__r = __str.__r_.first().__r;
1841 else
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001842 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant8ea98242013-08-23 17:37:05 +00001843#if _LIBCPP_DEBUG_LEVEL >= 2
1844 __get_db()->__insert_c(this);
1845#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846}
1847
1848template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001849basic_string<_CharT, _Traits, _Allocator>::basic_string(
1850 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001851 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852{
1853 if (!__str.__is_long())
1854 __r_.first().__r = __str.__r_.first().__r;
1855 else
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001856 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant8ea98242013-08-23 17:37:05 +00001857#if _LIBCPP_DEBUG_LEVEL >= 2
1858 __get_db()->__insert_c(this);
1859#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860}
1861
Eric Fiselierfc92be82017-04-19 00:28:44 +00001862#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863
1864template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001865inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001866basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001867#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001868 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001869#else
1870 _NOEXCEPT
1871#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001872 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873{
1874 __str.__zero();
Howard Hinnant8ea98242013-08-23 17:37:05 +00001875#if _LIBCPP_DEBUG_LEVEL >= 2
1876 __get_db()->__insert_c(this);
1877 if (__is_long())
1878 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879#endif
1880}
1881
1882template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001883inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001885 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886{
Marshall Clowd2d24692014-07-17 15:32:20 +00001887 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001888 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001889 else
1890 {
1891 __r_.first().__r = __str.__r_.first().__r;
1892 __str.__zero();
1893 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001894#if _LIBCPP_DEBUG_LEVEL >= 2
1895 __get_db()->__insert_c(this);
1896 if (__is_long())
1897 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898#endif
1899}
1900
Eric Fiselierfc92be82017-04-19 00:28:44 +00001901#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902
1903template <class _CharT, class _Traits, class _Allocator>
1904void
1905basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1906{
1907 if (__n > max_size())
1908 this->__throw_length_error();
1909 pointer __p;
1910 if (__n < __min_cap)
1911 {
1912 __set_short_size(__n);
1913 __p = __get_short_pointer();
1914 }
1915 else
1916 {
1917 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001918 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919 __set_long_pointer(__p);
1920 __set_long_cap(__cap+1);
1921 __set_long_size(__n);
1922 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001923 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924 traits_type::assign(__p[__n], value_type());
1925}
1926
1927template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001928inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001929basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001930 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931{
1932 __init(__n, __c);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001933#if _LIBCPP_DEBUG_LEVEL >= 2
1934 __get_db()->__insert_c(this);
1935#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936}
1937
1938template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001939template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001940basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001941 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942{
1943 __init(__n, __c);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001944#if _LIBCPP_DEBUG_LEVEL >= 2
1945 __get_db()->__insert_c(this);
1946#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947}
1948
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001950basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
1951 size_type __pos, size_type __n,
1952 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001953 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954{
1955 size_type __str_sz = __str.size();
1956 if (__pos > __str_sz)
1957 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001958 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant8ea98242013-08-23 17:37:05 +00001959#if _LIBCPP_DEBUG_LEVEL >= 2
1960 __get_db()->__insert_c(this);
1961#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962}
1963
1964template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001965inline
Marshall Clow83445802016-04-07 18:13:41 +00001966basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00001967 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001968 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00001969{
1970 size_type __str_sz = __str.size();
1971 if (__pos > __str_sz)
1972 this->__throw_out_of_range();
1973 __init(__str.data() + __pos, __str_sz - __pos);
1974#if _LIBCPP_DEBUG_LEVEL >= 2
1975 __get_db()->__insert_c(this);
1976#endif
1977}
1978
1979template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001980template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00001981basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00001982 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001983 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00001984{
Marshall Clowe46031a2018-07-02 18:41:15 +00001985 __self_view __sv0 = __t;
1986 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00001987 __init(__sv.data(), __sv.size());
1988#if _LIBCPP_DEBUG_LEVEL >= 2
1989 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00001990#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00001991}
1992
1993template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001994template <class _Tp, class>
1995basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001996 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001997{
Marshall Clowe46031a2018-07-02 18:41:15 +00001998 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001999 __init(__sv.data(), __sv.size());
2000#if _LIBCPP_DEBUG_LEVEL >= 2
2001 __get_db()->__insert_c(this);
2002#endif
2003}
2004
2005template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002006template <class _Tp, class>
2007basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002008 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002009{
Marshall Clowe46031a2018-07-02 18:41:15 +00002010 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002011 __init(__sv.data(), __sv.size());
2012#if _LIBCPP_DEBUG_LEVEL >= 2
2013 __get_db()->__insert_c(this);
2014#endif
2015}
2016
2017template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002019_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002021 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2022>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2024{
2025 __zero();
2026#ifndef _LIBCPP_NO_EXCEPTIONS
2027 try
2028 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002029#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030 for (; __first != __last; ++__first)
2031 push_back(*__first);
2032#ifndef _LIBCPP_NO_EXCEPTIONS
2033 }
2034 catch (...)
2035 {
2036 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002037 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038 throw;
2039 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002040#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041}
2042
2043template <class _CharT, class _Traits, class _Allocator>
2044template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002045_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002047 __is_cpp17_forward_iterator<_ForwardIterator>::value
2048>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2050{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002051 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052 if (__sz > max_size())
2053 this->__throw_length_error();
2054 pointer __p;
2055 if (__sz < __min_cap)
2056 {
2057 __set_short_size(__sz);
2058 __p = __get_short_pointer();
2059 }
2060 else
2061 {
2062 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002063 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064 __set_long_pointer(__p);
2065 __set_long_cap(__cap+1);
2066 __set_long_size(__sz);
2067 }
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002068 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069 traits_type::assign(*__p, *__first);
2070 traits_type::assign(*__p, value_type());
2071}
2072
2073template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002074template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002075inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002077 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078{
2079 __init(__first, __last);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002080#if _LIBCPP_DEBUG_LEVEL >= 2
2081 __get_db()->__insert_c(this);
2082#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083}
2084
2085template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002086template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002087inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002088basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2089 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002090 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091{
2092 __init(__first, __last);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002093#if _LIBCPP_DEBUG_LEVEL >= 2
2094 __get_db()->__insert_c(this);
2095#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002096}
2097
Eric Fiselierfc92be82017-04-19 00:28:44 +00002098#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002099
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002101inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002102basic_string<_CharT, _Traits, _Allocator>::basic_string(
2103 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002104 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105{
2106 __init(__il.begin(), __il.end());
Howard Hinnant8ea98242013-08-23 17:37:05 +00002107#if _LIBCPP_DEBUG_LEVEL >= 2
2108 __get_db()->__insert_c(this);
2109#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110}
2111
2112template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002113inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002114
Eric Fiselier812882b2017-02-17 01:17:10 +00002115basic_string<_CharT, _Traits, _Allocator>::basic_string(
2116 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002117 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118{
2119 __init(__il.begin(), __il.end());
Howard Hinnant8ea98242013-08-23 17:37:05 +00002120#if _LIBCPP_DEBUG_LEVEL >= 2
2121 __get_db()->__insert_c(this);
2122#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123}
2124
Eric Fiselierfc92be82017-04-19 00:28:44 +00002125#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002126
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2129{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002130#if _LIBCPP_DEBUG_LEVEL >= 2
2131 __get_db()->__erase_c(this);
2132#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002134 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135}
2136
2137template <class _CharT, class _Traits, class _Allocator>
2138void
2139basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2140 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002141 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 +00002142{
2143 size_type __ms = max_size();
2144 if (__delta_cap > __ms - __old_cap - 1)
2145 this->__throw_length_error();
2146 pointer __old_p = __get_pointer();
2147 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002148 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002150 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151 __invalidate_all_iterators();
2152 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002153 traits_type::copy(_VSTD::__to_address(__p),
2154 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002156 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2158 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002159 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2160 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002162 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163 __set_long_pointer(__p);
2164 __set_long_cap(__cap+1);
2165 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2166 __set_long_size(__old_sz);
2167 traits_type::assign(__p[__old_sz], value_type());
2168}
2169
2170template <class _CharT, class _Traits, class _Allocator>
2171void
2172basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2173 size_type __n_copy, size_type __n_del, size_type __n_add)
2174{
2175 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002176 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 this->__throw_length_error();
2178 pointer __old_p = __get_pointer();
2179 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002180 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002182 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183 __invalidate_all_iterators();
2184 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002185 traits_type::copy(_VSTD::__to_address(__p),
2186 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2188 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002189 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2190 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002191 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002193 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 __set_long_pointer(__p);
2195 __set_long_cap(__cap+1);
2196}
2197
2198// assign
2199
2200template <class _CharT, class _Traits, class _Allocator>
2201basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002202basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002204 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205 size_type __cap = capacity();
2206 if (__cap >= __n)
2207 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002208 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209 traits_type::move(__p, __s, __n);
2210 traits_type::assign(__p[__n], value_type());
2211 __set_size(__n);
2212 __invalidate_iterators_past(__n);
2213 }
2214 else
2215 {
2216 size_type __sz = size();
2217 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2218 }
2219 return *this;
2220}
2221
2222template <class _CharT, class _Traits, class _Allocator>
2223basic_string<_CharT, _Traits, _Allocator>&
2224basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2225{
2226 size_type __cap = capacity();
2227 if (__cap < __n)
2228 {
2229 size_type __sz = size();
2230 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2231 }
2232 else
2233 __invalidate_iterators_past(__n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002234 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 traits_type::assign(__p, __n, __c);
2236 traits_type::assign(__p[__n], value_type());
2237 __set_size(__n);
2238 return *this;
2239}
2240
2241template <class _CharT, class _Traits, class _Allocator>
2242basic_string<_CharT, _Traits, _Allocator>&
2243basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2244{
2245 pointer __p;
2246 if (__is_long())
2247 {
2248 __p = __get_long_pointer();
2249 __set_long_size(1);
2250 }
2251 else
2252 {
2253 __p = __get_short_pointer();
2254 __set_short_size(1);
2255 }
2256 traits_type::assign(*__p, __c);
2257 traits_type::assign(*++__p, value_type());
2258 __invalidate_iterators_past(1);
2259 return *this;
2260}
2261
2262template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002263basic_string<_CharT, _Traits, _Allocator>&
2264basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2265{
2266 if (this != &__str)
2267 {
2268 __copy_assign_alloc(__str);
Eric Fiselierbac2d652019-10-09 03:07:02 +00002269 return assign(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002270 }
2271 return *this;
2272}
2273
Eric Fiselierfc92be82017-04-19 00:28:44 +00002274#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002275
2276template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002277inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002278void
2279basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002280 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002281{
2282 if (__alloc() != __str.__alloc())
2283 assign(__str);
2284 else
2285 __move_assign(__str, true_type());
2286}
2287
2288template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002289inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002290void
2291basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002292#if _LIBCPP_STD_VER > 14
2293 _NOEXCEPT
2294#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002295 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002296#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002297{
marshall2ed41622019-11-27 07:13:00 -08002298 if (__is_long()) {
2299 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2300 __get_long_cap());
2301#if _LIBCPP_STD_VER <= 14
2302 if (!is_nothrow_move_assignable<allocator_type>::value) {
2303 __set_short_size(0);
2304 traits_type::assign(__get_short_pointer()[0], value_type());
2305 }
2306#endif
2307 }
2308 __move_assign_alloc(__str);
2309 __r_.first() = __str.__r_.first();
2310 __str.__set_short_size(0);
2311 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002312}
2313
2314template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002315inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002316basic_string<_CharT, _Traits, _Allocator>&
2317basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002318 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002319{
2320 __move_assign(__str, integral_constant<bool,
2321 __alloc_traits::propagate_on_container_move_assignment::value>());
2322 return *this;
2323}
2324
2325#endif
2326
2327template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002329_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002331 __is_exactly_cpp17_input_iterator <_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002332 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002334>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002335basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2336{
Marshall Clow958362f2016-09-05 01:54:30 +00002337 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002338 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002339 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340}
2341
2342template <class _CharT, class _Traits, class _Allocator>
2343template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002344_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002346 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002347 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002349>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2351{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002352 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353 size_type __cap = capacity();
2354 if (__cap < __n)
2355 {
2356 size_type __sz = size();
2357 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2358 }
2359 else
2360 __invalidate_iterators_past(__n);
2361 pointer __p = __get_pointer();
2362 for (; __first != __last; ++__first, ++__p)
2363 traits_type::assign(*__p, *__first);
2364 traits_type::assign(*__p, value_type());
2365 __set_size(__n);
2366 return *this;
2367}
2368
2369template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370basic_string<_CharT, _Traits, _Allocator>&
2371basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2372{
2373 size_type __sz = __str.size();
2374 if (__pos > __sz)
2375 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002376 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002377}
2378
2379template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002380template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002381_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002382<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002383 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2384 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002385 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002386>
Marshall Clow82513342016-09-24 22:45:42 +00002387basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002388{
Marshall Clow82513342016-09-24 22:45:42 +00002389 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002390 size_type __sz = __sv.size();
2391 if (__pos > __sz)
2392 this->__throw_out_of_range();
2393 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2394}
2395
2396
2397template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002399basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002401 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402 return assign(__s, traits_type::length(__s));
2403}
2404
2405// append
2406
2407template <class _CharT, class _Traits, class _Allocator>
2408basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002409basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002411 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412 size_type __cap = capacity();
2413 size_type __sz = size();
2414 if (__cap - __sz >= __n)
2415 {
2416 if (__n)
2417 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002418 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419 traits_type::copy(__p + __sz, __s, __n);
2420 __sz += __n;
2421 __set_size(__sz);
2422 traits_type::assign(__p[__sz], value_type());
2423 }
2424 }
2425 else
2426 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2427 return *this;
2428}
2429
2430template <class _CharT, class _Traits, class _Allocator>
2431basic_string<_CharT, _Traits, _Allocator>&
2432basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2433{
2434 if (__n)
2435 {
2436 size_type __cap = capacity();
2437 size_type __sz = size();
2438 if (__cap - __sz < __n)
2439 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2440 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002441 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442 __sz += __n;
2443 __set_size(__sz);
2444 traits_type::assign(__p[__sz], value_type());
2445 }
2446 return *this;
2447}
2448
2449template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002450inline void
2451basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2452{
2453 if (__n)
2454 {
2455 size_type __cap = capacity();
2456 size_type __sz = size();
2457 if (__cap - __sz < __n)
2458 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2459 pointer __p = __get_pointer();
2460 __sz += __n;
2461 __set_size(__sz);
2462 traits_type::assign(__p[__sz], value_type());
2463 }
2464}
2465
2466template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467void
2468basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2469{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002470 bool __is_short = !__is_long();
2471 size_type __cap;
2472 size_type __sz;
2473 if (__is_short)
2474 {
2475 __cap = __min_cap - 1;
2476 __sz = __get_short_size();
2477 }
2478 else
2479 {
2480 __cap = __get_long_cap() - 1;
2481 __sz = __get_long_size();
2482 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002484 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002486 __is_short = !__is_long();
2487 }
2488 pointer __p;
2489 if (__is_short)
2490 {
2491 __p = __get_short_pointer() + __sz;
2492 __set_short_size(__sz+1);
2493 }
2494 else
2495 {
2496 __p = __get_long_pointer() + __sz;
2497 __set_long_size(__sz+1);
2498 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499 traits_type::assign(*__p, __c);
2500 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002501}
2502
Marshall Clow6ebbf662016-09-07 03:32:06 +00002503template <class _Tp>
Marshall Clow958362f2016-09-05 01:54:30 +00002504bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2505{
2506 return __first <= __p && __p < __last;
2507}
2508
Marshall Clow6ebbf662016-09-07 03:32:06 +00002509template <class _Tp1, class _Tp2>
Eric Fiselier6003c772016-12-23 23:37:52 +00002510bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clow6ebbf662016-09-07 03:32:06 +00002511{
2512 return false;
2513}
2514
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515template <class _CharT, class _Traits, class _Allocator>
2516template<class _ForwardIterator>
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002517basic_string<_CharT, _Traits, _Allocator>&
2518basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2519 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520{
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002521 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002522 "function requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523 size_type __sz = size();
2524 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002525 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526 if (__n)
2527 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002528 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2529 _CharRef __tmp_ref = *__first;
2530 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002531 {
2532 const basic_string __temp (__first, __last, __alloc());
2533 append(__temp.data(), __temp.size());
2534 }
Louis Dionne173f29e2019-05-29 16:01:36 +00002535 else
Marshall Clow958362f2016-09-05 01:54:30 +00002536 {
2537 if (__cap - __sz < __n)
2538 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2539 pointer __p = __get_pointer() + __sz;
2540 for (; __first != __last; ++__p, ++__first)
2541 traits_type::assign(*__p, *__first);
2542 traits_type::assign(*__p, value_type());
2543 __set_size(__sz + __n);
2544 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545 }
2546 return *this;
2547}
2548
2549template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002550inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551basic_string<_CharT, _Traits, _Allocator>&
2552basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2553{
2554 return append(__str.data(), __str.size());
2555}
2556
2557template <class _CharT, class _Traits, class _Allocator>
2558basic_string<_CharT, _Traits, _Allocator>&
2559basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2560{
2561 size_type __sz = __str.size();
2562 if (__pos > __sz)
2563 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002564 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565}
2566
2567template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002568template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002569 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002570 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002571 __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 +00002572 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002573 >
Marshall Clow82513342016-09-24 22:45:42 +00002574basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002575{
Marshall Clow82513342016-09-24 22:45:42 +00002576 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002577 size_type __sz = __sv.size();
2578 if (__pos > __sz)
2579 this->__throw_out_of_range();
2580 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2581}
2582
2583template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002585basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002587 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588 return append(__s, traits_type::length(__s));
2589}
2590
2591// insert
2592
2593template <class _CharT, class _Traits, class _Allocator>
2594basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002595basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002597 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598 size_type __sz = size();
2599 if (__pos > __sz)
2600 this->__throw_out_of_range();
2601 size_type __cap = capacity();
2602 if (__cap - __sz >= __n)
2603 {
2604 if (__n)
2605 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002606 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607 size_type __n_move = __sz - __pos;
2608 if (__n_move != 0)
2609 {
2610 if (__p + __pos <= __s && __s < __p + __sz)
2611 __s += __n;
2612 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2613 }
2614 traits_type::move(__p + __pos, __s, __n);
2615 __sz += __n;
2616 __set_size(__sz);
2617 traits_type::assign(__p[__sz], value_type());
2618 }
2619 }
2620 else
2621 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2622 return *this;
2623}
2624
2625template <class _CharT, class _Traits, class _Allocator>
2626basic_string<_CharT, _Traits, _Allocator>&
2627basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2628{
2629 size_type __sz = size();
2630 if (__pos > __sz)
2631 this->__throw_out_of_range();
2632 if (__n)
2633 {
2634 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002635 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636 if (__cap - __sz >= __n)
2637 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002638 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639 size_type __n_move = __sz - __pos;
2640 if (__n_move != 0)
2641 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2642 }
2643 else
2644 {
2645 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002646 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002647 }
2648 traits_type::assign(__p + __pos, __n, __c);
2649 __sz += __n;
2650 __set_size(__sz);
2651 traits_type::assign(__p[__sz], value_type());
2652 }
2653 return *this;
2654}
2655
2656template <class _CharT, class _Traits, class _Allocator>
2657template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002658_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002660 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002661 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2662 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002663>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2665{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002666#if _LIBCPP_DEBUG_LEVEL >= 2
2667 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2668 "string::insert(iterator, range) called with an iterator not"
2669 " referring to this string");
2670#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002671 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002672 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673}
2674
2675template <class _CharT, class _Traits, class _Allocator>
2676template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002677_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002679 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002680 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002682>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2684{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002685#if _LIBCPP_DEBUG_LEVEL >= 2
2686 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2687 "string::insert(iterator, range) called with an iterator not"
2688 " referring to this string");
2689#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002691 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 if (__n)
2693 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002694 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2695 _CharRef __tmp_char = *__first;
2696 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002697 {
2698 const basic_string __temp(__first, __last, __alloc());
2699 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2700 }
2701
2702 size_type __sz = size();
2703 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002704 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705 if (__cap - __sz >= __n)
2706 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002707 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708 size_type __n_move = __sz - __ip;
2709 if (__n_move != 0)
2710 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2711 }
2712 else
2713 {
2714 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002715 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716 }
2717 __sz += __n;
2718 __set_size(__sz);
2719 traits_type::assign(__p[__sz], value_type());
2720 for (__p += __ip; __first != __last; ++__p, ++__first)
2721 traits_type::assign(*__p, *__first);
2722 }
2723 return begin() + __ip;
2724}
2725
2726template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002727inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728basic_string<_CharT, _Traits, _Allocator>&
2729basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2730{
2731 return insert(__pos1, __str.data(), __str.size());
2732}
2733
2734template <class _CharT, class _Traits, class _Allocator>
2735basic_string<_CharT, _Traits, _Allocator>&
2736basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2737 size_type __pos2, size_type __n)
2738{
2739 size_type __str_sz = __str.size();
2740 if (__pos2 > __str_sz)
2741 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002742 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743}
2744
2745template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002746template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002747_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002748<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002749 __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 +00002750 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002751>
Marshall Clow82513342016-09-24 22:45:42 +00002752basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002753 size_type __pos2, size_type __n)
2754{
Marshall Clow82513342016-09-24 22:45:42 +00002755 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002756 size_type __str_sz = __sv.size();
2757 if (__pos2 > __str_sz)
2758 this->__throw_out_of_range();
2759 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2760}
2761
2762template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002764basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002766 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767 return insert(__pos, __s, traits_type::length(__s));
2768}
2769
2770template <class _CharT, class _Traits, class _Allocator>
2771typename basic_string<_CharT, _Traits, _Allocator>::iterator
2772basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2773{
2774 size_type __ip = static_cast<size_type>(__pos - begin());
2775 size_type __sz = size();
2776 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002777 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 if (__cap == __sz)
2779 {
2780 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002781 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 }
2783 else
2784 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002785 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 size_type __n_move = __sz - __ip;
2787 if (__n_move != 0)
2788 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2789 }
2790 traits_type::assign(__p[__ip], __c);
2791 traits_type::assign(__p[++__sz], value_type());
2792 __set_size(__sz);
2793 return begin() + static_cast<difference_type>(__ip);
2794}
2795
2796template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002797inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798typename basic_string<_CharT, _Traits, _Allocator>::iterator
2799basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2800{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002801#if _LIBCPP_DEBUG_LEVEL >= 2
2802 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2803 "string::insert(iterator, n, value) called with an iterator not"
2804 " referring to this string");
2805#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806 difference_type __p = __pos - begin();
2807 insert(static_cast<size_type>(__p), __n, __c);
2808 return begin() + __p;
2809}
2810
2811// replace
2812
2813template <class _CharT, class _Traits, class _Allocator>
2814basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002815basic_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 +00002816 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002818 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 size_type __sz = size();
2820 if (__pos > __sz)
2821 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002822 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823 size_type __cap = capacity();
2824 if (__cap - __sz + __n1 >= __n2)
2825 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002826 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827 if (__n1 != __n2)
2828 {
2829 size_type __n_move = __sz - __pos - __n1;
2830 if (__n_move != 0)
2831 {
2832 if (__n1 > __n2)
2833 {
2834 traits_type::move(__p + __pos, __s, __n2);
2835 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2836 goto __finish;
2837 }
2838 if (__p + __pos < __s && __s < __p + __sz)
2839 {
2840 if (__p + __pos + __n1 <= __s)
2841 __s += __n2 - __n1;
2842 else // __p + __pos < __s < __p + __pos + __n1
2843 {
2844 traits_type::move(__p + __pos, __s, __n1);
2845 __pos += __n1;
2846 __s += __n2;
2847 __n2 -= __n1;
2848 __n1 = 0;
2849 }
2850 }
2851 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2852 }
2853 }
2854 traits_type::move(__p + __pos, __s, __n2);
2855__finish:
Eric Fiseliere5b21842017-03-09 01:54:13 +00002856// __sz += __n2 - __n1; in this and the below function below can cause unsigned integer overflow,
2857// but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858 __sz += __n2 - __n1;
2859 __set_size(__sz);
2860 __invalidate_iterators_past(__sz);
2861 traits_type::assign(__p[__sz], value_type());
2862 }
2863 else
2864 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2865 return *this;
2866}
2867
2868template <class _CharT, class _Traits, class _Allocator>
2869basic_string<_CharT, _Traits, _Allocator>&
2870basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00002871 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872{
2873 size_type __sz = size();
2874 if (__pos > __sz)
2875 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002876 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002878 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879 if (__cap - __sz + __n1 >= __n2)
2880 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002881 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882 if (__n1 != __n2)
2883 {
2884 size_type __n_move = __sz - __pos - __n1;
2885 if (__n_move != 0)
2886 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2887 }
2888 }
2889 else
2890 {
2891 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002892 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 }
2894 traits_type::assign(__p + __pos, __n2, __c);
2895 __sz += __n2 - __n1;
2896 __set_size(__sz);
2897 __invalidate_iterators_past(__sz);
2898 traits_type::assign(__p[__sz], value_type());
2899 return *this;
2900}
2901
2902template <class _CharT, class _Traits, class _Allocator>
2903template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002904_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002906 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002908>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002909basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910 _InputIterator __j1, _InputIterator __j2)
2911{
Marshall Clow958362f2016-09-05 01:54:30 +00002912 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002913 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914}
2915
2916template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002917inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918basic_string<_CharT, _Traits, _Allocator>&
2919basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2920{
2921 return replace(__pos1, __n1, __str.data(), __str.size());
2922}
2923
2924template <class _CharT, class _Traits, class _Allocator>
2925basic_string<_CharT, _Traits, _Allocator>&
2926basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2927 size_type __pos2, size_type __n2)
2928{
2929 size_type __str_sz = __str.size();
2930 if (__pos2 > __str_sz)
2931 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002932 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933}
2934
2935template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002936template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002937_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002938<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002939 __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 +00002940 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002941>
Marshall Clow82513342016-09-24 22:45:42 +00002942basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002943 size_type __pos2, size_type __n2)
2944{
Marshall Clow82513342016-09-24 22:45:42 +00002945 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002946 size_type __str_sz = __sv.size();
2947 if (__pos2 > __str_sz)
2948 this->__throw_out_of_range();
2949 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2950}
2951
2952template <class _CharT, class _Traits, class _Allocator>
2953basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002954basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002956 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957 return replace(__pos, __n1, __s, traits_type::length(__s));
2958}
2959
2960template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002961inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00002963basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964{
2965 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2966 __str.data(), __str.size());
2967}
2968
2969template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002970inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002972basic_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 +00002973{
2974 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2975}
2976
2977template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002978inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002980basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981{
2982 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2983}
2984
2985template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002986inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00002988basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989{
2990 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2991}
2992
2993// erase
2994
2995template <class _CharT, class _Traits, class _Allocator>
2996basic_string<_CharT, _Traits, _Allocator>&
2997basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2998{
2999 size_type __sz = size();
3000 if (__pos > __sz)
3001 this->__throw_out_of_range();
3002 if (__n)
3003 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003004 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003005 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006 size_type __n_move = __sz - __pos - __n;
3007 if (__n_move != 0)
3008 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3009 __sz -= __n;
3010 __set_size(__sz);
3011 __invalidate_iterators_past(__sz);
3012 traits_type::assign(__p[__sz], value_type());
3013 }
3014 return *this;
3015}
3016
3017template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003018inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019typename basic_string<_CharT, _Traits, _Allocator>::iterator
3020basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3021{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003022#if _LIBCPP_DEBUG_LEVEL >= 2
3023 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3024 "string::erase(iterator) called with an iterator not"
3025 " referring to this string");
3026#endif
3027 _LIBCPP_ASSERT(__pos != end(),
3028 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029 iterator __b = begin();
3030 size_type __r = static_cast<size_type>(__pos - __b);
3031 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003032 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033}
3034
3035template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003036inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037typename basic_string<_CharT, _Traits, _Allocator>::iterator
3038basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3039{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003040#if _LIBCPP_DEBUG_LEVEL >= 2
3041 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3042 "string::erase(iterator, iterator) called with an iterator not"
3043 " referring to this string");
3044#endif
3045 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046 iterator __b = begin();
3047 size_type __r = static_cast<size_type>(__first - __b);
3048 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003049 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050}
3051
3052template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003053inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054void
3055basic_string<_CharT, _Traits, _Allocator>::pop_back()
3056{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003057 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058 size_type __sz;
3059 if (__is_long())
3060 {
3061 __sz = __get_long_size() - 1;
3062 __set_long_size(__sz);
3063 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3064 }
3065 else
3066 {
3067 __sz = __get_short_size() - 1;
3068 __set_short_size(__sz);
3069 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3070 }
3071 __invalidate_iterators_past(__sz);
3072}
3073
3074template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003075inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003077basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078{
3079 __invalidate_all_iterators();
3080 if (__is_long())
3081 {
3082 traits_type::assign(*__get_long_pointer(), value_type());
3083 __set_long_size(0);
3084 }
3085 else
3086 {
3087 traits_type::assign(*__get_short_pointer(), value_type());
3088 __set_short_size(0);
3089 }
3090}
3091
3092template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003093inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094void
3095basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3096{
3097 if (__is_long())
3098 {
3099 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3100 __set_long_size(__pos);
3101 }
3102 else
3103 {
3104 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3105 __set_short_size(__pos);
3106 }
3107 __invalidate_iterators_past(__pos);
3108}
3109
3110template <class _CharT, class _Traits, class _Allocator>
3111void
3112basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3113{
3114 size_type __sz = size();
3115 if (__n > __sz)
3116 append(__n - __sz, __c);
3117 else
3118 __erase_to_end(__n);
3119}
3120
3121template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003122inline void
3123basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3124{
3125 size_type __sz = size();
3126 if (__n > __sz) {
3127 __append_default_init(__n - __sz);
3128 } else
3129 __erase_to_end(__n);
3130}
3131
3132template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003133inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003135basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003137 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003138#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003139 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003141 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142#endif
3143}
3144
3145template <class _CharT, class _Traits, class _Allocator>
3146void
3147basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
3148{
3149 if (__res_arg > max_size())
3150 this->__throw_length_error();
3151 size_type __cap = capacity();
3152 size_type __sz = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003153 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154 __res_arg = __recommend(__res_arg);
3155 if (__res_arg != __cap)
3156 {
3157 pointer __new_data, __p;
3158 bool __was_long, __now_long;
3159 if (__res_arg == __min_cap - 1)
3160 {
3161 __was_long = true;
3162 __now_long = false;
3163 __new_data = __get_short_pointer();
3164 __p = __get_long_pointer();
3165 }
3166 else
3167 {
3168 if (__res_arg > __cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003169 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170 else
3171 {
3172 #ifndef _LIBCPP_NO_EXCEPTIONS
3173 try
3174 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003175 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003176 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177 #ifndef _LIBCPP_NO_EXCEPTIONS
3178 }
3179 catch (...)
3180 {
3181 return;
3182 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003183 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd17880b2013-06-28 16:59:19 +00003184 if (__new_data == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185 return;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003186 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187 }
3188 __now_long = true;
3189 __was_long = __is_long();
3190 __p = __get_pointer();
3191 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003192 traits_type::copy(_VSTD::__to_address(__new_data),
3193 _VSTD::__to_address(__p), size()+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194 if (__was_long)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003195 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196 if (__now_long)
3197 {
3198 __set_long_cap(__res_arg+1);
3199 __set_long_size(__sz);
3200 __set_long_pointer(__new_data);
3201 }
3202 else
3203 __set_short_size(__sz);
3204 __invalidate_all_iterators();
3205 }
3206}
3207
3208template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003209inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003211basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003213 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214 return *(data() + __pos);
3215}
3216
3217template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003218inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003220basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003222 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223 return *(__get_pointer() + __pos);
3224}
3225
3226template <class _CharT, class _Traits, class _Allocator>
3227typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3228basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3229{
3230 if (__n >= size())
3231 this->__throw_out_of_range();
3232 return (*this)[__n];
3233}
3234
3235template <class _CharT, class _Traits, class _Allocator>
3236typename basic_string<_CharT, _Traits, _Allocator>::reference
3237basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3238{
3239 if (__n >= size())
3240 this->__throw_out_of_range();
3241 return (*this)[__n];
3242}
3243
3244template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003245inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003247basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003249 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250 return *__get_pointer();
3251}
3252
3253template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003254inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003256basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003258 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259 return *data();
3260}
3261
3262template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003263inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003265basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003267 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268 return *(__get_pointer() + size() - 1);
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>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003274basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003276 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277 return *(data() + size() - 1);
3278}
3279
3280template <class _CharT, class _Traits, class _Allocator>
3281typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003282basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003283{
3284 size_type __sz = size();
3285 if (__pos > __sz)
3286 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003287 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288 traits_type::copy(__s, data() + __pos, __rlen);
3289 return __rlen;
3290}
3291
3292template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003293inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294basic_string<_CharT, _Traits, _Allocator>
3295basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3296{
3297 return basic_string(*this, __pos, __n, __alloc());
3298}
3299
3300template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003301inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302void
3303basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003304#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003305 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003306#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003307 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003308 __is_nothrow_swappable<allocator_type>::value)
3309#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003311#if _LIBCPP_DEBUG_LEVEL >= 2
3312 if (!__is_long())
3313 __get_db()->__invalidate_all(this);
3314 if (!__str.__is_long())
3315 __get_db()->__invalidate_all(&__str);
3316 __get_db()->swap(this, &__str);
3317#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003318 _LIBCPP_ASSERT(
3319 __alloc_traits::propagate_on_container_swap::value ||
3320 __alloc_traits::is_always_equal::value ||
3321 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003322 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow8982dcd2015-07-13 20:04:56 +00003323 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324}
3325
3326// find
3327
3328template <class _Traits>
3329struct _LIBCPP_HIDDEN __traits_eq
3330{
3331 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003332 _LIBCPP_INLINE_VISIBILITY
3333 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3334 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003335};
3336
3337template<class _CharT, class _Traits, class _Allocator>
3338typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003339basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003340 size_type __pos,
3341 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003343 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003344 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003345 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346}
3347
3348template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003349inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003351basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3352 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003354 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003355 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356}
3357
3358template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003359template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003360_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003361<
3362 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3363 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003364>
Marshall Clowe46031a2018-07-02 18:41:15 +00003365basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
3366 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003367{
Marshall Clowe46031a2018-07-02 18:41:15 +00003368 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003369 return __str_find<value_type, size_type, traits_type, npos>
3370 (data(), size(), __sv.data(), __pos, __sv.size());
3371}
3372
3373template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003374inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003375typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003376basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003377 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003379 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003380 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003381 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382}
3383
3384template<class _CharT, class _Traits, class _Allocator>
3385typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003386basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3387 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003389 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003390 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391}
3392
3393// rfind
3394
3395template<class _CharT, class _Traits, class _Allocator>
3396typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003397basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003398 size_type __pos,
3399 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003401 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003402 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003403 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404}
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>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003409basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3410 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003412 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003413 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414}
3415
3416template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003417template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003418_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003419<
3420 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3421 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003422>
Marshall Clowe46031a2018-07-02 18:41:15 +00003423basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
3424 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003425{
Marshall Clowe46031a2018-07-02 18:41:15 +00003426 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003427 return __str_rfind<value_type, size_type, traits_type, npos>
3428 (data(), size(), __sv.data(), __pos, __sv.size());
3429}
3430
3431template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003432inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003433typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003434basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003435 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003437 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003438 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003439 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440}
3441
3442template<class _CharT, class _Traits, class _Allocator>
3443typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003444basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3445 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003447 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003448 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449}
3450
3451// find_first_of
3452
3453template<class _CharT, class _Traits, class _Allocator>
3454typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003455basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003456 size_type __pos,
3457 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003459 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003460 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003461 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462}
3463
3464template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003465inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003467basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3468 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003470 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003471 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472}
3473
3474template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003475template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003476_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003477<
3478 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3479 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003480>
Marshall Clowe46031a2018-07-02 18:41:15 +00003481basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
3482 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003483{
Marshall Clowe46031a2018-07-02 18:41:15 +00003484 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003485 return __str_find_first_of<value_type, size_type, traits_type, npos>
3486 (data(), size(), __sv.data(), __pos, __sv.size());
3487}
3488
3489template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003490inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003491typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003492basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003493 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003495 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003496 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003497 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498}
3499
3500template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003501inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003503basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3504 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505{
3506 return find(__c, __pos);
3507}
3508
3509// find_last_of
3510
3511template<class _CharT, class _Traits, class _Allocator>
3512typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003513basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003514 size_type __pos,
3515 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003517 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003518 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003519 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520}
3521
3522template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003523inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003525basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3526 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003528 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003529 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530}
3531
3532template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003533template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003534_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003535<
3536 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3537 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003538>
Marshall Clowe46031a2018-07-02 18:41:15 +00003539basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
3540 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003541{
Marshall Clowe46031a2018-07-02 18:41:15 +00003542 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003543 return __str_find_last_of<value_type, size_type, traits_type, npos>
3544 (data(), size(), __sv.data(), __pos, __sv.size());
3545}
3546
3547template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003548inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003549typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003550basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003551 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003553 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003554 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003555 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556}
3557
3558template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003559inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003560typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003561basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3562 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563{
3564 return rfind(__c, __pos);
3565}
3566
3567// find_first_not_of
3568
3569template<class _CharT, class _Traits, class _Allocator>
3570typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003571basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003572 size_type __pos,
3573 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003575 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003576 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003577 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578}
3579
3580template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003581inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003583basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3584 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003586 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003587 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588}
3589
3590template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003591template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003592_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003593<
3594 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3595 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003596>
Marshall Clowe46031a2018-07-02 18:41:15 +00003597basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
3598 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003599{
Marshall Clowe46031a2018-07-02 18:41:15 +00003600 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003601 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3602 (data(), size(), __sv.data(), __pos, __sv.size());
3603}
3604
3605template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003606inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003607typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003608basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003609 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003611 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003612 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003613 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614}
3615
3616template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003617inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003619basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3620 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003622 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003623 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624}
3625
3626// find_last_not_of
3627
3628template<class _CharT, class _Traits, class _Allocator>
3629typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003630basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003631 size_type __pos,
3632 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003634 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003635 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003636 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637}
3638
3639template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003640inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003642basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3643 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003645 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003646 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647}
3648
3649template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003650template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003651_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003652<
3653 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3654 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003655>
Marshall Clowe46031a2018-07-02 18:41:15 +00003656basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
3657 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003658{
Marshall Clowe46031a2018-07-02 18:41:15 +00003659 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003660 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3661 (data(), size(), __sv.data(), __pos, __sv.size());
3662}
3663
3664template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003665inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003666typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003667basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003668 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003670 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003671 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003672 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673}
3674
3675template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003676inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003678basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3679 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003681 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003682 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683}
3684
3685// compare
3686
3687template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003688template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003689_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003690<
3691 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3692 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003693>
Marshall Clowe46031a2018-07-02 18:41:15 +00003694basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003695{
Marshall Clowe46031a2018-07-02 18:41:15 +00003696 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003697 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003698 size_t __rhs_sz = __sv.size();
3699 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003700 _VSTD::min(__lhs_sz, __rhs_sz));
3701 if (__result != 0)
3702 return __result;
3703 if (__lhs_sz < __rhs_sz)
3704 return -1;
3705 if (__lhs_sz > __rhs_sz)
3706 return 1;
3707 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708}
3709
3710template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003711inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003712int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003713basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003715 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716}
3717
3718template <class _CharT, class _Traits, class _Allocator>
3719int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003720basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3721 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003722 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003723 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003725 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726 size_type __sz = size();
3727 if (__pos1 > __sz || __n2 == npos)
3728 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003729 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3730 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003731 if (__r == 0)
3732 {
3733 if (__rlen < __n2)
3734 __r = -1;
3735 else if (__rlen > __n2)
3736 __r = 1;
3737 }
3738 return __r;
3739}
3740
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003741template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003742template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003743_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003744<
3745 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3746 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003747>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003748basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3749 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003750 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003751{
Marshall Clowe46031a2018-07-02 18:41:15 +00003752 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003753 return compare(__pos1, __n1, __sv.data(), __sv.size());
3754}
3755
3756template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003757inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003758int
3759basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3760 size_type __n1,
3761 const basic_string& __str) const
3762{
3763 return compare(__pos1, __n1, __str.data(), __str.size());
3764}
3765
3766template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003767template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003768_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003769<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003770 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3771 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003772 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003773>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003774basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3775 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003776 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003777 size_type __pos2,
3778 size_type __n2) const
3779{
Marshall Clow82513342016-09-24 22:45:42 +00003780 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003781 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3782}
3783
3784template <class _CharT, class _Traits, class _Allocator>
3785int
3786basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3787 size_type __n1,
3788 const basic_string& __str,
3789 size_type __pos2,
3790 size_type __n2) const
3791{
3792 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3793}
3794
3795template <class _CharT, class _Traits, class _Allocator>
3796int
3797basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3798{
3799 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3800 return compare(0, npos, __s, traits_type::length(__s));
3801}
3802
3803template <class _CharT, class _Traits, class _Allocator>
3804int
3805basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3806 size_type __n1,
3807 const value_type* __s) const
3808{
3809 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3810 return compare(__pos1, __n1, __s, traits_type::length(__s));
3811}
3812
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813// __invariants
3814
3815template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003816inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817bool
3818basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3819{
3820 if (size() > capacity())
3821 return false;
3822 if (capacity() < __min_cap - 1)
3823 return false;
3824 if (data() == 0)
3825 return false;
3826 if (data()[size()] != value_type(0))
3827 return false;
3828 return true;
3829}
3830
Vedant Kumar55e007e2018-03-08 21:15:26 +00003831// __clear_and_shrink
3832
3833template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003834inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003835void
Marshall Clowe60a7182018-05-29 17:04:37 +00003836basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003837{
3838 clear();
3839 if(__is_long())
3840 {
3841 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3842 __set_long_cap(0);
3843 __set_short_size(0);
3844 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003845}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003846
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847// operator==
3848
3849template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003850inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851bool
3852operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003853 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003855 size_t __lhs_sz = __lhs.size();
3856 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3857 __rhs.data(),
3858 __lhs_sz) == 0;
3859}
3860
3861template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003862inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003863bool
3864operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3865 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3866{
3867 size_t __lhs_sz = __lhs.size();
3868 if (__lhs_sz != __rhs.size())
3869 return false;
3870 const char* __lp = __lhs.data();
3871 const char* __rp = __rhs.data();
3872 if (__lhs.__is_long())
3873 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3874 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3875 if (*__lp != *__rp)
3876 return false;
3877 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878}
3879
3880template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003881inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003883operator==(const _CharT* __lhs,
3884 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003885{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003886 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3887 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3888 size_t __lhs_len = _Traits::length(__lhs);
3889 if (__lhs_len != __rhs.size()) return false;
3890 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003891}
3892
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003894inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003895bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003896operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3897 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003899 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3900 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3901 size_t __rhs_len = _Traits::length(__rhs);
3902 if (__rhs_len != __lhs.size()) return false;
3903 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904}
3905
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003906template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003907inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908bool
3909operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003910 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911{
3912 return !(__lhs == __rhs);
3913}
3914
3915template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003916inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003918operator!=(const _CharT* __lhs,
3919 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920{
3921 return !(__lhs == __rhs);
3922}
3923
3924template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003925inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003927operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3928 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929{
3930 return !(__lhs == __rhs);
3931}
3932
3933// operator<
3934
3935template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003936inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937bool
3938operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003939 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940{
Howard Hinnanta2660c12011-07-24 15:07:21 +00003941 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942}
3943
3944template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003945inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003946bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003947operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3948 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003949{
Howard Hinnanta2660c12011-07-24 15:07:21 +00003950 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951}
3952
3953template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003954inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003956operator< (const _CharT* __lhs,
3957 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958{
3959 return __rhs.compare(__lhs) > 0;
3960}
3961
Howard Hinnantc51e1022010-05-11 19:42:16 +00003962// operator>
3963
3964template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003965inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966bool
3967operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003968 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969{
3970 return __rhs < __lhs;
3971}
3972
3973template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003974inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003976operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3977 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978{
3979 return __rhs < __lhs;
3980}
3981
3982template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003983inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003984bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003985operator> (const _CharT* __lhs,
3986 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987{
3988 return __rhs < __lhs;
3989}
3990
3991// operator<=
3992
3993template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003994inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995bool
3996operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003997 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998{
3999 return !(__rhs < __lhs);
4000}
4001
4002template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004003inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004005operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4006 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007{
4008 return !(__rhs < __lhs);
4009}
4010
4011template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004012inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004014operator<=(const _CharT* __lhs,
4015 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016{
4017 return !(__rhs < __lhs);
4018}
4019
4020// operator>=
4021
4022template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004023inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024bool
4025operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004026 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027{
4028 return !(__lhs < __rhs);
4029}
4030
4031template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004032inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004034operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4035 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036{
4037 return !(__lhs < __rhs);
4038}
4039
4040template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004041inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004043operator>=(const _CharT* __lhs,
4044 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045{
4046 return !(__lhs < __rhs);
4047}
4048
4049// operator +
4050
4051template<class _CharT, class _Traits, class _Allocator>
4052basic_string<_CharT, _Traits, _Allocator>
4053operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4054 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4055{
4056 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4057 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4058 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4059 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4060 __r.append(__rhs.data(), __rhs_sz);
4061 return __r;
4062}
4063
4064template<class _CharT, class _Traits, class _Allocator>
4065basic_string<_CharT, _Traits, _Allocator>
4066operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4067{
4068 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4069 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4070 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4071 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4072 __r.append(__rhs.data(), __rhs_sz);
4073 return __r;
4074}
4075
4076template<class _CharT, class _Traits, class _Allocator>
4077basic_string<_CharT, _Traits, _Allocator>
4078operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4079{
4080 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4081 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4082 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4083 __r.append(__rhs.data(), __rhs_sz);
4084 return __r;
4085}
4086
4087template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004088inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089basic_string<_CharT, _Traits, _Allocator>
4090operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4091{
4092 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4093 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4094 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4095 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4096 __r.append(__rhs, __rhs_sz);
4097 return __r;
4098}
4099
4100template<class _CharT, class _Traits, class _Allocator>
4101basic_string<_CharT, _Traits, _Allocator>
4102operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4103{
4104 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4105 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4106 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4107 __r.push_back(__rhs);
4108 return __r;
4109}
4110
Eric Fiselierfc92be82017-04-19 00:28:44 +00004111#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112
4113template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004114inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115basic_string<_CharT, _Traits, _Allocator>
4116operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4117{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004118 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119}
4120
4121template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004122inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123basic_string<_CharT, _Traits, _Allocator>
4124operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4125{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004126 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127}
4128
4129template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004130inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131basic_string<_CharT, _Traits, _Allocator>
4132operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4133{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004134 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135}
4136
4137template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004138inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139basic_string<_CharT, _Traits, _Allocator>
4140operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4141{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004142 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143}
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 +00004147basic_string<_CharT, _Traits, _Allocator>
4148operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4149{
4150 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004151 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152}
4153
4154template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004155inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156basic_string<_CharT, _Traits, _Allocator>
4157operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4158{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004159 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160}
4161
4162template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004163inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004164basic_string<_CharT, _Traits, _Allocator>
4165operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4166{
4167 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004168 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169}
4170
Eric Fiselierfc92be82017-04-19 00:28:44 +00004171#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172
4173// swap
4174
4175template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004176inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004178swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004179 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4180 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181{
4182 __lhs.swap(__rhs);
4183}
4184
Marshall Clow8732fed2018-12-11 04:35:44 +00004185#ifndef _LIBCPP_NO_HAS_CHAR8_T
4186typedef basic_string<char8_t> u8string;
4187#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188
Marshall Clow8732fed2018-12-11 04:35:44 +00004189#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190typedef basic_string<char16_t> u16string;
4191typedef basic_string<char32_t> u32string;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004192#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004194_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
4195_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
4196_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
4197_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
4198_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004199
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004200_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
4201_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
4202_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004203
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004204_LIBCPP_FUNC_VIS string to_string(int __val);
4205_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4206_LIBCPP_FUNC_VIS string to_string(long __val);
4207_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4208_LIBCPP_FUNC_VIS string to_string(long long __val);
4209_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4210_LIBCPP_FUNC_VIS string to_string(float __val);
4211_LIBCPP_FUNC_VIS string to_string(double __val);
4212_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004213
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004214_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
4215_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
4216_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
4217_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
4218_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004219
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004220_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
4221_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
4222_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004223
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004224_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4225_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4226_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4227_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4228_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4229_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4230_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4231_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4232_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004233
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234template<class _CharT, class _Traits, class _Allocator>
4235 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4236 basic_string<_CharT, _Traits, _Allocator>::npos;
4237
Marshall Clow851b9ec2019-05-20 21:56:51 +00004238template <class _CharT, class _Allocator>
4239struct _LIBCPP_TEMPLATE_VIS
4240 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4241 : public unary_function<
4242 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243{
4244 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004245 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4246 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247};
4248
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249
Howard Hinnantdc095972011-07-18 15:51:59 +00004250template<class _CharT, class _Traits, class _Allocator>
4251basic_ostream<_CharT, _Traits>&
4252operator<<(basic_ostream<_CharT, _Traits>& __os,
4253 const basic_string<_CharT, _Traits, _Allocator>& __str);
4254
4255template<class _CharT, class _Traits, class _Allocator>
4256basic_istream<_CharT, _Traits>&
4257operator>>(basic_istream<_CharT, _Traits>& __is,
4258 basic_string<_CharT, _Traits, _Allocator>& __str);
4259
4260template<class _CharT, class _Traits, class _Allocator>
4261basic_istream<_CharT, _Traits>&
4262getline(basic_istream<_CharT, _Traits>& __is,
4263 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4264
4265template<class _CharT, class _Traits, class _Allocator>
4266inline _LIBCPP_INLINE_VISIBILITY
4267basic_istream<_CharT, _Traits>&
4268getline(basic_istream<_CharT, _Traits>& __is,
4269 basic_string<_CharT, _Traits, _Allocator>& __str);
4270
Eric Fiselierfc92be82017-04-19 00:28:44 +00004271#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004272
4273template<class _CharT, class _Traits, class _Allocator>
4274inline _LIBCPP_INLINE_VISIBILITY
4275basic_istream<_CharT, _Traits>&
4276getline(basic_istream<_CharT, _Traits>&& __is,
4277 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4278
4279template<class _CharT, class _Traits, class _Allocator>
4280inline _LIBCPP_INLINE_VISIBILITY
4281basic_istream<_CharT, _Traits>&
4282getline(basic_istream<_CharT, _Traits>&& __is,
4283 basic_string<_CharT, _Traits, _Allocator>& __str);
4284
Eric Fiselierfc92be82017-04-19 00:28:44 +00004285#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004286
Marshall Clow29b53f22018-12-14 18:49:35 +00004287#if _LIBCPP_STD_VER > 17
4288template<class _CharT, class _Traits, class _Allocator, class _Up>
4289inline _LIBCPP_INLINE_VISIBILITY
4290void erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v)
4291{ __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end()); }
4292
4293template<class _CharT, class _Traits, class _Allocator, class _Predicate>
4294inline _LIBCPP_INLINE_VISIBILITY
4295void erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred)
4296{ __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred), __str.end()); }
4297#endif
4298
Howard Hinnant8ea98242013-08-23 17:37:05 +00004299#if _LIBCPP_DEBUG_LEVEL >= 2
4300
4301template<class _CharT, class _Traits, class _Allocator>
4302bool
4303basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4304{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004305 return this->data() <= _VSTD::__to_address(__i->base()) &&
4306 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004307}
4308
4309template<class _CharT, class _Traits, class _Allocator>
4310bool
4311basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4312{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004313 return this->data() < _VSTD::__to_address(__i->base()) &&
4314 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004315}
4316
4317template<class _CharT, class _Traits, class _Allocator>
4318bool
4319basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4320{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004321 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004322 return this->data() <= __p && __p <= this->data() + this->size();
4323}
4324
4325template<class _CharT, class _Traits, class _Allocator>
4326bool
4327basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4328{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004329 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004330 return this->data() <= __p && __p < this->data() + this->size();
4331}
4332
4333#endif // _LIBCPP_DEBUG_LEVEL >= 2
4334
Oliver Stannard998c5a02020-01-13 13:54:04 +00004335_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
4336_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<wchar_t>)
Shoaib Meenai6eb5f112017-06-29 02:52:46 +00004337
Louis Dionne173f29e2019-05-29 16:01:36 +00004338#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004339// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004340inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004341{
4342 inline namespace string_literals
4343 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004344 inline _LIBCPP_INLINE_VISIBILITY
4345 basic_string<char> operator "" s( const char *__str, size_t __len )
4346 {
4347 return basic_string<char> (__str, __len);
4348 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004349
Howard Hinnant5c167562013-08-07 19:39:48 +00004350 inline _LIBCPP_INLINE_VISIBILITY
4351 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4352 {
4353 return basic_string<wchar_t> (__str, __len);
4354 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004355
Marshall Clow8732fed2018-12-11 04:35:44 +00004356#ifndef _LIBCPP_NO_HAS_CHAR8_T
4357 inline _LIBCPP_INLINE_VISIBILITY
4358 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4359 {
4360 return basic_string<char8_t> (__str, __len);
4361 }
4362#endif
4363
Howard Hinnant5c167562013-08-07 19:39:48 +00004364 inline _LIBCPP_INLINE_VISIBILITY
4365 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4366 {
4367 return basic_string<char16_t> (__str, __len);
4368 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004369
Howard Hinnant5c167562013-08-07 19:39:48 +00004370 inline _LIBCPP_INLINE_VISIBILITY
4371 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4372 {
4373 return basic_string<char32_t> (__str, __len);
4374 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004375 }
4376}
4377#endif
4378
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379_LIBCPP_END_NAMESPACE_STD
4380
Eric Fiselierf4433a32017-05-31 22:07:49 +00004381_LIBCPP_POP_MACROS
4382
Howard Hinnantc51e1022010-05-11 19:42:16 +00004383#endif // _LIBCPP_STRING