blob: 179080dbb5798deab1a1e6db55e11c401b226fd8 [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>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200440typename basic_string<charT, traits, Allocator>::size_type
441erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000442template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200443typename basic_string<charT, traits, Allocator>::size_type
444erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000445
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446typedef basic_string<char> string;
447typedef basic_string<wchar_t> wstring;
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000448typedef basic_string<char16_t> u16string;
449typedef basic_string<char32_t> u32string;
450
451int stoi (const string& str, size_t* idx = 0, int base = 10);
452long stol (const string& str, size_t* idx = 0, int base = 10);
453unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
454long long stoll (const string& str, size_t* idx = 0, int base = 10);
455unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
456
457float stof (const string& str, size_t* idx = 0);
458double stod (const string& str, size_t* idx = 0);
459long double stold(const string& str, size_t* idx = 0);
460
461string to_string(int val);
462string to_string(unsigned val);
463string to_string(long val);
464string to_string(unsigned long val);
465string to_string(long long val);
466string to_string(unsigned long long val);
467string to_string(float val);
468string to_string(double val);
469string to_string(long double val);
470
471int stoi (const wstring& str, size_t* idx = 0, int base = 10);
472long stol (const wstring& str, size_t* idx = 0, int base = 10);
473unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
474long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
475unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
476
477float stof (const wstring& str, size_t* idx = 0);
478double stod (const wstring& str, size_t* idx = 0);
479long double stold(const wstring& str, size_t* idx = 0);
480
481wstring to_wstring(int val);
482wstring to_wstring(unsigned val);
483wstring to_wstring(long val);
484wstring to_wstring(unsigned long val);
485wstring to_wstring(long long val);
486wstring to_wstring(unsigned long long val);
487wstring to_wstring(float val);
488wstring to_wstring(double val);
489wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000490
491template <> struct hash<string>;
492template <> struct hash<u16string>;
493template <> struct hash<u32string>;
494template <> struct hash<wstring>;
495
Marshall Clowcba751f2013-07-23 17:05:24 +0000496basic_string<char> operator "" s( const char *str, size_t len ); // C++14
497basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
498basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
499basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
500
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501} // std
502
503*/
504
505#include <__config>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000506#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507#include <iosfwd>
508#include <cstring>
Howard Hinnant155c2af2010-05-24 17:49:41 +0000509#include <cstdio> // For EOF.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510#include <cwchar>
511#include <algorithm>
512#include <iterator>
513#include <utility>
514#include <memory>
515#include <stdexcept>
516#include <type_traits>
517#include <initializer_list>
518#include <__functional_base>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000519#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
521#include <cstdint>
522#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000523
Eric Fiselier14b6de92014-08-10 23:53:08 +0000524#include <__debug>
525
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000526#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000528#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529
Eric Fiselierf4433a32017-05-31 22:07:49 +0000530_LIBCPP_PUSH_MACROS
531#include <__undef_macros>
532
533
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534_LIBCPP_BEGIN_NAMESPACE_STD
535
536// fpos
537
538template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000539class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540{
541private:
542 _StateT __st_;
543 streamoff __off_;
544public:
545 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
546
547 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
548
549 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
550 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
551
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 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
555 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
556};
557
558template <class _StateT>
559inline _LIBCPP_INLINE_VISIBILITY
560streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
561 {return streamoff(__x) - streamoff(__y);}
562
563template <class _StateT>
564inline _LIBCPP_INLINE_VISIBILITY
565bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
566 {return streamoff(__x) == streamoff(__y);}
567
568template <class _StateT>
569inline _LIBCPP_INLINE_VISIBILITY
570bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
571 {return streamoff(__x) != streamoff(__y);}
572
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573// basic_string
574
575template<class _CharT, class _Traits, class _Allocator>
576basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000577operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
578 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579
580template<class _CharT, class _Traits, class _Allocator>
581basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000582operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000583
584template<class _CharT, class _Traits, class _Allocator>
585basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000586operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587
588template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000589inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000591operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592
593template<class _CharT, class _Traits, class _Allocator>
594basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000595operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596
Shoaib Meenaiea363712017-07-29 02:54:41 +0000597_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
598
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000600class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601{
602protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000603 _LIBCPP_NORETURN void __throw_length_error() const;
604 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605};
606
607template <bool __b>
608void
609__basic_string_common<__b>::__throw_length_error() const
610{
Marshall Clow8fea1612016-08-25 15:09:01 +0000611 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612}
613
614template <bool __b>
615void
616__basic_string_common<__b>::__throw_out_of_range() const
617{
Marshall Clow8fea1612016-08-25 15:09:01 +0000618 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619}
620
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000621_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622
Marshall Clow039b2f02016-01-13 21:54:34 +0000623#ifdef _LIBCPP_NO_EXCEPTIONS
624template <class _Iter>
625struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
626#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
627template <class _Iter>
628struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
629#else
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500630template <class _Iter, bool = __is_cpp17_forward_iterator<_Iter>::value>
Marshall Clow039b2f02016-01-13 21:54:34 +0000631struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
Louis Dionne173f29e2019-05-29 16:01:36 +0000632 noexcept(++(declval<_Iter&>())) &&
633 is_nothrow_assignable<_Iter&, _Iter>::value &&
634 noexcept(declval<_Iter>() == declval<_Iter>()) &&
Marshall Clow039b2f02016-01-13 21:54:34 +0000635 noexcept(*declval<_Iter>())
636)) {};
637
Louis Dionne173f29e2019-05-29 16:01:36 +0000638template <class _Iter>
Marshall Clow039b2f02016-01-13 21:54:34 +0000639struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
640#endif
641
642
643template <class _Iter>
644struct __libcpp_string_gets_noexcept_iterator
645 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
646
Marshall Clow82513342016-09-24 22:45:42 +0000647template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500648struct __can_be_converted_to_string_view : public _BoolConstant<
649 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
650 !is_convertible<const _Tp&, const _CharT*>::value
651 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000652
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000653#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000654
655template <class _CharT, size_t = sizeof(_CharT)>
656struct __padding
657{
658 unsigned char __xx[sizeof(_CharT)-1];
659};
660
661template <class _CharT>
662struct __padding<_CharT, 1>
663{
664};
665
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000666#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000667
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000668template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000669class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670 : private __basic_string_common<true>
671{
672public:
673 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000674 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000676 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000678 typedef allocator_traits<allocator_type> __alloc_traits;
679 typedef typename __alloc_traits::size_type size_type;
680 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000681 typedef value_type& reference;
682 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000683 typedef typename __alloc_traits::pointer pointer;
684 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685
Marshall Clow79f33542018-03-21 00:36:05 +0000686 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
687 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
688 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
689 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000690 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000691 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000692 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000693
Howard Hinnant8ea98242013-08-23 17:37:05 +0000694#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695 typedef pointer iterator;
696 typedef const_pointer const_iterator;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000697#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 typedef __wrap_iter<pointer> iterator;
699 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000700#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000701 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
702 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000703
704private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000705
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000706#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000707
708 struct __long
709 {
710 pointer __data_;
711 size_type __size_;
712 size_type __cap_;
713 };
714
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000715#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000716 static const size_type __short_mask = 0x01;
717 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000718#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000719 static const size_type __short_mask = 0x80;
720 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant68bf1812013-04-30 21:44:48 +0000721#endif // _LIBCPP_BIG_ENDIAN
722
723 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
724 (sizeof(__long) - 1)/sizeof(value_type) : 2};
725
726 struct __short
727 {
728 value_type __data_[__min_cap];
729 struct
730 : __padding<value_type>
731 {
732 unsigned char __size_;
733 };
734 };
735
736#else
737
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738 struct __long
739 {
740 size_type __cap_;
741 size_type __size_;
742 pointer __data_;
743 };
744
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000745#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000746 static const size_type __short_mask = 0x80;
747 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000748#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000749 static const size_type __short_mask = 0x01;
750 static const size_type __long_mask = 0x1ul;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000751#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
754 (sizeof(__long) - 1)/sizeof(value_type) : 2};
755
756 struct __short
757 {
758 union
759 {
760 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000761 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762 };
763 value_type __data_[__min_cap];
764 };
765
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000766#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000767
Howard Hinnant8ea98242013-08-23 17:37:05 +0000768 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769
Howard Hinnant8ea98242013-08-23 17:37:05 +0000770 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771
772 struct __raw
773 {
774 size_type __words[__n_words];
775 };
776
777 struct __rep
778 {
779 union
780 {
781 __long __l;
782 __short __s;
783 __raw __r;
784 };
785 };
786
787 __compressed_pair<__rep, allocator_type> __r_;
788
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789public:
Eric Fiselier2ae9e062020-01-16 15:00:34 -0500790 _LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791 static const size_type npos = -1;
792
Howard Hinnant3e276872011-06-03 18:40:47 +0000793 _LIBCPP_INLINE_VISIBILITY basic_string()
794 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000795
796 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
797#if _LIBCPP_STD_VER <= 14
798 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
799#else
800 _NOEXCEPT;
801#endif
802
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803 basic_string(const basic_string& __str);
804 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000805
Eric Fiselierfc92be82017-04-19 00:28:44 +0000806#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000808 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000809#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000810 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000811#else
812 _NOEXCEPT;
813#endif
814
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000817#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000818
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500819 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000820 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500821 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000822 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
823 __init(__s, traits_type::length(__s));
824# if _LIBCPP_DEBUG_LEVEL >= 2
825 __get_db()->__insert_c(this);
826# endif
827 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000828
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500829 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000830 _LIBCPP_INLINE_VISIBILITY
831 basic_string(const _CharT* __s, const _Allocator& __a);
832
Howard Hinnantcf823322010-12-17 14:46:43 +0000833 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000834 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000835 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000836 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000837 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000838 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000839
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500840 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000841 _LIBCPP_INLINE_VISIBILITY
842 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
843
Marshall Clow83445802016-04-07 18:13:41 +0000844 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000845 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000846 _LIBCPP_INLINE_VISIBILITY
847 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000849
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500850 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 +0000851 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000852 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500853 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000854
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500855 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
856 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000857 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
858 explicit basic_string(const _Tp& __t);
859
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500860 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 +0000861 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
862 explicit basic_string(const _Tp& __t, const allocator_type& __a);
863
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -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);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500867 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000870#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000871 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000872 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000873 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000874 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000875#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000877 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000879 _LIBCPP_INLINE_VISIBILITY
880 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
881
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000882 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000883
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500884 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 +0000885 basic_string& operator=(const _Tp& __t)
886 {__self_view __sv = __t; return assign(__sv);}
887
Eric Fiselierfc92be82017-04-19 00:28:44 +0000888#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000890 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000891 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000892 _LIBCPP_INLINE_VISIBILITY
893 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000895 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897
Howard Hinnant8ea98242013-08-23 17:37:05 +0000898#if _LIBCPP_DEBUG_LEVEL >= 2
899 _LIBCPP_INLINE_VISIBILITY
900 iterator begin() _NOEXCEPT
901 {return iterator(this, __get_pointer());}
902 _LIBCPP_INLINE_VISIBILITY
903 const_iterator begin() const _NOEXCEPT
904 {return const_iterator(this, __get_pointer());}
905 _LIBCPP_INLINE_VISIBILITY
906 iterator end() _NOEXCEPT
907 {return iterator(this, __get_pointer() + size());}
908 _LIBCPP_INLINE_VISIBILITY
909 const_iterator end() const _NOEXCEPT
910 {return const_iterator(this, __get_pointer() + size());}
911#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000912 _LIBCPP_INLINE_VISIBILITY
913 iterator begin() _NOEXCEPT
914 {return iterator(__get_pointer());}
915 _LIBCPP_INLINE_VISIBILITY
916 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000917 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000918 _LIBCPP_INLINE_VISIBILITY
919 iterator end() _NOEXCEPT
920 {return iterator(__get_pointer() + size());}
921 _LIBCPP_INLINE_VISIBILITY
922 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000923 {return const_iterator(__get_pointer() + size());}
Howard Hinnant8ea98242013-08-23 17:37:05 +0000924#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000925 _LIBCPP_INLINE_VISIBILITY
926 reverse_iterator rbegin() _NOEXCEPT
927 {return reverse_iterator(end());}
928 _LIBCPP_INLINE_VISIBILITY
929 const_reverse_iterator rbegin() const _NOEXCEPT
930 {return const_reverse_iterator(end());}
931 _LIBCPP_INLINE_VISIBILITY
932 reverse_iterator rend() _NOEXCEPT
933 {return reverse_iterator(begin());}
934 _LIBCPP_INLINE_VISIBILITY
935 const_reverse_iterator rend() const _NOEXCEPT
936 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000938 _LIBCPP_INLINE_VISIBILITY
939 const_iterator cbegin() const _NOEXCEPT
940 {return begin();}
941 _LIBCPP_INLINE_VISIBILITY
942 const_iterator cend() const _NOEXCEPT
943 {return end();}
944 _LIBCPP_INLINE_VISIBILITY
945 const_reverse_iterator crbegin() const _NOEXCEPT
946 {return rbegin();}
947 _LIBCPP_INLINE_VISIBILITY
948 const_reverse_iterator crend() const _NOEXCEPT
949 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000951 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000953 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
954 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
955 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000956 {return (__is_long() ? __get_long_cap()
957 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958
959 void resize(size_type __n, value_type __c);
960 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
961
Marshall Clow6ae12a82018-11-28 18:18:34 +0000962 void reserve(size_type __res_arg);
Eric Fiselier451d5582018-11-26 20:15:38 +0000963 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
964
Marshall Clow6ae12a82018-11-28 18:18:34 +0000965 _LIBCPP_INLINE_VISIBILITY
966 void reserve() _NOEXCEPT {reserve(0);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000968 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnantcf823322010-12-17 14:46:43 +0000969 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000970 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000971 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
972 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000974 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
975 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976
977 const_reference at(size_type __n) const;
978 reference at(size_type __n);
979
980 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000981
982 template <class _Tp>
983 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500984 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +0000985 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500986 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
987 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000988 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500989 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000990 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000991 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000993#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000995#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996
Howard Hinnantcf823322010-12-17 14:46:43 +0000997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000999
1000 template <class _Tp>
1001 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001002 _EnableIf<
1003 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1004 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001005 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001006 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001007 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001008 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001009
Marshall Clow62953962016-10-03 23:40:48 +00001010 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001011 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001012 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001013 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001014 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1015 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001016 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001017 >
Marshall Clow82513342016-09-24 22:45:42 +00001018 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001019 basic_string& append(const value_type* __s, size_type __n);
1020 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001022
1023 _LIBCPP_INLINE_VISIBILITY
1024 void __append_default_init(size_type __n);
1025
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001026 template <class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001027 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1028 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001030 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001031 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001033 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001034 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001036 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001037 _LIBCPP_INLINE_VISIBILITY
1038 append(_InputIterator __first, _InputIterator __last) {
1039 const basic_string __temp (__first, __last, __alloc());
1040 append(__temp.data(), __temp.size());
1041 return *this;
1042 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001044 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001045 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001047 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001048 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001050 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001051 _LIBCPP_INLINE_VISIBILITY
1052 append(_ForwardIterator __first, _ForwardIterator __last) {
1053 return __append_forward_unsafe(__first, __last);
1054 }
1055
Eric Fiselierfc92be82017-04-19 00:28:44 +00001056#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001059#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060
1061 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001064 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1065 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1066 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1067 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068
Marshall Clowe46031a2018-07-02 18:41:15 +00001069 template <class _Tp>
1070 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001071 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001072 <
1073 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1074 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001075 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001076 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001077 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001078 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001079#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001080 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001081 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001082 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001083 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001084#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001085 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001086 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001087 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001088 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001089 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001090 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1091 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001092 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001093 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001094 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001095 basic_string& assign(const value_type* __s, size_type __n);
1096 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097 basic_string& assign(size_type __n, value_type __c);
1098 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001099 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001100 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001102 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001103 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001105 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 assign(_InputIterator __first, _InputIterator __last);
1107 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001108 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001109 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001111 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001112 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001116#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001119#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120
Howard Hinnantcf823322010-12-17 14:46:43 +00001121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001123
1124 template <class _Tp>
1125 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001126 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001127 <
1128 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1129 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001130 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001131 insert(size_type __pos1, const _Tp& __t)
1132 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1133
Marshall Clow82513342016-09-24 22:45:42 +00001134 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001135 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001136 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001137 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001138 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001139 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001140 >
Marshall Clow82513342016-09-24 22:45:42 +00001141 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001142 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001143 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1144 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1146 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1149 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001150 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001151 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001153 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001154 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001156 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1158 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001159 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001160 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001162 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001163 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001165 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001167#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1170 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001171#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172
1173 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 iterator erase(const_iterator __first, const_iterator __last);
1178
Howard Hinnantcf823322010-12-17 14:46:43 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001181
1182 template <class _Tp>
1183 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001184 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001185 <
1186 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1187 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001188 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001189 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 +00001190 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 +00001191 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001192 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001193 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001194 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001195 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001196 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001197 >
Marshall Clow82513342016-09-24 22:45:42 +00001198 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001199 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1200 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001203 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001204
1205 template <class _Tp>
1206 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001207 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001208 <
1209 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1210 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001211 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001212 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1213
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001215 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001217 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001219 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001221 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001222 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001224 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001226 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001227 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001228#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001230 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001232#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233
Howard Hinnantd17880b2013-06-28 16:59:19 +00001234 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1237
Howard Hinnantcf823322010-12-17 14:46:43 +00001238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001239 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001240#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001241 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001242#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001243 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001244 __is_nothrow_swappable<allocator_type>::value);
1245#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246
Howard Hinnantcf823322010-12-17 14:46:43 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001248 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001249 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001250 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001251#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001252 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001253 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001254#endif
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 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
Howard Hinnantcf823322010-12-17 14:46:43 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001260 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001261
1262 template <class _Tp>
1263 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001264 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001265 <
1266 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1267 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001268 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001269 find(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001270 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001272 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001273 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274
Howard Hinnantcf823322010-12-17 14:46:43 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001276 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001277
1278 template <class _Tp>
1279 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001280 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001281 <
1282 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1283 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001284 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001285 rfind(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001286 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001288 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001289 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290
Howard Hinnantcf823322010-12-17 14:46:43 +00001291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001292 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001293
1294 template <class _Tp>
1295 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001296 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001297 <
1298 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1299 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001300 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001301 find_first_of(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001302 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001304 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001306 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307
Howard Hinnantcf823322010-12-17 14:46:43 +00001308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001309 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001310
1311 template <class _Tp>
1312 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001313 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001314 <
1315 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1316 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001317 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001318 find_last_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001319 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001321 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001323 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324
Howard Hinnantcf823322010-12-17 14:46:43 +00001325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001326 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001327
1328 template <class _Tp>
1329 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001330 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001331 <
1332 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1333 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001334 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001335 find_first_not_of(const _Tp &__t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001336 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 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001338 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001339 _LIBCPP_INLINE_VISIBILITY
1340 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1341
1342 _LIBCPP_INLINE_VISIBILITY
1343 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001344
1345 template <class _Tp>
1346 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001347 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001348 <
1349 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1350 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001351 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001352 find_last_not_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001353 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 +00001354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001355 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001356 _LIBCPP_INLINE_VISIBILITY
1357 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1358
1359 _LIBCPP_INLINE_VISIBILITY
1360 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001361
1362 template <class _Tp>
1363 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001364 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001365 <
1366 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1367 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001368 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001369 compare(const _Tp &__t) const;
1370
1371 template <class _Tp>
1372 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001373 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001374 <
1375 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1376 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001377 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001378 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1379
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001382 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 +00001383
Marshall Clow82513342016-09-24 22:45:42 +00001384 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001385 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001386 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001387 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001388 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001389 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001390 >
Marshall Clow82513342016-09-24 22:45:42 +00001391 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 +00001392 int compare(const value_type* __s) const _NOEXCEPT;
1393 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1394 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395
Marshall Clow18c293b2017-12-04 20:11:38 +00001396#if _LIBCPP_STD_VER > 17
1397 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1398 bool starts_with(__self_view __sv) const _NOEXCEPT
1399 { return __self_view(data(), size()).starts_with(__sv); }
1400
1401 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1402 bool starts_with(value_type __c) const _NOEXCEPT
1403 { return !empty() && _Traits::eq(front(), __c); }
1404
1405 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1406 bool starts_with(const value_type* __s) const _NOEXCEPT
1407 { return starts_with(__self_view(__s)); }
1408
1409 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1410 bool ends_with(__self_view __sv) const _NOEXCEPT
1411 { return __self_view(data(), size()).ends_with( __sv); }
1412
1413 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1414 bool ends_with(value_type __c) const _NOEXCEPT
1415 { return !empty() && _Traits::eq(back(), __c); }
1416
1417 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1418 bool ends_with(const value_type* __s) const _NOEXCEPT
1419 { return ends_with(__self_view(__s)); }
1420#endif
1421
Howard Hinnantcf823322010-12-17 14:46:43 +00001422 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001423
Marshall Clowe60a7182018-05-29 17:04:37 +00001424 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001425
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001426 _LIBCPP_INLINE_VISIBILITY
1427 bool __is_long() const _NOEXCEPT
1428 {return bool(__r_.first().__s.__size_ & __short_mask);}
1429
Howard Hinnant8ea98242013-08-23 17:37:05 +00001430#if _LIBCPP_DEBUG_LEVEL >= 2
1431
1432 bool __dereferenceable(const const_iterator* __i) const;
1433 bool __decrementable(const const_iterator* __i) const;
1434 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1435 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1436
1437#endif // _LIBCPP_DEBUG_LEVEL >= 2
1438
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001440 _LIBCPP_INLINE_VISIBILITY
1441 allocator_type& __alloc() _NOEXCEPT
1442 {return __r_.second();}
1443 _LIBCPP_INLINE_VISIBILITY
1444 const allocator_type& __alloc() const _NOEXCEPT
1445 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001447#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001448
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001450 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001451# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001453# else
1454 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1455# endif
1456
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001457 _LIBCPP_INLINE_VISIBILITY
1458 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001459# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001461# else
1462 {return __r_.first().__s.__size_;}
1463# endif
1464
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001465#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001466
1467 _LIBCPP_INLINE_VISIBILITY
1468 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001469# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001470 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1471# else
1472 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1473# endif
1474
1475 _LIBCPP_INLINE_VISIBILITY
1476 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001477# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001478 {return __r_.first().__s.__size_;}
1479# else
1480 {return __r_.first().__s.__size_ >> 1;}
1481# endif
1482
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001483#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001484
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001485 _LIBCPP_INLINE_VISIBILITY
1486 void __set_long_size(size_type __s) _NOEXCEPT
1487 {__r_.first().__l.__size_ = __s;}
1488 _LIBCPP_INLINE_VISIBILITY
1489 size_type __get_long_size() const _NOEXCEPT
1490 {return __r_.first().__l.__size_;}
1491 _LIBCPP_INLINE_VISIBILITY
1492 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1494
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001495 _LIBCPP_INLINE_VISIBILITY
1496 void __set_long_cap(size_type __s) _NOEXCEPT
1497 {__r_.first().__l.__cap_ = __long_mask | __s;}
1498 _LIBCPP_INLINE_VISIBILITY
1499 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001500 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001502 _LIBCPP_INLINE_VISIBILITY
1503 void __set_long_pointer(pointer __p) _NOEXCEPT
1504 {__r_.first().__l.__data_ = __p;}
1505 _LIBCPP_INLINE_VISIBILITY
1506 pointer __get_long_pointer() _NOEXCEPT
1507 {return __r_.first().__l.__data_;}
1508 _LIBCPP_INLINE_VISIBILITY
1509 const_pointer __get_long_pointer() const _NOEXCEPT
1510 {return __r_.first().__l.__data_;}
1511 _LIBCPP_INLINE_VISIBILITY
1512 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001513 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001514 _LIBCPP_INLINE_VISIBILITY
1515 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001516 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001517 _LIBCPP_INLINE_VISIBILITY
1518 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001520 _LIBCPP_INLINE_VISIBILITY
1521 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1523
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001524 _LIBCPP_INLINE_VISIBILITY
1525 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526 {
1527 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1528 for (unsigned __i = 0; __i < __n_words; ++__i)
1529 __a[__i] = 0;
1530 }
1531
1532 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001534 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001535 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001537 static _LIBCPP_INLINE_VISIBILITY
1538 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001539 {
1540 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1541 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1542 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1543 if (__guess == __min_cap) ++__guess;
1544 return __guess;
1545 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001547 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001548 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001549 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001550 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001551 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001553
Martijn Vels5e7c9752020-03-04 17:52:46 -05001554 // Slow path for the (inlined) copy constructor for 'long' strings.
1555 // Always externally instantiated and not inlined.
1556 // Requires that __s is zero terminated.
1557 // The main reason for this function to exist is because for unstable, we
1558 // want to allow inlining of the copy constructor. However, we don't want
1559 // to call the __init() functions as those are marked as inline which may
1560 // result in over-aggressive inlining by the compiler, where our aim is
1561 // to only inline the fast path code directly in the ctor.
1562 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1563
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001565 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001566 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001568 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1569 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 __init(_InputIterator __first, _InputIterator __last);
1571
1572 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001573 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001574 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001576 __is_cpp17_forward_iterator<_ForwardIterator>::value
1577 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 __init(_ForwardIterator __first, _ForwardIterator __last);
1579
1580 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001581 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1583 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001584 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
Martijn Vels596e3de2020-02-26 15:55:49 -05001586 // __assign_no_alias is invoked for assignment operations where we
1587 // have proof that the input does not alias the current instance.
1588 // For example, operator=(basic_string) performs a 'self' check.
1589 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001590 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001591
Howard Hinnantcf823322010-12-17 14:46:43 +00001592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 void __erase_to_end(size_type __pos);
1594
Martijn Velsa81fc792020-02-26 13:25:43 -05001595 // __erase_external_with_move is invoked for erase() invocations where
1596 // `n ~= npos`, likely requiring memory moves on the string data.
1597 void __erase_external_with_move(size_type __pos, size_type __n);
1598
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001599 _LIBCPP_INLINE_VISIBILITY
1600 void __copy_assign_alloc(const basic_string& __str)
1601 {__copy_assign_alloc(__str, integral_constant<bool,
1602 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1603
1604 _LIBCPP_INLINE_VISIBILITY
1605 void __copy_assign_alloc(const basic_string& __str, true_type)
1606 {
Marshall Clowf258c202017-01-31 03:40:52 +00001607 if (__alloc() == __str.__alloc())
1608 __alloc() = __str.__alloc();
1609 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001610 {
Marshall Clowf258c202017-01-31 03:40:52 +00001611 if (!__str.__is_long())
1612 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001613 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001614 __alloc() = __str.__alloc();
1615 }
1616 else
1617 {
1618 allocator_type __a = __str.__alloc();
1619 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001620 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001621 __alloc() = _VSTD::move(__a);
1622 __set_long_pointer(__p);
1623 __set_long_cap(__str.__get_long_cap());
1624 __set_long_size(__str.size());
1625 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001626 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001627 }
1628
1629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001630 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001631 {}
1632
Eric Fiselierfc92be82017-04-19 00:28:44 +00001633#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001634 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001635 void __move_assign(basic_string& __str, false_type)
1636 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001638 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001639#if _LIBCPP_STD_VER > 14
1640 _NOEXCEPT;
1641#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001642 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001643#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001644#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001645
1646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001647 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001648 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001649 _NOEXCEPT_(
1650 !__alloc_traits::propagate_on_container_move_assignment::value ||
1651 is_nothrow_move_assignable<allocator_type>::value)
1652 {__move_assign_alloc(__str, integral_constant<bool,
1653 __alloc_traits::propagate_on_container_move_assignment::value>());}
1654
1655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001656 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001657 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1658 {
1659 __alloc() = _VSTD::move(__c.__alloc());
1660 }
1661
1662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001663 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001664 _NOEXCEPT
1665 {}
1666
Howard Hinnantcf823322010-12-17 14:46:43 +00001667 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1668 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669
1670 friend basic_string operator+<>(const basic_string&, const basic_string&);
1671 friend basic_string operator+<>(const value_type*, const basic_string&);
1672 friend basic_string operator+<>(value_type, const basic_string&);
1673 friend basic_string operator+<>(const basic_string&, const value_type*);
1674 friend basic_string operator+<>(const basic_string&, value_type);
1675};
1676
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001677// These declarations must appear before any functions are implicitly used
1678// so that they have the correct visibility specifier.
1679#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1680_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1681_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1682#else
1683_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1684_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1685#endif
1686
1687
Marshall Clowa0563332018-02-08 06:34:03 +00001688#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1689template<class _InputIterator,
1690 class _CharT = typename iterator_traits<_InputIterator>::value_type,
1691 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001692 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1693 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001694 >
1695basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1696 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001697
1698template<class _CharT,
1699 class _Traits,
1700 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001701 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001702 >
1703explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1704 -> basic_string<_CharT, _Traits, _Allocator>;
1705
1706template<class _CharT,
1707 class _Traits,
1708 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001709 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001710 class _Sz = typename allocator_traits<_Allocator>::size_type
1711 >
1712basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1713 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001714#endif
1715
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001717inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718void
1719basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1720{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001721#if _LIBCPP_DEBUG_LEVEL >= 2
1722 __get_db()->__invalidate_all(this);
1723#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724}
1725
1726template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001727inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728void
Howard Hinnant28b24882011-12-01 20:21:04 +00001729basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant8ea98242013-08-23 17:37:05 +00001730#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant28b24882011-12-01 20:21:04 +00001731 __pos
1732#endif
1733 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001735#if _LIBCPP_DEBUG_LEVEL >= 2
1736 __c_node* __c = __get_db()->__find_c_and_lock(this);
1737 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001739 const_pointer __new_last = __get_pointer() + __pos;
1740 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001742 --__p;
1743 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1744 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001746 (*__p)->__c_ = nullptr;
1747 if (--__c->end_ != __p)
1748 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001751 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001753#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754}
1755
1756template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001757inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001758basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001759 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001760 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001762#if _LIBCPP_DEBUG_LEVEL >= 2
1763 __get_db()->__insert_c(this);
1764#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765 __zero();
1766}
1767
1768template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001769inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001771#if _LIBCPP_STD_VER <= 14
1772 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1773#else
1774 _NOEXCEPT
1775#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001776: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001778#if _LIBCPP_DEBUG_LEVEL >= 2
1779 __get_db()->__insert_c(this);
1780#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 __zero();
1782}
1783
1784template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001785void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1786 size_type __sz,
1787 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788{
1789 if (__reserve > max_size())
1790 this->__throw_length_error();
1791 pointer __p;
1792 if (__reserve < __min_cap)
1793 {
1794 __set_short_size(__sz);
1795 __p = __get_short_pointer();
1796 }
1797 else
1798 {
1799 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001800 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 __set_long_pointer(__p);
1802 __set_long_cap(__cap+1);
1803 __set_long_size(__sz);
1804 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001805 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 traits_type::assign(__p[__sz], value_type());
1807}
1808
1809template <class _CharT, class _Traits, class _Allocator>
1810void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001811basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812{
1813 if (__sz > max_size())
1814 this->__throw_length_error();
1815 pointer __p;
1816 if (__sz < __min_cap)
1817 {
1818 __set_short_size(__sz);
1819 __p = __get_short_pointer();
1820 }
1821 else
1822 {
1823 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001824 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 __set_long_pointer(__p);
1826 __set_long_cap(__cap+1);
1827 __set_long_size(__sz);
1828 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001829 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830 traits_type::assign(__p[__sz], value_type());
1831}
1832
1833template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001834template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001835basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001836 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001838 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 __init(__s, traits_type::length(__s));
Howard Hinnant8ea98242013-08-23 17:37:05 +00001840#if _LIBCPP_DEBUG_LEVEL >= 2
1841 __get_db()->__insert_c(this);
1842#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843}
1844
1845template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001846inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001847basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001848 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001850 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 __init(__s, __n);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001852#if _LIBCPP_DEBUG_LEVEL >= 2
1853 __get_db()->__insert_c(this);
1854#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855}
1856
1857template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001858inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001859basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001860 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001862 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 __init(__s, __n);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001864#if _LIBCPP_DEBUG_LEVEL >= 2
1865 __get_db()->__insert_c(this);
1866#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867}
1868
1869template <class _CharT, class _Traits, class _Allocator>
1870basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001871 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872{
1873 if (!__str.__is_long())
1874 __r_.first().__r = __str.__r_.first().__r;
1875 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001876 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1877 __str.__get_long_size());
1878
Howard Hinnant8ea98242013-08-23 17:37:05 +00001879#if _LIBCPP_DEBUG_LEVEL >= 2
1880 __get_db()->__insert_c(this);
1881#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882}
1883
1884template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001885basic_string<_CharT, _Traits, _Allocator>::basic_string(
1886 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001887 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888{
1889 if (!__str.__is_long())
1890 __r_.first().__r = __str.__r_.first().__r;
1891 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001892 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1893 __str.__get_long_size());
Howard Hinnant8ea98242013-08-23 17:37:05 +00001894#if _LIBCPP_DEBUG_LEVEL >= 2
1895 __get_db()->__insert_c(this);
1896#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897}
1898
Martijn Vels5e7c9752020-03-04 17:52:46 -05001899template <class _CharT, class _Traits, class _Allocator>
1900void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1901 const value_type* __s, size_type __sz) {
1902 pointer __p;
1903 if (__sz < __min_cap) {
1904 __p = __get_short_pointer();
1905 __set_short_size(__sz);
1906 } else {
1907 if (__sz > max_size())
1908 this->__throw_length_error();
1909 size_t __cap = __recommend(__sz);
1910 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1911 __set_long_pointer(__p);
1912 __set_long_cap(__cap + 1);
1913 __set_long_size(__sz);
1914 }
1915 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1916}
1917
Eric Fiselierfc92be82017-04-19 00:28:44 +00001918#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919
1920template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001921inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001922basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001923#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001924 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001925#else
1926 _NOEXCEPT
1927#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001928 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929{
1930 __str.__zero();
Howard Hinnant8ea98242013-08-23 17:37:05 +00001931#if _LIBCPP_DEBUG_LEVEL >= 2
1932 __get_db()->__insert_c(this);
1933 if (__is_long())
1934 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935#endif
1936}
1937
1938template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001939inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001941 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942{
Marshall Clowd2d24692014-07-17 15:32:20 +00001943 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001944 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001945 else
1946 {
1947 __r_.first().__r = __str.__r_.first().__r;
1948 __str.__zero();
1949 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001950#if _LIBCPP_DEBUG_LEVEL >= 2
1951 __get_db()->__insert_c(this);
1952 if (__is_long())
1953 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954#endif
1955}
1956
Eric Fiselierfc92be82017-04-19 00:28:44 +00001957#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958
1959template <class _CharT, class _Traits, class _Allocator>
1960void
1961basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1962{
1963 if (__n > max_size())
1964 this->__throw_length_error();
1965 pointer __p;
1966 if (__n < __min_cap)
1967 {
1968 __set_short_size(__n);
1969 __p = __get_short_pointer();
1970 }
1971 else
1972 {
1973 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001974 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975 __set_long_pointer(__p);
1976 __set_long_cap(__cap+1);
1977 __set_long_size(__n);
1978 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001979 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980 traits_type::assign(__p[__n], value_type());
1981}
1982
1983template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001984inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001985basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001986 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987{
1988 __init(__n, __c);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001989#if _LIBCPP_DEBUG_LEVEL >= 2
1990 __get_db()->__insert_c(this);
1991#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992}
1993
1994template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001995template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001996basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001997 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998{
1999 __init(__n, __c);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002000#if _LIBCPP_DEBUG_LEVEL >= 2
2001 __get_db()->__insert_c(this);
2002#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003}
2004
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002006basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2007 size_type __pos, size_type __n,
2008 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002009 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010{
2011 size_type __str_sz = __str.size();
2012 if (__pos > __str_sz)
2013 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002014 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant8ea98242013-08-23 17:37:05 +00002015#if _LIBCPP_DEBUG_LEVEL >= 2
2016 __get_db()->__insert_c(this);
2017#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018}
2019
2020template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002021inline
Marshall Clow83445802016-04-07 18:13:41 +00002022basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002023 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002024 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002025{
2026 size_type __str_sz = __str.size();
2027 if (__pos > __str_sz)
2028 this->__throw_out_of_range();
2029 __init(__str.data() + __pos, __str_sz - __pos);
2030#if _LIBCPP_DEBUG_LEVEL >= 2
2031 __get_db()->__insert_c(this);
2032#endif
2033}
2034
2035template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002036template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002037basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002038 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002039 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002040{
Marshall Clowe46031a2018-07-02 18:41:15 +00002041 __self_view __sv0 = __t;
2042 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002043 __init(__sv.data(), __sv.size());
2044#if _LIBCPP_DEBUG_LEVEL >= 2
2045 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002046#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002047}
2048
2049template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002050template <class _Tp, class>
2051basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002052 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002053{
Marshall Clowe46031a2018-07-02 18:41:15 +00002054 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002055 __init(__sv.data(), __sv.size());
2056#if _LIBCPP_DEBUG_LEVEL >= 2
2057 __get_db()->__insert_c(this);
2058#endif
2059}
2060
2061template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002062template <class _Tp, class>
2063basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002064 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002065{
Marshall Clowe46031a2018-07-02 18:41:15 +00002066 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002067 __init(__sv.data(), __sv.size());
2068#if _LIBCPP_DEBUG_LEVEL >= 2
2069 __get_db()->__insert_c(this);
2070#endif
2071}
2072
2073template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002075_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002077 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2078>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2080{
2081 __zero();
2082#ifndef _LIBCPP_NO_EXCEPTIONS
2083 try
2084 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002085#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086 for (; __first != __last; ++__first)
2087 push_back(*__first);
2088#ifndef _LIBCPP_NO_EXCEPTIONS
2089 }
2090 catch (...)
2091 {
2092 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002093 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094 throw;
2095 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002096#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097}
2098
2099template <class _CharT, class _Traits, class _Allocator>
2100template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002101_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002103 __is_cpp17_forward_iterator<_ForwardIterator>::value
2104>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2106{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002107 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108 if (__sz > max_size())
2109 this->__throw_length_error();
2110 pointer __p;
2111 if (__sz < __min_cap)
2112 {
2113 __set_short_size(__sz);
2114 __p = __get_short_pointer();
2115 }
2116 else
2117 {
2118 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002119 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 __set_long_pointer(__p);
2121 __set_long_cap(__cap+1);
2122 __set_long_size(__sz);
2123 }
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002124 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125 traits_type::assign(*__p, *__first);
2126 traits_type::assign(*__p, value_type());
2127}
2128
2129template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002130template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002131inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002133 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134{
2135 __init(__first, __last);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002136#if _LIBCPP_DEBUG_LEVEL >= 2
2137 __get_db()->__insert_c(this);
2138#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139}
2140
2141template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002142template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002143inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2145 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002146 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147{
2148 __init(__first, __last);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002149#if _LIBCPP_DEBUG_LEVEL >= 2
2150 __get_db()->__insert_c(this);
2151#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152}
2153
Eric Fiselierfc92be82017-04-19 00:28:44 +00002154#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002155
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002157inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002158basic_string<_CharT, _Traits, _Allocator>::basic_string(
2159 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002160 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161{
2162 __init(__il.begin(), __il.end());
Howard Hinnant8ea98242013-08-23 17:37:05 +00002163#if _LIBCPP_DEBUG_LEVEL >= 2
2164 __get_db()->__insert_c(this);
2165#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166}
2167
2168template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002169inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002170
Eric Fiselier812882b2017-02-17 01:17:10 +00002171basic_string<_CharT, _Traits, _Allocator>::basic_string(
2172 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002173 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174{
2175 __init(__il.begin(), __il.end());
Howard Hinnant8ea98242013-08-23 17:37:05 +00002176#if _LIBCPP_DEBUG_LEVEL >= 2
2177 __get_db()->__insert_c(this);
2178#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179}
2180
Eric Fiselierfc92be82017-04-19 00:28:44 +00002181#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002182
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2185{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002186#if _LIBCPP_DEBUG_LEVEL >= 2
2187 __get_db()->__erase_c(this);
2188#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002190 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191}
2192
2193template <class _CharT, class _Traits, class _Allocator>
2194void
2195basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2196 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002197 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 +00002198{
2199 size_type __ms = max_size();
2200 if (__delta_cap > __ms - __old_cap - 1)
2201 this->__throw_length_error();
2202 pointer __old_p = __get_pointer();
2203 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002204 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002206 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207 __invalidate_all_iterators();
2208 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002209 traits_type::copy(_VSTD::__to_address(__p),
2210 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002212 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2214 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002215 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2216 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002218 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219 __set_long_pointer(__p);
2220 __set_long_cap(__cap+1);
2221 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2222 __set_long_size(__old_sz);
2223 traits_type::assign(__p[__old_sz], value_type());
2224}
2225
2226template <class _CharT, class _Traits, class _Allocator>
2227void
2228basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2229 size_type __n_copy, size_type __n_del, size_type __n_add)
2230{
2231 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002232 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233 this->__throw_length_error();
2234 pointer __old_p = __get_pointer();
2235 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002236 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002238 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239 __invalidate_all_iterators();
2240 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002241 traits_type::copy(_VSTD::__to_address(__p),
2242 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2244 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002245 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2246 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002247 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002249 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250 __set_long_pointer(__p);
2251 __set_long_cap(__cap+1);
2252}
2253
2254// assign
2255
2256template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002257template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002258basic_string<_CharT, _Traits, _Allocator>&
2259basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002260 const value_type* __s, size_type __n) {
2261 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2262 if (__n < __cap) {
2263 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2264 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2265 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2266 traits_type::assign(__p[__n], value_type());
2267 __invalidate_iterators_past(__n);
2268 } else {
2269 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2270 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2271 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002272 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002273}
2274
2275template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002277basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002279 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280 size_type __cap = capacity();
2281 if (__cap >= __n)
2282 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002283 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 traits_type::move(__p, __s, __n);
2285 traits_type::assign(__p[__n], value_type());
2286 __set_size(__n);
2287 __invalidate_iterators_past(__n);
2288 }
2289 else
2290 {
2291 size_type __sz = size();
2292 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2293 }
2294 return *this;
2295}
2296
2297template <class _CharT, class _Traits, class _Allocator>
2298basic_string<_CharT, _Traits, _Allocator>&
2299basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2300{
2301 size_type __cap = capacity();
2302 if (__cap < __n)
2303 {
2304 size_type __sz = size();
2305 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2306 }
2307 else
2308 __invalidate_iterators_past(__n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002309 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310 traits_type::assign(__p, __n, __c);
2311 traits_type::assign(__p[__n], value_type());
2312 __set_size(__n);
2313 return *this;
2314}
2315
2316template <class _CharT, class _Traits, class _Allocator>
2317basic_string<_CharT, _Traits, _Allocator>&
2318basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2319{
2320 pointer __p;
2321 if (__is_long())
2322 {
2323 __p = __get_long_pointer();
2324 __set_long_size(1);
2325 }
2326 else
2327 {
2328 __p = __get_short_pointer();
2329 __set_short_size(1);
2330 }
2331 traits_type::assign(*__p, __c);
2332 traits_type::assign(*++__p, value_type());
2333 __invalidate_iterators_past(1);
2334 return *this;
2335}
2336
2337template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002338basic_string<_CharT, _Traits, _Allocator>&
2339basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2340{
Martijn Vels596e3de2020-02-26 15:55:49 -05002341 if (this != &__str) {
2342 __copy_assign_alloc(__str);
2343 if (!__is_long()) {
2344 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002345 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002346 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002347 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002348 }
2349 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002350 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002351 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002352 }
2353 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002354}
2355
Eric Fiselierfc92be82017-04-19 00:28:44 +00002356#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002357
2358template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002359inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002360void
2361basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002362 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002363{
2364 if (__alloc() != __str.__alloc())
2365 assign(__str);
2366 else
2367 __move_assign(__str, true_type());
2368}
2369
2370template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002371inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002372void
2373basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002374#if _LIBCPP_STD_VER > 14
2375 _NOEXCEPT
2376#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002377 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002378#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002379{
marshall2ed41622019-11-27 07:13:00 -08002380 if (__is_long()) {
2381 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2382 __get_long_cap());
2383#if _LIBCPP_STD_VER <= 14
2384 if (!is_nothrow_move_assignable<allocator_type>::value) {
2385 __set_short_size(0);
2386 traits_type::assign(__get_short_pointer()[0], value_type());
2387 }
2388#endif
2389 }
2390 __move_assign_alloc(__str);
2391 __r_.first() = __str.__r_.first();
2392 __str.__set_short_size(0);
2393 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002394}
2395
2396template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002397inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002398basic_string<_CharT, _Traits, _Allocator>&
2399basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002400 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002401{
2402 __move_assign(__str, integral_constant<bool,
2403 __alloc_traits::propagate_on_container_move_assignment::value>());
2404 return *this;
2405}
2406
2407#endif
2408
2409template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002411_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002413 __is_exactly_cpp17_input_iterator <_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002414 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002416>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2418{
Marshall Clow958362f2016-09-05 01:54:30 +00002419 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002420 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002421 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422}
2423
2424template <class _CharT, class _Traits, class _Allocator>
2425template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002426_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002428 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002429 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002431>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2433{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002434 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435 size_type __cap = capacity();
2436 if (__cap < __n)
2437 {
2438 size_type __sz = size();
2439 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2440 }
2441 else
2442 __invalidate_iterators_past(__n);
2443 pointer __p = __get_pointer();
2444 for (; __first != __last; ++__first, ++__p)
2445 traits_type::assign(*__p, *__first);
2446 traits_type::assign(*__p, value_type());
2447 __set_size(__n);
2448 return *this;
2449}
2450
2451template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452basic_string<_CharT, _Traits, _Allocator>&
2453basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2454{
2455 size_type __sz = __str.size();
2456 if (__pos > __sz)
2457 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002458 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459}
2460
2461template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002462template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002463_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002464<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002465 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2466 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002467 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002468>
Marshall Clow82513342016-09-24 22:45:42 +00002469basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002470{
Marshall Clow82513342016-09-24 22:45:42 +00002471 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002472 size_type __sz = __sv.size();
2473 if (__pos > __sz)
2474 this->__throw_out_of_range();
2475 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2476}
2477
2478
2479template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002481basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002483 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484 return assign(__s, traits_type::length(__s));
2485}
2486
2487// append
2488
2489template <class _CharT, class _Traits, class _Allocator>
2490basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002491basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002493 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494 size_type __cap = capacity();
2495 size_type __sz = size();
2496 if (__cap - __sz >= __n)
2497 {
2498 if (__n)
2499 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002500 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002501 traits_type::copy(__p + __sz, __s, __n);
2502 __sz += __n;
2503 __set_size(__sz);
2504 traits_type::assign(__p[__sz], value_type());
2505 }
2506 }
2507 else
2508 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2509 return *this;
2510}
2511
2512template <class _CharT, class _Traits, class _Allocator>
2513basic_string<_CharT, _Traits, _Allocator>&
2514basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2515{
2516 if (__n)
2517 {
2518 size_type __cap = capacity();
2519 size_type __sz = size();
2520 if (__cap - __sz < __n)
2521 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2522 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002523 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524 __sz += __n;
2525 __set_size(__sz);
2526 traits_type::assign(__p[__sz], value_type());
2527 }
2528 return *this;
2529}
2530
2531template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002532inline void
2533basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2534{
2535 if (__n)
2536 {
2537 size_type __cap = capacity();
2538 size_type __sz = size();
2539 if (__cap - __sz < __n)
2540 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2541 pointer __p = __get_pointer();
2542 __sz += __n;
2543 __set_size(__sz);
2544 traits_type::assign(__p[__sz], value_type());
2545 }
2546}
2547
2548template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549void
2550basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2551{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002552 bool __is_short = !__is_long();
2553 size_type __cap;
2554 size_type __sz;
2555 if (__is_short)
2556 {
2557 __cap = __min_cap - 1;
2558 __sz = __get_short_size();
2559 }
2560 else
2561 {
2562 __cap = __get_long_cap() - 1;
2563 __sz = __get_long_size();
2564 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002566 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002568 __is_short = !__is_long();
2569 }
2570 pointer __p;
2571 if (__is_short)
2572 {
2573 __p = __get_short_pointer() + __sz;
2574 __set_short_size(__sz+1);
2575 }
2576 else
2577 {
2578 __p = __get_long_pointer() + __sz;
2579 __set_long_size(__sz+1);
2580 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581 traits_type::assign(*__p, __c);
2582 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583}
2584
Marshall Clow6ebbf662016-09-07 03:32:06 +00002585template <class _Tp>
Marshall Clow958362f2016-09-05 01:54:30 +00002586bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2587{
2588 return __first <= __p && __p < __last;
2589}
2590
Marshall Clow6ebbf662016-09-07 03:32:06 +00002591template <class _Tp1, class _Tp2>
Eric Fiselier6003c772016-12-23 23:37:52 +00002592bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clow6ebbf662016-09-07 03:32:06 +00002593{
2594 return false;
2595}
2596
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597template <class _CharT, class _Traits, class _Allocator>
2598template<class _ForwardIterator>
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002599basic_string<_CharT, _Traits, _Allocator>&
2600basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2601 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602{
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002603 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002604 "function requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605 size_type __sz = size();
2606 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002607 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608 if (__n)
2609 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002610 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2611 _CharRef __tmp_ref = *__first;
2612 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002613 {
2614 const basic_string __temp (__first, __last, __alloc());
2615 append(__temp.data(), __temp.size());
2616 }
Louis Dionne173f29e2019-05-29 16:01:36 +00002617 else
Marshall Clow958362f2016-09-05 01:54:30 +00002618 {
2619 if (__cap - __sz < __n)
2620 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2621 pointer __p = __get_pointer() + __sz;
2622 for (; __first != __last; ++__p, ++__first)
2623 traits_type::assign(*__p, *__first);
2624 traits_type::assign(*__p, value_type());
2625 __set_size(__sz + __n);
2626 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627 }
2628 return *this;
2629}
2630
2631template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002632inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633basic_string<_CharT, _Traits, _Allocator>&
2634basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2635{
2636 return append(__str.data(), __str.size());
2637}
2638
2639template <class _CharT, class _Traits, class _Allocator>
2640basic_string<_CharT, _Traits, _Allocator>&
2641basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2642{
2643 size_type __sz = __str.size();
2644 if (__pos > __sz)
2645 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002646 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002647}
2648
2649template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002650template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002651 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002652 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002653 __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 +00002654 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002655 >
Marshall Clow82513342016-09-24 22:45:42 +00002656basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002657{
Marshall Clow82513342016-09-24 22:45:42 +00002658 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002659 size_type __sz = __sv.size();
2660 if (__pos > __sz)
2661 this->__throw_out_of_range();
2662 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2663}
2664
2665template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002667basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002669 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670 return append(__s, traits_type::length(__s));
2671}
2672
2673// insert
2674
2675template <class _CharT, class _Traits, class _Allocator>
2676basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002677basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002679 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680 size_type __sz = size();
2681 if (__pos > __sz)
2682 this->__throw_out_of_range();
2683 size_type __cap = capacity();
2684 if (__cap - __sz >= __n)
2685 {
2686 if (__n)
2687 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002688 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689 size_type __n_move = __sz - __pos;
2690 if (__n_move != 0)
2691 {
2692 if (__p + __pos <= __s && __s < __p + __sz)
2693 __s += __n;
2694 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2695 }
2696 traits_type::move(__p + __pos, __s, __n);
2697 __sz += __n;
2698 __set_size(__sz);
2699 traits_type::assign(__p[__sz], value_type());
2700 }
2701 }
2702 else
2703 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2704 return *this;
2705}
2706
2707template <class _CharT, class _Traits, class _Allocator>
2708basic_string<_CharT, _Traits, _Allocator>&
2709basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2710{
2711 size_type __sz = size();
2712 if (__pos > __sz)
2713 this->__throw_out_of_range();
2714 if (__n)
2715 {
2716 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002717 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718 if (__cap - __sz >= __n)
2719 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002720 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721 size_type __n_move = __sz - __pos;
2722 if (__n_move != 0)
2723 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2724 }
2725 else
2726 {
2727 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002728 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729 }
2730 traits_type::assign(__p + __pos, __n, __c);
2731 __sz += __n;
2732 __set_size(__sz);
2733 traits_type::assign(__p[__sz], value_type());
2734 }
2735 return *this;
2736}
2737
2738template <class _CharT, class _Traits, class _Allocator>
2739template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002740_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002742 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002743 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2744 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002745>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2747{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002748#if _LIBCPP_DEBUG_LEVEL >= 2
2749 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2750 "string::insert(iterator, range) called with an iterator not"
2751 " referring to this string");
2752#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002753 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002754 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755}
2756
2757template <class _CharT, class _Traits, class _Allocator>
2758template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002759_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002761 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002762 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002764>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2766{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002767#if _LIBCPP_DEBUG_LEVEL >= 2
2768 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2769 "string::insert(iterator, range) called with an iterator not"
2770 " referring to this string");
2771#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002773 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774 if (__n)
2775 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002776 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2777 _CharRef __tmp_char = *__first;
2778 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002779 {
2780 const basic_string __temp(__first, __last, __alloc());
2781 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2782 }
2783
2784 size_type __sz = size();
2785 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002786 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787 if (__cap - __sz >= __n)
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 + __n, __p + __ip, __n_move);
2793 }
2794 else
2795 {
2796 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002797 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 }
2799 __sz += __n;
2800 __set_size(__sz);
2801 traits_type::assign(__p[__sz], value_type());
2802 for (__p += __ip; __first != __last; ++__p, ++__first)
2803 traits_type::assign(*__p, *__first);
2804 }
2805 return begin() + __ip;
2806}
2807
2808template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002809inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810basic_string<_CharT, _Traits, _Allocator>&
2811basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2812{
2813 return insert(__pos1, __str.data(), __str.size());
2814}
2815
2816template <class _CharT, class _Traits, class _Allocator>
2817basic_string<_CharT, _Traits, _Allocator>&
2818basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2819 size_type __pos2, size_type __n)
2820{
2821 size_type __str_sz = __str.size();
2822 if (__pos2 > __str_sz)
2823 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002824 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825}
2826
2827template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002828template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002829_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002830<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002831 __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 +00002832 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002833>
Marshall Clow82513342016-09-24 22:45:42 +00002834basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002835 size_type __pos2, size_type __n)
2836{
Marshall Clow82513342016-09-24 22:45:42 +00002837 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002838 size_type __str_sz = __sv.size();
2839 if (__pos2 > __str_sz)
2840 this->__throw_out_of_range();
2841 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2842}
2843
2844template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002846basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002848 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 return insert(__pos, __s, traits_type::length(__s));
2850}
2851
2852template <class _CharT, class _Traits, class _Allocator>
2853typename basic_string<_CharT, _Traits, _Allocator>::iterator
2854basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2855{
2856 size_type __ip = static_cast<size_type>(__pos - begin());
2857 size_type __sz = size();
2858 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002859 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 if (__cap == __sz)
2861 {
2862 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002863 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864 }
2865 else
2866 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002867 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868 size_type __n_move = __sz - __ip;
2869 if (__n_move != 0)
2870 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2871 }
2872 traits_type::assign(__p[__ip], __c);
2873 traits_type::assign(__p[++__sz], value_type());
2874 __set_size(__sz);
2875 return begin() + static_cast<difference_type>(__ip);
2876}
2877
2878template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002879inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880typename basic_string<_CharT, _Traits, _Allocator>::iterator
2881basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2882{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002883#if _LIBCPP_DEBUG_LEVEL >= 2
2884 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2885 "string::insert(iterator, n, value) called with an iterator not"
2886 " referring to this string");
2887#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 difference_type __p = __pos - begin();
2889 insert(static_cast<size_type>(__p), __n, __c);
2890 return begin() + __p;
2891}
2892
2893// replace
2894
2895template <class _CharT, class _Traits, class _Allocator>
2896basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002897basic_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 +00002898 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002900 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901 size_type __sz = size();
2902 if (__pos > __sz)
2903 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002904 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905 size_type __cap = capacity();
2906 if (__cap - __sz + __n1 >= __n2)
2907 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002908 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 if (__n1 != __n2)
2910 {
2911 size_type __n_move = __sz - __pos - __n1;
2912 if (__n_move != 0)
2913 {
2914 if (__n1 > __n2)
2915 {
2916 traits_type::move(__p + __pos, __s, __n2);
2917 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2918 goto __finish;
2919 }
2920 if (__p + __pos < __s && __s < __p + __sz)
2921 {
2922 if (__p + __pos + __n1 <= __s)
2923 __s += __n2 - __n1;
2924 else // __p + __pos < __s < __p + __pos + __n1
2925 {
2926 traits_type::move(__p + __pos, __s, __n1);
2927 __pos += __n1;
2928 __s += __n2;
2929 __n2 -= __n1;
2930 __n1 = 0;
2931 }
2932 }
2933 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2934 }
2935 }
2936 traits_type::move(__p + __pos, __s, __n2);
2937__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05002938// __sz += __n2 - __n1; in this and the below function below can cause unsigned
2939// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940 __sz += __n2 - __n1;
2941 __set_size(__sz);
2942 __invalidate_iterators_past(__sz);
2943 traits_type::assign(__p[__sz], value_type());
2944 }
2945 else
2946 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2947 return *this;
2948}
2949
2950template <class _CharT, class _Traits, class _Allocator>
2951basic_string<_CharT, _Traits, _Allocator>&
2952basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00002953 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954{
2955 size_type __sz = size();
2956 if (__pos > __sz)
2957 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002958 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002960 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961 if (__cap - __sz + __n1 >= __n2)
2962 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002963 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 if (__n1 != __n2)
2965 {
2966 size_type __n_move = __sz - __pos - __n1;
2967 if (__n_move != 0)
2968 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2969 }
2970 }
2971 else
2972 {
2973 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002974 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975 }
2976 traits_type::assign(__p + __pos, __n2, __c);
2977 __sz += __n2 - __n1;
2978 __set_size(__sz);
2979 __invalidate_iterators_past(__sz);
2980 traits_type::assign(__p[__sz], value_type());
2981 return *this;
2982}
2983
2984template <class _CharT, class _Traits, class _Allocator>
2985template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002986_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002988 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002990>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002991basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992 _InputIterator __j1, _InputIterator __j2)
2993{
Marshall Clow958362f2016-09-05 01:54:30 +00002994 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002995 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002996}
2997
2998template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002999inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000basic_string<_CharT, _Traits, _Allocator>&
3001basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3002{
3003 return replace(__pos1, __n1, __str.data(), __str.size());
3004}
3005
3006template <class _CharT, class _Traits, class _Allocator>
3007basic_string<_CharT, _Traits, _Allocator>&
3008basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3009 size_type __pos2, size_type __n2)
3010{
3011 size_type __str_sz = __str.size();
3012 if (__pos2 > __str_sz)
3013 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003014 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015}
3016
3017template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003018template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003019_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003020<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003021 __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 +00003022 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003023>
Marshall Clow82513342016-09-24 22:45:42 +00003024basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003025 size_type __pos2, size_type __n2)
3026{
Marshall Clow82513342016-09-24 22:45:42 +00003027 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003028 size_type __str_sz = __sv.size();
3029 if (__pos2 > __str_sz)
3030 this->__throw_out_of_range();
3031 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3032}
3033
3034template <class _CharT, class _Traits, class _Allocator>
3035basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003036basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003038 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039 return replace(__pos, __n1, __s, traits_type::length(__s));
3040}
3041
3042template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003043inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003044basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003045basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046{
3047 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3048 __str.data(), __str.size());
3049}
3050
3051template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003052inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003054basic_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 +00003055{
3056 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3057}
3058
3059template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003060inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003062basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063{
3064 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3065}
3066
3067template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003068inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003070basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003071{
3072 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3073}
3074
3075// erase
3076
Martijn Velsa81fc792020-02-26 13:25:43 -05003077// 'externally instantiated' erase() implementation, called when __n != npos.
3078// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003080void
3081basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3082 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084 if (__n)
3085 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003086 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003087 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003088 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089 size_type __n_move = __sz - __pos - __n;
3090 if (__n_move != 0)
3091 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3092 __sz -= __n;
3093 __set_size(__sz);
3094 __invalidate_iterators_past(__sz);
3095 traits_type::assign(__p[__sz], value_type());
3096 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003097}
3098
3099template <class _CharT, class _Traits, class _Allocator>
3100basic_string<_CharT, _Traits, _Allocator>&
3101basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3102 size_type __n) {
3103 if (__pos > size()) this->__throw_out_of_range();
3104 if (__n == npos) {
3105 __erase_to_end(__pos);
3106 } else {
3107 __erase_external_with_move(__pos, __n);
3108 }
3109 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110}
3111
3112template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003113inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114typename basic_string<_CharT, _Traits, _Allocator>::iterator
3115basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3116{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003117#if _LIBCPP_DEBUG_LEVEL >= 2
3118 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3119 "string::erase(iterator) called with an iterator not"
3120 " referring to this string");
3121#endif
3122 _LIBCPP_ASSERT(__pos != end(),
3123 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124 iterator __b = begin();
3125 size_type __r = static_cast<size_type>(__pos - __b);
3126 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003127 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128}
3129
3130template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003131inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132typename basic_string<_CharT, _Traits, _Allocator>::iterator
3133basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3134{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003135#if _LIBCPP_DEBUG_LEVEL >= 2
3136 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3137 "string::erase(iterator, iterator) called with an iterator not"
3138 " referring to this string");
3139#endif
3140 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141 iterator __b = begin();
3142 size_type __r = static_cast<size_type>(__first - __b);
3143 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003144 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145}
3146
3147template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003148inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149void
3150basic_string<_CharT, _Traits, _Allocator>::pop_back()
3151{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003152 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153 size_type __sz;
3154 if (__is_long())
3155 {
3156 __sz = __get_long_size() - 1;
3157 __set_long_size(__sz);
3158 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3159 }
3160 else
3161 {
3162 __sz = __get_short_size() - 1;
3163 __set_short_size(__sz);
3164 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3165 }
3166 __invalidate_iterators_past(__sz);
3167}
3168
3169template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003170inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003172basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173{
3174 __invalidate_all_iterators();
3175 if (__is_long())
3176 {
3177 traits_type::assign(*__get_long_pointer(), value_type());
3178 __set_long_size(0);
3179 }
3180 else
3181 {
3182 traits_type::assign(*__get_short_pointer(), value_type());
3183 __set_short_size(0);
3184 }
3185}
3186
3187template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003188inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189void
3190basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3191{
3192 if (__is_long())
3193 {
3194 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3195 __set_long_size(__pos);
3196 }
3197 else
3198 {
3199 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3200 __set_short_size(__pos);
3201 }
3202 __invalidate_iterators_past(__pos);
3203}
3204
3205template <class _CharT, class _Traits, class _Allocator>
3206void
3207basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3208{
3209 size_type __sz = size();
3210 if (__n > __sz)
3211 append(__n - __sz, __c);
3212 else
3213 __erase_to_end(__n);
3214}
3215
3216template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003217inline void
3218basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3219{
3220 size_type __sz = size();
3221 if (__n > __sz) {
3222 __append_default_init(__n - __sz);
3223 } else
3224 __erase_to_end(__n);
3225}
3226
3227template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003228inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003230basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003232 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003233#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003234 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003236 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237#endif
3238}
3239
3240template <class _CharT, class _Traits, class _Allocator>
3241void
3242basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
3243{
3244 if (__res_arg > max_size())
3245 this->__throw_length_error();
3246 size_type __cap = capacity();
3247 size_type __sz = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003248 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249 __res_arg = __recommend(__res_arg);
3250 if (__res_arg != __cap)
3251 {
3252 pointer __new_data, __p;
3253 bool __was_long, __now_long;
3254 if (__res_arg == __min_cap - 1)
3255 {
3256 __was_long = true;
3257 __now_long = false;
3258 __new_data = __get_short_pointer();
3259 __p = __get_long_pointer();
3260 }
3261 else
3262 {
3263 if (__res_arg > __cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003264 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265 else
3266 {
3267 #ifndef _LIBCPP_NO_EXCEPTIONS
3268 try
3269 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003270 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003271 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272 #ifndef _LIBCPP_NO_EXCEPTIONS
3273 }
3274 catch (...)
3275 {
3276 return;
3277 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003278 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd17880b2013-06-28 16:59:19 +00003279 if (__new_data == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280 return;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003281 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282 }
3283 __now_long = true;
3284 __was_long = __is_long();
3285 __p = __get_pointer();
3286 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003287 traits_type::copy(_VSTD::__to_address(__new_data),
3288 _VSTD::__to_address(__p), size()+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289 if (__was_long)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003290 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291 if (__now_long)
3292 {
3293 __set_long_cap(__res_arg+1);
3294 __set_long_size(__sz);
3295 __set_long_pointer(__new_data);
3296 }
3297 else
3298 __set_short_size(__sz);
3299 __invalidate_all_iterators();
3300 }
3301}
3302
3303template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003304inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003306basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003308 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309 return *(data() + __pos);
3310}
3311
3312template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003313inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003315basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003317 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318 return *(__get_pointer() + __pos);
3319}
3320
3321template <class _CharT, class _Traits, class _Allocator>
3322typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3323basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3324{
3325 if (__n >= size())
3326 this->__throw_out_of_range();
3327 return (*this)[__n];
3328}
3329
3330template <class _CharT, class _Traits, class _Allocator>
3331typename basic_string<_CharT, _Traits, _Allocator>::reference
3332basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3333{
3334 if (__n >= size())
3335 this->__throw_out_of_range();
3336 return (*this)[__n];
3337}
3338
3339template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003340inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003342basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003344 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345 return *__get_pointer();
3346}
3347
3348template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003349inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003351basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003353 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354 return *data();
3355}
3356
3357template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003358inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003360basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003362 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363 return *(__get_pointer() + size() - 1);
3364}
3365
3366template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003367inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003369basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003371 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372 return *(data() + size() - 1);
3373}
3374
3375template <class _CharT, class _Traits, class _Allocator>
3376typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003377basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378{
3379 size_type __sz = size();
3380 if (__pos > __sz)
3381 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003382 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383 traits_type::copy(__s, data() + __pos, __rlen);
3384 return __rlen;
3385}
3386
3387template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003388inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389basic_string<_CharT, _Traits, _Allocator>
3390basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3391{
3392 return basic_string(*this, __pos, __n, __alloc());
3393}
3394
3395template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003396inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397void
3398basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003399#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003400 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003401#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003402 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003403 __is_nothrow_swappable<allocator_type>::value)
3404#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003406#if _LIBCPP_DEBUG_LEVEL >= 2
3407 if (!__is_long())
3408 __get_db()->__invalidate_all(this);
3409 if (!__str.__is_long())
3410 __get_db()->__invalidate_all(&__str);
3411 __get_db()->swap(this, &__str);
3412#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003413 _LIBCPP_ASSERT(
3414 __alloc_traits::propagate_on_container_swap::value ||
3415 __alloc_traits::is_always_equal::value ||
3416 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003417 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow8982dcd2015-07-13 20:04:56 +00003418 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003419}
3420
3421// find
3422
3423template <class _Traits>
3424struct _LIBCPP_HIDDEN __traits_eq
3425{
3426 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003427 _LIBCPP_INLINE_VISIBILITY
3428 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3429 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430};
3431
3432template<class _CharT, class _Traits, class _Allocator>
3433typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003434basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003435 size_type __pos,
3436 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003438 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003439 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003440 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441}
3442
3443template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003444inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003446basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3447 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003449 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003450 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003451}
3452
3453template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003454template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003455_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003456<
3457 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3458 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003459>
Marshall Clowe46031a2018-07-02 18:41:15 +00003460basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
3461 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003462{
Marshall Clowe46031a2018-07-02 18:41:15 +00003463 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003464 return __str_find<value_type, size_type, traits_type, npos>
3465 (data(), size(), __sv.data(), __pos, __sv.size());
3466}
3467
3468template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003469inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003470typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003471basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003472 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003474 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003475 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003476 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477}
3478
3479template<class _CharT, class _Traits, class _Allocator>
3480typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003481basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3482 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003484 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003485 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486}
3487
3488// rfind
3489
3490template<class _CharT, class _Traits, class _Allocator>
3491typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003492basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003493 size_type __pos,
3494 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003496 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003497 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003498 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499}
3500
3501template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003502inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003504basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3505 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003507 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003508 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509}
3510
3511template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003512template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003513_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003514<
3515 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3516 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003517>
Marshall Clowe46031a2018-07-02 18:41:15 +00003518basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
3519 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003520{
Marshall Clowe46031a2018-07-02 18:41:15 +00003521 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003522 return __str_rfind<value_type, size_type, traits_type, npos>
3523 (data(), size(), __sv.data(), __pos, __sv.size());
3524}
3525
3526template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003527inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003528typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003529basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003530 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003532 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003533 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003534 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535}
3536
3537template<class _CharT, class _Traits, class _Allocator>
3538typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003539basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3540 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003542 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003543 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544}
3545
3546// find_first_of
3547
3548template<class _CharT, class _Traits, class _Allocator>
3549typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003550basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003551 size_type __pos,
3552 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003554 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003555 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003556 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557}
3558
3559template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003560inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003562basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3563 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003565 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003566 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567}
3568
3569template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003570template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003571_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003572<
3573 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3574 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003575>
Marshall Clowe46031a2018-07-02 18:41:15 +00003576basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
3577 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003578{
Marshall Clowe46031a2018-07-02 18:41:15 +00003579 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003580 return __str_find_first_of<value_type, size_type, traits_type, npos>
3581 (data(), size(), __sv.data(), __pos, __sv.size());
3582}
3583
3584template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003585inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003586typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003587basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003588 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003590 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003591 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003592 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593}
3594
3595template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003596inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003598basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3599 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600{
3601 return find(__c, __pos);
3602}
3603
3604// find_last_of
3605
3606template<class _CharT, class _Traits, class _Allocator>
3607typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003608basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003609 size_type __pos,
3610 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003612 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003613 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003614 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615}
3616
3617template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003618inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003620basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3621 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003623 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003624 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625}
3626
3627template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003628template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003629_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003630<
3631 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3632 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003633>
Marshall Clowe46031a2018-07-02 18:41:15 +00003634basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
3635 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003636{
Marshall Clowe46031a2018-07-02 18:41:15 +00003637 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003638 return __str_find_last_of<value_type, size_type, traits_type, npos>
3639 (data(), size(), __sv.data(), __pos, __sv.size());
3640}
3641
3642template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003643inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003644typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003645basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003646 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003648 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003649 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003650 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651}
3652
3653template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003654inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003656basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3657 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658{
3659 return rfind(__c, __pos);
3660}
3661
3662// find_first_not_of
3663
3664template<class _CharT, class _Traits, class _Allocator>
3665typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003666basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003667 size_type __pos,
3668 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003670 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003671 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003672 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673}
3674
3675template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003676inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003678basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3679 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003681 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003682 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683}
3684
3685template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003686template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003687_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003688<
3689 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3690 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003691>
Marshall Clowe46031a2018-07-02 18:41:15 +00003692basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
3693 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694{
Marshall Clowe46031a2018-07-02 18:41:15 +00003695 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003696 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3697 (data(), size(), __sv.data(), __pos, __sv.size());
3698}
3699
3700template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003701inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003703basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003704 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003706 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003707 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003708 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003709}
3710
3711template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003712inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003714basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3715 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003717 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003718 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719}
3720
3721// find_last_not_of
3722
3723template<class _CharT, class _Traits, class _Allocator>
3724typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003725basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003726 size_type __pos,
3727 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003729 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003730 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003731 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732}
3733
3734template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003735inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003737basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3738 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003739{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003740 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003741 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003742}
3743
3744template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003745template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003746_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003747<
3748 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3749 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003750>
Marshall Clowe46031a2018-07-02 18:41:15 +00003751basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
3752 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003753{
Marshall Clowe46031a2018-07-02 18:41:15 +00003754 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003755 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3756 (data(), size(), __sv.data(), __pos, __sv.size());
3757}
3758
3759template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003760inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003761typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003762basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003763 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003765 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003766 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003767 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768}
3769
3770template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003771inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003773basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3774 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003776 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003777 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778}
3779
3780// compare
3781
3782template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003783template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003784_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003785<
3786 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3787 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003788>
Marshall Clowe46031a2018-07-02 18:41:15 +00003789basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790{
Marshall Clowe46031a2018-07-02 18:41:15 +00003791 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003792 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003793 size_t __rhs_sz = __sv.size();
3794 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003795 _VSTD::min(__lhs_sz, __rhs_sz));
3796 if (__result != 0)
3797 return __result;
3798 if (__lhs_sz < __rhs_sz)
3799 return -1;
3800 if (__lhs_sz > __rhs_sz)
3801 return 1;
3802 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803}
3804
3805template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003806inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003808basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003810 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811}
3812
3813template <class _CharT, class _Traits, class _Allocator>
3814int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003815basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3816 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003817 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003818 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003820 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821 size_type __sz = size();
3822 if (__pos1 > __sz || __n2 == npos)
3823 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003824 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3825 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003826 if (__r == 0)
3827 {
3828 if (__rlen < __n2)
3829 __r = -1;
3830 else if (__rlen > __n2)
3831 __r = 1;
3832 }
3833 return __r;
3834}
3835
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003836template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003837template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003838_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003839<
3840 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3841 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003842>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003843basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3844 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003845 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003846{
Marshall Clowe46031a2018-07-02 18:41:15 +00003847 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003848 return compare(__pos1, __n1, __sv.data(), __sv.size());
3849}
3850
3851template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003852inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003853int
3854basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3855 size_type __n1,
3856 const basic_string& __str) const
3857{
3858 return compare(__pos1, __n1, __str.data(), __str.size());
3859}
3860
3861template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003862template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003863_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003864<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003865 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3866 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003867 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003868>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003869basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3870 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003871 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003872 size_type __pos2,
3873 size_type __n2) const
3874{
Marshall Clow82513342016-09-24 22:45:42 +00003875 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003876 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3877}
3878
3879template <class _CharT, class _Traits, class _Allocator>
3880int
3881basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3882 size_type __n1,
3883 const basic_string& __str,
3884 size_type __pos2,
3885 size_type __n2) const
3886{
3887 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3888}
3889
3890template <class _CharT, class _Traits, class _Allocator>
3891int
3892basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3893{
3894 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3895 return compare(0, npos, __s, traits_type::length(__s));
3896}
3897
3898template <class _CharT, class _Traits, class _Allocator>
3899int
3900basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3901 size_type __n1,
3902 const value_type* __s) const
3903{
3904 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3905 return compare(__pos1, __n1, __s, traits_type::length(__s));
3906}
3907
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908// __invariants
3909
3910template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003911inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912bool
3913basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3914{
3915 if (size() > capacity())
3916 return false;
3917 if (capacity() < __min_cap - 1)
3918 return false;
3919 if (data() == 0)
3920 return false;
3921 if (data()[size()] != value_type(0))
3922 return false;
3923 return true;
3924}
3925
Vedant Kumar55e007e2018-03-08 21:15:26 +00003926// __clear_and_shrink
3927
3928template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003929inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003930void
Marshall Clowe60a7182018-05-29 17:04:37 +00003931basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003932{
3933 clear();
3934 if(__is_long())
3935 {
3936 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3937 __set_long_cap(0);
3938 __set_short_size(0);
3939 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003940}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003941
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942// operator==
3943
3944template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003945inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003946bool
3947operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003948 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003949{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003950 size_t __lhs_sz = __lhs.size();
3951 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3952 __rhs.data(),
3953 __lhs_sz) == 0;
3954}
3955
3956template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003958bool
3959operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3960 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3961{
3962 size_t __lhs_sz = __lhs.size();
3963 if (__lhs_sz != __rhs.size())
3964 return false;
3965 const char* __lp = __lhs.data();
3966 const char* __rp = __rhs.data();
3967 if (__lhs.__is_long())
3968 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3969 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3970 if (*__lp != *__rp)
3971 return false;
3972 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973}
3974
3975template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003976inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003978operator==(const _CharT* __lhs,
3979 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003980{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003981 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3982 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3983 size_t __lhs_len = _Traits::length(__lhs);
3984 if (__lhs_len != __rhs.size()) return false;
3985 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986}
3987
Howard Hinnantc51e1022010-05-11 19:42:16 +00003988template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003991operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3992 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003994 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3995 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3996 size_t __rhs_len = _Traits::length(__rhs);
3997 if (__rhs_len != __lhs.size()) return false;
3998 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999}
4000
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004001template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004002inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003bool
4004operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004005 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006{
4007 return !(__lhs == __rhs);
4008}
4009
4010template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004013operator!=(const _CharT* __lhs,
4014 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015{
4016 return !(__lhs == __rhs);
4017}
4018
4019template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004020inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004021bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004022operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4023 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024{
4025 return !(__lhs == __rhs);
4026}
4027
4028// operator<
4029
4030template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004031inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004032bool
4033operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004034 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004036 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037}
4038
4039template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004042operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4043 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004045 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046}
4047
4048template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004049inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004051operator< (const _CharT* __lhs,
4052 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053{
4054 return __rhs.compare(__lhs) > 0;
4055}
4056
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057// operator>
4058
4059template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004060inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061bool
4062operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004063 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064{
4065 return __rhs < __lhs;
4066}
4067
4068template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004069inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004071operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4072 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073{
4074 return __rhs < __lhs;
4075}
4076
4077template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004078inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004080operator> (const _CharT* __lhs,
4081 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082{
4083 return __rhs < __lhs;
4084}
4085
4086// operator<=
4087
4088template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004089inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090bool
4091operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004092 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093{
4094 return !(__rhs < __lhs);
4095}
4096
4097template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004098inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004100operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4101 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102{
4103 return !(__rhs < __lhs);
4104}
4105
4106template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004107inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004109operator<=(const _CharT* __lhs,
4110 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111{
4112 return !(__rhs < __lhs);
4113}
4114
4115// operator>=
4116
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 +00004119bool
4120operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004121 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122{
4123 return !(__lhs < __rhs);
4124}
4125
4126template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004127inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004129operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4130 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131{
4132 return !(__lhs < __rhs);
4133}
4134
4135template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004136inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004138operator>=(const _CharT* __lhs,
4139 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140{
4141 return !(__lhs < __rhs);
4142}
4143
4144// operator +
4145
4146template<class _CharT, class _Traits, class _Allocator>
4147basic_string<_CharT, _Traits, _Allocator>
4148operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4149 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4150{
4151 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4152 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4153 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4154 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4155 __r.append(__rhs.data(), __rhs_sz);
4156 return __r;
4157}
4158
4159template<class _CharT, class _Traits, class _Allocator>
4160basic_string<_CharT, _Traits, _Allocator>
4161operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4162{
4163 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4164 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4165 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4166 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4167 __r.append(__rhs.data(), __rhs_sz);
4168 return __r;
4169}
4170
4171template<class _CharT, class _Traits, class _Allocator>
4172basic_string<_CharT, _Traits, _Allocator>
4173operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4174{
4175 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4176 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4177 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4178 __r.append(__rhs.data(), __rhs_sz);
4179 return __r;
4180}
4181
4182template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004183inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184basic_string<_CharT, _Traits, _Allocator>
4185operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4186{
4187 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4188 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4189 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4190 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4191 __r.append(__rhs, __rhs_sz);
4192 return __r;
4193}
4194
4195template<class _CharT, class _Traits, class _Allocator>
4196basic_string<_CharT, _Traits, _Allocator>
4197operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4198{
4199 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4200 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4201 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4202 __r.push_back(__rhs);
4203 return __r;
4204}
4205
Eric Fiselierfc92be82017-04-19 00:28:44 +00004206#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207
4208template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004209inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210basic_string<_CharT, _Traits, _Allocator>
4211operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4212{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004213 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214}
4215
4216template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004217inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218basic_string<_CharT, _Traits, _Allocator>
4219operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4220{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004221 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222}
4223
4224template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226basic_string<_CharT, _Traits, _Allocator>
4227operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4228{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004229 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230}
4231
4232template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004233inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234basic_string<_CharT, _Traits, _Allocator>
4235operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4236{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004237 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238}
4239
4240template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004241inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242basic_string<_CharT, _Traits, _Allocator>
4243operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4244{
4245 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004246 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247}
4248
4249template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004250inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251basic_string<_CharT, _Traits, _Allocator>
4252operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4253{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004254 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255}
4256
4257template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004258inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259basic_string<_CharT, _Traits, _Allocator>
4260operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4261{
4262 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004263 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264}
4265
Eric Fiselierfc92be82017-04-19 00:28:44 +00004266#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267
4268// swap
4269
4270template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004271inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004273swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004274 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4275 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276{
4277 __lhs.swap(__rhs);
4278}
4279
Marshall Clow8732fed2018-12-11 04:35:44 +00004280#ifndef _LIBCPP_NO_HAS_CHAR8_T
4281typedef basic_string<char8_t> u8string;
4282#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283
Marshall Clow8732fed2018-12-11 04:35:44 +00004284#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285typedef basic_string<char16_t> u16string;
4286typedef basic_string<char32_t> u32string;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004287#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004289_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
4290_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
4291_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
4292_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
4293_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004294
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004295_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
4296_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
4297_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004298
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004299_LIBCPP_FUNC_VIS string to_string(int __val);
4300_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4301_LIBCPP_FUNC_VIS string to_string(long __val);
4302_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4303_LIBCPP_FUNC_VIS string to_string(long long __val);
4304_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4305_LIBCPP_FUNC_VIS string to_string(float __val);
4306_LIBCPP_FUNC_VIS string to_string(double __val);
4307_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004308
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004309_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
4310_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
4311_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
4312_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
4313_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004314
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004315_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
4316_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
4317_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004318
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004319_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4320_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4321_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4322_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4323_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4324_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4325_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4326_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4327_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004328
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004330_LIBCPP_FUNC_VIS
4331const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4332 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333
Marshall Clow851b9ec2019-05-20 21:56:51 +00004334template <class _CharT, class _Allocator>
4335struct _LIBCPP_TEMPLATE_VIS
4336 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4337 : public unary_function<
4338 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339{
4340 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004341 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4342 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343};
4344
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345
Howard Hinnantdc095972011-07-18 15:51:59 +00004346template<class _CharT, class _Traits, class _Allocator>
4347basic_ostream<_CharT, _Traits>&
4348operator<<(basic_ostream<_CharT, _Traits>& __os,
4349 const basic_string<_CharT, _Traits, _Allocator>& __str);
4350
4351template<class _CharT, class _Traits, class _Allocator>
4352basic_istream<_CharT, _Traits>&
4353operator>>(basic_istream<_CharT, _Traits>& __is,
4354 basic_string<_CharT, _Traits, _Allocator>& __str);
4355
4356template<class _CharT, class _Traits, class _Allocator>
4357basic_istream<_CharT, _Traits>&
4358getline(basic_istream<_CharT, _Traits>& __is,
4359 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4360
4361template<class _CharT, class _Traits, class _Allocator>
4362inline _LIBCPP_INLINE_VISIBILITY
4363basic_istream<_CharT, _Traits>&
4364getline(basic_istream<_CharT, _Traits>& __is,
4365 basic_string<_CharT, _Traits, _Allocator>& __str);
4366
Eric Fiselierfc92be82017-04-19 00:28:44 +00004367#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004368
4369template<class _CharT, class _Traits, class _Allocator>
4370inline _LIBCPP_INLINE_VISIBILITY
4371basic_istream<_CharT, _Traits>&
4372getline(basic_istream<_CharT, _Traits>&& __is,
4373 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4374
4375template<class _CharT, class _Traits, class _Allocator>
4376inline _LIBCPP_INLINE_VISIBILITY
4377basic_istream<_CharT, _Traits>&
4378getline(basic_istream<_CharT, _Traits>&& __is,
4379 basic_string<_CharT, _Traits, _Allocator>& __str);
4380
Eric Fiselierfc92be82017-04-19 00:28:44 +00004381#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004382
Marshall Clow29b53f22018-12-14 18:49:35 +00004383#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004384template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004385inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004386 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4387 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4388 auto __old_size = __str.size();
4389 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4390 return __old_size - __str.size();
4391}
Marshall Clow29b53f22018-12-14 18:49:35 +00004392
Marek Kurdeja98b1412020-05-02 13:58:03 +02004393template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004394inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004395 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4396 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4397 _Predicate __pred) {
4398 auto __old_size = __str.size();
4399 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4400 __str.end());
4401 return __old_size - __str.size();
4402}
Marshall Clow29b53f22018-12-14 18:49:35 +00004403#endif
4404
Howard Hinnant8ea98242013-08-23 17:37:05 +00004405#if _LIBCPP_DEBUG_LEVEL >= 2
4406
4407template<class _CharT, class _Traits, class _Allocator>
4408bool
4409basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4410{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004411 return this->data() <= _VSTD::__to_address(__i->base()) &&
4412 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004413}
4414
4415template<class _CharT, class _Traits, class _Allocator>
4416bool
4417basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4418{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004419 return this->data() < _VSTD::__to_address(__i->base()) &&
4420 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004421}
4422
4423template<class _CharT, class _Traits, class _Allocator>
4424bool
4425basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4426{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004427 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004428 return this->data() <= __p && __p <= this->data() + this->size();
4429}
4430
4431template<class _CharT, class _Traits, class _Allocator>
4432bool
4433basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4434{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004435 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004436 return this->data() <= __p && __p < this->data() + this->size();
4437}
4438
4439#endif // _LIBCPP_DEBUG_LEVEL >= 2
4440
Louis Dionne173f29e2019-05-29 16:01:36 +00004441#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004442// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004443inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004444{
4445 inline namespace string_literals
4446 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004447 inline _LIBCPP_INLINE_VISIBILITY
4448 basic_string<char> operator "" s( const char *__str, size_t __len )
4449 {
4450 return basic_string<char> (__str, __len);
4451 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004452
Howard Hinnant5c167562013-08-07 19:39:48 +00004453 inline _LIBCPP_INLINE_VISIBILITY
4454 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4455 {
4456 return basic_string<wchar_t> (__str, __len);
4457 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004458
Marshall Clow8732fed2018-12-11 04:35:44 +00004459#ifndef _LIBCPP_NO_HAS_CHAR8_T
4460 inline _LIBCPP_INLINE_VISIBILITY
4461 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4462 {
4463 return basic_string<char8_t> (__str, __len);
4464 }
4465#endif
4466
Howard Hinnant5c167562013-08-07 19:39:48 +00004467 inline _LIBCPP_INLINE_VISIBILITY
4468 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4469 {
4470 return basic_string<char16_t> (__str, __len);
4471 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004472
Howard Hinnant5c167562013-08-07 19:39:48 +00004473 inline _LIBCPP_INLINE_VISIBILITY
4474 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4475 {
4476 return basic_string<char32_t> (__str, __len);
4477 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004478 }
4479}
4480#endif
4481
Howard Hinnantc51e1022010-05-11 19:42:16 +00004482_LIBCPP_END_NAMESPACE_STD
4483
Eric Fiselierf4433a32017-05-31 22:07:49 +00004484_LIBCPP_POP_MACROS
4485
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486#endif // _LIBCPP_STRING