blob: d233369df1ab9489545cd25d57e830559a7ea4e5 [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>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
114 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000115 basic_string(InputIterator begin, InputIterator end,
116 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
118 basic_string(const basic_string&, const Allocator&);
119 basic_string(basic_string&&, const Allocator&);
120
121 ~basic_string();
122
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000123 operator basic_string_view<charT, traits>() const noexcept;
124
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000126 template <class T>
127 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000128 basic_string& operator=(basic_string&& str)
129 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000130 allocator_type::propagate_on_container_move_assignment::value ||
131 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000132 basic_string& operator=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133 basic_string& operator=(value_type c);
134 basic_string& operator=(initializer_list<value_type>);
135
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000136 iterator begin() noexcept;
137 const_iterator begin() const noexcept;
138 iterator end() noexcept;
139 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000141 reverse_iterator rbegin() noexcept;
142 const_reverse_iterator rbegin() const noexcept;
143 reverse_iterator rend() noexcept;
144 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000145
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000146 const_iterator cbegin() const noexcept;
147 const_iterator cend() const noexcept;
148 const_reverse_iterator crbegin() const noexcept;
149 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000151 size_type size() const noexcept;
152 size_type length() const noexcept;
153 size_type max_size() const noexcept;
154 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155
156 void resize(size_type n, value_type c);
157 void resize(size_type n);
158
Marek Kurdejc9848142020-11-26 10:07:16 +0100159 void reserve(size_type res_arg);
160 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000162 void clear() noexcept;
163 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164
165 const_reference operator[](size_type pos) const;
166 reference operator[](size_type pos);
167
168 const_reference at(size_type n) const;
169 reference at(size_type n);
170
171 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000172 template <class T>
173 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000174 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175 basic_string& operator+=(value_type c);
176 basic_string& operator+=(initializer_list<value_type>);
177
178 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000179 template <class T>
180 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000181 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000182 template <class T>
183 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000184 basic_string& append(const value_type* s, size_type n);
185 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000187 template<class InputIterator>
188 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189 basic_string& append(initializer_list<value_type>);
190
191 void push_back(value_type c);
192 void pop_back();
193 reference front();
194 const_reference front() const;
195 reference back();
196 const_reference back() const;
197
198 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000199 template <class T>
200 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000201 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000202 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000203 template <class T>
204 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000205 basic_string& assign(const value_type* s, size_type n);
206 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000208 template<class InputIterator>
209 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210 basic_string& assign(initializer_list<value_type>);
211
212 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000213 template <class T>
214 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000215 basic_string& insert(size_type pos1, const basic_string& str,
216 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000217 template <class T>
218 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000219 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000220 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 basic_string& insert(size_type pos, size_type n, value_type c);
222 iterator insert(const_iterator p, value_type c);
223 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000224 template<class InputIterator>
225 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 iterator insert(const_iterator p, initializer_list<value_type>);
227
228 basic_string& erase(size_type pos = 0, size_type n = npos);
229 iterator erase(const_iterator position);
230 iterator erase(const_iterator first, const_iterator last);
231
232 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000233 template <class T>
234 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000235 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000236 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000237 template <class T>
238 basic_string& replace(size_type pos1, size_type n1, const T& t,
239 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000240 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
241 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000243 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000244 template <class T>
245 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000246 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
247 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000249 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000250 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
251 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252
Howard Hinnantd17880b2013-06-28 16:59:19 +0000253 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 basic_string substr(size_type pos = 0, size_type n = npos) const;
255
Howard Hinnant3e276872011-06-03 18:40:47 +0000256 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000257 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
258 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259
Howard Hinnantd17880b2013-06-28 16:59:19 +0000260 const value_type* c_str() const noexcept;
261 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000262 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000264 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000266 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000267 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800268 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000269 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
270 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000273 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000274 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800275 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000276 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
277 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000279
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000280 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000281 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800282 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000283 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
284 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000287 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000288 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800289 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000290 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
291 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000294 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000295 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800296 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000297 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
298 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000301 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000302 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800303 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000304 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
305 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000308 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000309 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800310 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000312 template <class T>
313 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000314 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000315 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000316 template <class T>
317 int compare(size_type pos1, size_type n1, const T& t,
318 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000319 int compare(const value_type* s) const noexcept;
320 int compare(size_type pos1, size_type n1, const value_type* s) const;
321 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322
Marek Kurdej24b4c512021-01-07 12:29:04 +0100323 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
324 bool starts_with(charT c) const noexcept; // C++20
325 bool starts_with(const charT* s) const; // C++20
326 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
327 bool ends_with(charT c) const noexcept; // C++20
328 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000329
Wim Leflere023c3542021-01-19 14:33:30 -0500330 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
331 constexpr bool contains(charT c) const noexcept; // C++2b
332 constexpr bool contains(const charT* s) const; // C++2b
333
Howard Hinnantc51e1022010-05-11 19:42:16 +0000334 bool __invariants() const;
335};
336
Marshall Clowa0563332018-02-08 06:34:03 +0000337template<class InputIterator,
338 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
339basic_string(InputIterator, InputIterator, Allocator = Allocator())
340 -> basic_string<typename iterator_traits<InputIterator>::value_type,
341 char_traits<typename iterator_traits<InputIterator>::value_type>,
342 Allocator>; // C++17
343
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344template<class charT, class traits, class Allocator>
345basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000346operator+(const basic_string<charT, traits, Allocator>& lhs,
347 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348
349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
351operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
352
353template<class charT, class traits, class Allocator>
354basic_string<charT, traits, Allocator>
355operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
356
357template<class charT, class traits, class Allocator>
358basic_string<charT, traits, Allocator>
359operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
360
361template<class charT, class traits, class Allocator>
362basic_string<charT, traits, Allocator>
363operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
364
365template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000366bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000367 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368
369template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000370bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000373bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000375template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000376bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000377 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378
379template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000380bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000383bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000386bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000387 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388
389template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000390bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000393bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394
395template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000396bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000397 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398
399template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000400bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000403bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000406bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000407 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408
409template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000410bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
412template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000413bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414
415template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000416bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000417 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418
419template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000420bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000423bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424
425template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000426void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000427 basic_string<charT, traits, Allocator>& rhs)
428 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
431basic_istream<charT, traits>&
432operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
433
434template<class charT, class traits, class Allocator>
435basic_ostream<charT, traits>&
436operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
437
438template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000439basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000440getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
441 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442
443template<class charT, class traits, class Allocator>
444basic_istream<charT, traits>&
445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
446
Marshall Clow29b53f22018-12-14 18:49:35 +0000447template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200448typename basic_string<charT, traits, Allocator>::size_type
449erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000450template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200451typename basic_string<charT, traits, Allocator>::size_type
452erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000453
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454typedef basic_string<char> string;
455typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100456typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000457typedef basic_string<char16_t> u16string;
458typedef basic_string<char32_t> u32string;
459
Bruce Mitchener170d8972020-11-24 12:53:53 -0500460int stoi (const string& str, size_t* idx = nullptr, int base = 10);
461long stol (const string& str, size_t* idx = nullptr, int base = 10);
462unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
463long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
464unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000465
Bruce Mitchener170d8972020-11-24 12:53:53 -0500466float stof (const string& str, size_t* idx = nullptr);
467double stod (const string& str, size_t* idx = nullptr);
468long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000469
470string to_string(int val);
471string to_string(unsigned val);
472string to_string(long val);
473string to_string(unsigned long val);
474string to_string(long long val);
475string to_string(unsigned long long val);
476string to_string(float val);
477string to_string(double val);
478string to_string(long double val);
479
Bruce Mitchener170d8972020-11-24 12:53:53 -0500480int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
481long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
482unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
483long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
484unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000485
Bruce Mitchener170d8972020-11-24 12:53:53 -0500486float stof (const wstring& str, size_t* idx = nullptr);
487double stod (const wstring& str, size_t* idx = nullptr);
488long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000489
490wstring to_wstring(int val);
491wstring to_wstring(unsigned val);
492wstring to_wstring(long val);
493wstring to_wstring(unsigned long val);
494wstring to_wstring(long long val);
495wstring to_wstring(unsigned long long val);
496wstring to_wstring(float val);
497wstring to_wstring(double val);
498wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499
500template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100501template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502template <> struct hash<u16string>;
503template <> struct hash<u32string>;
504template <> struct hash<wstring>;
505
Marshall Clowcba751f2013-07-23 17:05:24 +0000506basic_string<char> operator "" s( const char *str, size_t len ); // C++14
507basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100508basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000509basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
510basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
511
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512} // std
513
514*/
515
516#include <__config>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400517#include <compare>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000518#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519#include <iosfwd>
520#include <cstring>
Howard Hinnant155c2af2010-05-24 17:49:41 +0000521#include <cstdio> // For EOF.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522#include <cwchar>
523#include <algorithm>
524#include <iterator>
525#include <utility>
526#include <memory>
527#include <stdexcept>
528#include <type_traits>
529#include <initializer_list>
530#include <__functional_base>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000531#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
533#include <cstdint>
534#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000535
Eric Fiselier14b6de92014-08-10 23:53:08 +0000536#include <__debug>
537
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000538#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000540#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541
Eric Fiselierf4433a32017-05-31 22:07:49 +0000542_LIBCPP_PUSH_MACROS
543#include <__undef_macros>
544
545
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546_LIBCPP_BEGIN_NAMESPACE_STD
547
548// fpos
549
550template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000551class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552{
553private:
554 _StateT __st_;
555 streamoff __off_;
556public:
557 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
558
559 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
560
561 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
562 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
563
564 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
565 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
566 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
567 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
568};
569
570template <class _StateT>
571inline _LIBCPP_INLINE_VISIBILITY
572streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
573 {return streamoff(__x) - streamoff(__y);}
574
575template <class _StateT>
576inline _LIBCPP_INLINE_VISIBILITY
577bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
578 {return streamoff(__x) == streamoff(__y);}
579
580template <class _StateT>
581inline _LIBCPP_INLINE_VISIBILITY
582bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
583 {return streamoff(__x) != streamoff(__y);}
584
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585// basic_string
586
587template<class _CharT, class _Traits, class _Allocator>
588basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000589operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
590 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591
592template<class _CharT, class _Traits, class _Allocator>
593basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000594operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
596template<class _CharT, class _Traits, class _Allocator>
597basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000598operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
600template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000601inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000603operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604
605template<class _CharT, class _Traits, class _Allocator>
606basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000607operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608
Shoaib Meenaiea363712017-07-29 02:54:41 +0000609_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
610
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000612class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613{
614protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000615 _LIBCPP_NORETURN void __throw_length_error() const;
616 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617};
618
619template <bool __b>
620void
621__basic_string_common<__b>::__throw_length_error() const
622{
Marshall Clow8fea1612016-08-25 15:09:01 +0000623 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624}
625
626template <bool __b>
627void
628__basic_string_common<__b>::__throw_out_of_range() const
629{
Marshall Clow8fea1612016-08-25 15:09:01 +0000630 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631}
632
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000633_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634
Marshall Clow039b2f02016-01-13 21:54:34 +0000635template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400636struct __string_is_trivial_iterator : public false_type {};
637
638template <class _Tp>
639struct __string_is_trivial_iterator<_Tp*>
640 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000641
Louis Dionne173f29e2019-05-29 16:01:36 +0000642template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400643struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
644 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000645
Marshall Clow82513342016-09-24 22:45:42 +0000646template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500647struct __can_be_converted_to_string_view : public _BoolConstant<
648 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
649 !is_convertible<const _Tp&, const _CharT*>::value
650 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000651
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000652#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000653
654template <class _CharT, size_t = sizeof(_CharT)>
655struct __padding
656{
657 unsigned char __xx[sizeof(_CharT)-1];
658};
659
660template <class _CharT>
661struct __padding<_CharT, 1>
662{
663};
664
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400665#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000666
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400667#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800668typedef basic_string<char8_t> u8string;
669#endif
670
671#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
672typedef basic_string<char16_t> u16string;
673typedef basic_string<char32_t> u32string;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400674#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Richard Smith256954d2020-11-11 17:12:18 -0800675
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000676template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800677class
678 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400679#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800680 _LIBCPP_PREFERRED_NAME(u8string)
681#endif
682#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
683 _LIBCPP_PREFERRED_NAME(u16string)
684 _LIBCPP_PREFERRED_NAME(u32string)
685#endif
686 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687 : private __basic_string_common<true>
688{
689public:
690 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000691 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000693 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000695 typedef allocator_traits<allocator_type> __alloc_traits;
696 typedef typename __alloc_traits::size_type size_type;
697 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000698 typedef value_type& reference;
699 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000700 typedef typename __alloc_traits::pointer pointer;
701 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702
Marshall Clow79f33542018-03-21 00:36:05 +0000703 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
704 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
705 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
706 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000707 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000708 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000709 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000710
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 typedef __wrap_iter<pointer> iterator;
712 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000713 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
714 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715
716private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000717
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000718#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000719
720 struct __long
721 {
722 pointer __data_;
723 size_type __size_;
724 size_type __cap_;
725 };
726
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000727#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000728 static const size_type __short_mask = 0x01;
729 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000730#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000731 static const size_type __short_mask = 0x80;
732 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400733#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000734
735 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
736 (sizeof(__long) - 1)/sizeof(value_type) : 2};
737
738 struct __short
739 {
740 value_type __data_[__min_cap];
741 struct
742 : __padding<value_type>
743 {
744 unsigned char __size_;
745 };
746 };
747
748#else
749
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 struct __long
751 {
752 size_type __cap_;
753 size_type __size_;
754 pointer __data_;
755 };
756
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000757#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000758 static const size_type __short_mask = 0x80;
759 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000760#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000761 static const size_type __short_mask = 0x01;
762 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400763#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
766 (sizeof(__long) - 1)/sizeof(value_type) : 2};
767
768 struct __short
769 {
770 union
771 {
772 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000773 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 };
775 value_type __data_[__min_cap];
776 };
777
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400778#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000779
Howard Hinnant8ea98242013-08-23 17:37:05 +0000780 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781
Howard Hinnant8ea98242013-08-23 17:37:05 +0000782 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783
784 struct __raw
785 {
786 size_type __words[__n_words];
787 };
788
789 struct __rep
790 {
791 union
792 {
793 __long __l;
794 __short __s;
795 __raw __r;
796 };
797 };
798
799 __compressed_pair<__rep, allocator_type> __r_;
800
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200802 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803 static const size_type npos = -1;
804
Howard Hinnant3e276872011-06-03 18:40:47 +0000805 _LIBCPP_INLINE_VISIBILITY basic_string()
806 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000807
808 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
809#if _LIBCPP_STD_VER <= 14
810 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
811#else
812 _NOEXCEPT;
813#endif
814
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815 basic_string(const basic_string& __str);
816 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000817
Eric Fiselierfc92be82017-04-19 00:28:44 +0000818#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000820 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000821#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000822 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000823#else
824 _NOEXCEPT;
825#endif
826
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400829#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000830
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500831 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500833 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000834 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
835 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400836# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000837 __get_db()->__insert_c(this);
838# endif
839 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000840
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500841 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000842 _LIBCPP_INLINE_VISIBILITY
843 basic_string(const _CharT* __s, const _Allocator& __a);
844
Howard Hinnantcf823322010-12-17 14:46:43 +0000845 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000846 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000847 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000849 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000850 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000851
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500852 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000853 _LIBCPP_INLINE_VISIBILITY
854 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
855
Marshall Clow83445802016-04-07 18:13:41 +0000856 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000857 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000858 _LIBCPP_INLINE_VISIBILITY
859 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000860 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000861
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500862 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 +0000863 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000864 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500865 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000866
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500867 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
868 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000869 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
870 explicit basic_string(const _Tp& __t);
871
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500872 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 +0000873 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
874 explicit basic_string(const _Tp& __t, const allocator_type& __a);
875
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500876 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500879 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000882#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000883 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000884 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000885 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000886 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400887#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000889 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000891 _LIBCPP_INLINE_VISIBILITY
892 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
893
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000894 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000895
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500896 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 +0000897 basic_string& operator=(const _Tp& __t)
898 {__self_view __sv = __t; return assign(__sv);}
899
Eric Fiselierfc92be82017-04-19 00:28:44 +0000900#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000902 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000903 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000904 _LIBCPP_INLINE_VISIBILITY
905 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000907 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909
Louis Dionneba400782020-10-02 15:02:52 -0400910#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000911 _LIBCPP_INLINE_VISIBILITY
912 iterator begin() _NOEXCEPT
913 {return iterator(this, __get_pointer());}
914 _LIBCPP_INLINE_VISIBILITY
915 const_iterator begin() const _NOEXCEPT
916 {return const_iterator(this, __get_pointer());}
917 _LIBCPP_INLINE_VISIBILITY
918 iterator end() _NOEXCEPT
919 {return iterator(this, __get_pointer() + size());}
920 _LIBCPP_INLINE_VISIBILITY
921 const_iterator end() const _NOEXCEPT
922 {return const_iterator(this, __get_pointer() + size());}
923#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000924 _LIBCPP_INLINE_VISIBILITY
925 iterator begin() _NOEXCEPT
926 {return iterator(__get_pointer());}
927 _LIBCPP_INLINE_VISIBILITY
928 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000929 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000930 _LIBCPP_INLINE_VISIBILITY
931 iterator end() _NOEXCEPT
932 {return iterator(__get_pointer() + size());}
933 _LIBCPP_INLINE_VISIBILITY
934 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000935 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400936#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000937 _LIBCPP_INLINE_VISIBILITY
938 reverse_iterator rbegin() _NOEXCEPT
939 {return reverse_iterator(end());}
940 _LIBCPP_INLINE_VISIBILITY
941 const_reverse_iterator rbegin() const _NOEXCEPT
942 {return const_reverse_iterator(end());}
943 _LIBCPP_INLINE_VISIBILITY
944 reverse_iterator rend() _NOEXCEPT
945 {return reverse_iterator(begin());}
946 _LIBCPP_INLINE_VISIBILITY
947 const_reverse_iterator rend() const _NOEXCEPT
948 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000950 _LIBCPP_INLINE_VISIBILITY
951 const_iterator cbegin() const _NOEXCEPT
952 {return begin();}
953 _LIBCPP_INLINE_VISIBILITY
954 const_iterator cend() const _NOEXCEPT
955 {return end();}
956 _LIBCPP_INLINE_VISIBILITY
957 const_reverse_iterator crbegin() const _NOEXCEPT
958 {return rbegin();}
959 _LIBCPP_INLINE_VISIBILITY
960 const_reverse_iterator crend() const _NOEXCEPT
961 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000963 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000965 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
966 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
967 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000968 {return (__is_long() ? __get_long_cap()
969 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970
971 void resize(size_type __n, value_type __c);
972 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
973
Marek Kurdejc9848142020-11-26 10:07:16 +0100974 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000975 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
976
Marek Kurdejc9848142020-11-26 10:07:16 +0100977 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
978 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000979 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100980 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000982 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000983 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
984 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000986 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
987 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988
989 const_reference at(size_type __n) const;
990 reference at(size_type __n);
991
992 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000993
994 template <class _Tp>
995 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500996 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +0000997 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500998 __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 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001003 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001005#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001007#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008
Howard Hinnantcf823322010-12-17 14:46:43 +00001009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001011
1012 template <class _Tp>
1013 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001014 _EnableIf<
1015 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1016 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001017 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001018 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001019 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001020 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001021
Marshall Clow62953962016-10-03 23:40:48 +00001022 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001023 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001024 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001025 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1027 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001028 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001029 >
Marshall Clow82513342016-09-24 22:45:42 +00001030 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001031 basic_string& append(const value_type* __s, size_type __n);
1032 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001034
1035 _LIBCPP_INLINE_VISIBILITY
1036 void __append_default_init(size_type __n);
1037
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 template<class _InputIterator>
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 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001042 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001044 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001045 _LIBCPP_INLINE_VISIBILITY
1046 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001047 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001048 append(__temp.data(), __temp.size());
1049 return *this;
1050 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001052 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001053 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001055 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001057 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001058 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001059 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001060
Eric Fiselierfc92be82017-04-19 00:28:44 +00001061#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001064#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065
1066 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001069 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1070 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1071 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1072 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073
Marshall Clowe46031a2018-07-02 18:41:15 +00001074 template <class _Tp>
1075 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001076 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001077 <
1078 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1079 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001080 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001081 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001082 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001083 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001084#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001085 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001086 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001087 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001088 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001089#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001090 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001091 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001092 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001093 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001094 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1096 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001097 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001098 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001099 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001100 basic_string& assign(const value_type* __s, size_type __n);
1101 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 basic_string& assign(size_type __n, value_type __c);
1103 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001104 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001105 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001107 __is_exactly_cpp17_input_iterator<_InputIterator>::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(_InputIterator __first, _InputIterator __last);
1111 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001112 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001113 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001115 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001117 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001119#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001122#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123
Howard Hinnantcf823322010-12-17 14:46:43 +00001124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001126
1127 template <class _Tp>
1128 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001129 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001130 <
1131 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1132 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001134 insert(size_type __pos1, const _Tp& __t)
1135 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1136
Marshall Clow82513342016-09-24 22:45:42 +00001137 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001138 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001139 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001140 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001141 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001142 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001143 >
Marshall Clow82513342016-09-24 22:45:42 +00001144 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001145 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001146 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1147 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1149 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1152 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001153 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001154 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001156 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001158 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1160 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001161 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001162 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001164 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001166 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001168#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1171 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001172#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173
1174 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 iterator erase(const_iterator __first, const_iterator __last);
1179
Howard Hinnantcf823322010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001182
1183 template <class _Tp>
1184 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001185 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001186 <
1187 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1188 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001189 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001190 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 +00001191 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 +00001192 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001193 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001194 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001195 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001196 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001197 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001198 >
Marshall Clow82513342016-09-24 22:45:42 +00001199 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001200 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1201 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001204 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001205
1206 template <class _Tp>
1207 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001208 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001209 <
1210 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1211 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001212 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001213 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1214
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001216 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001218 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001220 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001222 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001223 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001225 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001227 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001228 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001229#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001231 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001233#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234
Howard Hinnantd17880b2013-06-28 16:59:19 +00001235 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1238
Howard Hinnantcf823322010-12-17 14:46:43 +00001239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001240 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001241#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001242 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001243#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001244 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001245 __is_nothrow_swappable<allocator_type>::value);
1246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247
Howard Hinnantcf823322010-12-17 14:46:43 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001249 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001250 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001251 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001252#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001253 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001254 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001255#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256
Howard Hinnantcf823322010-12-17 14:46:43 +00001257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001258 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259
Howard Hinnantcf823322010-12-17 14:46:43 +00001260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001261 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001262
1263 template <class _Tp>
1264 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001265 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001266 <
1267 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1268 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001269 >
zoecarver1997e0a2021-02-05 11:54:47 -08001270 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001271 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001273 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001274 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Howard Hinnantcf823322010-12-17 14:46:43 +00001276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001277 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001278
1279 template <class _Tp>
1280 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001281 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001282 <
1283 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1284 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001285 >
zoecarver1997e0a2021-02-05 11:54:47 -08001286 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001287 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001289 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001290 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291
Howard Hinnantcf823322010-12-17 14:46:43 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001293 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001294
1295 template <class _Tp>
1296 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001297 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001298 <
1299 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1300 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001301 >
zoecarver1997e0a2021-02-05 11:54:47 -08001302 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001303 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001305 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001307 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308
Howard Hinnantcf823322010-12-17 14:46:43 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001310 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001311
1312 template <class _Tp>
1313 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001314 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001315 <
1316 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1317 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001318 >
zoecarver1997e0a2021-02-05 11:54:47 -08001319 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001320 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001322 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001324 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
Howard Hinnantcf823322010-12-17 14:46:43 +00001326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001327 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001328
1329 template <class _Tp>
1330 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001331 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001332 <
1333 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1334 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001335 >
zoecarver1997e0a2021-02-05 11:54:47 -08001336 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001337 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 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001339 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001340 _LIBCPP_INLINE_VISIBILITY
1341 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1342
1343 _LIBCPP_INLINE_VISIBILITY
1344 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001345
1346 template <class _Tp>
1347 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001348 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001349 <
1350 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1351 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001352 >
zoecarver1997e0a2021-02-05 11:54:47 -08001353 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001354 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 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001356 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001357 _LIBCPP_INLINE_VISIBILITY
1358 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1359
1360 _LIBCPP_INLINE_VISIBILITY
1361 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001362
1363 template <class _Tp>
1364 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001365 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001366 <
1367 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1368 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001369 >
zoecarver1997e0a2021-02-05 11:54:47 -08001370 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001371
1372 template <class _Tp>
1373 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001374 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001375 <
1376 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1377 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001378 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001379 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1380
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001383 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 +00001384
Marshall Clow82513342016-09-24 22:45:42 +00001385 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001386 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001387 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001388 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001389 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001390 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001391 >
Marshall Clow82513342016-09-24 22:45:42 +00001392 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 +00001393 int compare(const value_type* __s) const _NOEXCEPT;
1394 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1395 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396
Marshall Clow18c293b2017-12-04 20:11:38 +00001397#if _LIBCPP_STD_VER > 17
1398 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1399 bool starts_with(__self_view __sv) const _NOEXCEPT
1400 { return __self_view(data(), size()).starts_with(__sv); }
1401
1402 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1403 bool starts_with(value_type __c) const _NOEXCEPT
1404 { return !empty() && _Traits::eq(front(), __c); }
1405
1406 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1407 bool starts_with(const value_type* __s) const _NOEXCEPT
1408 { return starts_with(__self_view(__s)); }
1409
1410 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1411 bool ends_with(__self_view __sv) const _NOEXCEPT
1412 { return __self_view(data(), size()).ends_with( __sv); }
1413
1414 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1415 bool ends_with(value_type __c) const _NOEXCEPT
1416 { return !empty() && _Traits::eq(back(), __c); }
1417
1418 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1419 bool ends_with(const value_type* __s) const _NOEXCEPT
1420 { return ends_with(__self_view(__s)); }
1421#endif
1422
Wim Leflere023c3542021-01-19 14:33:30 -05001423#if _LIBCPP_STD_VER > 20
1424 constexpr _LIBCPP_INLINE_VISIBILITY
1425 bool contains(__self_view __sv) const noexcept
1426 { return __self_view(data(), size()).contains(__sv); }
1427
1428 constexpr _LIBCPP_INLINE_VISIBILITY
1429 bool contains(value_type __c) const noexcept
1430 { return __self_view(data(), size()).contains(__c); }
1431
1432 constexpr _LIBCPP_INLINE_VISIBILITY
1433 bool contains(const value_type* __s) const
1434 { return __self_view(data(), size()).contains(__s); }
1435#endif
1436
Howard Hinnantcf823322010-12-17 14:46:43 +00001437 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001438
Marshall Clowe60a7182018-05-29 17:04:37 +00001439 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001440
Marek Kurdejc9848142020-11-26 10:07:16 +01001441 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1442
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001443 _LIBCPP_INLINE_VISIBILITY
1444 bool __is_long() const _NOEXCEPT
1445 {return bool(__r_.first().__s.__size_ & __short_mask);}
1446
Louis Dionneba400782020-10-02 15:02:52 -04001447#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001448
1449 bool __dereferenceable(const const_iterator* __i) const;
1450 bool __decrementable(const const_iterator* __i) const;
1451 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1452 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1453
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001454#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001455
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001457 _LIBCPP_INLINE_VISIBILITY
1458 allocator_type& __alloc() _NOEXCEPT
1459 {return __r_.second();}
1460 _LIBCPP_INLINE_VISIBILITY
1461 const allocator_type& __alloc() const _NOEXCEPT
1462 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001464#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001465
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001467 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001468# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001470# else
1471 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1472# endif
1473
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001474 _LIBCPP_INLINE_VISIBILITY
1475 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001476# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001478# else
1479 {return __r_.first().__s.__size_;}
1480# endif
1481
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001482#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001483
1484 _LIBCPP_INLINE_VISIBILITY
1485 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001486# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001487 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1488# else
1489 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1490# endif
1491
1492 _LIBCPP_INLINE_VISIBILITY
1493 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001494# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001495 {return __r_.first().__s.__size_;}
1496# else
1497 {return __r_.first().__s.__size_ >> 1;}
1498# endif
1499
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001500#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001501
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001502 _LIBCPP_INLINE_VISIBILITY
1503 void __set_long_size(size_type __s) _NOEXCEPT
1504 {__r_.first().__l.__size_ = __s;}
1505 _LIBCPP_INLINE_VISIBILITY
1506 size_type __get_long_size() const _NOEXCEPT
1507 {return __r_.first().__l.__size_;}
1508 _LIBCPP_INLINE_VISIBILITY
1509 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1511
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 void __set_long_cap(size_type __s) _NOEXCEPT
1514 {__r_.first().__l.__cap_ = __long_mask | __s;}
1515 _LIBCPP_INLINE_VISIBILITY
1516 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001517 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __set_long_pointer(pointer __p) _NOEXCEPT
1521 {__r_.first().__l.__data_ = __p;}
1522 _LIBCPP_INLINE_VISIBILITY
1523 pointer __get_long_pointer() _NOEXCEPT
1524 {return __r_.first().__l.__data_;}
1525 _LIBCPP_INLINE_VISIBILITY
1526 const_pointer __get_long_pointer() const _NOEXCEPT
1527 {return __r_.first().__l.__data_;}
1528 _LIBCPP_INLINE_VISIBILITY
1529 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001530 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001531 _LIBCPP_INLINE_VISIBILITY
1532 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001533 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001534 _LIBCPP_INLINE_VISIBILITY
1535 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001537 _LIBCPP_INLINE_VISIBILITY
1538 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1540
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001541 _LIBCPP_INLINE_VISIBILITY
1542 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 {
1544 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1545 for (unsigned __i = 0; __i < __n_words; ++__i)
1546 __a[__i] = 0;
1547 }
1548
1549 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001551 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001552 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001554 static _LIBCPP_INLINE_VISIBILITY
1555 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001556 {
1557 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1558 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1559 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1560 if (__guess == __min_cap) ++__guess;
1561 return __guess;
1562 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001564 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001565 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001566 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001567 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001568 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001570
Martijn Vels5e7c9752020-03-04 17:52:46 -05001571 // Slow path for the (inlined) copy constructor for 'long' strings.
1572 // Always externally instantiated and not inlined.
1573 // Requires that __s is zero terminated.
1574 // The main reason for this function to exist is because for unstable, we
1575 // want to allow inlining of the copy constructor. However, we don't want
1576 // to call the __init() functions as those are marked as inline which may
1577 // result in over-aggressive inlining by the compiler, where our aim is
1578 // to only inline the fast path code directly in the ctor.
1579 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1580
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001582 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001583 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001585 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1586 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 __init(_InputIterator __first, _InputIterator __last);
1588
1589 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001590 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001591 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001593 __is_cpp17_forward_iterator<_ForwardIterator>::value
1594 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 __init(_ForwardIterator __first, _ForwardIterator __last);
1596
1597 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001598 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1600 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001601 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602
Martijn Vels596e3de2020-02-26 15:55:49 -05001603 // __assign_no_alias is invoked for assignment operations where we
1604 // have proof that the input does not alias the current instance.
1605 // For example, operator=(basic_string) performs a 'self' check.
1606 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001607 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001608
Howard Hinnantcf823322010-12-17 14:46:43 +00001609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 void __erase_to_end(size_type __pos);
1611
Martijn Velsa81fc792020-02-26 13:25:43 -05001612 // __erase_external_with_move is invoked for erase() invocations where
1613 // `n ~= npos`, likely requiring memory moves on the string data.
1614 void __erase_external_with_move(size_type __pos, size_type __n);
1615
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001616 _LIBCPP_INLINE_VISIBILITY
1617 void __copy_assign_alloc(const basic_string& __str)
1618 {__copy_assign_alloc(__str, integral_constant<bool,
1619 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1620
1621 _LIBCPP_INLINE_VISIBILITY
1622 void __copy_assign_alloc(const basic_string& __str, true_type)
1623 {
Marshall Clowf258c202017-01-31 03:40:52 +00001624 if (__alloc() == __str.__alloc())
1625 __alloc() = __str.__alloc();
1626 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001627 {
Marshall Clowf258c202017-01-31 03:40:52 +00001628 if (!__str.__is_long())
1629 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001630 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001631 __alloc() = __str.__alloc();
1632 }
1633 else
1634 {
1635 allocator_type __a = __str.__alloc();
1636 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001637 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001638 __alloc() = _VSTD::move(__a);
1639 __set_long_pointer(__p);
1640 __set_long_cap(__str.__get_long_cap());
1641 __set_long_size(__str.size());
1642 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001643 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001644 }
1645
1646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001647 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001648 {}
1649
Eric Fiselierfc92be82017-04-19 00:28:44 +00001650#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001651 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001652 void __move_assign(basic_string& __str, false_type)
1653 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001655 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001656#if _LIBCPP_STD_VER > 14
1657 _NOEXCEPT;
1658#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001659 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001660#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001661#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001662
1663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001664 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001665 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001666 _NOEXCEPT_(
1667 !__alloc_traits::propagate_on_container_move_assignment::value ||
1668 is_nothrow_move_assignable<allocator_type>::value)
1669 {__move_assign_alloc(__str, integral_constant<bool,
1670 __alloc_traits::propagate_on_container_move_assignment::value>());}
1671
1672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001673 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001674 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1675 {
1676 __alloc() = _VSTD::move(__c.__alloc());
1677 }
1678
1679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001680 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001681 _NOEXCEPT
1682 {}
1683
Martijn Velsda7d94f2020-06-19 14:24:03 -04001684 basic_string& __assign_external(const value_type* __s);
1685 basic_string& __assign_external(const value_type* __s, size_type __n);
1686
1687 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1688 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1689 pointer __p = __is_long()
1690 ? (__set_long_size(__n), __get_long_pointer())
1691 : (__set_short_size(__n), __get_short_pointer());
1692 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1693 traits_type::assign(__p[__n], value_type());
1694 return *this;
1695 }
1696
Howard Hinnantcf823322010-12-17 14:46:43 +00001697 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1698 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001700 template<class _Tp>
1701 _LIBCPP_INLINE_VISIBILITY
1702 bool __addr_in_range(_Tp&& __t) const {
1703 const volatile void *__p = _VSTD::addressof(__t);
1704 return data() <= __p && __p <= data() + size();
1705 }
1706
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707 friend basic_string operator+<>(const basic_string&, const basic_string&);
1708 friend basic_string operator+<>(const value_type*, const basic_string&);
1709 friend basic_string operator+<>(value_type, const basic_string&);
1710 friend basic_string operator+<>(const basic_string&, const value_type*);
1711 friend basic_string operator+<>(const basic_string&, value_type);
1712};
1713
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001714// These declarations must appear before any functions are implicitly used
1715// so that they have the correct visibility specifier.
1716#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1717_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1718_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1719#else
1720_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1721_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1722#endif
1723
1724
Marshall Clowa0563332018-02-08 06:34:03 +00001725#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1726template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001727 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001728 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001729 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1730 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001731 >
1732basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1733 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001734
1735template<class _CharT,
1736 class _Traits,
1737 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001738 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001739 >
1740explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1741 -> basic_string<_CharT, _Traits, _Allocator>;
1742
1743template<class _CharT,
1744 class _Traits,
1745 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001746 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001747 class _Sz = typename allocator_traits<_Allocator>::size_type
1748 >
1749basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1750 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001751#endif
1752
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001754inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755void
1756basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1757{
Louis Dionneba400782020-10-02 15:02:52 -04001758#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001759 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001760#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761}
1762
1763template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001764inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765void
Howard Hinnant28b24882011-12-01 20:21:04 +00001766basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Louis Dionneba400782020-10-02 15:02:52 -04001767#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant28b24882011-12-01 20:21:04 +00001768 __pos
1769#endif
1770 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771{
Louis Dionneba400782020-10-02 15:02:52 -04001772#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001773 __c_node* __c = __get_db()->__find_c_and_lock(this);
1774 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001776 const_pointer __new_last = __get_pointer() + __pos;
1777 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001779 --__p;
1780 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1781 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001783 (*__p)->__c_ = nullptr;
1784 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001785 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001788 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001790#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791}
1792
1793template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001794inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001795basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001796 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001797 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798{
Louis Dionneba400782020-10-02 15:02:52 -04001799#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001800 __get_db()->__insert_c(this);
1801#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 __zero();
1803}
1804
1805template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001806inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001808#if _LIBCPP_STD_VER <= 14
1809 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1810#else
1811 _NOEXCEPT
1812#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001813: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814{
Louis Dionneba400782020-10-02 15:02:52 -04001815#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001816 __get_db()->__insert_c(this);
1817#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 __zero();
1819}
1820
1821template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001822void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1823 size_type __sz,
1824 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825{
1826 if (__reserve > max_size())
1827 this->__throw_length_error();
1828 pointer __p;
1829 if (__reserve < __min_cap)
1830 {
1831 __set_short_size(__sz);
1832 __p = __get_short_pointer();
1833 }
1834 else
1835 {
1836 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001837 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 __set_long_pointer(__p);
1839 __set_long_cap(__cap+1);
1840 __set_long_size(__sz);
1841 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001842 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 traits_type::assign(__p[__sz], value_type());
1844}
1845
1846template <class _CharT, class _Traits, class _Allocator>
1847void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001848basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849{
1850 if (__sz > max_size())
1851 this->__throw_length_error();
1852 pointer __p;
1853 if (__sz < __min_cap)
1854 {
1855 __set_short_size(__sz);
1856 __p = __get_short_pointer();
1857 }
1858 else
1859 {
1860 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001861 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862 __set_long_pointer(__p);
1863 __set_long_cap(__cap+1);
1864 __set_long_size(__sz);
1865 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001866 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 traits_type::assign(__p[__sz], value_type());
1868}
1869
1870template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001871template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001872basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001873 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001875 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001877#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001878 __get_db()->__insert_c(this);
1879#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880}
1881
1882template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001883inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001884basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001885 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001887 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001889#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001890 __get_db()->__insert_c(this);
1891#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892}
1893
1894template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001895inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001896basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001897 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001899 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001901#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001902 __get_db()->__insert_c(this);
1903#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904}
1905
1906template <class _CharT, class _Traits, class _Allocator>
1907basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001908 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909{
1910 if (!__str.__is_long())
1911 __r_.first().__r = __str.__r_.first().__r;
1912 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001913 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1914 __str.__get_long_size());
1915
Louis Dionneba400782020-10-02 15:02:52 -04001916#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001917 __get_db()->__insert_c(this);
1918#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919}
1920
1921template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001922basic_string<_CharT, _Traits, _Allocator>::basic_string(
1923 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001924 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925{
1926 if (!__str.__is_long())
1927 __r_.first().__r = __str.__r_.first().__r;
1928 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001929 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1930 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001931#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001932 __get_db()->__insert_c(this);
1933#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934}
1935
Martijn Vels5e7c9752020-03-04 17:52:46 -05001936template <class _CharT, class _Traits, class _Allocator>
1937void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1938 const value_type* __s, size_type __sz) {
1939 pointer __p;
1940 if (__sz < __min_cap) {
1941 __p = __get_short_pointer();
1942 __set_short_size(__sz);
1943 } else {
1944 if (__sz > max_size())
1945 this->__throw_length_error();
1946 size_t __cap = __recommend(__sz);
1947 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1948 __set_long_pointer(__p);
1949 __set_long_cap(__cap + 1);
1950 __set_long_size(__sz);
1951 }
1952 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1953}
1954
Eric Fiselierfc92be82017-04-19 00:28:44 +00001955#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956
1957template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001958inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001959basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001960#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001961 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001962#else
1963 _NOEXCEPT
1964#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001965 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966{
1967 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001968#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001969 __get_db()->__insert_c(this);
1970 if (__is_long())
1971 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972#endif
1973}
1974
1975template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001976inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001978 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979{
Marshall Clowd2d24692014-07-17 15:32:20 +00001980 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001981 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001982 else
1983 {
1984 __r_.first().__r = __str.__r_.first().__r;
1985 __str.__zero();
1986 }
Louis Dionneba400782020-10-02 15:02:52 -04001987#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001988 __get_db()->__insert_c(this);
1989 if (__is_long())
1990 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991#endif
1992}
1993
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001994#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995
1996template <class _CharT, class _Traits, class _Allocator>
1997void
1998basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1999{
2000 if (__n > max_size())
2001 this->__throw_length_error();
2002 pointer __p;
2003 if (__n < __min_cap)
2004 {
2005 __set_short_size(__n);
2006 __p = __get_short_pointer();
2007 }
2008 else
2009 {
2010 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002011 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012 __set_long_pointer(__p);
2013 __set_long_cap(__cap+1);
2014 __set_long_size(__n);
2015 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002016 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017 traits_type::assign(__p[__n], value_type());
2018}
2019
2020template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002021inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002022basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002023 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024{
2025 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002026#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002027 __get_db()->__insert_c(this);
2028#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029}
2030
2031template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002032template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002033basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002034 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035{
2036 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002037#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002038 __get_db()->__insert_c(this);
2039#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040}
2041
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002043basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2044 size_type __pos, size_type __n,
2045 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002046 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047{
2048 size_type __str_sz = __str.size();
2049 if (__pos > __str_sz)
2050 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002051 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002052#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002053 __get_db()->__insert_c(this);
2054#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055}
2056
2057template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002058inline
Marshall Clow83445802016-04-07 18:13:41 +00002059basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002060 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002061 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002062{
2063 size_type __str_sz = __str.size();
2064 if (__pos > __str_sz)
2065 this->__throw_out_of_range();
2066 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002067#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002068 __get_db()->__insert_c(this);
2069#endif
2070}
2071
2072template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002073template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002074basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002075 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002076 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002077{
Marshall Clowe46031a2018-07-02 18:41:15 +00002078 __self_view __sv0 = __t;
2079 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002080 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002081#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002082 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002083#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002084}
2085
2086template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002087template <class _Tp, class>
2088basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002089 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002090{
Marshall Clowe46031a2018-07-02 18:41:15 +00002091 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002092 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002093#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002094 __get_db()->__insert_c(this);
2095#endif
2096}
2097
2098template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002099template <class _Tp, class>
2100basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002101 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002102{
Marshall Clowe46031a2018-07-02 18:41:15 +00002103 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002104 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002105#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002106 __get_db()->__insert_c(this);
2107#endif
2108}
2109
2110template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002112_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002114 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2115>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2117{
2118 __zero();
2119#ifndef _LIBCPP_NO_EXCEPTIONS
2120 try
2121 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002122#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123 for (; __first != __last; ++__first)
2124 push_back(*__first);
2125#ifndef _LIBCPP_NO_EXCEPTIONS
2126 }
2127 catch (...)
2128 {
2129 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002130 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 throw;
2132 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002133#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134}
2135
2136template <class _CharT, class _Traits, class _Allocator>
2137template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002138_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002140 __is_cpp17_forward_iterator<_ForwardIterator>::value
2141>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2143{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002144 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 if (__sz > max_size())
2146 this->__throw_length_error();
2147 pointer __p;
2148 if (__sz < __min_cap)
2149 {
2150 __set_short_size(__sz);
2151 __p = __get_short_pointer();
2152 }
2153 else
2154 {
2155 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002156 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 __set_long_pointer(__p);
2158 __set_long_cap(__cap+1);
2159 __set_long_size(__sz);
2160 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002161
2162#ifndef _LIBCPP_NO_EXCEPTIONS
2163 try
2164 {
2165#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002166 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 traits_type::assign(*__p, *__first);
2168 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002169#ifndef _LIBCPP_NO_EXCEPTIONS
2170 }
2171 catch (...)
2172 {
2173 if (__is_long())
2174 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2175 throw;
2176 }
2177#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178}
2179
2180template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002181template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002182inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002184 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185{
2186 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002187#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002188 __get_db()->__insert_c(this);
2189#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190}
2191
2192template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002193template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002194inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2196 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002197 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198{
2199 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002200#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002201 __get_db()->__insert_c(this);
2202#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203}
2204
Eric Fiselierfc92be82017-04-19 00:28:44 +00002205#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002206
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002208inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002209basic_string<_CharT, _Traits, _Allocator>::basic_string(
2210 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002211 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212{
2213 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002214#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002215 __get_db()->__insert_c(this);
2216#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217}
2218
2219template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002220inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002221
Eric Fiselier812882b2017-02-17 01:17:10 +00002222basic_string<_CharT, _Traits, _Allocator>::basic_string(
2223 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002224 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225{
2226 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002227#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002228 __get_db()->__insert_c(this);
2229#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230}
2231
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002232#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002233
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2236{
Louis Dionneba400782020-10-02 15:02:52 -04002237#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002238 __get_db()->__erase_c(this);
2239#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002241 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242}
2243
2244template <class _CharT, class _Traits, class _Allocator>
2245void
2246basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2247 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002248 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 +00002249{
2250 size_type __ms = max_size();
2251 if (__delta_cap > __ms - __old_cap - 1)
2252 this->__throw_length_error();
2253 pointer __old_p = __get_pointer();
2254 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002255 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002257 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 __invalidate_all_iterators();
2259 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002260 traits_type::copy(_VSTD::__to_address(__p),
2261 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002263 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2265 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002266 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2267 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002269 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 __set_long_pointer(__p);
2271 __set_long_cap(__cap+1);
2272 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2273 __set_long_size(__old_sz);
2274 traits_type::assign(__p[__old_sz], value_type());
2275}
2276
2277template <class _CharT, class _Traits, class _Allocator>
2278void
2279basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2280 size_type __n_copy, size_type __n_del, size_type __n_add)
2281{
2282 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002283 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 this->__throw_length_error();
2285 pointer __old_p = __get_pointer();
2286 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002287 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002289 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290 __invalidate_all_iterators();
2291 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002292 traits_type::copy(_VSTD::__to_address(__p),
2293 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2295 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002296 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2297 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002298 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002300 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301 __set_long_pointer(__p);
2302 __set_long_cap(__cap+1);
2303}
2304
2305// assign
2306
2307template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002308template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002309basic_string<_CharT, _Traits, _Allocator>&
2310basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002311 const value_type* __s, size_type __n) {
2312 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2313 if (__n < __cap) {
2314 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2315 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2316 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2317 traits_type::assign(__p[__n], value_type());
2318 __invalidate_iterators_past(__n);
2319 } else {
2320 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2321 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2322 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002323 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002324}
2325
2326template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002328basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2329 const value_type* __s, size_type __n) {
2330 size_type __cap = capacity();
2331 if (__cap >= __n) {
2332 value_type* __p = _VSTD::__to_address(__get_pointer());
2333 traits_type::move(__p, __s, __n);
2334 traits_type::assign(__p[__n], value_type());
2335 __set_size(__n);
2336 __invalidate_iterators_past(__n);
2337 } else {
2338 size_type __sz = size();
2339 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2340 }
2341 return *this;
2342}
2343
2344template <class _CharT, class _Traits, class _Allocator>
2345basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002346basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002348 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002349 return (_LIBCPP_BUILTIN_CONSTANT_P(__n) && __n < __min_cap)
2350 ? __assign_short(__s, __n)
2351 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352}
2353
2354template <class _CharT, class _Traits, class _Allocator>
2355basic_string<_CharT, _Traits, _Allocator>&
2356basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2357{
2358 size_type __cap = capacity();
2359 if (__cap < __n)
2360 {
2361 size_type __sz = size();
2362 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2363 }
2364 else
2365 __invalidate_iterators_past(__n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002366 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002367 traits_type::assign(__p, __n, __c);
2368 traits_type::assign(__p[__n], value_type());
2369 __set_size(__n);
2370 return *this;
2371}
2372
2373template <class _CharT, class _Traits, class _Allocator>
2374basic_string<_CharT, _Traits, _Allocator>&
2375basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2376{
2377 pointer __p;
2378 if (__is_long())
2379 {
2380 __p = __get_long_pointer();
2381 __set_long_size(1);
2382 }
2383 else
2384 {
2385 __p = __get_short_pointer();
2386 __set_short_size(1);
2387 }
2388 traits_type::assign(*__p, __c);
2389 traits_type::assign(*++__p, value_type());
2390 __invalidate_iterators_past(1);
2391 return *this;
2392}
2393
2394template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002395basic_string<_CharT, _Traits, _Allocator>&
2396basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2397{
Martijn Vels596e3de2020-02-26 15:55:49 -05002398 if (this != &__str) {
2399 __copy_assign_alloc(__str);
2400 if (!__is_long()) {
2401 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002402 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002403 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002404 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002405 }
2406 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002407 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002408 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002409 }
2410 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002411}
2412
Eric Fiselierfc92be82017-04-19 00:28:44 +00002413#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002414
2415template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002416inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002417void
2418basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002419 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002420{
2421 if (__alloc() != __str.__alloc())
2422 assign(__str);
2423 else
2424 __move_assign(__str, true_type());
2425}
2426
2427template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002428inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002429void
2430basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002431#if _LIBCPP_STD_VER > 14
2432 _NOEXCEPT
2433#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002434 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002435#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002436{
marshall2ed41622019-11-27 07:13:00 -08002437 if (__is_long()) {
2438 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2439 __get_long_cap());
2440#if _LIBCPP_STD_VER <= 14
2441 if (!is_nothrow_move_assignable<allocator_type>::value) {
2442 __set_short_size(0);
2443 traits_type::assign(__get_short_pointer()[0], value_type());
2444 }
2445#endif
2446 }
2447 __move_assign_alloc(__str);
2448 __r_.first() = __str.__r_.first();
2449 __str.__set_short_size(0);
2450 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002451}
2452
2453template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002454inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002455basic_string<_CharT, _Traits, _Allocator>&
2456basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002457 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002458{
2459 __move_assign(__str, integral_constant<bool,
2460 __alloc_traits::propagate_on_container_move_assignment::value>());
2461 return *this;
2462}
2463
2464#endif
2465
2466template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002468_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002470 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002472>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2474{
Marshall Clow958362f2016-09-05 01:54:30 +00002475 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002476 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002477 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478}
2479
2480template <class _CharT, class _Traits, class _Allocator>
2481template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002482_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002484 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002486>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2488{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002490 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2491 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2492
2493 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2494 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002496 if (__cap < __n)
2497 {
2498 size_type __sz = size();
2499 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2500 }
2501 else
2502 __invalidate_iterators_past(__n);
2503 pointer __p = __get_pointer();
2504 for (; __first != __last; ++__first, ++__p)
2505 traits_type::assign(*__p, *__first);
2506 traits_type::assign(*__p, value_type());
2507 __set_size(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002508 }
2509 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002510 {
2511 const basic_string __temp(__first, __last, __alloc());
2512 assign(__temp.data(), __temp.size());
2513 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514 return *this;
2515}
2516
2517template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518basic_string<_CharT, _Traits, _Allocator>&
2519basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2520{
2521 size_type __sz = __str.size();
2522 if (__pos > __sz)
2523 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002524 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525}
2526
2527template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002528template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002529_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002530<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002531 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2532 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002533 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002534>
Marshall Clow82513342016-09-24 22:45:42 +00002535basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002536{
Marshall Clow82513342016-09-24 22:45:42 +00002537 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002538 size_type __sz = __sv.size();
2539 if (__pos > __sz)
2540 this->__throw_out_of_range();
2541 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2542}
2543
2544
2545template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002547basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2548 return __assign_external(__s, traits_type::length(__s));
2549}
2550
2551template <class _CharT, class _Traits, class _Allocator>
2552basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002553basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002555 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002556 return _LIBCPP_BUILTIN_CONSTANT_P(*__s)
2557 ? (traits_type::length(__s) < __min_cap
2558 ? __assign_short(__s, traits_type::length(__s))
2559 : __assign_external(__s, traits_type::length(__s)))
2560 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562// append
2563
2564template <class _CharT, class _Traits, class _Allocator>
2565basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002566basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002568 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569 size_type __cap = capacity();
2570 size_type __sz = size();
2571 if (__cap - __sz >= __n)
2572 {
2573 if (__n)
2574 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002575 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576 traits_type::copy(__p + __sz, __s, __n);
2577 __sz += __n;
2578 __set_size(__sz);
2579 traits_type::assign(__p[__sz], value_type());
2580 }
2581 }
2582 else
2583 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2584 return *this;
2585}
2586
2587template <class _CharT, class _Traits, class _Allocator>
2588basic_string<_CharT, _Traits, _Allocator>&
2589basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2590{
2591 if (__n)
2592 {
2593 size_type __cap = capacity();
2594 size_type __sz = size();
2595 if (__cap - __sz < __n)
2596 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2597 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002598 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599 __sz += __n;
2600 __set_size(__sz);
2601 traits_type::assign(__p[__sz], value_type());
2602 }
2603 return *this;
2604}
2605
2606template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002607inline void
2608basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2609{
2610 if (__n)
2611 {
2612 size_type __cap = capacity();
2613 size_type __sz = size();
2614 if (__cap - __sz < __n)
2615 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2616 pointer __p = __get_pointer();
2617 __sz += __n;
2618 __set_size(__sz);
2619 traits_type::assign(__p[__sz], value_type());
2620 }
2621}
2622
2623template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624void
2625basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2626{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002627 bool __is_short = !__is_long();
2628 size_type __cap;
2629 size_type __sz;
2630 if (__is_short)
2631 {
2632 __cap = __min_cap - 1;
2633 __sz = __get_short_size();
2634 }
2635 else
2636 {
2637 __cap = __get_long_cap() - 1;
2638 __sz = __get_long_size();
2639 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002641 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002643 __is_short = !__is_long();
2644 }
2645 pointer __p;
2646 if (__is_short)
2647 {
2648 __p = __get_short_pointer() + __sz;
2649 __set_short_size(__sz+1);
2650 }
2651 else
2652 {
2653 __p = __get_long_pointer() + __sz;
2654 __set_long_size(__sz+1);
2655 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656 traits_type::assign(*__p, __c);
2657 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658}
2659
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660template <class _CharT, class _Traits, class _Allocator>
2661template<class _ForwardIterator>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002662_EnableIf
2663<
2664 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2665 basic_string<_CharT, _Traits, _Allocator>&
2666>
2667basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002668 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669{
2670 size_type __sz = size();
2671 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002672 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673 if (__n)
2674 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002675 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2676 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002677 {
2678 if (__cap - __sz < __n)
2679 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2680 pointer __p = __get_pointer() + __sz;
2681 for (; __first != __last; ++__p, ++__first)
2682 traits_type::assign(*__p, *__first);
2683 traits_type::assign(*__p, value_type());
2684 __set_size(__sz + __n);
2685 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002686 else
2687 {
2688 const basic_string __temp(__first, __last, __alloc());
2689 append(__temp.data(), __temp.size());
2690 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 }
2692 return *this;
2693}
2694
2695template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002696inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697basic_string<_CharT, _Traits, _Allocator>&
2698basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2699{
2700 return append(__str.data(), __str.size());
2701}
2702
2703template <class _CharT, class _Traits, class _Allocator>
2704basic_string<_CharT, _Traits, _Allocator>&
2705basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2706{
2707 size_type __sz = __str.size();
2708 if (__pos > __sz)
2709 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002710 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711}
2712
2713template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002714template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002715 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002716 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002717 __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 +00002718 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002719 >
Marshall Clow82513342016-09-24 22:45:42 +00002720basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002721{
Marshall Clow82513342016-09-24 22:45:42 +00002722 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002723 size_type __sz = __sv.size();
2724 if (__pos > __sz)
2725 this->__throw_out_of_range();
2726 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2727}
2728
2729template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002731basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002733 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734 return append(__s, traits_type::length(__s));
2735}
2736
2737// insert
2738
2739template <class _CharT, class _Traits, class _Allocator>
2740basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002741basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002743 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744 size_type __sz = size();
2745 if (__pos > __sz)
2746 this->__throw_out_of_range();
2747 size_type __cap = capacity();
2748 if (__cap - __sz >= __n)
2749 {
2750 if (__n)
2751 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002752 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753 size_type __n_move = __sz - __pos;
2754 if (__n_move != 0)
2755 {
2756 if (__p + __pos <= __s && __s < __p + __sz)
2757 __s += __n;
2758 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2759 }
2760 traits_type::move(__p + __pos, __s, __n);
2761 __sz += __n;
2762 __set_size(__sz);
2763 traits_type::assign(__p[__sz], value_type());
2764 }
2765 }
2766 else
2767 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2768 return *this;
2769}
2770
2771template <class _CharT, class _Traits, class _Allocator>
2772basic_string<_CharT, _Traits, _Allocator>&
2773basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2774{
2775 size_type __sz = size();
2776 if (__pos > __sz)
2777 this->__throw_out_of_range();
2778 if (__n)
2779 {
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 - __pos;
2786 if (__n_move != 0)
2787 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2788 }
2789 else
2790 {
2791 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 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 traits_type::assign(__p + __pos, __n, __c);
2795 __sz += __n;
2796 __set_size(__sz);
2797 traits_type::assign(__p[__sz], value_type());
2798 }
2799 return *this;
2800}
2801
2802template <class _CharT, class _Traits, class _Allocator>
2803template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002804_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002806 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002807 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002808>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2810{
Louis Dionneba400782020-10-02 15:02:52 -04002811#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002812 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2813 "string::insert(iterator, range) called with an iterator not"
2814 " referring to this string");
2815#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002816 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002817 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818}
2819
2820template <class _CharT, class _Traits, class _Allocator>
2821template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002822_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002824 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002826>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2828{
Louis Dionneba400782020-10-02 15:02:52 -04002829#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002830 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2831 "string::insert(iterator, range) called with an iterator not"
2832 " referring to this string");
2833#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002835 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 if (__n)
2837 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002838 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2839 !__addr_in_range(*__first))
2840 {
2841 size_type __sz = size();
2842 size_type __cap = capacity();
2843 value_type* __p;
2844 if (__cap - __sz >= __n)
2845 {
2846 __p = _VSTD::__to_address(__get_pointer());
2847 size_type __n_move = __sz - __ip;
2848 if (__n_move != 0)
2849 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2850 }
2851 else
2852 {
2853 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2854 __p = _VSTD::__to_address(__get_long_pointer());
2855 }
2856 __sz += __n;
2857 __set_size(__sz);
2858 traits_type::assign(__p[__sz], value_type());
2859 for (__p += __ip; __first != __last; ++__p, ++__first)
2860 traits_type::assign(*__p, *__first);
2861 }
2862 else
Marshall Clow958362f2016-09-05 01:54:30 +00002863 {
2864 const basic_string __temp(__first, __last, __alloc());
2865 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2866 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 }
2868 return begin() + __ip;
2869}
2870
2871template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002872inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873basic_string<_CharT, _Traits, _Allocator>&
2874basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2875{
2876 return insert(__pos1, __str.data(), __str.size());
2877}
2878
2879template <class _CharT, class _Traits, class _Allocator>
2880basic_string<_CharT, _Traits, _Allocator>&
2881basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2882 size_type __pos2, size_type __n)
2883{
2884 size_type __str_sz = __str.size();
2885 if (__pos2 > __str_sz)
2886 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002887 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888}
2889
2890template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002891template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002892_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002893<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002894 __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 +00002895 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002896>
Marshall Clow82513342016-09-24 22:45:42 +00002897basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002898 size_type __pos2, size_type __n)
2899{
Marshall Clow82513342016-09-24 22:45:42 +00002900 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002901 size_type __str_sz = __sv.size();
2902 if (__pos2 > __str_sz)
2903 this->__throw_out_of_range();
2904 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2905}
2906
2907template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002909basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002911 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 return insert(__pos, __s, traits_type::length(__s));
2913}
2914
2915template <class _CharT, class _Traits, class _Allocator>
2916typename basic_string<_CharT, _Traits, _Allocator>::iterator
2917basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2918{
2919 size_type __ip = static_cast<size_type>(__pos - begin());
2920 size_type __sz = size();
2921 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002922 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 if (__cap == __sz)
2924 {
2925 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002926 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 }
2928 else
2929 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002930 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 size_type __n_move = __sz - __ip;
2932 if (__n_move != 0)
2933 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2934 }
2935 traits_type::assign(__p[__ip], __c);
2936 traits_type::assign(__p[++__sz], value_type());
2937 __set_size(__sz);
2938 return begin() + static_cast<difference_type>(__ip);
2939}
2940
2941template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002942inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943typename basic_string<_CharT, _Traits, _Allocator>::iterator
2944basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2945{
Louis Dionneba400782020-10-02 15:02:52 -04002946#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002947 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2948 "string::insert(iterator, n, value) called with an iterator not"
2949 " referring to this string");
2950#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951 difference_type __p = __pos - begin();
2952 insert(static_cast<size_type>(__p), __n, __c);
2953 return begin() + __p;
2954}
2955
2956// replace
2957
2958template <class _CharT, class _Traits, class _Allocator>
2959basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002960basic_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 +00002961 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002963 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 size_type __sz = size();
2965 if (__pos > __sz)
2966 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002967 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968 size_type __cap = capacity();
2969 if (__cap - __sz + __n1 >= __n2)
2970 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002971 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972 if (__n1 != __n2)
2973 {
2974 size_type __n_move = __sz - __pos - __n1;
2975 if (__n_move != 0)
2976 {
2977 if (__n1 > __n2)
2978 {
2979 traits_type::move(__p + __pos, __s, __n2);
2980 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2981 goto __finish;
2982 }
2983 if (__p + __pos < __s && __s < __p + __sz)
2984 {
2985 if (__p + __pos + __n1 <= __s)
2986 __s += __n2 - __n1;
2987 else // __p + __pos < __s < __p + __pos + __n1
2988 {
2989 traits_type::move(__p + __pos, __s, __n1);
2990 __pos += __n1;
2991 __s += __n2;
2992 __n2 -= __n1;
2993 __n1 = 0;
2994 }
2995 }
2996 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2997 }
2998 }
2999 traits_type::move(__p + __pos, __s, __n2);
3000__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05003001// __sz += __n2 - __n1; in this and the below function below can cause unsigned
3002// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00003003 __sz += __n2 - __n1;
3004 __set_size(__sz);
3005 __invalidate_iterators_past(__sz);
3006 traits_type::assign(__p[__sz], value_type());
3007 }
3008 else
3009 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3010 return *this;
3011}
3012
3013template <class _CharT, class _Traits, class _Allocator>
3014basic_string<_CharT, _Traits, _Allocator>&
3015basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00003016 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003017{
3018 size_type __sz = size();
3019 if (__pos > __sz)
3020 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003021 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003022 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003023 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024 if (__cap - __sz + __n1 >= __n2)
3025 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003026 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003027 if (__n1 != __n2)
3028 {
3029 size_type __n_move = __sz - __pos - __n1;
3030 if (__n_move != 0)
3031 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3032 }
3033 }
3034 else
3035 {
3036 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003037 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038 }
3039 traits_type::assign(__p + __pos, __n2, __c);
3040 __sz += __n2 - __n1;
3041 __set_size(__sz);
3042 __invalidate_iterators_past(__sz);
3043 traits_type::assign(__p[__sz], value_type());
3044 return *this;
3045}
3046
3047template <class _CharT, class _Traits, class _Allocator>
3048template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003049_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003051 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003053>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003054basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055 _InputIterator __j1, _InputIterator __j2)
3056{
Marshall Clow958362f2016-09-05 01:54:30 +00003057 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003058 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059}
3060
3061template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003062inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063basic_string<_CharT, _Traits, _Allocator>&
3064basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3065{
3066 return replace(__pos1, __n1, __str.data(), __str.size());
3067}
3068
3069template <class _CharT, class _Traits, class _Allocator>
3070basic_string<_CharT, _Traits, _Allocator>&
3071basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3072 size_type __pos2, size_type __n2)
3073{
3074 size_type __str_sz = __str.size();
3075 if (__pos2 > __str_sz)
3076 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003077 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078}
3079
3080template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003081template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003082_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003083<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003084 __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 +00003085 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003086>
Marshall Clow82513342016-09-24 22:45:42 +00003087basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003088 size_type __pos2, size_type __n2)
3089{
Marshall Clow82513342016-09-24 22:45:42 +00003090 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003091 size_type __str_sz = __sv.size();
3092 if (__pos2 > __str_sz)
3093 this->__throw_out_of_range();
3094 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3095}
3096
3097template <class _CharT, class _Traits, class _Allocator>
3098basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003099basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003101 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102 return replace(__pos, __n1, __s, traits_type::length(__s));
3103}
3104
3105template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003106inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003108basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109{
3110 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3111 __str.data(), __str.size());
3112}
3113
3114template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003115inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003117basic_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 +00003118{
3119 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3120}
3121
3122template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003123inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003125basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126{
3127 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3128}
3129
3130template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003131inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003133basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134{
3135 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3136}
3137
3138// erase
3139
Martijn Velsa81fc792020-02-26 13:25:43 -05003140// 'externally instantiated' erase() implementation, called when __n != npos.
3141// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003143void
3144basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3145 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 if (__n)
3148 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003149 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003150 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003151 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152 size_type __n_move = __sz - __pos - __n;
3153 if (__n_move != 0)
3154 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3155 __sz -= __n;
3156 __set_size(__sz);
3157 __invalidate_iterators_past(__sz);
3158 traits_type::assign(__p[__sz], value_type());
3159 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003160}
3161
3162template <class _CharT, class _Traits, class _Allocator>
3163basic_string<_CharT, _Traits, _Allocator>&
3164basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3165 size_type __n) {
3166 if (__pos > size()) this->__throw_out_of_range();
3167 if (__n == npos) {
3168 __erase_to_end(__pos);
3169 } else {
3170 __erase_external_with_move(__pos, __n);
3171 }
3172 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173}
3174
3175template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003176inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177typename basic_string<_CharT, _Traits, _Allocator>::iterator
3178basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3179{
Louis Dionneba400782020-10-02 15:02:52 -04003180#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003181 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3182 "string::erase(iterator) called with an iterator not"
3183 " referring to this string");
3184#endif
3185 _LIBCPP_ASSERT(__pos != end(),
3186 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187 iterator __b = begin();
3188 size_type __r = static_cast<size_type>(__pos - __b);
3189 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003190 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191}
3192
3193template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003194inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195typename basic_string<_CharT, _Traits, _Allocator>::iterator
3196basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3197{
Louis Dionneba400782020-10-02 15:02:52 -04003198#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003199 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3200 "string::erase(iterator, iterator) called with an iterator not"
3201 " referring to this string");
3202#endif
3203 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204 iterator __b = begin();
3205 size_type __r = static_cast<size_type>(__first - __b);
3206 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003207 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208}
3209
3210template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003211inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212void
3213basic_string<_CharT, _Traits, _Allocator>::pop_back()
3214{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003215 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216 size_type __sz;
3217 if (__is_long())
3218 {
3219 __sz = __get_long_size() - 1;
3220 __set_long_size(__sz);
3221 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3222 }
3223 else
3224 {
3225 __sz = __get_short_size() - 1;
3226 __set_short_size(__sz);
3227 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3228 }
3229 __invalidate_iterators_past(__sz);
3230}
3231
3232template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003233inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003235basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003236{
3237 __invalidate_all_iterators();
3238 if (__is_long())
3239 {
3240 traits_type::assign(*__get_long_pointer(), value_type());
3241 __set_long_size(0);
3242 }
3243 else
3244 {
3245 traits_type::assign(*__get_short_pointer(), value_type());
3246 __set_short_size(0);
3247 }
3248}
3249
3250template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003251inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252void
3253basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3254{
3255 if (__is_long())
3256 {
3257 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3258 __set_long_size(__pos);
3259 }
3260 else
3261 {
3262 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3263 __set_short_size(__pos);
3264 }
3265 __invalidate_iterators_past(__pos);
3266}
3267
3268template <class _CharT, class _Traits, class _Allocator>
3269void
3270basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3271{
3272 size_type __sz = size();
3273 if (__n > __sz)
3274 append(__n - __sz, __c);
3275 else
3276 __erase_to_end(__n);
3277}
3278
3279template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003280inline void
3281basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3282{
3283 size_type __sz = size();
3284 if (__n > __sz) {
3285 __append_default_init(__n - __sz);
3286 } else
3287 __erase_to_end(__n);
3288}
3289
3290template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003291inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003292typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003293basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003295 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003296#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003297 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003299 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300#endif
3301}
3302
3303template <class _CharT, class _Traits, class _Allocator>
3304void
Marek Kurdejc9848142020-11-26 10:07:16 +01003305basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003306{
Marek Kurdejc9848142020-11-26 10:07:16 +01003307 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003309
3310#if _LIBCPP_STD_VER > 17
3311 // Reserve never shrinks as of C++20.
3312 if (__requested_capacity <= capacity()) return;
3313#endif
3314
3315 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3316 __target_capacity = __recommend(__target_capacity);
3317 if (__target_capacity == capacity()) return;
3318
3319 __shrink_or_extend(__target_capacity);
3320}
3321
3322template <class _CharT, class _Traits, class _Allocator>
3323void
3324basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3325{
3326 size_type __target_capacity = __recommend(size());
3327 if (__target_capacity == capacity()) return;
3328
3329 __shrink_or_extend(__target_capacity);
3330}
3331
3332template <class _CharT, class _Traits, class _Allocator>
3333void
3334basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3335{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003336 size_type __cap = capacity();
3337 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003338
3339 pointer __new_data, __p;
3340 bool __was_long, __now_long;
3341 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003343 __was_long = true;
3344 __now_long = false;
3345 __new_data = __get_short_pointer();
3346 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003348 else
3349 {
3350 if (__target_capacity > __cap)
3351 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3352 else
3353 {
3354 #ifndef _LIBCPP_NO_EXCEPTIONS
3355 try
3356 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003357 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003358 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3359 #ifndef _LIBCPP_NO_EXCEPTIONS
3360 }
3361 catch (...)
3362 {
3363 return;
3364 }
3365 #else // _LIBCPP_NO_EXCEPTIONS
3366 if (__new_data == nullptr)
3367 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003368 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003369 }
3370 __now_long = true;
3371 __was_long = __is_long();
3372 __p = __get_pointer();
3373 }
3374 traits_type::copy(_VSTD::__to_address(__new_data),
3375 _VSTD::__to_address(__p), size()+1);
3376 if (__was_long)
3377 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3378 if (__now_long)
3379 {
3380 __set_long_cap(__target_capacity+1);
3381 __set_long_size(__sz);
3382 __set_long_pointer(__new_data);
3383 }
3384 else
3385 __set_short_size(__sz);
3386 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387}
3388
3389template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003390inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003392basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003394 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395 return *(data() + __pos);
3396}
3397
3398template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003399inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003401basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003402{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003403 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404 return *(__get_pointer() + __pos);
3405}
3406
3407template <class _CharT, class _Traits, class _Allocator>
3408typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3409basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3410{
3411 if (__n >= size())
3412 this->__throw_out_of_range();
3413 return (*this)[__n];
3414}
3415
3416template <class _CharT, class _Traits, class _Allocator>
3417typename basic_string<_CharT, _Traits, _Allocator>::reference
3418basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3419{
3420 if (__n >= size())
3421 this->__throw_out_of_range();
3422 return (*this)[__n];
3423}
3424
3425template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003426inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003428basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003429{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003430 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431 return *__get_pointer();
3432}
3433
3434template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003435inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003437basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003439 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440 return *data();
3441}
3442
3443template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003444inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003446basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003448 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449 return *(__get_pointer() + size() - 1);
3450}
3451
3452template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003453inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003455basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003457 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458 return *(data() + size() - 1);
3459}
3460
3461template <class _CharT, class _Traits, class _Allocator>
3462typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003463basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464{
3465 size_type __sz = size();
3466 if (__pos > __sz)
3467 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003468 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469 traits_type::copy(__s, data() + __pos, __rlen);
3470 return __rlen;
3471}
3472
3473template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003474inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475basic_string<_CharT, _Traits, _Allocator>
3476basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3477{
3478 return basic_string(*this, __pos, __n, __alloc());
3479}
3480
3481template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003482inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483void
3484basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003485#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003486 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003487#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003488 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003489 __is_nothrow_swappable<allocator_type>::value)
3490#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491{
Louis Dionneba400782020-10-02 15:02:52 -04003492#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003493 if (!__is_long())
3494 __get_db()->__invalidate_all(this);
3495 if (!__str.__is_long())
3496 __get_db()->__invalidate_all(&__str);
3497 __get_db()->swap(this, &__str);
3498#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003499 _LIBCPP_ASSERT(
3500 __alloc_traits::propagate_on_container_swap::value ||
3501 __alloc_traits::is_always_equal::value ||
3502 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003503 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003504 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505}
3506
3507// find
3508
3509template <class _Traits>
3510struct _LIBCPP_HIDDEN __traits_eq
3511{
3512 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003513 _LIBCPP_INLINE_VISIBILITY
3514 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3515 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516};
3517
3518template<class _CharT, class _Traits, class _Allocator>
3519typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003520basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003521 size_type __pos,
3522 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003524 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003525 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003526 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527}
3528
3529template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003530inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003532basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3533 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003535 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003536 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537}
3538
3539template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003540template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003541_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003542<
3543 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3544 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003545>
Marshall Clowe46031a2018-07-02 18:41:15 +00003546basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003547 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003548{
Marshall Clowe46031a2018-07-02 18:41:15 +00003549 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003550 return __str_find<value_type, size_type, traits_type, npos>
3551 (data(), size(), __sv.data(), __pos, __sv.size());
3552}
3553
3554template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003555inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003556typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003557basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003558 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003560 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003561 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003562 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563}
3564
3565template<class _CharT, class _Traits, class _Allocator>
3566typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003567basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3568 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003570 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003571 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572}
3573
3574// rfind
3575
3576template<class _CharT, class _Traits, class _Allocator>
3577typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003578basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003579 size_type __pos,
3580 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003582 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003583 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003584 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585}
3586
3587template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003588inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003590basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3591 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003593 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003594 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595}
3596
3597template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003598template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003599_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003600<
3601 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3602 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003603>
Marshall Clowe46031a2018-07-02 18:41:15 +00003604basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003605 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003606{
Marshall Clowe46031a2018-07-02 18:41:15 +00003607 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003608 return __str_rfind<value_type, size_type, traits_type, npos>
3609 (data(), size(), __sv.data(), __pos, __sv.size());
3610}
3611
3612template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003613inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003614typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003615basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003616 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003618 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003619 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003620 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621}
3622
3623template<class _CharT, class _Traits, class _Allocator>
3624typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003625basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3626 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003628 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003629 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630}
3631
3632// find_first_of
3633
3634template<class _CharT, class _Traits, class _Allocator>
3635typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003636basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003637 size_type __pos,
3638 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003640 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003641 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003642 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643}
3644
3645template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003646inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003648basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3649 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003651 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003652 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653}
3654
3655template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003656template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003657_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003658<
3659 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3660 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003661>
Marshall Clowe46031a2018-07-02 18:41:15 +00003662basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003663 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003664{
Marshall Clowe46031a2018-07-02 18:41:15 +00003665 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003666 return __str_find_first_of<value_type, size_type, traits_type, npos>
3667 (data(), size(), __sv.data(), __pos, __sv.size());
3668}
3669
3670template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003671inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003672typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003673basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003674 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003676 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003677 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003678 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679}
3680
3681template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003682inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003684basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3685 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686{
3687 return find(__c, __pos);
3688}
3689
3690// find_last_of
3691
3692template<class _CharT, class _Traits, class _Allocator>
3693typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003694basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003695 size_type __pos,
3696 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003698 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003699 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003700 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701}
3702
3703template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003704inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003706basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3707 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003709 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003710 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711}
3712
3713template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003714template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003715_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003716<
3717 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3718 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003719>
Marshall Clowe46031a2018-07-02 18:41:15 +00003720basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003721 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003722{
Marshall Clowe46031a2018-07-02 18:41:15 +00003723 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003724 return __str_find_last_of<value_type, size_type, traits_type, npos>
3725 (data(), size(), __sv.data(), __pos, __sv.size());
3726}
3727
3728template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003729inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003730typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003731basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003732 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003734 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003735 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003736 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737}
3738
3739template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003740inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003741typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003742basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3743 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744{
3745 return rfind(__c, __pos);
3746}
3747
3748// find_first_not_of
3749
3750template<class _CharT, class _Traits, class _Allocator>
3751typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003752basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003753 size_type __pos,
3754 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003756 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003757 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003758 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759}
3760
3761template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003762inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003764basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3765 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003767 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003768 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769}
3770
3771template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003772template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003773_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003774<
3775 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3776 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003777>
Marshall Clowe46031a2018-07-02 18:41:15 +00003778basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003779 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003780{
Marshall Clowe46031a2018-07-02 18:41:15 +00003781 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003782 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3783 (data(), size(), __sv.data(), __pos, __sv.size());
3784}
3785
3786template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003787inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003788typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003789basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003790 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003792 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003793 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003794 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795}
3796
3797template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003798inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003800basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3801 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003803 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003804 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003805}
3806
3807// find_last_not_of
3808
3809template<class _CharT, class _Traits, class _Allocator>
3810typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003811basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003812 size_type __pos,
3813 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003815 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003816 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003817 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818}
3819
3820template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003821inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003823basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3824 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003826 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003827 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828}
3829
3830template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003831template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003832_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003833<
3834 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3835 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003836>
Marshall Clowe46031a2018-07-02 18:41:15 +00003837basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003838 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003839{
Marshall Clowe46031a2018-07-02 18:41:15 +00003840 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003841 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3842 (data(), size(), __sv.data(), __pos, __sv.size());
3843}
3844
3845template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003846inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003847typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003848basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003849 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003850{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003851 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003852 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003853 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854}
3855
3856template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003857inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003859basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3860 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003862 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003863 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864}
3865
3866// compare
3867
3868template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003869template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003870_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003871<
3872 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3873 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003874>
zoecarver1997e0a2021-02-05 11:54:47 -08003875basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876{
Marshall Clowe46031a2018-07-02 18:41:15 +00003877 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003878 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003879 size_t __rhs_sz = __sv.size();
3880 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003881 _VSTD::min(__lhs_sz, __rhs_sz));
3882 if (__result != 0)
3883 return __result;
3884 if (__lhs_sz < __rhs_sz)
3885 return -1;
3886 if (__lhs_sz > __rhs_sz)
3887 return 1;
3888 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889}
3890
3891template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003892inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003894basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003895{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003896 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003897}
3898
3899template <class _CharT, class _Traits, class _Allocator>
3900int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003901basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3902 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003903 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003904 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003906 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907 size_type __sz = size();
3908 if (__pos1 > __sz || __n2 == npos)
3909 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003910 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3911 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912 if (__r == 0)
3913 {
3914 if (__rlen < __n2)
3915 __r = -1;
3916 else if (__rlen > __n2)
3917 __r = 1;
3918 }
3919 return __r;
3920}
3921
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003922template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003923template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003924_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003925<
3926 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3927 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003928>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003929basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3930 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003931 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003932{
Marshall Clowe46031a2018-07-02 18:41:15 +00003933 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003934 return compare(__pos1, __n1, __sv.data(), __sv.size());
3935}
3936
3937template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003938inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003939int
3940basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3941 size_type __n1,
3942 const basic_string& __str) const
3943{
3944 return compare(__pos1, __n1, __str.data(), __str.size());
3945}
3946
3947template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003948template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003949_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003950<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003951 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3952 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003953 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003954>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003955basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3956 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003957 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003958 size_type __pos2,
3959 size_type __n2) const
3960{
Marshall Clow82513342016-09-24 22:45:42 +00003961 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003962 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3963}
3964
3965template <class _CharT, class _Traits, class _Allocator>
3966int
3967basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3968 size_type __n1,
3969 const basic_string& __str,
3970 size_type __pos2,
3971 size_type __n2) const
3972{
3973 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3974}
3975
3976template <class _CharT, class _Traits, class _Allocator>
3977int
3978basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3979{
3980 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3981 return compare(0, npos, __s, traits_type::length(__s));
3982}
3983
3984template <class _CharT, class _Traits, class _Allocator>
3985int
3986basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3987 size_type __n1,
3988 const value_type* __s) const
3989{
3990 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3991 return compare(__pos1, __n1, __s, traits_type::length(__s));
3992}
3993
Howard Hinnantc51e1022010-05-11 19:42:16 +00003994// __invariants
3995
3996template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003997inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998bool
3999basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4000{
4001 if (size() > capacity())
4002 return false;
4003 if (capacity() < __min_cap - 1)
4004 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004005 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004007 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008 return false;
4009 return true;
4010}
4011
Vedant Kumar55e007e2018-03-08 21:15:26 +00004012// __clear_and_shrink
4013
4014template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004015inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004016void
Marshall Clowe60a7182018-05-29 17:04:37 +00004017basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004018{
4019 clear();
4020 if(__is_long())
4021 {
4022 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4023 __set_long_cap(0);
4024 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004025 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004026 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004027}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004028
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029// operator==
4030
4031template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004032inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033bool
4034operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004035 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004037 size_t __lhs_sz = __lhs.size();
4038 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4039 __rhs.data(),
4040 __lhs_sz) == 0;
4041}
4042
4043template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004044inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004045bool
4046operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4047 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4048{
4049 size_t __lhs_sz = __lhs.size();
4050 if (__lhs_sz != __rhs.size())
4051 return false;
4052 const char* __lp = __lhs.data();
4053 const char* __rp = __rhs.data();
4054 if (__lhs.__is_long())
4055 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4056 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4057 if (*__lp != *__rp)
4058 return false;
4059 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060}
4061
4062template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004063inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004065operator==(const _CharT* __lhs,
4066 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004068 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4069 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4070 size_t __lhs_len = _Traits::length(__lhs);
4071 if (__lhs_len != __rhs.size()) return false;
4072 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073}
4074
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004076inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004078operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4079 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004081 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4082 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4083 size_t __rhs_len = _Traits::length(__rhs);
4084 if (__rhs_len != __lhs.size()) return false;
4085 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086}
4087
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004088template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004089inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090bool
4091operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004092 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093{
4094 return !(__lhs == __rhs);
4095}
4096
4097template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004098inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004100operator!=(const _CharT* __lhs,
4101 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102{
4103 return !(__lhs == __rhs);
4104}
4105
4106template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004107inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004109operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4110 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111{
4112 return !(__lhs == __rhs);
4113}
4114
4115// operator<
4116
4117template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004118inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119bool
4120operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004121 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004123 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124}
4125
4126template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004127inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004129operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4130 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004132 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133}
4134
4135template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004136inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004138operator< (const _CharT* __lhs,
4139 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140{
4141 return __rhs.compare(__lhs) > 0;
4142}
4143
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144// operator>
4145
4146template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004147inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004148bool
4149operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004150 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151{
4152 return __rhs < __lhs;
4153}
4154
4155template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004156inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004158operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4159 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160{
4161 return __rhs < __lhs;
4162}
4163
4164template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004165inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004167operator> (const _CharT* __lhs,
4168 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169{
4170 return __rhs < __lhs;
4171}
4172
4173// operator<=
4174
4175template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004176inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177bool
4178operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004179 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180{
4181 return !(__rhs < __lhs);
4182}
4183
4184template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004185inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004187operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4188 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004189{
4190 return !(__rhs < __lhs);
4191}
4192
4193template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004194inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004196operator<=(const _CharT* __lhs,
4197 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198{
4199 return !(__rhs < __lhs);
4200}
4201
4202// operator>=
4203
4204template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004205inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206bool
4207operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004208 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209{
4210 return !(__lhs < __rhs);
4211}
4212
4213template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004214inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004216operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4217 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218{
4219 return !(__lhs < __rhs);
4220}
4221
4222template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004223inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004225operator>=(const _CharT* __lhs,
4226 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227{
4228 return !(__lhs < __rhs);
4229}
4230
4231// operator +
4232
4233template<class _CharT, class _Traits, class _Allocator>
4234basic_string<_CharT, _Traits, _Allocator>
4235operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4236 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4237{
4238 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4239 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4240 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4241 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4242 __r.append(__rhs.data(), __rhs_sz);
4243 return __r;
4244}
4245
4246template<class _CharT, class _Traits, class _Allocator>
4247basic_string<_CharT, _Traits, _Allocator>
4248operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4249{
4250 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4251 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4252 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4253 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4254 __r.append(__rhs.data(), __rhs_sz);
4255 return __r;
4256}
4257
4258template<class _CharT, class _Traits, class _Allocator>
4259basic_string<_CharT, _Traits, _Allocator>
4260operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4261{
4262 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4263 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4264 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4265 __r.append(__rhs.data(), __rhs_sz);
4266 return __r;
4267}
4268
4269template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004270inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271basic_string<_CharT, _Traits, _Allocator>
4272operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4273{
4274 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4275 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4276 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4277 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4278 __r.append(__rhs, __rhs_sz);
4279 return __r;
4280}
4281
4282template<class _CharT, class _Traits, class _Allocator>
4283basic_string<_CharT, _Traits, _Allocator>
4284operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4285{
4286 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4287 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4288 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4289 __r.push_back(__rhs);
4290 return __r;
4291}
4292
Eric Fiselierfc92be82017-04-19 00:28:44 +00004293#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294
4295template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004296inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297basic_string<_CharT, _Traits, _Allocator>
4298operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4299{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004300 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004301}
4302
4303template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004304inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305basic_string<_CharT, _Traits, _Allocator>
4306operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4307{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004308 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004309}
4310
4311template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004312inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313basic_string<_CharT, _Traits, _Allocator>
4314operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4315{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004316 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004317}
4318
4319template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004320inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321basic_string<_CharT, _Traits, _Allocator>
4322operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4323{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004324 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325}
4326
4327template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004328inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329basic_string<_CharT, _Traits, _Allocator>
4330operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4331{
4332 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004333 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334}
4335
4336template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004337inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338basic_string<_CharT, _Traits, _Allocator>
4339operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4340{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004341 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342}
4343
4344template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004345inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004346basic_string<_CharT, _Traits, _Allocator>
4347operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4348{
4349 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004350 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351}
4352
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004353#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354
4355// swap
4356
4357template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004358inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004360swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004361 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4362 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363{
4364 __lhs.swap(__rhs);
4365}
4366
Bruce Mitchener170d8972020-11-24 12:53:53 -05004367_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4368_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4369_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4370_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4371_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004372
Bruce Mitchener170d8972020-11-24 12:53:53 -05004373_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4374_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4375_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004376
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004377_LIBCPP_FUNC_VIS string to_string(int __val);
4378_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4379_LIBCPP_FUNC_VIS string to_string(long __val);
4380_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4381_LIBCPP_FUNC_VIS string to_string(long long __val);
4382_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4383_LIBCPP_FUNC_VIS string to_string(float __val);
4384_LIBCPP_FUNC_VIS string to_string(double __val);
4385_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004386
Bruce Mitchener170d8972020-11-24 12:53:53 -05004387_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4388_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4389_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4390_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4391_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004392
Bruce Mitchener170d8972020-11-24 12:53:53 -05004393_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4394_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4395_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004396
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004397_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4398_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4399_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4400_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4401_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4402_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4403_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4404_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4405_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004406
Howard Hinnantc51e1022010-05-11 19:42:16 +00004407template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004408_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004409const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4410 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004411
Marshall Clow851b9ec2019-05-20 21:56:51 +00004412template <class _CharT, class _Allocator>
4413struct _LIBCPP_TEMPLATE_VIS
4414 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4415 : public unary_function<
4416 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417{
4418 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004419 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4420 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004421};
4422
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423
Howard Hinnantdc095972011-07-18 15:51:59 +00004424template<class _CharT, class _Traits, class _Allocator>
4425basic_ostream<_CharT, _Traits>&
4426operator<<(basic_ostream<_CharT, _Traits>& __os,
4427 const basic_string<_CharT, _Traits, _Allocator>& __str);
4428
4429template<class _CharT, class _Traits, class _Allocator>
4430basic_istream<_CharT, _Traits>&
4431operator>>(basic_istream<_CharT, _Traits>& __is,
4432 basic_string<_CharT, _Traits, _Allocator>& __str);
4433
4434template<class _CharT, class _Traits, class _Allocator>
4435basic_istream<_CharT, _Traits>&
4436getline(basic_istream<_CharT, _Traits>& __is,
4437 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4438
4439template<class _CharT, class _Traits, class _Allocator>
4440inline _LIBCPP_INLINE_VISIBILITY
4441basic_istream<_CharT, _Traits>&
4442getline(basic_istream<_CharT, _Traits>& __is,
4443 basic_string<_CharT, _Traits, _Allocator>& __str);
4444
Eric Fiselierfc92be82017-04-19 00:28:44 +00004445#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004446
4447template<class _CharT, class _Traits, class _Allocator>
4448inline _LIBCPP_INLINE_VISIBILITY
4449basic_istream<_CharT, _Traits>&
4450getline(basic_istream<_CharT, _Traits>&& __is,
4451 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4452
4453template<class _CharT, class _Traits, class _Allocator>
4454inline _LIBCPP_INLINE_VISIBILITY
4455basic_istream<_CharT, _Traits>&
4456getline(basic_istream<_CharT, _Traits>&& __is,
4457 basic_string<_CharT, _Traits, _Allocator>& __str);
4458
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004459#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004460
Marshall Clow29b53f22018-12-14 18:49:35 +00004461#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004462template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004463inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004464 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4465 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4466 auto __old_size = __str.size();
4467 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4468 return __old_size - __str.size();
4469}
Marshall Clow29b53f22018-12-14 18:49:35 +00004470
Marek Kurdeja98b1412020-05-02 13:58:03 +02004471template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004472inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004473 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4474 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4475 _Predicate __pred) {
4476 auto __old_size = __str.size();
4477 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4478 __str.end());
4479 return __old_size - __str.size();
4480}
Marshall Clow29b53f22018-12-14 18:49:35 +00004481#endif
4482
Louis Dionneba400782020-10-02 15:02:52 -04004483#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004484
4485template<class _CharT, class _Traits, class _Allocator>
4486bool
4487basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4488{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004489 return this->data() <= _VSTD::__to_address(__i->base()) &&
4490 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004491}
4492
4493template<class _CharT, class _Traits, class _Allocator>
4494bool
4495basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4496{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004497 return this->data() < _VSTD::__to_address(__i->base()) &&
4498 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004499}
4500
4501template<class _CharT, class _Traits, class _Allocator>
4502bool
4503basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4504{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004505 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004506 return this->data() <= __p && __p <= this->data() + this->size();
4507}
4508
4509template<class _CharT, class _Traits, class _Allocator>
4510bool
4511basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4512{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004513 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004514 return this->data() <= __p && __p < this->data() + this->size();
4515}
4516
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004517#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004518
Louis Dionne173f29e2019-05-29 16:01:36 +00004519#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004520// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004521inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004522{
4523 inline namespace string_literals
4524 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004525 inline _LIBCPP_INLINE_VISIBILITY
4526 basic_string<char> operator "" s( const char *__str, size_t __len )
4527 {
4528 return basic_string<char> (__str, __len);
4529 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004530
Howard Hinnant5c167562013-08-07 19:39:48 +00004531 inline _LIBCPP_INLINE_VISIBILITY
4532 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4533 {
4534 return basic_string<wchar_t> (__str, __len);
4535 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004536
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004537#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004538 inline _LIBCPP_INLINE_VISIBILITY
4539 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4540 {
4541 return basic_string<char8_t> (__str, __len);
4542 }
4543#endif
4544
Howard Hinnant5c167562013-08-07 19:39:48 +00004545 inline _LIBCPP_INLINE_VISIBILITY
4546 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4547 {
4548 return basic_string<char16_t> (__str, __len);
4549 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004550
Howard Hinnant5c167562013-08-07 19:39:48 +00004551 inline _LIBCPP_INLINE_VISIBILITY
4552 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4553 {
4554 return basic_string<char32_t> (__str, __len);
4555 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004556 }
4557}
4558#endif
4559
Howard Hinnantc51e1022010-05-11 19:42:16 +00004560_LIBCPP_END_NAMESPACE_STD
4561
Eric Fiselierf4433a32017-05-31 22:07:49 +00004562_LIBCPP_POP_MACROS
4563
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004564#endif // _LIBCPP_STRING