blob: a63c3e1b402b438172eed927e02fd7caae2dcf3a [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 Fiselier03bbc922020-01-15 17:27:10 -05002269 const bool __str_is_long = __str.__is_long(); // Force single branch
2270 if (__is_long() || __str_is_long) {
2271 return assign(__str.data(), __str.size());
2272 }
2273 __r_.first().__r = __str.__r_.first().__r;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002274 }
2275 return *this;
2276}
2277
Eric Fiselierfc92be82017-04-19 00:28:44 +00002278#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002279
2280template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002281inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002282void
2283basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002284 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002285{
2286 if (__alloc() != __str.__alloc())
2287 assign(__str);
2288 else
2289 __move_assign(__str, true_type());
2290}
2291
2292template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002293inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002294void
2295basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002296#if _LIBCPP_STD_VER > 14
2297 _NOEXCEPT
2298#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002299 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002300#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002301{
marshall2ed41622019-11-27 07:13:00 -08002302 if (__is_long()) {
2303 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2304 __get_long_cap());
2305#if _LIBCPP_STD_VER <= 14
2306 if (!is_nothrow_move_assignable<allocator_type>::value) {
2307 __set_short_size(0);
2308 traits_type::assign(__get_short_pointer()[0], value_type());
2309 }
2310#endif
2311 }
2312 __move_assign_alloc(__str);
2313 __r_.first() = __str.__r_.first();
2314 __str.__set_short_size(0);
2315 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002316}
2317
2318template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002319inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002320basic_string<_CharT, _Traits, _Allocator>&
2321basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002322 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002323{
2324 __move_assign(__str, integral_constant<bool,
2325 __alloc_traits::propagate_on_container_move_assignment::value>());
2326 return *this;
2327}
2328
2329#endif
2330
2331template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002332template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002333_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002335 __is_exactly_cpp17_input_iterator <_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002336 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002338>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2340{
Marshall Clow958362f2016-09-05 01:54:30 +00002341 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002342 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002343 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002344}
2345
2346template <class _CharT, class _Traits, class _Allocator>
2347template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002348_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002350 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002351 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002353>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2355{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002356 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002357 size_type __cap = capacity();
2358 if (__cap < __n)
2359 {
2360 size_type __sz = size();
2361 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2362 }
2363 else
2364 __invalidate_iterators_past(__n);
2365 pointer __p = __get_pointer();
2366 for (; __first != __last; ++__first, ++__p)
2367 traits_type::assign(*__p, *__first);
2368 traits_type::assign(*__p, value_type());
2369 __set_size(__n);
2370 return *this;
2371}
2372
2373template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374basic_string<_CharT, _Traits, _Allocator>&
2375basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2376{
2377 size_type __sz = __str.size();
2378 if (__pos > __sz)
2379 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002380 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381}
2382
2383template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002384template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002385_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002386<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002387 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2388 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002389 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002390>
Marshall Clow82513342016-09-24 22:45:42 +00002391basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002392{
Marshall Clow82513342016-09-24 22:45:42 +00002393 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002394 size_type __sz = __sv.size();
2395 if (__pos > __sz)
2396 this->__throw_out_of_range();
2397 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2398}
2399
2400
2401template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002403basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002405 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002406 return assign(__s, traits_type::length(__s));
2407}
2408
2409// append
2410
2411template <class _CharT, class _Traits, class _Allocator>
2412basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002413basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002415 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 size_type __cap = capacity();
2417 size_type __sz = size();
2418 if (__cap - __sz >= __n)
2419 {
2420 if (__n)
2421 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002422 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423 traits_type::copy(__p + __sz, __s, __n);
2424 __sz += __n;
2425 __set_size(__sz);
2426 traits_type::assign(__p[__sz], value_type());
2427 }
2428 }
2429 else
2430 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2431 return *this;
2432}
2433
2434template <class _CharT, class _Traits, class _Allocator>
2435basic_string<_CharT, _Traits, _Allocator>&
2436basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2437{
2438 if (__n)
2439 {
2440 size_type __cap = capacity();
2441 size_type __sz = size();
2442 if (__cap - __sz < __n)
2443 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2444 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002445 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446 __sz += __n;
2447 __set_size(__sz);
2448 traits_type::assign(__p[__sz], value_type());
2449 }
2450 return *this;
2451}
2452
2453template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002454inline void
2455basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2456{
2457 if (__n)
2458 {
2459 size_type __cap = capacity();
2460 size_type __sz = size();
2461 if (__cap - __sz < __n)
2462 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2463 pointer __p = __get_pointer();
2464 __sz += __n;
2465 __set_size(__sz);
2466 traits_type::assign(__p[__sz], value_type());
2467 }
2468}
2469
2470template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471void
2472basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2473{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002474 bool __is_short = !__is_long();
2475 size_type __cap;
2476 size_type __sz;
2477 if (__is_short)
2478 {
2479 __cap = __min_cap - 1;
2480 __sz = __get_short_size();
2481 }
2482 else
2483 {
2484 __cap = __get_long_cap() - 1;
2485 __sz = __get_long_size();
2486 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002488 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002490 __is_short = !__is_long();
2491 }
2492 pointer __p;
2493 if (__is_short)
2494 {
2495 __p = __get_short_pointer() + __sz;
2496 __set_short_size(__sz+1);
2497 }
2498 else
2499 {
2500 __p = __get_long_pointer() + __sz;
2501 __set_long_size(__sz+1);
2502 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503 traits_type::assign(*__p, __c);
2504 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505}
2506
Marshall Clow6ebbf662016-09-07 03:32:06 +00002507template <class _Tp>
Marshall Clow958362f2016-09-05 01:54:30 +00002508bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2509{
2510 return __first <= __p && __p < __last;
2511}
2512
Marshall Clow6ebbf662016-09-07 03:32:06 +00002513template <class _Tp1, class _Tp2>
Eric Fiselier6003c772016-12-23 23:37:52 +00002514bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clow6ebbf662016-09-07 03:32:06 +00002515{
2516 return false;
2517}
2518
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519template <class _CharT, class _Traits, class _Allocator>
2520template<class _ForwardIterator>
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002521basic_string<_CharT, _Traits, _Allocator>&
2522basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2523 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524{
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002525 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002526 "function requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527 size_type __sz = size();
2528 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002529 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530 if (__n)
2531 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002532 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2533 _CharRef __tmp_ref = *__first;
2534 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002535 {
2536 const basic_string __temp (__first, __last, __alloc());
2537 append(__temp.data(), __temp.size());
2538 }
Louis Dionne173f29e2019-05-29 16:01:36 +00002539 else
Marshall Clow958362f2016-09-05 01:54:30 +00002540 {
2541 if (__cap - __sz < __n)
2542 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2543 pointer __p = __get_pointer() + __sz;
2544 for (; __first != __last; ++__p, ++__first)
2545 traits_type::assign(*__p, *__first);
2546 traits_type::assign(*__p, value_type());
2547 __set_size(__sz + __n);
2548 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549 }
2550 return *this;
2551}
2552
2553template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002554inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555basic_string<_CharT, _Traits, _Allocator>&
2556basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2557{
2558 return append(__str.data(), __str.size());
2559}
2560
2561template <class _CharT, class _Traits, class _Allocator>
2562basic_string<_CharT, _Traits, _Allocator>&
2563basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2564{
2565 size_type __sz = __str.size();
2566 if (__pos > __sz)
2567 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002568 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569}
2570
2571template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002572template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002573 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002574 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002575 __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 +00002576 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002577 >
Marshall Clow82513342016-09-24 22:45:42 +00002578basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002579{
Marshall Clow82513342016-09-24 22:45:42 +00002580 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002581 size_type __sz = __sv.size();
2582 if (__pos > __sz)
2583 this->__throw_out_of_range();
2584 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2585}
2586
2587template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002589basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002591 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592 return append(__s, traits_type::length(__s));
2593}
2594
2595// insert
2596
2597template <class _CharT, class _Traits, class _Allocator>
2598basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002599basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002601 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602 size_type __sz = size();
2603 if (__pos > __sz)
2604 this->__throw_out_of_range();
2605 size_type __cap = capacity();
2606 if (__cap - __sz >= __n)
2607 {
2608 if (__n)
2609 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002610 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611 size_type __n_move = __sz - __pos;
2612 if (__n_move != 0)
2613 {
2614 if (__p + __pos <= __s && __s < __p + __sz)
2615 __s += __n;
2616 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2617 }
2618 traits_type::move(__p + __pos, __s, __n);
2619 __sz += __n;
2620 __set_size(__sz);
2621 traits_type::assign(__p[__sz], value_type());
2622 }
2623 }
2624 else
2625 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2626 return *this;
2627}
2628
2629template <class _CharT, class _Traits, class _Allocator>
2630basic_string<_CharT, _Traits, _Allocator>&
2631basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2632{
2633 size_type __sz = size();
2634 if (__pos > __sz)
2635 this->__throw_out_of_range();
2636 if (__n)
2637 {
2638 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002639 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640 if (__cap - __sz >= __n)
2641 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002642 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 size_type __n_move = __sz - __pos;
2644 if (__n_move != 0)
2645 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2646 }
2647 else
2648 {
2649 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002650 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651 }
2652 traits_type::assign(__p + __pos, __n, __c);
2653 __sz += __n;
2654 __set_size(__sz);
2655 traits_type::assign(__p[__sz], value_type());
2656 }
2657 return *this;
2658}
2659
2660template <class _CharT, class _Traits, class _Allocator>
2661template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002662_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002664 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002665 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2666 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002667>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2669{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002670#if _LIBCPP_DEBUG_LEVEL >= 2
2671 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2672 "string::insert(iterator, range) called with an iterator not"
2673 " referring to this string");
2674#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002675 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002676 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677}
2678
2679template <class _CharT, class _Traits, class _Allocator>
2680template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002681_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002683 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002684 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002686>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2688{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002689#if _LIBCPP_DEBUG_LEVEL >= 2
2690 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2691 "string::insert(iterator, range) called with an iterator not"
2692 " referring to this string");
2693#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002695 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696 if (__n)
2697 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002698 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2699 _CharRef __tmp_char = *__first;
2700 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002701 {
2702 const basic_string __temp(__first, __last, __alloc());
2703 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2704 }
2705
2706 size_type __sz = size();
2707 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002708 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709 if (__cap - __sz >= __n)
2710 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002711 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 size_type __n_move = __sz - __ip;
2713 if (__n_move != 0)
2714 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2715 }
2716 else
2717 {
2718 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002719 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720 }
2721 __sz += __n;
2722 __set_size(__sz);
2723 traits_type::assign(__p[__sz], value_type());
2724 for (__p += __ip; __first != __last; ++__p, ++__first)
2725 traits_type::assign(*__p, *__first);
2726 }
2727 return begin() + __ip;
2728}
2729
2730template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002731inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732basic_string<_CharT, _Traits, _Allocator>&
2733basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2734{
2735 return insert(__pos1, __str.data(), __str.size());
2736}
2737
2738template <class _CharT, class _Traits, class _Allocator>
2739basic_string<_CharT, _Traits, _Allocator>&
2740basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2741 size_type __pos2, size_type __n)
2742{
2743 size_type __str_sz = __str.size();
2744 if (__pos2 > __str_sz)
2745 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002746 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747}
2748
2749template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002750template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002751_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002752<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002753 __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 +00002754 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002755>
Marshall Clow82513342016-09-24 22:45:42 +00002756basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002757 size_type __pos2, size_type __n)
2758{
Marshall Clow82513342016-09-24 22:45:42 +00002759 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002760 size_type __str_sz = __sv.size();
2761 if (__pos2 > __str_sz)
2762 this->__throw_out_of_range();
2763 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2764}
2765
2766template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002768basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002770 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 return insert(__pos, __s, traits_type::length(__s));
2772}
2773
2774template <class _CharT, class _Traits, class _Allocator>
2775typename basic_string<_CharT, _Traits, _Allocator>::iterator
2776basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2777{
2778 size_type __ip = static_cast<size_type>(__pos - begin());
2779 size_type __sz = size();
2780 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002781 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 if (__cap == __sz)
2783 {
2784 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002785 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 }
2787 else
2788 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002789 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 size_type __n_move = __sz - __ip;
2791 if (__n_move != 0)
2792 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2793 }
2794 traits_type::assign(__p[__ip], __c);
2795 traits_type::assign(__p[++__sz], value_type());
2796 __set_size(__sz);
2797 return begin() + static_cast<difference_type>(__ip);
2798}
2799
2800template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002801inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802typename basic_string<_CharT, _Traits, _Allocator>::iterator
2803basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2804{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002805#if _LIBCPP_DEBUG_LEVEL >= 2
2806 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2807 "string::insert(iterator, n, value) called with an iterator not"
2808 " referring to this string");
2809#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810 difference_type __p = __pos - begin();
2811 insert(static_cast<size_type>(__p), __n, __c);
2812 return begin() + __p;
2813}
2814
2815// replace
2816
2817template <class _CharT, class _Traits, class _Allocator>
2818basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002819basic_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 +00002820 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002822 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823 size_type __sz = size();
2824 if (__pos > __sz)
2825 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002826 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827 size_type __cap = capacity();
2828 if (__cap - __sz + __n1 >= __n2)
2829 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002830 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 if (__n1 != __n2)
2832 {
2833 size_type __n_move = __sz - __pos - __n1;
2834 if (__n_move != 0)
2835 {
2836 if (__n1 > __n2)
2837 {
2838 traits_type::move(__p + __pos, __s, __n2);
2839 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2840 goto __finish;
2841 }
2842 if (__p + __pos < __s && __s < __p + __sz)
2843 {
2844 if (__p + __pos + __n1 <= __s)
2845 __s += __n2 - __n1;
2846 else // __p + __pos < __s < __p + __pos + __n1
2847 {
2848 traits_type::move(__p + __pos, __s, __n1);
2849 __pos += __n1;
2850 __s += __n2;
2851 __n2 -= __n1;
2852 __n1 = 0;
2853 }
2854 }
2855 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2856 }
2857 }
2858 traits_type::move(__p + __pos, __s, __n2);
2859__finish:
Eric Fiseliere5b21842017-03-09 01:54:13 +00002860// __sz += __n2 - __n1; in this and the below function below can cause unsigned integer overflow,
2861// but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862 __sz += __n2 - __n1;
2863 __set_size(__sz);
2864 __invalidate_iterators_past(__sz);
2865 traits_type::assign(__p[__sz], value_type());
2866 }
2867 else
2868 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2869 return *this;
2870}
2871
2872template <class _CharT, class _Traits, class _Allocator>
2873basic_string<_CharT, _Traits, _Allocator>&
2874basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00002875 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876{
2877 size_type __sz = size();
2878 if (__pos > __sz)
2879 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002880 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002882 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883 if (__cap - __sz + __n1 >= __n2)
2884 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002885 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 if (__n1 != __n2)
2887 {
2888 size_type __n_move = __sz - __pos - __n1;
2889 if (__n_move != 0)
2890 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2891 }
2892 }
2893 else
2894 {
2895 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002896 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897 }
2898 traits_type::assign(__p + __pos, __n2, __c);
2899 __sz += __n2 - __n1;
2900 __set_size(__sz);
2901 __invalidate_iterators_past(__sz);
2902 traits_type::assign(__p[__sz], value_type());
2903 return *this;
2904}
2905
2906template <class _CharT, class _Traits, class _Allocator>
2907template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002908_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002910 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002912>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002913basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914 _InputIterator __j1, _InputIterator __j2)
2915{
Marshall Clow958362f2016-09-05 01:54:30 +00002916 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002917 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918}
2919
2920template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002921inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922basic_string<_CharT, _Traits, _Allocator>&
2923basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2924{
2925 return replace(__pos1, __n1, __str.data(), __str.size());
2926}
2927
2928template <class _CharT, class _Traits, class _Allocator>
2929basic_string<_CharT, _Traits, _Allocator>&
2930basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2931 size_type __pos2, size_type __n2)
2932{
2933 size_type __str_sz = __str.size();
2934 if (__pos2 > __str_sz)
2935 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002936 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937}
2938
2939template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002940template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002941_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002942<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002943 __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 +00002944 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002945>
Marshall Clow82513342016-09-24 22:45:42 +00002946basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002947 size_type __pos2, size_type __n2)
2948{
Marshall Clow82513342016-09-24 22:45:42 +00002949 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002950 size_type __str_sz = __sv.size();
2951 if (__pos2 > __str_sz)
2952 this->__throw_out_of_range();
2953 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2954}
2955
2956template <class _CharT, class _Traits, class _Allocator>
2957basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002958basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002960 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961 return replace(__pos, __n1, __s, traits_type::length(__s));
2962}
2963
2964template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002965inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00002967basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968{
2969 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2970 __str.data(), __str.size());
2971}
2972
2973template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002974inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002976basic_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 +00002977{
2978 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2979}
2980
2981template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002982inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002984basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985{
2986 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2987}
2988
2989template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002990inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00002992basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993{
2994 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2995}
2996
2997// erase
2998
2999template <class _CharT, class _Traits, class _Allocator>
3000basic_string<_CharT, _Traits, _Allocator>&
3001basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
3002{
3003 size_type __sz = size();
3004 if (__pos > __sz)
3005 this->__throw_out_of_range();
3006 if (__n)
3007 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003008 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003009 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010 size_type __n_move = __sz - __pos - __n;
3011 if (__n_move != 0)
3012 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3013 __sz -= __n;
3014 __set_size(__sz);
3015 __invalidate_iterators_past(__sz);
3016 traits_type::assign(__p[__sz], value_type());
3017 }
3018 return *this;
3019}
3020
3021template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003022inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023typename basic_string<_CharT, _Traits, _Allocator>::iterator
3024basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3025{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003026#if _LIBCPP_DEBUG_LEVEL >= 2
3027 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3028 "string::erase(iterator) called with an iterator not"
3029 " referring to this string");
3030#endif
3031 _LIBCPP_ASSERT(__pos != end(),
3032 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033 iterator __b = begin();
3034 size_type __r = static_cast<size_type>(__pos - __b);
3035 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003036 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037}
3038
3039template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003040inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041typename basic_string<_CharT, _Traits, _Allocator>::iterator
3042basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3043{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003044#if _LIBCPP_DEBUG_LEVEL >= 2
3045 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3046 "string::erase(iterator, iterator) called with an iterator not"
3047 " referring to this string");
3048#endif
3049 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050 iterator __b = begin();
3051 size_type __r = static_cast<size_type>(__first - __b);
3052 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003053 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054}
3055
3056template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003057inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058void
3059basic_string<_CharT, _Traits, _Allocator>::pop_back()
3060{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003061 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062 size_type __sz;
3063 if (__is_long())
3064 {
3065 __sz = __get_long_size() - 1;
3066 __set_long_size(__sz);
3067 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3068 }
3069 else
3070 {
3071 __sz = __get_short_size() - 1;
3072 __set_short_size(__sz);
3073 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3074 }
3075 __invalidate_iterators_past(__sz);
3076}
3077
3078template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003079inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003081basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082{
3083 __invalidate_all_iterators();
3084 if (__is_long())
3085 {
3086 traits_type::assign(*__get_long_pointer(), value_type());
3087 __set_long_size(0);
3088 }
3089 else
3090 {
3091 traits_type::assign(*__get_short_pointer(), value_type());
3092 __set_short_size(0);
3093 }
3094}
3095
3096template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003097inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003098void
3099basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3100{
3101 if (__is_long())
3102 {
3103 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3104 __set_long_size(__pos);
3105 }
3106 else
3107 {
3108 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3109 __set_short_size(__pos);
3110 }
3111 __invalidate_iterators_past(__pos);
3112}
3113
3114template <class _CharT, class _Traits, class _Allocator>
3115void
3116basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3117{
3118 size_type __sz = size();
3119 if (__n > __sz)
3120 append(__n - __sz, __c);
3121 else
3122 __erase_to_end(__n);
3123}
3124
3125template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003126inline void
3127basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3128{
3129 size_type __sz = size();
3130 if (__n > __sz) {
3131 __append_default_init(__n - __sz);
3132 } else
3133 __erase_to_end(__n);
3134}
3135
3136template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003137inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003139basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003141 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003142#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003143 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003145 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146#endif
3147}
3148
3149template <class _CharT, class _Traits, class _Allocator>
3150void
3151basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
3152{
3153 if (__res_arg > max_size())
3154 this->__throw_length_error();
3155 size_type __cap = capacity();
3156 size_type __sz = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003157 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158 __res_arg = __recommend(__res_arg);
3159 if (__res_arg != __cap)
3160 {
3161 pointer __new_data, __p;
3162 bool __was_long, __now_long;
3163 if (__res_arg == __min_cap - 1)
3164 {
3165 __was_long = true;
3166 __now_long = false;
3167 __new_data = __get_short_pointer();
3168 __p = __get_long_pointer();
3169 }
3170 else
3171 {
3172 if (__res_arg > __cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003173 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174 else
3175 {
3176 #ifndef _LIBCPP_NO_EXCEPTIONS
3177 try
3178 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003179 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003180 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003181 #ifndef _LIBCPP_NO_EXCEPTIONS
3182 }
3183 catch (...)
3184 {
3185 return;
3186 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003187 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd17880b2013-06-28 16:59:19 +00003188 if (__new_data == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189 return;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003190 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191 }
3192 __now_long = true;
3193 __was_long = __is_long();
3194 __p = __get_pointer();
3195 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003196 traits_type::copy(_VSTD::__to_address(__new_data),
3197 _VSTD::__to_address(__p), size()+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198 if (__was_long)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003199 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 if (__now_long)
3201 {
3202 __set_long_cap(__res_arg+1);
3203 __set_long_size(__sz);
3204 __set_long_pointer(__new_data);
3205 }
3206 else
3207 __set_short_size(__sz);
3208 __invalidate_all_iterators();
3209 }
3210}
3211
3212template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003213inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003215basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003217 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218 return *(data() + __pos);
3219}
3220
3221template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003222inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003224basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003226 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227 return *(__get_pointer() + __pos);
3228}
3229
3230template <class _CharT, class _Traits, class _Allocator>
3231typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3232basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3233{
3234 if (__n >= size())
3235 this->__throw_out_of_range();
3236 return (*this)[__n];
3237}
3238
3239template <class _CharT, class _Traits, class _Allocator>
3240typename basic_string<_CharT, _Traits, _Allocator>::reference
3241basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3242{
3243 if (__n >= size())
3244 this->__throw_out_of_range();
3245 return (*this)[__n];
3246}
3247
3248template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003249inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003251basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003253 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254 return *__get_pointer();
3255}
3256
3257template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003258inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003260basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003262 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 return *data();
3264}
3265
3266template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003267inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003269basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003271 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272 return *(__get_pointer() + size() - 1);
3273}
3274
3275template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003276inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003278basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003280 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281 return *(data() + size() - 1);
3282}
3283
3284template <class _CharT, class _Traits, class _Allocator>
3285typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003286basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003287{
3288 size_type __sz = size();
3289 if (__pos > __sz)
3290 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003291 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003292 traits_type::copy(__s, data() + __pos, __rlen);
3293 return __rlen;
3294}
3295
3296template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003297inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298basic_string<_CharT, _Traits, _Allocator>
3299basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3300{
3301 return basic_string(*this, __pos, __n, __alloc());
3302}
3303
3304template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003305inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003306void
3307basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003308#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003309 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003310#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003311 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003312 __is_nothrow_swappable<allocator_type>::value)
3313#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003315#if _LIBCPP_DEBUG_LEVEL >= 2
3316 if (!__is_long())
3317 __get_db()->__invalidate_all(this);
3318 if (!__str.__is_long())
3319 __get_db()->__invalidate_all(&__str);
3320 __get_db()->swap(this, &__str);
3321#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003322 _LIBCPP_ASSERT(
3323 __alloc_traits::propagate_on_container_swap::value ||
3324 __alloc_traits::is_always_equal::value ||
3325 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003326 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow8982dcd2015-07-13 20:04:56 +00003327 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328}
3329
3330// find
3331
3332template <class _Traits>
3333struct _LIBCPP_HIDDEN __traits_eq
3334{
3335 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003336 _LIBCPP_INLINE_VISIBILITY
3337 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3338 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339};
3340
3341template<class _CharT, class _Traits, class _Allocator>
3342typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003343basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003344 size_type __pos,
3345 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003347 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003348 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003349 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350}
3351
3352template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003353inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003355basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3356 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003358 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003359 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360}
3361
3362template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003363template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003364_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003365<
3366 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3367 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003368>
Marshall Clowe46031a2018-07-02 18:41:15 +00003369basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
3370 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003371{
Marshall Clowe46031a2018-07-02 18:41:15 +00003372 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003373 return __str_find<value_type, size_type, traits_type, npos>
3374 (data(), size(), __sv.data(), __pos, __sv.size());
3375}
3376
3377template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003378inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003379typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003380basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003381 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003383 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003384 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003385 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386}
3387
3388template<class _CharT, class _Traits, class _Allocator>
3389typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003390basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3391 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003393 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003394 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395}
3396
3397// rfind
3398
3399template<class _CharT, class _Traits, class _Allocator>
3400typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003401basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003402 size_type __pos,
3403 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003405 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003406 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003407 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408}
3409
3410template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003411inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003413basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3414 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003416 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003417 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418}
3419
3420template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003421template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003422_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003423<
3424 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3425 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003426>
Marshall Clowe46031a2018-07-02 18:41:15 +00003427basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
3428 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003429{
Marshall Clowe46031a2018-07-02 18:41:15 +00003430 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003431 return __str_rfind<value_type, size_type, traits_type, npos>
3432 (data(), size(), __sv.data(), __pos, __sv.size());
3433}
3434
3435template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003436inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003437typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003438basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003439 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003441 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003442 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003443 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444}
3445
3446template<class _CharT, class _Traits, class _Allocator>
3447typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003448basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3449 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003451 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003452 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453}
3454
3455// find_first_of
3456
3457template<class _CharT, class _Traits, class _Allocator>
3458typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003459basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003460 size_type __pos,
3461 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003463 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003464 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003465 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466}
3467
3468template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003469inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003470typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003471basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3472 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003474 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003475 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476}
3477
3478template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003479template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003480_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003481<
3482 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3483 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003484>
Marshall Clowe46031a2018-07-02 18:41:15 +00003485basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
3486 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003487{
Marshall Clowe46031a2018-07-02 18:41:15 +00003488 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003489 return __str_find_first_of<value_type, size_type, traits_type, npos>
3490 (data(), size(), __sv.data(), __pos, __sv.size());
3491}
3492
3493template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003494inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003495typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003496basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003497 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003499 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003500 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003501 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502}
3503
3504template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003505inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003507basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3508 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509{
3510 return find(__c, __pos);
3511}
3512
3513// find_last_of
3514
3515template<class _CharT, class _Traits, class _Allocator>
3516typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003517basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003518 size_type __pos,
3519 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003521 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003522 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003523 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524}
3525
3526template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003527inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003529basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3530 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003532 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003533 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534}
3535
3536template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003537template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003538_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003539<
3540 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3541 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003542>
Marshall Clowe46031a2018-07-02 18:41:15 +00003543basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
3544 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003545{
Marshall Clowe46031a2018-07-02 18:41:15 +00003546 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003547 return __str_find_last_of<value_type, size_type, traits_type, npos>
3548 (data(), size(), __sv.data(), __pos, __sv.size());
3549}
3550
3551template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003552inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003553typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003554basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003555 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003557 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003558 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003559 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003560}
3561
3562template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003563inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003565basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3566 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567{
3568 return rfind(__c, __pos);
3569}
3570
3571// find_first_not_of
3572
3573template<class _CharT, class _Traits, class _Allocator>
3574typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003575basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003576 size_type __pos,
3577 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003579 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003580 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003581 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582}
3583
3584template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003585inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003587basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3588 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003590 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003591 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592}
3593
3594template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003595template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003596_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003597<
3598 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3599 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003600>
Marshall Clowe46031a2018-07-02 18:41:15 +00003601basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
3602 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003603{
Marshall Clowe46031a2018-07-02 18:41:15 +00003604 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003605 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3606 (data(), size(), __sv.data(), __pos, __sv.size());
3607}
3608
3609template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003610inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003611typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003612basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003613 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003615 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003616 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003617 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618}
3619
3620template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003621inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003623basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3624 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003626 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003627 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628}
3629
3630// find_last_not_of
3631
3632template<class _CharT, class _Traits, class _Allocator>
3633typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003634basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003635 size_type __pos,
3636 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003638 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003639 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003640 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641}
3642
3643template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003644inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003646basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3647 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003649 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003650 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651}
3652
3653template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003654template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003655_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003656<
3657 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3658 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003659>
Marshall Clowe46031a2018-07-02 18:41:15 +00003660basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
3661 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003662{
Marshall Clowe46031a2018-07-02 18:41:15 +00003663 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003664 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3665 (data(), size(), __sv.data(), __pos, __sv.size());
3666}
3667
3668template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003669inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003670typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003671basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003672 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003674 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003675 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003676 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677}
3678
3679template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003680inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003682basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3683 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003685 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003686 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687}
3688
3689// compare
3690
3691template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003692template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003693_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003694<
3695 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3696 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003697>
Marshall Clowe46031a2018-07-02 18:41:15 +00003698basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003699{
Marshall Clowe46031a2018-07-02 18:41:15 +00003700 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003701 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702 size_t __rhs_sz = __sv.size();
3703 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003704 _VSTD::min(__lhs_sz, __rhs_sz));
3705 if (__result != 0)
3706 return __result;
3707 if (__lhs_sz < __rhs_sz)
3708 return -1;
3709 if (__lhs_sz > __rhs_sz)
3710 return 1;
3711 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003712}
3713
3714template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003715inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003717basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003719 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720}
3721
3722template <class _CharT, class _Traits, class _Allocator>
3723int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003724basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3725 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003726 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003727 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003729 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730 size_type __sz = size();
3731 if (__pos1 > __sz || __n2 == npos)
3732 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003733 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3734 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735 if (__r == 0)
3736 {
3737 if (__rlen < __n2)
3738 __r = -1;
3739 else if (__rlen > __n2)
3740 __r = 1;
3741 }
3742 return __r;
3743}
3744
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003745template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003746template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003747_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003748<
3749 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3750 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003751>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003752basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3753 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003754 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003755{
Marshall Clowe46031a2018-07-02 18:41:15 +00003756 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003757 return compare(__pos1, __n1, __sv.data(), __sv.size());
3758}
3759
3760template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003761inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003762int
3763basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3764 size_type __n1,
3765 const basic_string& __str) const
3766{
3767 return compare(__pos1, __n1, __str.data(), __str.size());
3768}
3769
3770template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003771template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003772_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003773<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003774 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3775 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003776 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003777>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003778basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3779 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003780 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003781 size_type __pos2,
3782 size_type __n2) const
3783{
Marshall Clow82513342016-09-24 22:45:42 +00003784 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003785 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3786}
3787
3788template <class _CharT, class _Traits, class _Allocator>
3789int
3790basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3791 size_type __n1,
3792 const basic_string& __str,
3793 size_type __pos2,
3794 size_type __n2) const
3795{
3796 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3797}
3798
3799template <class _CharT, class _Traits, class _Allocator>
3800int
3801basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3802{
3803 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3804 return compare(0, npos, __s, traits_type::length(__s));
3805}
3806
3807template <class _CharT, class _Traits, class _Allocator>
3808int
3809basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3810 size_type __n1,
3811 const value_type* __s) const
3812{
3813 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3814 return compare(__pos1, __n1, __s, traits_type::length(__s));
3815}
3816
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817// __invariants
3818
3819template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003820inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821bool
3822basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3823{
3824 if (size() > capacity())
3825 return false;
3826 if (capacity() < __min_cap - 1)
3827 return false;
3828 if (data() == 0)
3829 return false;
3830 if (data()[size()] != value_type(0))
3831 return false;
3832 return true;
3833}
3834
Vedant Kumar55e007e2018-03-08 21:15:26 +00003835// __clear_and_shrink
3836
3837template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003838inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003839void
Marshall Clowe60a7182018-05-29 17:04:37 +00003840basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003841{
3842 clear();
3843 if(__is_long())
3844 {
3845 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3846 __set_long_cap(0);
3847 __set_short_size(0);
3848 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003849}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003850
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851// operator==
3852
3853template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003854inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855bool
3856operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003857 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003859 size_t __lhs_sz = __lhs.size();
3860 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3861 __rhs.data(),
3862 __lhs_sz) == 0;
3863}
3864
3865template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003866inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003867bool
3868operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3869 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3870{
3871 size_t __lhs_sz = __lhs.size();
3872 if (__lhs_sz != __rhs.size())
3873 return false;
3874 const char* __lp = __lhs.data();
3875 const char* __rp = __rhs.data();
3876 if (__lhs.__is_long())
3877 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3878 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3879 if (*__lp != *__rp)
3880 return false;
3881 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882}
3883
3884template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003885inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003887operator==(const _CharT* __lhs,
3888 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003890 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3891 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3892 size_t __lhs_len = _Traits::length(__lhs);
3893 if (__lhs_len != __rhs.size()) return false;
3894 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003895}
3896
Howard Hinnantc51e1022010-05-11 19:42:16 +00003897template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003898inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003899bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003900operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3901 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003903 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3904 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3905 size_t __rhs_len = _Traits::length(__rhs);
3906 if (__rhs_len != __lhs.size()) return false;
3907 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908}
3909
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003910template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003911inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912bool
3913operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003914 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915{
3916 return !(__lhs == __rhs);
3917}
3918
3919template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003920inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003922operator!=(const _CharT* __lhs,
3923 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924{
3925 return !(__lhs == __rhs);
3926}
3927
3928template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003929inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003931operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3932 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003933{
3934 return !(__lhs == __rhs);
3935}
3936
3937// operator<
3938
3939template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003940inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003941bool
3942operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003943 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944{
Howard Hinnanta2660c12011-07-24 15:07:21 +00003945 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003946}
3947
3948template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003949inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003951operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3952 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003953{
Howard Hinnanta2660c12011-07-24 15:07:21 +00003954 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955}
3956
3957template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003960operator< (const _CharT* __lhs,
3961 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003962{
3963 return __rhs.compare(__lhs) > 0;
3964}
3965
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966// operator>
3967
3968template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003969inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003970bool
3971operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003972 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973{
3974 return __rhs < __lhs;
3975}
3976
3977template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003978inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003980operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3981 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003982{
3983 return __rhs < __lhs;
3984}
3985
3986template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003987inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003988bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003989operator> (const _CharT* __lhs,
3990 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991{
3992 return __rhs < __lhs;
3993}
3994
3995// operator<=
3996
3997template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003998inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999bool
4000operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004001 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002{
4003 return !(__rhs < __lhs);
4004}
4005
4006template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004009operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4010 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011{
4012 return !(__rhs < __lhs);
4013}
4014
4015template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004016inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004018operator<=(const _CharT* __lhs,
4019 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020{
4021 return !(__rhs < __lhs);
4022}
4023
4024// operator>=
4025
4026template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004027inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028bool
4029operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004030 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031{
4032 return !(__lhs < __rhs);
4033}
4034
4035template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004036inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004038operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4039 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040{
4041 return !(__lhs < __rhs);
4042}
4043
4044template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004045inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004047operator>=(const _CharT* __lhs,
4048 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049{
4050 return !(__lhs < __rhs);
4051}
4052
4053// operator +
4054
4055template<class _CharT, class _Traits, class _Allocator>
4056basic_string<_CharT, _Traits, _Allocator>
4057operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4058 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4059{
4060 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4061 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4062 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4063 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4064 __r.append(__rhs.data(), __rhs_sz);
4065 return __r;
4066}
4067
4068template<class _CharT, class _Traits, class _Allocator>
4069basic_string<_CharT, _Traits, _Allocator>
4070operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4071{
4072 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4073 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4074 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4075 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4076 __r.append(__rhs.data(), __rhs_sz);
4077 return __r;
4078}
4079
4080template<class _CharT, class _Traits, class _Allocator>
4081basic_string<_CharT, _Traits, _Allocator>
4082operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4083{
4084 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4085 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4086 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4087 __r.append(__rhs.data(), __rhs_sz);
4088 return __r;
4089}
4090
4091template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004092inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093basic_string<_CharT, _Traits, _Allocator>
4094operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4095{
4096 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4097 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4098 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4099 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4100 __r.append(__rhs, __rhs_sz);
4101 return __r;
4102}
4103
4104template<class _CharT, class _Traits, class _Allocator>
4105basic_string<_CharT, _Traits, _Allocator>
4106operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4107{
4108 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4109 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4110 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4111 __r.push_back(__rhs);
4112 return __r;
4113}
4114
Eric Fiselierfc92be82017-04-19 00:28:44 +00004115#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116
4117template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004118inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119basic_string<_CharT, _Traits, _Allocator>
4120operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4121{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004122 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123}
4124
4125template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004126inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127basic_string<_CharT, _Traits, _Allocator>
4128operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4129{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004130 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131}
4132
4133template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135basic_string<_CharT, _Traits, _Allocator>
4136operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4137{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004138 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139}
4140
4141template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004142inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143basic_string<_CharT, _Traits, _Allocator>
4144operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4145{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004146 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147}
4148
4149template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151basic_string<_CharT, _Traits, _Allocator>
4152operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4153{
4154 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004155 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156}
4157
4158template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004159inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160basic_string<_CharT, _Traits, _Allocator>
4161operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4162{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004163 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004164}
4165
4166template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004167inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168basic_string<_CharT, _Traits, _Allocator>
4169operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4170{
4171 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004172 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173}
4174
Eric Fiselierfc92be82017-04-19 00:28:44 +00004175#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176
4177// swap
4178
4179template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004180inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004182swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004183 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4184 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185{
4186 __lhs.swap(__rhs);
4187}
4188
Marshall Clow8732fed2018-12-11 04:35:44 +00004189#ifndef _LIBCPP_NO_HAS_CHAR8_T
4190typedef basic_string<char8_t> u8string;
4191#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192
Marshall Clow8732fed2018-12-11 04:35:44 +00004193#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194typedef basic_string<char16_t> u16string;
4195typedef basic_string<char32_t> u32string;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004196#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004198_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
4199_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
4200_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
4201_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
4202_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004203
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004204_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
4205_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
4206_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004207
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004208_LIBCPP_FUNC_VIS string to_string(int __val);
4209_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4210_LIBCPP_FUNC_VIS string to_string(long __val);
4211_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4212_LIBCPP_FUNC_VIS string to_string(long long __val);
4213_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4214_LIBCPP_FUNC_VIS string to_string(float __val);
4215_LIBCPP_FUNC_VIS string to_string(double __val);
4216_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004217
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004218_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
4219_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
4220_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
4221_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
4222_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004223
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004224_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
4225_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
4226_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004227
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004228_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4229_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4230_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4231_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4232_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4233_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4234_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4235_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4236_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004237
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004239_LIBCPP_FUNC_VIS
4240const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4241 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242
Marshall Clow851b9ec2019-05-20 21:56:51 +00004243template <class _CharT, class _Allocator>
4244struct _LIBCPP_TEMPLATE_VIS
4245 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4246 : public unary_function<
4247 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248{
4249 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004250 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4251 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252};
4253
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254
Howard Hinnantdc095972011-07-18 15:51:59 +00004255template<class _CharT, class _Traits, class _Allocator>
4256basic_ostream<_CharT, _Traits>&
4257operator<<(basic_ostream<_CharT, _Traits>& __os,
4258 const basic_string<_CharT, _Traits, _Allocator>& __str);
4259
4260template<class _CharT, class _Traits, class _Allocator>
4261basic_istream<_CharT, _Traits>&
4262operator>>(basic_istream<_CharT, _Traits>& __is,
4263 basic_string<_CharT, _Traits, _Allocator>& __str);
4264
4265template<class _CharT, class _Traits, class _Allocator>
4266basic_istream<_CharT, _Traits>&
4267getline(basic_istream<_CharT, _Traits>& __is,
4268 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4269
4270template<class _CharT, class _Traits, class _Allocator>
4271inline _LIBCPP_INLINE_VISIBILITY
4272basic_istream<_CharT, _Traits>&
4273getline(basic_istream<_CharT, _Traits>& __is,
4274 basic_string<_CharT, _Traits, _Allocator>& __str);
4275
Eric Fiselierfc92be82017-04-19 00:28:44 +00004276#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004277
4278template<class _CharT, class _Traits, class _Allocator>
4279inline _LIBCPP_INLINE_VISIBILITY
4280basic_istream<_CharT, _Traits>&
4281getline(basic_istream<_CharT, _Traits>&& __is,
4282 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4283
4284template<class _CharT, class _Traits, class _Allocator>
4285inline _LIBCPP_INLINE_VISIBILITY
4286basic_istream<_CharT, _Traits>&
4287getline(basic_istream<_CharT, _Traits>&& __is,
4288 basic_string<_CharT, _Traits, _Allocator>& __str);
4289
Eric Fiselierfc92be82017-04-19 00:28:44 +00004290#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004291
Marshall Clow29b53f22018-12-14 18:49:35 +00004292#if _LIBCPP_STD_VER > 17
4293template<class _CharT, class _Traits, class _Allocator, class _Up>
4294inline _LIBCPP_INLINE_VISIBILITY
4295void erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v)
4296{ __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end()); }
4297
4298template<class _CharT, class _Traits, class _Allocator, class _Predicate>
4299inline _LIBCPP_INLINE_VISIBILITY
4300void erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred)
4301{ __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred), __str.end()); }
4302#endif
4303
Howard Hinnant8ea98242013-08-23 17:37:05 +00004304#if _LIBCPP_DEBUG_LEVEL >= 2
4305
4306template<class _CharT, class _Traits, class _Allocator>
4307bool
4308basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4309{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004310 return this->data() <= _VSTD::__to_address(__i->base()) &&
4311 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004312}
4313
4314template<class _CharT, class _Traits, class _Allocator>
4315bool
4316basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4317{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004318 return this->data() < _VSTD::__to_address(__i->base()) &&
4319 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004320}
4321
4322template<class _CharT, class _Traits, class _Allocator>
4323bool
4324basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4325{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004326 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004327 return this->data() <= __p && __p <= this->data() + this->size();
4328}
4329
4330template<class _CharT, class _Traits, class _Allocator>
4331bool
4332basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4333{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004334 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004335 return this->data() <= __p && __p < this->data() + this->size();
4336}
4337
4338#endif // _LIBCPP_DEBUG_LEVEL >= 2
4339
Eric Fiselier5dcce5c2020-01-15 17:12:09 -05004340_LIBCPP_STRING_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
4341_LIBCPP_STRING_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
Shoaib Meenai6eb5f112017-06-29 02:52:46 +00004342
Louis Dionne173f29e2019-05-29 16:01:36 +00004343#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004344// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004345inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004346{
4347 inline namespace string_literals
4348 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004349 inline _LIBCPP_INLINE_VISIBILITY
4350 basic_string<char> operator "" s( const char *__str, size_t __len )
4351 {
4352 return basic_string<char> (__str, __len);
4353 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004354
Howard Hinnant5c167562013-08-07 19:39:48 +00004355 inline _LIBCPP_INLINE_VISIBILITY
4356 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4357 {
4358 return basic_string<wchar_t> (__str, __len);
4359 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004360
Marshall Clow8732fed2018-12-11 04:35:44 +00004361#ifndef _LIBCPP_NO_HAS_CHAR8_T
4362 inline _LIBCPP_INLINE_VISIBILITY
4363 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4364 {
4365 return basic_string<char8_t> (__str, __len);
4366 }
4367#endif
4368
Howard Hinnant5c167562013-08-07 19:39:48 +00004369 inline _LIBCPP_INLINE_VISIBILITY
4370 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4371 {
4372 return basic_string<char16_t> (__str, __len);
4373 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004374
Howard Hinnant5c167562013-08-07 19:39:48 +00004375 inline _LIBCPP_INLINE_VISIBILITY
4376 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4377 {
4378 return basic_string<char32_t> (__str, __len);
4379 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004380 }
4381}
4382#endif
4383
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384_LIBCPP_END_NAMESPACE_STD
4385
Eric Fiselierf4433a32017-05-31 22:07:49 +00004386_LIBCPP_POP_MACROS
4387
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388#endif // _LIBCPP_STRING