blob: 7fc428027a1b21cb45eea2e582341aecfa112326 [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 Hinnantc51e1022010-05-11 19:42:16 +0000694 typedef __wrap_iter<pointer> iterator;
695 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000696 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
697 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698
699private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000700
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000701#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000702
703 struct __long
704 {
705 pointer __data_;
706 size_type __size_;
707 size_type __cap_;
708 };
709
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000710#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000711 static const size_type __short_mask = 0x01;
712 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000713#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000714 static const size_type __short_mask = 0x80;
715 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant68bf1812013-04-30 21:44:48 +0000716#endif // _LIBCPP_BIG_ENDIAN
717
718 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
719 (sizeof(__long) - 1)/sizeof(value_type) : 2};
720
721 struct __short
722 {
723 value_type __data_[__min_cap];
724 struct
725 : __padding<value_type>
726 {
727 unsigned char __size_;
728 };
729 };
730
731#else
732
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733 struct __long
734 {
735 size_type __cap_;
736 size_type __size_;
737 pointer __data_;
738 };
739
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000740#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000741 static const size_type __short_mask = 0x80;
742 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000743#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000744 static const size_type __short_mask = 0x01;
745 static const size_type __long_mask = 0x1ul;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000746#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
749 (sizeof(__long) - 1)/sizeof(value_type) : 2};
750
751 struct __short
752 {
753 union
754 {
755 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000756 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757 };
758 value_type __data_[__min_cap];
759 };
760
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000761#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000762
Howard Hinnant8ea98242013-08-23 17:37:05 +0000763 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764
Howard Hinnant8ea98242013-08-23 17:37:05 +0000765 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766
767 struct __raw
768 {
769 size_type __words[__n_words];
770 };
771
772 struct __rep
773 {
774 union
775 {
776 __long __l;
777 __short __s;
778 __raw __r;
779 };
780 };
781
782 __compressed_pair<__rep, allocator_type> __r_;
783
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784public:
Eric Fiselier2ae9e062020-01-16 15:00:34 -0500785 _LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 static const size_type npos = -1;
787
Howard Hinnant3e276872011-06-03 18:40:47 +0000788 _LIBCPP_INLINE_VISIBILITY basic_string()
789 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000790
791 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
792#if _LIBCPP_STD_VER <= 14
793 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
794#else
795 _NOEXCEPT;
796#endif
797
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798 basic_string(const basic_string& __str);
799 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000800
Eric Fiselierfc92be82017-04-19 00:28:44 +0000801#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000803 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000804#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000805 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000806#else
807 _NOEXCEPT;
808#endif
809
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000812#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000813
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500814 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000815 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500816 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000817 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
818 __init(__s, traits_type::length(__s));
819# if _LIBCPP_DEBUG_LEVEL >= 2
820 __get_db()->__insert_c(this);
821# endif
822 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000823
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500824 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000825 _LIBCPP_INLINE_VISIBILITY
826 basic_string(const _CharT* __s, const _Allocator& __a);
827
Howard Hinnantcf823322010-12-17 14:46:43 +0000828 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000829 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000830 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000831 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000833 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000834
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500835 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000836 _LIBCPP_INLINE_VISIBILITY
837 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
838
Marshall Clow83445802016-04-07 18:13:41 +0000839 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000840 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000841 _LIBCPP_INLINE_VISIBILITY
842 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000843 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000844
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500845 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 +0000846 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000847 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500848 const allocator_type& __a = allocator_type());
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 &&
851 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000852 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
853 explicit basic_string(const _Tp& __t);
854
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500855 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 +0000856 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
857 explicit basic_string(const _Tp& __t, const allocator_type& __a);
858
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500859 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500862 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000865#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000866 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000867 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000868 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000869 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000870#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000872 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000874 _LIBCPP_INLINE_VISIBILITY
875 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
876
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000877 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000878
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500879 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 +0000880 basic_string& operator=(const _Tp& __t)
881 {__self_view __sv = __t; return assign(__sv);}
882
Eric Fiselierfc92be82017-04-19 00:28:44 +0000883#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000885 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000886 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000887 _LIBCPP_INLINE_VISIBILITY
888 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000890 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892
Howard Hinnant8ea98242013-08-23 17:37:05 +0000893#if _LIBCPP_DEBUG_LEVEL >= 2
894 _LIBCPP_INLINE_VISIBILITY
895 iterator begin() _NOEXCEPT
896 {return iterator(this, __get_pointer());}
897 _LIBCPP_INLINE_VISIBILITY
898 const_iterator begin() const _NOEXCEPT
899 {return const_iterator(this, __get_pointer());}
900 _LIBCPP_INLINE_VISIBILITY
901 iterator end() _NOEXCEPT
902 {return iterator(this, __get_pointer() + size());}
903 _LIBCPP_INLINE_VISIBILITY
904 const_iterator end() const _NOEXCEPT
905 {return const_iterator(this, __get_pointer() + size());}
906#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000907 _LIBCPP_INLINE_VISIBILITY
908 iterator begin() _NOEXCEPT
909 {return iterator(__get_pointer());}
910 _LIBCPP_INLINE_VISIBILITY
911 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000912 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000913 _LIBCPP_INLINE_VISIBILITY
914 iterator end() _NOEXCEPT
915 {return iterator(__get_pointer() + size());}
916 _LIBCPP_INLINE_VISIBILITY
917 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000918 {return const_iterator(__get_pointer() + size());}
Howard Hinnant8ea98242013-08-23 17:37:05 +0000919#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000920 _LIBCPP_INLINE_VISIBILITY
921 reverse_iterator rbegin() _NOEXCEPT
922 {return reverse_iterator(end());}
923 _LIBCPP_INLINE_VISIBILITY
924 const_reverse_iterator rbegin() const _NOEXCEPT
925 {return const_reverse_iterator(end());}
926 _LIBCPP_INLINE_VISIBILITY
927 reverse_iterator rend() _NOEXCEPT
928 {return reverse_iterator(begin());}
929 _LIBCPP_INLINE_VISIBILITY
930 const_reverse_iterator rend() const _NOEXCEPT
931 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000933 _LIBCPP_INLINE_VISIBILITY
934 const_iterator cbegin() const _NOEXCEPT
935 {return begin();}
936 _LIBCPP_INLINE_VISIBILITY
937 const_iterator cend() const _NOEXCEPT
938 {return end();}
939 _LIBCPP_INLINE_VISIBILITY
940 const_reverse_iterator crbegin() const _NOEXCEPT
941 {return rbegin();}
942 _LIBCPP_INLINE_VISIBILITY
943 const_reverse_iterator crend() const _NOEXCEPT
944 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000946 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000948 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
949 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
950 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000951 {return (__is_long() ? __get_long_cap()
952 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953
954 void resize(size_type __n, value_type __c);
955 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
956
Marshall Clow6ae12a82018-11-28 18:18:34 +0000957 void reserve(size_type __res_arg);
Eric Fiselier451d5582018-11-26 20:15:38 +0000958 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
959
Marshall Clow6ae12a82018-11-28 18:18:34 +0000960 _LIBCPP_INLINE_VISIBILITY
961 void reserve() _NOEXCEPT {reserve(0);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000963 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnantcf823322010-12-17 14:46:43 +0000964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000965 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000966 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
967 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000969 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
970 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971
972 const_reference at(size_type __n) const;
973 reference at(size_type __n);
974
975 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000976
977 template <class _Tp>
978 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500979 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +0000980 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500981 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
982 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000983 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500984 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000985 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000986 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000988#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000990#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991
Howard Hinnantcf823322010-12-17 14:46:43 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000994
995 template <class _Tp>
996 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500997 _EnableIf<
998 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
999 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001000 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001001 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001002 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001003 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001004
Marshall Clow62953962016-10-03 23:40:48 +00001005 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001006 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001007 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001008 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001009 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1010 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001011 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001012 >
Marshall Clow82513342016-09-24 22:45:42 +00001013 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001014 basic_string& append(const value_type* __s, size_type __n);
1015 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001017
1018 _LIBCPP_INLINE_VISIBILITY
1019 void __append_default_init(size_type __n);
1020
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001021 template <class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001022 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1023 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001025 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001028 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001029 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001031 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001032 _LIBCPP_INLINE_VISIBILITY
1033 append(_InputIterator __first, _InputIterator __last) {
1034 const basic_string __temp (__first, __last, __alloc());
1035 append(__temp.data(), __temp.size());
1036 return *this;
1037 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001039 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001040 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001042 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001043 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001045 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001046 _LIBCPP_INLINE_VISIBILITY
1047 append(_ForwardIterator __first, _ForwardIterator __last) {
1048 return __append_forward_unsafe(__first, __last);
1049 }
1050
Eric Fiselierfc92be82017-04-19 00:28:44 +00001051#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001054#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055
1056 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001059 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1060 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1061 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1062 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063
Marshall Clowe46031a2018-07-02 18:41:15 +00001064 template <class _Tp>
1065 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001066 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001067 <
1068 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1069 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001070 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001071 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001072 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001073 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001074#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001075 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001076 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001077 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001078 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001079#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001080 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001081 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001082 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001083 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001084 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001085 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1086 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001087 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001088 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001089 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001090 basic_string& assign(const value_type* __s, size_type __n);
1091 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 basic_string& assign(size_type __n, value_type __c);
1093 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001094 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001097 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001098 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001100 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101 assign(_InputIterator __first, _InputIterator __last);
1102 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001103 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001104 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001106 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001107 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001109 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001111#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001114#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115
Howard Hinnantcf823322010-12-17 14:46:43 +00001116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001118
1119 template <class _Tp>
1120 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001121 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001122 <
1123 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1124 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001125 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001126 insert(size_type __pos1, const _Tp& __t)
1127 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1128
Marshall Clow82513342016-09-24 22:45:42 +00001129 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001130 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001131 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001132 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001134 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001135 >
Marshall Clow82513342016-09-24 22:45:42 +00001136 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001137 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001138 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1139 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1141 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1144 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001145 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001146 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001148 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001149 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001151 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1153 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001154 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001155 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001157 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001158 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001160 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001162#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1165 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001166#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167
1168 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 iterator erase(const_iterator __first, const_iterator __last);
1173
Howard Hinnantcf823322010-12-17 14:46:43 +00001174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001176
1177 template <class _Tp>
1178 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001179 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001180 <
1181 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1182 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001183 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001184 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 +00001185 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 +00001186 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001187 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001188 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001189 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001190 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001191 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001192 >
Marshall Clow82513342016-09-24 22:45:42 +00001193 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001194 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1195 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001198 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001199
1200 template <class _Tp>
1201 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001202 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001203 <
1204 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1205 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001206 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001207 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1208
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001210 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001212 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001214 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001216 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001217 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001219 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001221 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001222 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001223#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001225 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001227#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228
Howard Hinnantd17880b2013-06-28 16:59:19 +00001229 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1232
Howard Hinnantcf823322010-12-17 14:46:43 +00001233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001234 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001235#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001236 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001237#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001238 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001239 __is_nothrow_swappable<allocator_type>::value);
1240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
Howard Hinnantcf823322010-12-17 14:46:43 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001243 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001244 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001245 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001246#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001247 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001248 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001249#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250
Howard Hinnantcf823322010-12-17 14:46:43 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001252 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253
Howard Hinnantcf823322010-12-17 14:46:43 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001255 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001256
1257 template <class _Tp>
1258 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001259 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001260 <
1261 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1262 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001263 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001264 find(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001265 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001267 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001268 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269
Howard Hinnantcf823322010-12-17 14:46:43 +00001270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001271 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001272
1273 template <class _Tp>
1274 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001275 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001276 <
1277 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1278 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001279 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001280 rfind(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001281 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001283 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001284 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Howard Hinnantcf823322010-12-17 14:46:43 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001287 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001288
1289 template <class _Tp>
1290 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001291 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001292 <
1293 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1294 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001295 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001296 find_first_of(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001297 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001299 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001301 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302
Howard Hinnantcf823322010-12-17 14:46:43 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001304 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001305
1306 template <class _Tp>
1307 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001308 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001309 <
1310 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1311 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001312 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001313 find_last_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001314 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001318 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319
Howard Hinnantcf823322010-12-17 14:46:43 +00001320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001321 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001322
1323 template <class _Tp>
1324 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001325 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001326 <
1327 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1328 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001329 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001330 find_first_not_of(const _Tp &__t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001331 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 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001334 _LIBCPP_INLINE_VISIBILITY
1335 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1336
1337 _LIBCPP_INLINE_VISIBILITY
1338 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001339
1340 template <class _Tp>
1341 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001342 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001343 <
1344 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1345 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001346 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001347 find_last_not_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001348 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 +00001349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001350 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001351 _LIBCPP_INLINE_VISIBILITY
1352 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1353
1354 _LIBCPP_INLINE_VISIBILITY
1355 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001356
1357 template <class _Tp>
1358 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001359 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001360 <
1361 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1362 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001363 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001364 compare(const _Tp &__t) const;
1365
1366 template <class _Tp>
1367 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001368 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001369 <
1370 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1371 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001372 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001373 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1374
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001377 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 +00001378
Marshall Clow82513342016-09-24 22:45:42 +00001379 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001380 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001381 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001382 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001383 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001384 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001385 >
Marshall Clow82513342016-09-24 22:45:42 +00001386 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 +00001387 int compare(const value_type* __s) const _NOEXCEPT;
1388 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1389 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390
Marshall Clow18c293b2017-12-04 20:11:38 +00001391#if _LIBCPP_STD_VER > 17
1392 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1393 bool starts_with(__self_view __sv) const _NOEXCEPT
1394 { return __self_view(data(), size()).starts_with(__sv); }
1395
1396 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1397 bool starts_with(value_type __c) const _NOEXCEPT
1398 { return !empty() && _Traits::eq(front(), __c); }
1399
1400 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1401 bool starts_with(const value_type* __s) const _NOEXCEPT
1402 { return starts_with(__self_view(__s)); }
1403
1404 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1405 bool ends_with(__self_view __sv) const _NOEXCEPT
1406 { return __self_view(data(), size()).ends_with( __sv); }
1407
1408 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1409 bool ends_with(value_type __c) const _NOEXCEPT
1410 { return !empty() && _Traits::eq(back(), __c); }
1411
1412 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1413 bool ends_with(const value_type* __s) const _NOEXCEPT
1414 { return ends_with(__self_view(__s)); }
1415#endif
1416
Howard Hinnantcf823322010-12-17 14:46:43 +00001417 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001418
Marshall Clowe60a7182018-05-29 17:04:37 +00001419 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001420
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001421 _LIBCPP_INLINE_VISIBILITY
1422 bool __is_long() const _NOEXCEPT
1423 {return bool(__r_.first().__s.__size_ & __short_mask);}
1424
Howard Hinnant8ea98242013-08-23 17:37:05 +00001425#if _LIBCPP_DEBUG_LEVEL >= 2
1426
1427 bool __dereferenceable(const const_iterator* __i) const;
1428 bool __decrementable(const const_iterator* __i) const;
1429 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1430 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1431
1432#endif // _LIBCPP_DEBUG_LEVEL >= 2
1433
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001435 _LIBCPP_INLINE_VISIBILITY
1436 allocator_type& __alloc() _NOEXCEPT
1437 {return __r_.second();}
1438 _LIBCPP_INLINE_VISIBILITY
1439 const allocator_type& __alloc() const _NOEXCEPT
1440 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001442#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001443
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001445 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001446# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001448# else
1449 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1450# endif
1451
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001452 _LIBCPP_INLINE_VISIBILITY
1453 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001454# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001456# else
1457 {return __r_.first().__s.__size_;}
1458# endif
1459
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001460#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001461
1462 _LIBCPP_INLINE_VISIBILITY
1463 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001464# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001465 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1466# else
1467 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1468# endif
1469
1470 _LIBCPP_INLINE_VISIBILITY
1471 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001472# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001473 {return __r_.first().__s.__size_;}
1474# else
1475 {return __r_.first().__s.__size_ >> 1;}
1476# endif
1477
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001478#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001479
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001480 _LIBCPP_INLINE_VISIBILITY
1481 void __set_long_size(size_type __s) _NOEXCEPT
1482 {__r_.first().__l.__size_ = __s;}
1483 _LIBCPP_INLINE_VISIBILITY
1484 size_type __get_long_size() const _NOEXCEPT
1485 {return __r_.first().__l.__size_;}
1486 _LIBCPP_INLINE_VISIBILITY
1487 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1489
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001490 _LIBCPP_INLINE_VISIBILITY
1491 void __set_long_cap(size_type __s) _NOEXCEPT
1492 {__r_.first().__l.__cap_ = __long_mask | __s;}
1493 _LIBCPP_INLINE_VISIBILITY
1494 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001495 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001497 _LIBCPP_INLINE_VISIBILITY
1498 void __set_long_pointer(pointer __p) _NOEXCEPT
1499 {__r_.first().__l.__data_ = __p;}
1500 _LIBCPP_INLINE_VISIBILITY
1501 pointer __get_long_pointer() _NOEXCEPT
1502 {return __r_.first().__l.__data_;}
1503 _LIBCPP_INLINE_VISIBILITY
1504 const_pointer __get_long_pointer() const _NOEXCEPT
1505 {return __r_.first().__l.__data_;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001508 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001509 _LIBCPP_INLINE_VISIBILITY
1510 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001511 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001515 _LIBCPP_INLINE_VISIBILITY
1516 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521 {
1522 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1523 for (unsigned __i = 0; __i < __n_words; ++__i)
1524 __a[__i] = 0;
1525 }
1526
1527 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001529 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001530 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 static _LIBCPP_INLINE_VISIBILITY
1533 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001534 {
1535 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1536 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1537 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1538 if (__guess == __min_cap) ++__guess;
1539 return __guess;
1540 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001542 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001543 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001544 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001545 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001546 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001548
Martijn Vels5e7c9752020-03-04 17:52:46 -05001549 // Slow path for the (inlined) copy constructor for 'long' strings.
1550 // Always externally instantiated and not inlined.
1551 // Requires that __s is zero terminated.
1552 // The main reason for this function to exist is because for unstable, we
1553 // want to allow inlining of the copy constructor. However, we don't want
1554 // to call the __init() functions as those are marked as inline which may
1555 // result in over-aggressive inlining by the compiler, where our aim is
1556 // to only inline the fast path code directly in the ctor.
1557 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1558
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001560 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001561 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001563 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1564 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 __init(_InputIterator __first, _InputIterator __last);
1566
1567 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001568 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001569 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001571 __is_cpp17_forward_iterator<_ForwardIterator>::value
1572 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 __init(_ForwardIterator __first, _ForwardIterator __last);
1574
1575 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001576 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1578 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001579 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
Martijn Vels596e3de2020-02-26 15:55:49 -05001581 // __assign_no_alias is invoked for assignment operations where we
1582 // have proof that the input does not alias the current instance.
1583 // For example, operator=(basic_string) performs a 'self' check.
1584 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001585 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001586
Howard Hinnantcf823322010-12-17 14:46:43 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 void __erase_to_end(size_type __pos);
1589
Martijn Velsa81fc792020-02-26 13:25:43 -05001590 // __erase_external_with_move is invoked for erase() invocations where
1591 // `n ~= npos`, likely requiring memory moves on the string data.
1592 void __erase_external_with_move(size_type __pos, size_type __n);
1593
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001594 _LIBCPP_INLINE_VISIBILITY
1595 void __copy_assign_alloc(const basic_string& __str)
1596 {__copy_assign_alloc(__str, integral_constant<bool,
1597 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1598
1599 _LIBCPP_INLINE_VISIBILITY
1600 void __copy_assign_alloc(const basic_string& __str, true_type)
1601 {
Marshall Clowf258c202017-01-31 03:40:52 +00001602 if (__alloc() == __str.__alloc())
1603 __alloc() = __str.__alloc();
1604 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001605 {
Marshall Clowf258c202017-01-31 03:40:52 +00001606 if (!__str.__is_long())
1607 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001608 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001609 __alloc() = __str.__alloc();
1610 }
1611 else
1612 {
1613 allocator_type __a = __str.__alloc();
1614 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001615 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001616 __alloc() = _VSTD::move(__a);
1617 __set_long_pointer(__p);
1618 __set_long_cap(__str.__get_long_cap());
1619 __set_long_size(__str.size());
1620 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001621 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001622 }
1623
1624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001625 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001626 {}
1627
Eric Fiselierfc92be82017-04-19 00:28:44 +00001628#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001629 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001630 void __move_assign(basic_string& __str, false_type)
1631 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001633 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001634#if _LIBCPP_STD_VER > 14
1635 _NOEXCEPT;
1636#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001637 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001638#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001639#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001640
1641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001642 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001643 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001644 _NOEXCEPT_(
1645 !__alloc_traits::propagate_on_container_move_assignment::value ||
1646 is_nothrow_move_assignable<allocator_type>::value)
1647 {__move_assign_alloc(__str, integral_constant<bool,
1648 __alloc_traits::propagate_on_container_move_assignment::value>());}
1649
1650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001651 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001652 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1653 {
1654 __alloc() = _VSTD::move(__c.__alloc());
1655 }
1656
1657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001658 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001659 _NOEXCEPT
1660 {}
1661
Howard Hinnantcf823322010-12-17 14:46:43 +00001662 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1663 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664
1665 friend basic_string operator+<>(const basic_string&, const basic_string&);
1666 friend basic_string operator+<>(const value_type*, const basic_string&);
1667 friend basic_string operator+<>(value_type, const basic_string&);
1668 friend basic_string operator+<>(const basic_string&, const value_type*);
1669 friend basic_string operator+<>(const basic_string&, value_type);
1670};
1671
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001672// These declarations must appear before any functions are implicitly used
1673// so that they have the correct visibility specifier.
1674#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1675_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1676_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1677#else
1678_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1679_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1680#endif
1681
1682
Marshall Clowa0563332018-02-08 06:34:03 +00001683#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1684template<class _InputIterator,
1685 class _CharT = typename iterator_traits<_InputIterator>::value_type,
1686 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001687 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1688 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001689 >
1690basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1691 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001692
1693template<class _CharT,
1694 class _Traits,
1695 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001696 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001697 >
1698explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1699 -> basic_string<_CharT, _Traits, _Allocator>;
1700
1701template<class _CharT,
1702 class _Traits,
1703 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001704 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001705 class _Sz = typename allocator_traits<_Allocator>::size_type
1706 >
1707basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1708 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001709#endif
1710
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001712inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713void
1714basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1715{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001716#if _LIBCPP_DEBUG_LEVEL >= 2
1717 __get_db()->__invalidate_all(this);
1718#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719}
1720
1721template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001722inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723void
Howard Hinnant28b24882011-12-01 20:21:04 +00001724basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant8ea98242013-08-23 17:37:05 +00001725#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant28b24882011-12-01 20:21:04 +00001726 __pos
1727#endif
1728 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001730#if _LIBCPP_DEBUG_LEVEL >= 2
1731 __c_node* __c = __get_db()->__find_c_and_lock(this);
1732 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001734 const_pointer __new_last = __get_pointer() + __pos;
1735 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001737 --__p;
1738 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1739 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001741 (*__p)->__c_ = nullptr;
1742 if (--__c->end_ != __p)
1743 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001746 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001748#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749}
1750
1751template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001752inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001753basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001754 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001755 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001757#if _LIBCPP_DEBUG_LEVEL >= 2
1758 __get_db()->__insert_c(this);
1759#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760 __zero();
1761}
1762
1763template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001764inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001766#if _LIBCPP_STD_VER <= 14
1767 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1768#else
1769 _NOEXCEPT
1770#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001771: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001773#if _LIBCPP_DEBUG_LEVEL >= 2
1774 __get_db()->__insert_c(this);
1775#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 __zero();
1777}
1778
1779template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001780void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1781 size_type __sz,
1782 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783{
1784 if (__reserve > max_size())
1785 this->__throw_length_error();
1786 pointer __p;
1787 if (__reserve < __min_cap)
1788 {
1789 __set_short_size(__sz);
1790 __p = __get_short_pointer();
1791 }
1792 else
1793 {
1794 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001795 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 __set_long_pointer(__p);
1797 __set_long_cap(__cap+1);
1798 __set_long_size(__sz);
1799 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001800 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 traits_type::assign(__p[__sz], value_type());
1802}
1803
1804template <class _CharT, class _Traits, class _Allocator>
1805void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001806basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807{
1808 if (__sz > max_size())
1809 this->__throw_length_error();
1810 pointer __p;
1811 if (__sz < __min_cap)
1812 {
1813 __set_short_size(__sz);
1814 __p = __get_short_pointer();
1815 }
1816 else
1817 {
1818 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001819 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 __set_long_pointer(__p);
1821 __set_long_cap(__cap+1);
1822 __set_long_size(__sz);
1823 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001824 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 traits_type::assign(__p[__sz], value_type());
1826}
1827
1828template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001829template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001830basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001831 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001833 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834 __init(__s, traits_type::length(__s));
Howard Hinnant8ea98242013-08-23 17:37:05 +00001835#if _LIBCPP_DEBUG_LEVEL >= 2
1836 __get_db()->__insert_c(this);
1837#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838}
1839
1840template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001841inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001842basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001843 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001844{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001845 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846 __init(__s, __n);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001847#if _LIBCPP_DEBUG_LEVEL >= 2
1848 __get_db()->__insert_c(this);
1849#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850}
1851
1852template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001853inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001854basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001855 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001857 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 __init(__s, __n);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001859#if _LIBCPP_DEBUG_LEVEL >= 2
1860 __get_db()->__insert_c(this);
1861#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862}
1863
1864template <class _CharT, class _Traits, class _Allocator>
1865basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001866 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867{
1868 if (!__str.__is_long())
1869 __r_.first().__r = __str.__r_.first().__r;
1870 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001871 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1872 __str.__get_long_size());
1873
Howard Hinnant8ea98242013-08-23 17:37:05 +00001874#if _LIBCPP_DEBUG_LEVEL >= 2
1875 __get_db()->__insert_c(this);
1876#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877}
1878
1879template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001880basic_string<_CharT, _Traits, _Allocator>::basic_string(
1881 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001882 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883{
1884 if (!__str.__is_long())
1885 __r_.first().__r = __str.__r_.first().__r;
1886 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001887 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1888 __str.__get_long_size());
Howard Hinnant8ea98242013-08-23 17:37:05 +00001889#if _LIBCPP_DEBUG_LEVEL >= 2
1890 __get_db()->__insert_c(this);
1891#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892}
1893
Martijn Vels5e7c9752020-03-04 17:52:46 -05001894template <class _CharT, class _Traits, class _Allocator>
1895void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1896 const value_type* __s, size_type __sz) {
1897 pointer __p;
1898 if (__sz < __min_cap) {
1899 __p = __get_short_pointer();
1900 __set_short_size(__sz);
1901 } else {
1902 if (__sz > max_size())
1903 this->__throw_length_error();
1904 size_t __cap = __recommend(__sz);
1905 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1906 __set_long_pointer(__p);
1907 __set_long_cap(__cap + 1);
1908 __set_long_size(__sz);
1909 }
1910 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1911}
1912
Eric Fiselierfc92be82017-04-19 00:28:44 +00001913#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914
1915template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001916inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001917basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001918#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001919 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001920#else
1921 _NOEXCEPT
1922#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001923 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924{
1925 __str.__zero();
Howard Hinnant8ea98242013-08-23 17:37:05 +00001926#if _LIBCPP_DEBUG_LEVEL >= 2
1927 __get_db()->__insert_c(this);
1928 if (__is_long())
1929 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930#endif
1931}
1932
1933template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001934inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001936 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937{
Marshall Clowd2d24692014-07-17 15:32:20 +00001938 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001939 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001940 else
1941 {
1942 __r_.first().__r = __str.__r_.first().__r;
1943 __str.__zero();
1944 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001945#if _LIBCPP_DEBUG_LEVEL >= 2
1946 __get_db()->__insert_c(this);
1947 if (__is_long())
1948 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949#endif
1950}
1951
Eric Fiselierfc92be82017-04-19 00:28:44 +00001952#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953
1954template <class _CharT, class _Traits, class _Allocator>
1955void
1956basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1957{
1958 if (__n > max_size())
1959 this->__throw_length_error();
1960 pointer __p;
1961 if (__n < __min_cap)
1962 {
1963 __set_short_size(__n);
1964 __p = __get_short_pointer();
1965 }
1966 else
1967 {
1968 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001969 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970 __set_long_pointer(__p);
1971 __set_long_cap(__cap+1);
1972 __set_long_size(__n);
1973 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001974 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975 traits_type::assign(__p[__n], value_type());
1976}
1977
1978template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001979inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001980basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001981 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982{
1983 __init(__n, __c);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001984#if _LIBCPP_DEBUG_LEVEL >= 2
1985 __get_db()->__insert_c(this);
1986#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987}
1988
1989template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001990template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001991basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001992 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993{
1994 __init(__n, __c);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001995#if _LIBCPP_DEBUG_LEVEL >= 2
1996 __get_db()->__insert_c(this);
1997#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998}
1999
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002001basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2002 size_type __pos, size_type __n,
2003 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002004 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005{
2006 size_type __str_sz = __str.size();
2007 if (__pos > __str_sz)
2008 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002009 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant8ea98242013-08-23 17:37:05 +00002010#if _LIBCPP_DEBUG_LEVEL >= 2
2011 __get_db()->__insert_c(this);
2012#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013}
2014
2015template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002016inline
Marshall Clow83445802016-04-07 18:13:41 +00002017basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002018 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002019 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002020{
2021 size_type __str_sz = __str.size();
2022 if (__pos > __str_sz)
2023 this->__throw_out_of_range();
2024 __init(__str.data() + __pos, __str_sz - __pos);
2025#if _LIBCPP_DEBUG_LEVEL >= 2
2026 __get_db()->__insert_c(this);
2027#endif
2028}
2029
2030template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002031template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002032basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002033 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002034 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002035{
Marshall Clowe46031a2018-07-02 18:41:15 +00002036 __self_view __sv0 = __t;
2037 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002038 __init(__sv.data(), __sv.size());
2039#if _LIBCPP_DEBUG_LEVEL >= 2
2040 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002041#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002042}
2043
2044template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002045template <class _Tp, class>
2046basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002047 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002048{
Marshall Clowe46031a2018-07-02 18:41:15 +00002049 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002050 __init(__sv.data(), __sv.size());
2051#if _LIBCPP_DEBUG_LEVEL >= 2
2052 __get_db()->__insert_c(this);
2053#endif
2054}
2055
2056template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002057template <class _Tp, class>
2058basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002059 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002060{
Marshall Clowe46031a2018-07-02 18:41:15 +00002061 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002062 __init(__sv.data(), __sv.size());
2063#if _LIBCPP_DEBUG_LEVEL >= 2
2064 __get_db()->__insert_c(this);
2065#endif
2066}
2067
2068template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002070_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002072 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2073>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2075{
2076 __zero();
2077#ifndef _LIBCPP_NO_EXCEPTIONS
2078 try
2079 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002080#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081 for (; __first != __last; ++__first)
2082 push_back(*__first);
2083#ifndef _LIBCPP_NO_EXCEPTIONS
2084 }
2085 catch (...)
2086 {
2087 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002088 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089 throw;
2090 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002091#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092}
2093
2094template <class _CharT, class _Traits, class _Allocator>
2095template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002096_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002098 __is_cpp17_forward_iterator<_ForwardIterator>::value
2099>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2101{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002102 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103 if (__sz > max_size())
2104 this->__throw_length_error();
2105 pointer __p;
2106 if (__sz < __min_cap)
2107 {
2108 __set_short_size(__sz);
2109 __p = __get_short_pointer();
2110 }
2111 else
2112 {
2113 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002114 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115 __set_long_pointer(__p);
2116 __set_long_cap(__cap+1);
2117 __set_long_size(__sz);
2118 }
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002119 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 traits_type::assign(*__p, *__first);
2121 traits_type::assign(*__p, value_type());
2122}
2123
2124template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002125template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002126inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002128 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129{
2130 __init(__first, __last);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002131#if _LIBCPP_DEBUG_LEVEL >= 2
2132 __get_db()->__insert_c(this);
2133#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134}
2135
2136template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002137template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002138inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2140 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002141 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142{
2143 __init(__first, __last);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002144#if _LIBCPP_DEBUG_LEVEL >= 2
2145 __get_db()->__insert_c(this);
2146#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147}
2148
Eric Fiselierfc92be82017-04-19 00:28:44 +00002149#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002150
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002152inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002153basic_string<_CharT, _Traits, _Allocator>::basic_string(
2154 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002155 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156{
2157 __init(__il.begin(), __il.end());
Howard Hinnant8ea98242013-08-23 17:37:05 +00002158#if _LIBCPP_DEBUG_LEVEL >= 2
2159 __get_db()->__insert_c(this);
2160#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161}
2162
2163template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002164inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002165
Eric Fiselier812882b2017-02-17 01:17:10 +00002166basic_string<_CharT, _Traits, _Allocator>::basic_string(
2167 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002168 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169{
2170 __init(__il.begin(), __il.end());
Howard Hinnant8ea98242013-08-23 17:37:05 +00002171#if _LIBCPP_DEBUG_LEVEL >= 2
2172 __get_db()->__insert_c(this);
2173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174}
2175
Eric Fiselierfc92be82017-04-19 00:28:44 +00002176#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002177
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2180{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002181#if _LIBCPP_DEBUG_LEVEL >= 2
2182 __get_db()->__erase_c(this);
2183#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002185 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186}
2187
2188template <class _CharT, class _Traits, class _Allocator>
2189void
2190basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2191 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002192 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 +00002193{
2194 size_type __ms = max_size();
2195 if (__delta_cap > __ms - __old_cap - 1)
2196 this->__throw_length_error();
2197 pointer __old_p = __get_pointer();
2198 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002199 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002201 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202 __invalidate_all_iterators();
2203 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002204 traits_type::copy(_VSTD::__to_address(__p),
2205 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002207 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2209 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002210 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2211 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002213 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 __set_long_pointer(__p);
2215 __set_long_cap(__cap+1);
2216 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2217 __set_long_size(__old_sz);
2218 traits_type::assign(__p[__old_sz], value_type());
2219}
2220
2221template <class _CharT, class _Traits, class _Allocator>
2222void
2223basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2224 size_type __n_copy, size_type __n_del, size_type __n_add)
2225{
2226 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002227 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 this->__throw_length_error();
2229 pointer __old_p = __get_pointer();
2230 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002231 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002233 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 __invalidate_all_iterators();
2235 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002236 traits_type::copy(_VSTD::__to_address(__p),
2237 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2239 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002240 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2241 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002242 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002244 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 __set_long_pointer(__p);
2246 __set_long_cap(__cap+1);
2247}
2248
2249// assign
2250
2251template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002252template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002253basic_string<_CharT, _Traits, _Allocator>&
2254basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002255 const value_type* __s, size_type __n) {
2256 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2257 if (__n < __cap) {
2258 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2259 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2260 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2261 traits_type::assign(__p[__n], value_type());
2262 __invalidate_iterators_past(__n);
2263 } else {
2264 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2265 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2266 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002267 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002268}
2269
2270template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002272basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002274 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 size_type __cap = capacity();
2276 if (__cap >= __n)
2277 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002278 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279 traits_type::move(__p, __s, __n);
2280 traits_type::assign(__p[__n], value_type());
2281 __set_size(__n);
2282 __invalidate_iterators_past(__n);
2283 }
2284 else
2285 {
2286 size_type __sz = size();
2287 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2288 }
2289 return *this;
2290}
2291
2292template <class _CharT, class _Traits, class _Allocator>
2293basic_string<_CharT, _Traits, _Allocator>&
2294basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2295{
2296 size_type __cap = capacity();
2297 if (__cap < __n)
2298 {
2299 size_type __sz = size();
2300 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2301 }
2302 else
2303 __invalidate_iterators_past(__n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002304 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305 traits_type::assign(__p, __n, __c);
2306 traits_type::assign(__p[__n], value_type());
2307 __set_size(__n);
2308 return *this;
2309}
2310
2311template <class _CharT, class _Traits, class _Allocator>
2312basic_string<_CharT, _Traits, _Allocator>&
2313basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2314{
2315 pointer __p;
2316 if (__is_long())
2317 {
2318 __p = __get_long_pointer();
2319 __set_long_size(1);
2320 }
2321 else
2322 {
2323 __p = __get_short_pointer();
2324 __set_short_size(1);
2325 }
2326 traits_type::assign(*__p, __c);
2327 traits_type::assign(*++__p, value_type());
2328 __invalidate_iterators_past(1);
2329 return *this;
2330}
2331
2332template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002333basic_string<_CharT, _Traits, _Allocator>&
2334basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2335{
Martijn Vels596e3de2020-02-26 15:55:49 -05002336 if (this != &__str) {
2337 __copy_assign_alloc(__str);
2338 if (!__is_long()) {
2339 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002340 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002341 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002342 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002343 }
2344 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002345 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002346 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002347 }
2348 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002349}
2350
Eric Fiselierfc92be82017-04-19 00:28:44 +00002351#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002352
2353template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002354inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002355void
2356basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002357 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002358{
2359 if (__alloc() != __str.__alloc())
2360 assign(__str);
2361 else
2362 __move_assign(__str, true_type());
2363}
2364
2365template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002366inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002367void
2368basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002369#if _LIBCPP_STD_VER > 14
2370 _NOEXCEPT
2371#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002372 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002373#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002374{
marshall2ed41622019-11-27 07:13:00 -08002375 if (__is_long()) {
2376 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2377 __get_long_cap());
2378#if _LIBCPP_STD_VER <= 14
2379 if (!is_nothrow_move_assignable<allocator_type>::value) {
2380 __set_short_size(0);
2381 traits_type::assign(__get_short_pointer()[0], value_type());
2382 }
2383#endif
2384 }
2385 __move_assign_alloc(__str);
2386 __r_.first() = __str.__r_.first();
2387 __str.__set_short_size(0);
2388 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002389}
2390
2391template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002392inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002393basic_string<_CharT, _Traits, _Allocator>&
2394basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002395 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002396{
2397 __move_assign(__str, integral_constant<bool,
2398 __alloc_traits::propagate_on_container_move_assignment::value>());
2399 return *this;
2400}
2401
2402#endif
2403
2404template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002406_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002408 __is_exactly_cpp17_input_iterator <_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002409 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002411>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2413{
Marshall Clow958362f2016-09-05 01:54:30 +00002414 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002415 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002416 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417}
2418
2419template <class _CharT, class _Traits, class _Allocator>
2420template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002421_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002423 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002424 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002426>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2428{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002429 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430 size_type __cap = capacity();
2431 if (__cap < __n)
2432 {
2433 size_type __sz = size();
2434 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2435 }
2436 else
2437 __invalidate_iterators_past(__n);
2438 pointer __p = __get_pointer();
2439 for (; __first != __last; ++__first, ++__p)
2440 traits_type::assign(*__p, *__first);
2441 traits_type::assign(*__p, value_type());
2442 __set_size(__n);
2443 return *this;
2444}
2445
2446template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447basic_string<_CharT, _Traits, _Allocator>&
2448basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2449{
2450 size_type __sz = __str.size();
2451 if (__pos > __sz)
2452 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002453 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454}
2455
2456template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002457template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002458_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002459<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002460 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2461 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002462 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002463>
Marshall Clow82513342016-09-24 22:45:42 +00002464basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002465{
Marshall Clow82513342016-09-24 22:45:42 +00002466 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002467 size_type __sz = __sv.size();
2468 if (__pos > __sz)
2469 this->__throw_out_of_range();
2470 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2471}
2472
2473
2474template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002476basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002478 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479 return assign(__s, traits_type::length(__s));
2480}
2481
2482// append
2483
2484template <class _CharT, class _Traits, class _Allocator>
2485basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002486basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002488 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 size_type __cap = capacity();
2490 size_type __sz = size();
2491 if (__cap - __sz >= __n)
2492 {
2493 if (__n)
2494 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002495 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496 traits_type::copy(__p + __sz, __s, __n);
2497 __sz += __n;
2498 __set_size(__sz);
2499 traits_type::assign(__p[__sz], value_type());
2500 }
2501 }
2502 else
2503 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2504 return *this;
2505}
2506
2507template <class _CharT, class _Traits, class _Allocator>
2508basic_string<_CharT, _Traits, _Allocator>&
2509basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2510{
2511 if (__n)
2512 {
2513 size_type __cap = capacity();
2514 size_type __sz = size();
2515 if (__cap - __sz < __n)
2516 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2517 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002518 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519 __sz += __n;
2520 __set_size(__sz);
2521 traits_type::assign(__p[__sz], value_type());
2522 }
2523 return *this;
2524}
2525
2526template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002527inline void
2528basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2529{
2530 if (__n)
2531 {
2532 size_type __cap = capacity();
2533 size_type __sz = size();
2534 if (__cap - __sz < __n)
2535 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2536 pointer __p = __get_pointer();
2537 __sz += __n;
2538 __set_size(__sz);
2539 traits_type::assign(__p[__sz], value_type());
2540 }
2541}
2542
2543template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544void
2545basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2546{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002547 bool __is_short = !__is_long();
2548 size_type __cap;
2549 size_type __sz;
2550 if (__is_short)
2551 {
2552 __cap = __min_cap - 1;
2553 __sz = __get_short_size();
2554 }
2555 else
2556 {
2557 __cap = __get_long_cap() - 1;
2558 __sz = __get_long_size();
2559 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002561 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002563 __is_short = !__is_long();
2564 }
2565 pointer __p;
2566 if (__is_short)
2567 {
2568 __p = __get_short_pointer() + __sz;
2569 __set_short_size(__sz+1);
2570 }
2571 else
2572 {
2573 __p = __get_long_pointer() + __sz;
2574 __set_long_size(__sz+1);
2575 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576 traits_type::assign(*__p, __c);
2577 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578}
2579
Marshall Clow6ebbf662016-09-07 03:32:06 +00002580template <class _Tp>
Marshall Clow958362f2016-09-05 01:54:30 +00002581bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2582{
2583 return __first <= __p && __p < __last;
2584}
2585
Marshall Clow6ebbf662016-09-07 03:32:06 +00002586template <class _Tp1, class _Tp2>
Eric Fiselier6003c772016-12-23 23:37:52 +00002587bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clow6ebbf662016-09-07 03:32:06 +00002588{
2589 return false;
2590}
2591
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592template <class _CharT, class _Traits, class _Allocator>
2593template<class _ForwardIterator>
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002594basic_string<_CharT, _Traits, _Allocator>&
2595basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2596 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597{
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002598 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002599 "function requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600 size_type __sz = size();
2601 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002602 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603 if (__n)
2604 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002605 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2606 _CharRef __tmp_ref = *__first;
2607 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002608 {
2609 const basic_string __temp (__first, __last, __alloc());
2610 append(__temp.data(), __temp.size());
2611 }
Louis Dionne173f29e2019-05-29 16:01:36 +00002612 else
Marshall Clow958362f2016-09-05 01:54:30 +00002613 {
2614 if (__cap - __sz < __n)
2615 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2616 pointer __p = __get_pointer() + __sz;
2617 for (; __first != __last; ++__p, ++__first)
2618 traits_type::assign(*__p, *__first);
2619 traits_type::assign(*__p, value_type());
2620 __set_size(__sz + __n);
2621 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 }
2623 return *this;
2624}
2625
2626template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002627inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628basic_string<_CharT, _Traits, _Allocator>&
2629basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2630{
2631 return append(__str.data(), __str.size());
2632}
2633
2634template <class _CharT, class _Traits, class _Allocator>
2635basic_string<_CharT, _Traits, _Allocator>&
2636basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2637{
2638 size_type __sz = __str.size();
2639 if (__pos > __sz)
2640 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002641 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642}
2643
2644template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002645template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002646 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002647 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002648 __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 +00002649 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002650 >
Marshall Clow82513342016-09-24 22:45:42 +00002651basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002652{
Marshall Clow82513342016-09-24 22:45:42 +00002653 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002654 size_type __sz = __sv.size();
2655 if (__pos > __sz)
2656 this->__throw_out_of_range();
2657 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2658}
2659
2660template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002662basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002664 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665 return append(__s, traits_type::length(__s));
2666}
2667
2668// insert
2669
2670template <class _CharT, class _Traits, class _Allocator>
2671basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002672basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002674 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675 size_type __sz = size();
2676 if (__pos > __sz)
2677 this->__throw_out_of_range();
2678 size_type __cap = capacity();
2679 if (__cap - __sz >= __n)
2680 {
2681 if (__n)
2682 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002683 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684 size_type __n_move = __sz - __pos;
2685 if (__n_move != 0)
2686 {
2687 if (__p + __pos <= __s && __s < __p + __sz)
2688 __s += __n;
2689 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2690 }
2691 traits_type::move(__p + __pos, __s, __n);
2692 __sz += __n;
2693 __set_size(__sz);
2694 traits_type::assign(__p[__sz], value_type());
2695 }
2696 }
2697 else
2698 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2699 return *this;
2700}
2701
2702template <class _CharT, class _Traits, class _Allocator>
2703basic_string<_CharT, _Traits, _Allocator>&
2704basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2705{
2706 size_type __sz = size();
2707 if (__pos > __sz)
2708 this->__throw_out_of_range();
2709 if (__n)
2710 {
2711 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002712 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713 if (__cap - __sz >= __n)
2714 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002715 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716 size_type __n_move = __sz - __pos;
2717 if (__n_move != 0)
2718 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2719 }
2720 else
2721 {
2722 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002723 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724 }
2725 traits_type::assign(__p + __pos, __n, __c);
2726 __sz += __n;
2727 __set_size(__sz);
2728 traits_type::assign(__p[__sz], value_type());
2729 }
2730 return *this;
2731}
2732
2733template <class _CharT, class _Traits, class _Allocator>
2734template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002735_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002737 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002738 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2739 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002740>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2742{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002743#if _LIBCPP_DEBUG_LEVEL >= 2
2744 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2745 "string::insert(iterator, range) called with an iterator not"
2746 " referring to this string");
2747#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002748 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002749 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750}
2751
2752template <class _CharT, class _Traits, class _Allocator>
2753template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002754_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002756 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002757 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002759>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2761{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002762#if _LIBCPP_DEBUG_LEVEL >= 2
2763 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2764 "string::insert(iterator, range) called with an iterator not"
2765 " referring to this string");
2766#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002768 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769 if (__n)
2770 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002771 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2772 _CharRef __tmp_char = *__first;
2773 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002774 {
2775 const basic_string __temp(__first, __last, __alloc());
2776 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2777 }
2778
2779 size_type __sz = size();
2780 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002781 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 if (__cap - __sz >= __n)
2783 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002784 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785 size_type __n_move = __sz - __ip;
2786 if (__n_move != 0)
2787 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2788 }
2789 else
2790 {
2791 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002792 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 }
2794 __sz += __n;
2795 __set_size(__sz);
2796 traits_type::assign(__p[__sz], value_type());
2797 for (__p += __ip; __first != __last; ++__p, ++__first)
2798 traits_type::assign(*__p, *__first);
2799 }
2800 return begin() + __ip;
2801}
2802
2803template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002804inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805basic_string<_CharT, _Traits, _Allocator>&
2806basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2807{
2808 return insert(__pos1, __str.data(), __str.size());
2809}
2810
2811template <class _CharT, class _Traits, class _Allocator>
2812basic_string<_CharT, _Traits, _Allocator>&
2813basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2814 size_type __pos2, size_type __n)
2815{
2816 size_type __str_sz = __str.size();
2817 if (__pos2 > __str_sz)
2818 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002819 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820}
2821
2822template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002823template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002824_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002825<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002826 __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 +00002827 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002828>
Marshall Clow82513342016-09-24 22:45:42 +00002829basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002830 size_type __pos2, size_type __n)
2831{
Marshall Clow82513342016-09-24 22:45:42 +00002832 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002833 size_type __str_sz = __sv.size();
2834 if (__pos2 > __str_sz)
2835 this->__throw_out_of_range();
2836 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2837}
2838
2839template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002841basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002843 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844 return insert(__pos, __s, traits_type::length(__s));
2845}
2846
2847template <class _CharT, class _Traits, class _Allocator>
2848typename basic_string<_CharT, _Traits, _Allocator>::iterator
2849basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2850{
2851 size_type __ip = static_cast<size_type>(__pos - begin());
2852 size_type __sz = size();
2853 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002854 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 if (__cap == __sz)
2856 {
2857 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002858 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859 }
2860 else
2861 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002862 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 size_type __n_move = __sz - __ip;
2864 if (__n_move != 0)
2865 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2866 }
2867 traits_type::assign(__p[__ip], __c);
2868 traits_type::assign(__p[++__sz], value_type());
2869 __set_size(__sz);
2870 return begin() + static_cast<difference_type>(__ip);
2871}
2872
2873template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002874inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875typename basic_string<_CharT, _Traits, _Allocator>::iterator
2876basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2877{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002878#if _LIBCPP_DEBUG_LEVEL >= 2
2879 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2880 "string::insert(iterator, n, value) called with an iterator not"
2881 " referring to this string");
2882#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883 difference_type __p = __pos - begin();
2884 insert(static_cast<size_type>(__p), __n, __c);
2885 return begin() + __p;
2886}
2887
2888// replace
2889
2890template <class _CharT, class _Traits, class _Allocator>
2891basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002892basic_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 +00002893 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002895 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896 size_type __sz = size();
2897 if (__pos > __sz)
2898 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002899 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900 size_type __cap = capacity();
2901 if (__cap - __sz + __n1 >= __n2)
2902 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002903 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904 if (__n1 != __n2)
2905 {
2906 size_type __n_move = __sz - __pos - __n1;
2907 if (__n_move != 0)
2908 {
2909 if (__n1 > __n2)
2910 {
2911 traits_type::move(__p + __pos, __s, __n2);
2912 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2913 goto __finish;
2914 }
2915 if (__p + __pos < __s && __s < __p + __sz)
2916 {
2917 if (__p + __pos + __n1 <= __s)
2918 __s += __n2 - __n1;
2919 else // __p + __pos < __s < __p + __pos + __n1
2920 {
2921 traits_type::move(__p + __pos, __s, __n1);
2922 __pos += __n1;
2923 __s += __n2;
2924 __n2 -= __n1;
2925 __n1 = 0;
2926 }
2927 }
2928 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2929 }
2930 }
2931 traits_type::move(__p + __pos, __s, __n2);
2932__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05002933// __sz += __n2 - __n1; in this and the below function below can cause unsigned
2934// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935 __sz += __n2 - __n1;
2936 __set_size(__sz);
2937 __invalidate_iterators_past(__sz);
2938 traits_type::assign(__p[__sz], value_type());
2939 }
2940 else
2941 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2942 return *this;
2943}
2944
2945template <class _CharT, class _Traits, class _Allocator>
2946basic_string<_CharT, _Traits, _Allocator>&
2947basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00002948 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949{
2950 size_type __sz = size();
2951 if (__pos > __sz)
2952 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002953 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002955 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956 if (__cap - __sz + __n1 >= __n2)
2957 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002958 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959 if (__n1 != __n2)
2960 {
2961 size_type __n_move = __sz - __pos - __n1;
2962 if (__n_move != 0)
2963 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2964 }
2965 }
2966 else
2967 {
2968 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002969 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970 }
2971 traits_type::assign(__p + __pos, __n2, __c);
2972 __sz += __n2 - __n1;
2973 __set_size(__sz);
2974 __invalidate_iterators_past(__sz);
2975 traits_type::assign(__p[__sz], value_type());
2976 return *this;
2977}
2978
2979template <class _CharT, class _Traits, class _Allocator>
2980template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002981_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002983 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002985>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002986basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 _InputIterator __j1, _InputIterator __j2)
2988{
Marshall Clow958362f2016-09-05 01:54:30 +00002989 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002990 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991}
2992
2993template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002994inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995basic_string<_CharT, _Traits, _Allocator>&
2996basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2997{
2998 return replace(__pos1, __n1, __str.data(), __str.size());
2999}
3000
3001template <class _CharT, class _Traits, class _Allocator>
3002basic_string<_CharT, _Traits, _Allocator>&
3003basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3004 size_type __pos2, size_type __n2)
3005{
3006 size_type __str_sz = __str.size();
3007 if (__pos2 > __str_sz)
3008 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003009 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010}
3011
3012template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003013template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003014_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003015<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003016 __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 +00003017 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003018>
Marshall Clow82513342016-09-24 22:45:42 +00003019basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003020 size_type __pos2, size_type __n2)
3021{
Marshall Clow82513342016-09-24 22:45:42 +00003022 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003023 size_type __str_sz = __sv.size();
3024 if (__pos2 > __str_sz)
3025 this->__throw_out_of_range();
3026 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3027}
3028
3029template <class _CharT, class _Traits, class _Allocator>
3030basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003031basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003033 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034 return replace(__pos, __n1, __s, traits_type::length(__s));
3035}
3036
3037template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003038inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003040basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041{
3042 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3043 __str.data(), __str.size());
3044}
3045
3046template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003047inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003049basic_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 +00003050{
3051 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3052}
3053
3054template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003055inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003057basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058{
3059 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3060}
3061
3062template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003063inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003065basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066{
3067 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3068}
3069
3070// erase
3071
Martijn Velsa81fc792020-02-26 13:25:43 -05003072// 'externally instantiated' erase() implementation, called when __n != npos.
3073// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003075void
3076basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3077 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079 if (__n)
3080 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003081 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003082 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003083 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084 size_type __n_move = __sz - __pos - __n;
3085 if (__n_move != 0)
3086 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3087 __sz -= __n;
3088 __set_size(__sz);
3089 __invalidate_iterators_past(__sz);
3090 traits_type::assign(__p[__sz], value_type());
3091 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003092}
3093
3094template <class _CharT, class _Traits, class _Allocator>
3095basic_string<_CharT, _Traits, _Allocator>&
3096basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3097 size_type __n) {
3098 if (__pos > size()) this->__throw_out_of_range();
3099 if (__n == npos) {
3100 __erase_to_end(__pos);
3101 } else {
3102 __erase_external_with_move(__pos, __n);
3103 }
3104 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105}
3106
3107template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003108inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109typename basic_string<_CharT, _Traits, _Allocator>::iterator
3110basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3111{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003112#if _LIBCPP_DEBUG_LEVEL >= 2
3113 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3114 "string::erase(iterator) called with an iterator not"
3115 " referring to this string");
3116#endif
3117 _LIBCPP_ASSERT(__pos != end(),
3118 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119 iterator __b = begin();
3120 size_type __r = static_cast<size_type>(__pos - __b);
3121 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003122 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123}
3124
3125template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003126inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127typename basic_string<_CharT, _Traits, _Allocator>::iterator
3128basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3129{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003130#if _LIBCPP_DEBUG_LEVEL >= 2
3131 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3132 "string::erase(iterator, iterator) called with an iterator not"
3133 " referring to this string");
3134#endif
3135 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136 iterator __b = begin();
3137 size_type __r = static_cast<size_type>(__first - __b);
3138 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003139 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140}
3141
3142template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003143inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144void
3145basic_string<_CharT, _Traits, _Allocator>::pop_back()
3146{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003147 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148 size_type __sz;
3149 if (__is_long())
3150 {
3151 __sz = __get_long_size() - 1;
3152 __set_long_size(__sz);
3153 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3154 }
3155 else
3156 {
3157 __sz = __get_short_size() - 1;
3158 __set_short_size(__sz);
3159 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3160 }
3161 __invalidate_iterators_past(__sz);
3162}
3163
3164template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003165inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003167basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168{
3169 __invalidate_all_iterators();
3170 if (__is_long())
3171 {
3172 traits_type::assign(*__get_long_pointer(), value_type());
3173 __set_long_size(0);
3174 }
3175 else
3176 {
3177 traits_type::assign(*__get_short_pointer(), value_type());
3178 __set_short_size(0);
3179 }
3180}
3181
3182template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003183inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184void
3185basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3186{
3187 if (__is_long())
3188 {
3189 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3190 __set_long_size(__pos);
3191 }
3192 else
3193 {
3194 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3195 __set_short_size(__pos);
3196 }
3197 __invalidate_iterators_past(__pos);
3198}
3199
3200template <class _CharT, class _Traits, class _Allocator>
3201void
3202basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3203{
3204 size_type __sz = size();
3205 if (__n > __sz)
3206 append(__n - __sz, __c);
3207 else
3208 __erase_to_end(__n);
3209}
3210
3211template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003212inline void
3213basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3214{
3215 size_type __sz = size();
3216 if (__n > __sz) {
3217 __append_default_init(__n - __sz);
3218 } else
3219 __erase_to_end(__n);
3220}
3221
3222template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003223inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003225basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003227 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003228#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003229 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003231 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232#endif
3233}
3234
3235template <class _CharT, class _Traits, class _Allocator>
3236void
3237basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
3238{
3239 if (__res_arg > max_size())
3240 this->__throw_length_error();
3241 size_type __cap = capacity();
3242 size_type __sz = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003243 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003244 __res_arg = __recommend(__res_arg);
3245 if (__res_arg != __cap)
3246 {
3247 pointer __new_data, __p;
3248 bool __was_long, __now_long;
3249 if (__res_arg == __min_cap - 1)
3250 {
3251 __was_long = true;
3252 __now_long = false;
3253 __new_data = __get_short_pointer();
3254 __p = __get_long_pointer();
3255 }
3256 else
3257 {
3258 if (__res_arg > __cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003259 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260 else
3261 {
3262 #ifndef _LIBCPP_NO_EXCEPTIONS
3263 try
3264 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003265 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003266 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267 #ifndef _LIBCPP_NO_EXCEPTIONS
3268 }
3269 catch (...)
3270 {
3271 return;
3272 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003273 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd17880b2013-06-28 16:59:19 +00003274 if (__new_data == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275 return;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003276 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277 }
3278 __now_long = true;
3279 __was_long = __is_long();
3280 __p = __get_pointer();
3281 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003282 traits_type::copy(_VSTD::__to_address(__new_data),
3283 _VSTD::__to_address(__p), size()+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003284 if (__was_long)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003285 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286 if (__now_long)
3287 {
3288 __set_long_cap(__res_arg+1);
3289 __set_long_size(__sz);
3290 __set_long_pointer(__new_data);
3291 }
3292 else
3293 __set_short_size(__sz);
3294 __invalidate_all_iterators();
3295 }
3296}
3297
3298template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003299inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003301basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003303 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304 return *(data() + __pos);
3305}
3306
3307template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003308inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003310basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003312 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003313 return *(__get_pointer() + __pos);
3314}
3315
3316template <class _CharT, class _Traits, class _Allocator>
3317typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3318basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3319{
3320 if (__n >= size())
3321 this->__throw_out_of_range();
3322 return (*this)[__n];
3323}
3324
3325template <class _CharT, class _Traits, class _Allocator>
3326typename basic_string<_CharT, _Traits, _Allocator>::reference
3327basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3328{
3329 if (__n >= size())
3330 this->__throw_out_of_range();
3331 return (*this)[__n];
3332}
3333
3334template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003335inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003336typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003337basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003339 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340 return *__get_pointer();
3341}
3342
3343template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003344inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003346basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003348 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349 return *data();
3350}
3351
3352template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003353inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003355basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003357 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358 return *(__get_pointer() + size() - 1);
3359}
3360
3361template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003362inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003364basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003366 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367 return *(data() + size() - 1);
3368}
3369
3370template <class _CharT, class _Traits, class _Allocator>
3371typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003372basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373{
3374 size_type __sz = size();
3375 if (__pos > __sz)
3376 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003377 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378 traits_type::copy(__s, data() + __pos, __rlen);
3379 return __rlen;
3380}
3381
3382template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003383inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384basic_string<_CharT, _Traits, _Allocator>
3385basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3386{
3387 return basic_string(*this, __pos, __n, __alloc());
3388}
3389
3390template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003391inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392void
3393basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003394#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003395 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003396#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003397 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003398 __is_nothrow_swappable<allocator_type>::value)
3399#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003401#if _LIBCPP_DEBUG_LEVEL >= 2
3402 if (!__is_long())
3403 __get_db()->__invalidate_all(this);
3404 if (!__str.__is_long())
3405 __get_db()->__invalidate_all(&__str);
3406 __get_db()->swap(this, &__str);
3407#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003408 _LIBCPP_ASSERT(
3409 __alloc_traits::propagate_on_container_swap::value ||
3410 __alloc_traits::is_always_equal::value ||
3411 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003412 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow8982dcd2015-07-13 20:04:56 +00003413 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414}
3415
3416// find
3417
3418template <class _Traits>
3419struct _LIBCPP_HIDDEN __traits_eq
3420{
3421 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003422 _LIBCPP_INLINE_VISIBILITY
3423 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3424 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425};
3426
3427template<class _CharT, class _Traits, class _Allocator>
3428typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003429basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003430 size_type __pos,
3431 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003433 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003434 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003435 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436}
3437
3438template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003439inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003441basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3442 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003444 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003445 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446}
3447
3448template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003449template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003450_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003451<
3452 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3453 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003454>
Marshall Clowe46031a2018-07-02 18:41:15 +00003455basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
3456 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003457{
Marshall Clowe46031a2018-07-02 18:41:15 +00003458 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003459 return __str_find<value_type, size_type, traits_type, npos>
3460 (data(), size(), __sv.data(), __pos, __sv.size());
3461}
3462
3463template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003464inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003465typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003466basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003467 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003469 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003470 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003471 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472}
3473
3474template<class _CharT, class _Traits, class _Allocator>
3475typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003476basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3477 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003478{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003479 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003480 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481}
3482
3483// rfind
3484
3485template<class _CharT, class _Traits, class _Allocator>
3486typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003487basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003488 size_type __pos,
3489 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003491 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003492 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003493 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494}
3495
3496template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003497inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003499basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3500 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003502 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003503 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504}
3505
3506template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003507template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003508_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003509<
3510 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3511 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003512>
Marshall Clowe46031a2018-07-02 18:41:15 +00003513basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
3514 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003515{
Marshall Clowe46031a2018-07-02 18:41:15 +00003516 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003517 return __str_rfind<value_type, size_type, traits_type, npos>
3518 (data(), size(), __sv.data(), __pos, __sv.size());
3519}
3520
3521template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003522inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003523typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003524basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003525 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003527 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003528 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003529 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530}
3531
3532template<class _CharT, class _Traits, class _Allocator>
3533typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003534basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3535 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003537 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003538 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539}
3540
3541// find_first_of
3542
3543template<class _CharT, class _Traits, class _Allocator>
3544typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003545basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003546 size_type __pos,
3547 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003549 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003550 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003551 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552}
3553
3554template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003555inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003557basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3558 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003560 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003561 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562}
3563
3564template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003565template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003566_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003567<
3568 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3569 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003570>
Marshall Clowe46031a2018-07-02 18:41:15 +00003571basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
3572 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003573{
Marshall Clowe46031a2018-07-02 18:41:15 +00003574 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003575 return __str_find_first_of<value_type, size_type, traits_type, npos>
3576 (data(), size(), __sv.data(), __pos, __sv.size());
3577}
3578
3579template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003580inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003581typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003582basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003583 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003585 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003586 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003587 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588}
3589
3590template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003591inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003593basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3594 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595{
3596 return find(__c, __pos);
3597}
3598
3599// find_last_of
3600
3601template<class _CharT, class _Traits, class _Allocator>
3602typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003603basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003604 size_type __pos,
3605 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003607 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003608 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003609 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610}
3611
3612template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003613inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003615basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3616 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003618 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003619 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620}
3621
3622template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003623template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003624_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003625<
3626 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3627 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003628>
Marshall Clowe46031a2018-07-02 18:41:15 +00003629basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
3630 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003631{
Marshall Clowe46031a2018-07-02 18:41:15 +00003632 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003633 return __str_find_last_of<value_type, size_type, traits_type, npos>
3634 (data(), size(), __sv.data(), __pos, __sv.size());
3635}
3636
3637template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003638inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003639typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003640basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003641 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003643 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003644 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003645 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646}
3647
3648template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003649inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003651basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3652 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653{
3654 return rfind(__c, __pos);
3655}
3656
3657// find_first_not_of
3658
3659template<class _CharT, class _Traits, class _Allocator>
3660typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003661basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003662 size_type __pos,
3663 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003665 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003666 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003667 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668}
3669
3670template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003671inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003673basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3674 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003676 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003677 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678}
3679
3680template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003681template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003682_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003683<
3684 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3685 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003686>
Marshall Clowe46031a2018-07-02 18:41:15 +00003687basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
3688 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003689{
Marshall Clowe46031a2018-07-02 18:41:15 +00003690 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003691 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3692 (data(), size(), __sv.data(), __pos, __sv.size());
3693}
3694
3695template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003696inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003697typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003698basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003699 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003701 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003703 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704}
3705
3706template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003707inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003709basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3710 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003712 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003713 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714}
3715
3716// find_last_not_of
3717
3718template<class _CharT, class _Traits, class _Allocator>
3719typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003720basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003721 size_type __pos,
3722 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003724 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003725 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003726 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727}
3728
3729template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003730inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003731typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003732basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3733 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003735 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003736 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737}
3738
3739template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003740template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003741_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003742<
3743 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3744 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003745>
Marshall Clowe46031a2018-07-02 18:41:15 +00003746basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
3747 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003748{
Marshall Clowe46031a2018-07-02 18:41:15 +00003749 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003750 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3751 (data(), size(), __sv.data(), __pos, __sv.size());
3752}
3753
3754template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003755inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003756typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003757basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003758 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003760 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003761 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003762 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763}
3764
3765template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003766inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003768basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3769 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003771 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003772 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773}
3774
3775// compare
3776
3777template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003778template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003779_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003780<
3781 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3782 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003783>
Marshall Clowe46031a2018-07-02 18:41:15 +00003784basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785{
Marshall Clowe46031a2018-07-02 18:41:15 +00003786 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003787 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003788 size_t __rhs_sz = __sv.size();
3789 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003790 _VSTD::min(__lhs_sz, __rhs_sz));
3791 if (__result != 0)
3792 return __result;
3793 if (__lhs_sz < __rhs_sz)
3794 return -1;
3795 if (__lhs_sz > __rhs_sz)
3796 return 1;
3797 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798}
3799
3800template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003801inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003803basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003805 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806}
3807
3808template <class _CharT, class _Traits, class _Allocator>
3809int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003810basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3811 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003812 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003813 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003815 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816 size_type __sz = size();
3817 if (__pos1 > __sz || __n2 == npos)
3818 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003819 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3820 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821 if (__r == 0)
3822 {
3823 if (__rlen < __n2)
3824 __r = -1;
3825 else if (__rlen > __n2)
3826 __r = 1;
3827 }
3828 return __r;
3829}
3830
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003831template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003832template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003833_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003834<
3835 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3836 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003837>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003838basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3839 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003840 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003841{
Marshall Clowe46031a2018-07-02 18:41:15 +00003842 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003843 return compare(__pos1, __n1, __sv.data(), __sv.size());
3844}
3845
3846template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003847inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003848int
3849basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3850 size_type __n1,
3851 const basic_string& __str) const
3852{
3853 return compare(__pos1, __n1, __str.data(), __str.size());
3854}
3855
3856template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003857template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003858_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003859<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003860 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3861 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003862 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003863>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003864basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3865 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003866 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003867 size_type __pos2,
3868 size_type __n2) const
3869{
Marshall Clow82513342016-09-24 22:45:42 +00003870 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003871 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3872}
3873
3874template <class _CharT, class _Traits, class _Allocator>
3875int
3876basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3877 size_type __n1,
3878 const basic_string& __str,
3879 size_type __pos2,
3880 size_type __n2) const
3881{
3882 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3883}
3884
3885template <class _CharT, class _Traits, class _Allocator>
3886int
3887basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3888{
3889 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3890 return compare(0, npos, __s, traits_type::length(__s));
3891}
3892
3893template <class _CharT, class _Traits, class _Allocator>
3894int
3895basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3896 size_type __n1,
3897 const value_type* __s) const
3898{
3899 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3900 return compare(__pos1, __n1, __s, traits_type::length(__s));
3901}
3902
Howard Hinnantc51e1022010-05-11 19:42:16 +00003903// __invariants
3904
3905template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003906inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907bool
3908basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3909{
3910 if (size() > capacity())
3911 return false;
3912 if (capacity() < __min_cap - 1)
3913 return false;
3914 if (data() == 0)
3915 return false;
3916 if (data()[size()] != value_type(0))
3917 return false;
3918 return true;
3919}
3920
Vedant Kumar55e007e2018-03-08 21:15:26 +00003921// __clear_and_shrink
3922
3923template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003924inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003925void
Marshall Clowe60a7182018-05-29 17:04:37 +00003926basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003927{
3928 clear();
3929 if(__is_long())
3930 {
3931 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3932 __set_long_cap(0);
3933 __set_short_size(0);
3934 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003935}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003936
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937// operator==
3938
3939template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003940inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003941bool
3942operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003943 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003945 size_t __lhs_sz = __lhs.size();
3946 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3947 __rhs.data(),
3948 __lhs_sz) == 0;
3949}
3950
3951template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003952inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003953bool
3954operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3955 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3956{
3957 size_t __lhs_sz = __lhs.size();
3958 if (__lhs_sz != __rhs.size())
3959 return false;
3960 const char* __lp = __lhs.data();
3961 const char* __rp = __rhs.data();
3962 if (__lhs.__is_long())
3963 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3964 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3965 if (*__lp != *__rp)
3966 return false;
3967 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968}
3969
3970template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003971inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003973operator==(const _CharT* __lhs,
3974 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003976 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3977 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3978 size_t __lhs_len = _Traits::length(__lhs);
3979 if (__lhs_len != __rhs.size()) return false;
3980 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981}
3982
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003984inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003986operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3987 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003988{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003989 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3990 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3991 size_t __rhs_len = _Traits::length(__rhs);
3992 if (__rhs_len != __lhs.size()) return false;
3993 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003994}
3995
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003996template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003997inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998bool
3999operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004000 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004001{
4002 return !(__lhs == __rhs);
4003}
4004
4005template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004006inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004008operator!=(const _CharT* __lhs,
4009 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004010{
4011 return !(__lhs == __rhs);
4012}
4013
4014template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004015inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004017operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4018 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019{
4020 return !(__lhs == __rhs);
4021}
4022
4023// operator<
4024
4025template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004026inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027bool
4028operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004029 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004031 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004032}
4033
4034template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004035inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004037operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4038 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004040 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041}
4042
4043template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004044inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004046operator< (const _CharT* __lhs,
4047 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048{
4049 return __rhs.compare(__lhs) > 0;
4050}
4051
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052// operator>
4053
4054template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004055inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056bool
4057operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004058 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059{
4060 return __rhs < __lhs;
4061}
4062
4063template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004064inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004066operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4067 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004068{
4069 return __rhs < __lhs;
4070}
4071
4072template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004073inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004075operator> (const _CharT* __lhs,
4076 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077{
4078 return __rhs < __lhs;
4079}
4080
4081// operator<=
4082
4083template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004084inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085bool
4086operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004087 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088{
4089 return !(__rhs < __lhs);
4090}
4091
4092template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004093inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004094bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004095operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4096 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097{
4098 return !(__rhs < __lhs);
4099}
4100
4101template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004102inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004104operator<=(const _CharT* __lhs,
4105 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106{
4107 return !(__rhs < __lhs);
4108}
4109
4110// operator>=
4111
4112template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004113inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114bool
4115operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004116 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117{
4118 return !(__lhs < __rhs);
4119}
4120
4121template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004122inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004124operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4125 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126{
4127 return !(__lhs < __rhs);
4128}
4129
4130template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004131inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004133operator>=(const _CharT* __lhs,
4134 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135{
4136 return !(__lhs < __rhs);
4137}
4138
4139// operator +
4140
4141template<class _CharT, class _Traits, class _Allocator>
4142basic_string<_CharT, _Traits, _Allocator>
4143operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4144 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4145{
4146 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4147 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4148 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4149 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4150 __r.append(__rhs.data(), __rhs_sz);
4151 return __r;
4152}
4153
4154template<class _CharT, class _Traits, class _Allocator>
4155basic_string<_CharT, _Traits, _Allocator>
4156operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4157{
4158 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4159 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4160 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4161 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4162 __r.append(__rhs.data(), __rhs_sz);
4163 return __r;
4164}
4165
4166template<class _CharT, class _Traits, class _Allocator>
4167basic_string<_CharT, _Traits, _Allocator>
4168operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4169{
4170 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4171 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4172 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4173 __r.append(__rhs.data(), __rhs_sz);
4174 return __r;
4175}
4176
4177template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004178inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179basic_string<_CharT, _Traits, _Allocator>
4180operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4181{
4182 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4183 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4184 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4185 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4186 __r.append(__rhs, __rhs_sz);
4187 return __r;
4188}
4189
4190template<class _CharT, class _Traits, class _Allocator>
4191basic_string<_CharT, _Traits, _Allocator>
4192operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4193{
4194 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4195 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4196 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4197 __r.push_back(__rhs);
4198 return __r;
4199}
4200
Eric Fiselierfc92be82017-04-19 00:28:44 +00004201#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202
4203template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205basic_string<_CharT, _Traits, _Allocator>
4206operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4207{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004208 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209}
4210
4211template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004212inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213basic_string<_CharT, _Traits, _Allocator>
4214operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4215{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004216 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217}
4218
4219template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221basic_string<_CharT, _Traits, _Allocator>
4222operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4223{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004224 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225}
4226
4227template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229basic_string<_CharT, _Traits, _Allocator>
4230operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4231{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004232 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233}
4234
4235template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237basic_string<_CharT, _Traits, _Allocator>
4238operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4239{
4240 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004241 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242}
4243
4244template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246basic_string<_CharT, _Traits, _Allocator>
4247operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4248{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004249 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004250}
4251
4252template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004253inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254basic_string<_CharT, _Traits, _Allocator>
4255operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4256{
4257 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004258 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259}
4260
Eric Fiselierfc92be82017-04-19 00:28:44 +00004261#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004262
4263// swap
4264
4265template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004266inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004268swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004269 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4270 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271{
4272 __lhs.swap(__rhs);
4273}
4274
Marshall Clow8732fed2018-12-11 04:35:44 +00004275#ifndef _LIBCPP_NO_HAS_CHAR8_T
4276typedef basic_string<char8_t> u8string;
4277#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278
Marshall Clow8732fed2018-12-11 04:35:44 +00004279#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280typedef basic_string<char16_t> u16string;
4281typedef basic_string<char32_t> u32string;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004282#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004284_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
4285_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
4286_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
4287_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
4288_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004289
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004290_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
4291_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
4292_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004293
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004294_LIBCPP_FUNC_VIS string to_string(int __val);
4295_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4296_LIBCPP_FUNC_VIS string to_string(long __val);
4297_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4298_LIBCPP_FUNC_VIS string to_string(long long __val);
4299_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4300_LIBCPP_FUNC_VIS string to_string(float __val);
4301_LIBCPP_FUNC_VIS string to_string(double __val);
4302_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004303
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004304_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
4305_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
4306_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
4307_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
4308_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004309
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004310_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
4311_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
4312_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004313
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004314_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4315_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4316_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4317_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4318_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4319_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4320_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4321_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4322_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004323
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004325_LIBCPP_FUNC_VIS
4326const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4327 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004328
Marshall Clow851b9ec2019-05-20 21:56:51 +00004329template <class _CharT, class _Allocator>
4330struct _LIBCPP_TEMPLATE_VIS
4331 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4332 : public unary_function<
4333 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334{
4335 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004336 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4337 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338};
4339
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340
Howard Hinnantdc095972011-07-18 15:51:59 +00004341template<class _CharT, class _Traits, class _Allocator>
4342basic_ostream<_CharT, _Traits>&
4343operator<<(basic_ostream<_CharT, _Traits>& __os,
4344 const basic_string<_CharT, _Traits, _Allocator>& __str);
4345
4346template<class _CharT, class _Traits, class _Allocator>
4347basic_istream<_CharT, _Traits>&
4348operator>>(basic_istream<_CharT, _Traits>& __is,
4349 basic_string<_CharT, _Traits, _Allocator>& __str);
4350
4351template<class _CharT, class _Traits, class _Allocator>
4352basic_istream<_CharT, _Traits>&
4353getline(basic_istream<_CharT, _Traits>& __is,
4354 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4355
4356template<class _CharT, class _Traits, class _Allocator>
4357inline _LIBCPP_INLINE_VISIBILITY
4358basic_istream<_CharT, _Traits>&
4359getline(basic_istream<_CharT, _Traits>& __is,
4360 basic_string<_CharT, _Traits, _Allocator>& __str);
4361
Eric Fiselierfc92be82017-04-19 00:28:44 +00004362#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004363
4364template<class _CharT, class _Traits, class _Allocator>
4365inline _LIBCPP_INLINE_VISIBILITY
4366basic_istream<_CharT, _Traits>&
4367getline(basic_istream<_CharT, _Traits>&& __is,
4368 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4369
4370template<class _CharT, class _Traits, class _Allocator>
4371inline _LIBCPP_INLINE_VISIBILITY
4372basic_istream<_CharT, _Traits>&
4373getline(basic_istream<_CharT, _Traits>&& __is,
4374 basic_string<_CharT, _Traits, _Allocator>& __str);
4375
Eric Fiselierfc92be82017-04-19 00:28:44 +00004376#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004377
Marshall Clow29b53f22018-12-14 18:49:35 +00004378#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004379template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004380inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004381 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4382 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4383 auto __old_size = __str.size();
4384 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4385 return __old_size - __str.size();
4386}
Marshall Clow29b53f22018-12-14 18:49:35 +00004387
Marek Kurdeja98b1412020-05-02 13:58:03 +02004388template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004389inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004390 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4391 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4392 _Predicate __pred) {
4393 auto __old_size = __str.size();
4394 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4395 __str.end());
4396 return __old_size - __str.size();
4397}
Marshall Clow29b53f22018-12-14 18:49:35 +00004398#endif
4399
Howard Hinnant8ea98242013-08-23 17:37:05 +00004400#if _LIBCPP_DEBUG_LEVEL >= 2
4401
4402template<class _CharT, class _Traits, class _Allocator>
4403bool
4404basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4405{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004406 return this->data() <= _VSTD::__to_address(__i->base()) &&
4407 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004408}
4409
4410template<class _CharT, class _Traits, class _Allocator>
4411bool
4412basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4413{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004414 return this->data() < _VSTD::__to_address(__i->base()) &&
4415 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004416}
4417
4418template<class _CharT, class _Traits, class _Allocator>
4419bool
4420basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4421{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004422 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004423 return this->data() <= __p && __p <= this->data() + this->size();
4424}
4425
4426template<class _CharT, class _Traits, class _Allocator>
4427bool
4428basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4429{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004430 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004431 return this->data() <= __p && __p < this->data() + this->size();
4432}
4433
4434#endif // _LIBCPP_DEBUG_LEVEL >= 2
4435
Louis Dionne173f29e2019-05-29 16:01:36 +00004436#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004437// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004438inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004439{
4440 inline namespace string_literals
4441 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004442 inline _LIBCPP_INLINE_VISIBILITY
4443 basic_string<char> operator "" s( const char *__str, size_t __len )
4444 {
4445 return basic_string<char> (__str, __len);
4446 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004447
Howard Hinnant5c167562013-08-07 19:39:48 +00004448 inline _LIBCPP_INLINE_VISIBILITY
4449 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4450 {
4451 return basic_string<wchar_t> (__str, __len);
4452 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004453
Marshall Clow8732fed2018-12-11 04:35:44 +00004454#ifndef _LIBCPP_NO_HAS_CHAR8_T
4455 inline _LIBCPP_INLINE_VISIBILITY
4456 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4457 {
4458 return basic_string<char8_t> (__str, __len);
4459 }
4460#endif
4461
Howard Hinnant5c167562013-08-07 19:39:48 +00004462 inline _LIBCPP_INLINE_VISIBILITY
4463 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4464 {
4465 return basic_string<char16_t> (__str, __len);
4466 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004467
Howard Hinnant5c167562013-08-07 19:39:48 +00004468 inline _LIBCPP_INLINE_VISIBILITY
4469 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4470 {
4471 return basic_string<char32_t> (__str, __len);
4472 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004473 }
4474}
4475#endif
4476
Howard Hinnantc51e1022010-05-11 19:42:16 +00004477_LIBCPP_END_NAMESPACE_STD
4478
Eric Fiselierf4433a32017-05-31 22:07:49 +00004479_LIBCPP_POP_MACROS
4480
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481#endif // _LIBCPP_STRING