blob: 5f49b4774e8b2341c0a9b3d18073548c66243840 [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>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000517#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518#include <iosfwd>
519#include <cstring>
Howard Hinnant155c2af2010-05-24 17:49:41 +0000520#include <cstdio> // For EOF.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521#include <cwchar>
522#include <algorithm>
523#include <iterator>
524#include <utility>
525#include <memory>
526#include <stdexcept>
527#include <type_traits>
528#include <initializer_list>
529#include <__functional_base>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000530#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
532#include <cstdint>
533#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000534
Eric Fiselier14b6de92014-08-10 23:53:08 +0000535#include <__debug>
536
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000537#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000538#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000539#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540
Eric Fiselierf4433a32017-05-31 22:07:49 +0000541_LIBCPP_PUSH_MACROS
542#include <__undef_macros>
543
544
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545_LIBCPP_BEGIN_NAMESPACE_STD
546
547// fpos
548
549template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000550class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551{
552private:
553 _StateT __st_;
554 streamoff __off_;
555public:
556 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
557
558 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
559
560 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
561 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
562
563 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
564 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
565 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
566 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
567};
568
569template <class _StateT>
570inline _LIBCPP_INLINE_VISIBILITY
571streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
572 {return streamoff(__x) - streamoff(__y);}
573
574template <class _StateT>
575inline _LIBCPP_INLINE_VISIBILITY
576bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
577 {return streamoff(__x) == streamoff(__y);}
578
579template <class _StateT>
580inline _LIBCPP_INLINE_VISIBILITY
581bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
582 {return streamoff(__x) != streamoff(__y);}
583
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584// basic_string
585
586template<class _CharT, class _Traits, class _Allocator>
587basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000588operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
589 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590
591template<class _CharT, class _Traits, class _Allocator>
592basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000593operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594
595template<class _CharT, class _Traits, class _Allocator>
596basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000597operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598
599template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000602operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603
604template<class _CharT, class _Traits, class _Allocator>
605basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000606operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607
Shoaib Meenaiea363712017-07-29 02:54:41 +0000608_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
609
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000611class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612{
613protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000614 _LIBCPP_NORETURN void __throw_length_error() const;
615 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616};
617
618template <bool __b>
619void
620__basic_string_common<__b>::__throw_length_error() const
621{
Marshall Clow8fea1612016-08-25 15:09:01 +0000622 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623}
624
625template <bool __b>
626void
627__basic_string_common<__b>::__throw_out_of_range() const
628{
Marshall Clow8fea1612016-08-25 15:09:01 +0000629 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630}
631
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000632_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000633
Marshall Clow039b2f02016-01-13 21:54:34 +0000634#ifdef _LIBCPP_NO_EXCEPTIONS
635template <class _Iter>
636struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
637#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
638template <class _Iter>
639struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
640#else
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500641template <class _Iter, bool = __is_cpp17_forward_iterator<_Iter>::value>
Marshall Clow039b2f02016-01-13 21:54:34 +0000642struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
Louis Dionne173f29e2019-05-29 16:01:36 +0000643 noexcept(++(declval<_Iter&>())) &&
644 is_nothrow_assignable<_Iter&, _Iter>::value &&
645 noexcept(declval<_Iter>() == declval<_Iter>()) &&
Marshall Clow039b2f02016-01-13 21:54:34 +0000646 noexcept(*declval<_Iter>())
647)) {};
648
Louis Dionne173f29e2019-05-29 16:01:36 +0000649template <class _Iter>
Marshall Clow039b2f02016-01-13 21:54:34 +0000650struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
651#endif
652
653
654template <class _Iter>
655struct __libcpp_string_gets_noexcept_iterator
656 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
657
Marshall Clow82513342016-09-24 22:45:42 +0000658template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500659struct __can_be_converted_to_string_view : public _BoolConstant<
660 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
661 !is_convertible<const _Tp&, const _CharT*>::value
662 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000663
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000664#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000665
666template <class _CharT, size_t = sizeof(_CharT)>
667struct __padding
668{
669 unsigned char __xx[sizeof(_CharT)-1];
670};
671
672template <class _CharT>
673struct __padding<_CharT, 1>
674{
675};
676
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000677#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000678
Richard Smith256954d2020-11-11 17:12:18 -0800679#ifndef _LIBCPP_NO_HAS_CHAR8_T
680typedef basic_string<char8_t> u8string;
681#endif
682
683#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
684typedef basic_string<char16_t> u16string;
685typedef basic_string<char32_t> u32string;
686#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
687
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000688template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800689class
690 _LIBCPP_TEMPLATE_VIS
691#ifndef _LIBCPP_NO_HAS_CHAR8_T
692 _LIBCPP_PREFERRED_NAME(u8string)
693#endif
694#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
695 _LIBCPP_PREFERRED_NAME(u16string)
696 _LIBCPP_PREFERRED_NAME(u32string)
697#endif
698 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699 : private __basic_string_common<true>
700{
701public:
702 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000703 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000705 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000707 typedef allocator_traits<allocator_type> __alloc_traits;
708 typedef typename __alloc_traits::size_type size_type;
709 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000710 typedef value_type& reference;
711 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000712 typedef typename __alloc_traits::pointer pointer;
713 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714
Marshall Clow79f33542018-03-21 00:36:05 +0000715 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
716 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
717 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
718 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000719 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000720 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000721 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000722
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723 typedef __wrap_iter<pointer> iterator;
724 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000725 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
726 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727
728private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000729
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000730#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000731
732 struct __long
733 {
734 pointer __data_;
735 size_type __size_;
736 size_type __cap_;
737 };
738
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000739#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000740 static const size_type __short_mask = 0x01;
741 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000742#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000743 static const size_type __short_mask = 0x80;
744 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant68bf1812013-04-30 21:44:48 +0000745#endif // _LIBCPP_BIG_ENDIAN
746
747 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
748 (sizeof(__long) - 1)/sizeof(value_type) : 2};
749
750 struct __short
751 {
752 value_type __data_[__min_cap];
753 struct
754 : __padding<value_type>
755 {
756 unsigned char __size_;
757 };
758 };
759
760#else
761
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762 struct __long
763 {
764 size_type __cap_;
765 size_type __size_;
766 pointer __data_;
767 };
768
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000769#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000770 static const size_type __short_mask = 0x80;
771 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000772#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000773 static const size_type __short_mask = 0x01;
774 static const size_type __long_mask = 0x1ul;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000775#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
778 (sizeof(__long) - 1)/sizeof(value_type) : 2};
779
780 struct __short
781 {
782 union
783 {
784 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000785 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 };
787 value_type __data_[__min_cap];
788 };
789
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000790#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000791
Howard Hinnant8ea98242013-08-23 17:37:05 +0000792 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793
Howard Hinnant8ea98242013-08-23 17:37:05 +0000794 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000795
796 struct __raw
797 {
798 size_type __words[__n_words];
799 };
800
801 struct __rep
802 {
803 union
804 {
805 __long __l;
806 __short __s;
807 __raw __r;
808 };
809 };
810
811 __compressed_pair<__rep, allocator_type> __r_;
812
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200814 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815 static const size_type npos = -1;
816
Howard Hinnant3e276872011-06-03 18:40:47 +0000817 _LIBCPP_INLINE_VISIBILITY basic_string()
818 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000819
820 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
821#if _LIBCPP_STD_VER <= 14
822 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
823#else
824 _NOEXCEPT;
825#endif
826
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827 basic_string(const basic_string& __str);
828 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000829
Eric Fiselierfc92be82017-04-19 00:28:44 +0000830#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000832 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000833#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000834 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000835#else
836 _NOEXCEPT;
837#endif
838
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000840 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000841#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000842
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500843 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000844 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500845 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000846 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
847 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400848# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000849 __get_db()->__insert_c(this);
850# endif
851 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000852
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500853 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000854 _LIBCPP_INLINE_VISIBILITY
855 basic_string(const _CharT* __s, const _Allocator& __a);
856
Howard Hinnantcf823322010-12-17 14:46:43 +0000857 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000858 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000859 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000860 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000861 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000862 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000863
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500864 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000865 _LIBCPP_INLINE_VISIBILITY
866 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
867
Marshall Clow83445802016-04-07 18:13:41 +0000868 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000869 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000870 _LIBCPP_INLINE_VISIBILITY
871 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000872 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000873
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500874 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 +0000875 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000876 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500877 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000878
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500879 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
880 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000881 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
882 explicit basic_string(const _Tp& __t);
883
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500884 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000885 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
886 explicit basic_string(const _Tp& __t, const allocator_type& __a);
887
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500888 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500891 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000894#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000895 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000896 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000897 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000898 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000899#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000901 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000903 _LIBCPP_INLINE_VISIBILITY
904 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
905
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000906 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000907
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500908 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 +0000909 basic_string& operator=(const _Tp& __t)
910 {__self_view __sv = __t; return assign(__sv);}
911
Eric Fiselierfc92be82017-04-19 00:28:44 +0000912#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000914 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000915 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000916 _LIBCPP_INLINE_VISIBILITY
917 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000919 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921
Louis Dionneba400782020-10-02 15:02:52 -0400922#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000923 _LIBCPP_INLINE_VISIBILITY
924 iterator begin() _NOEXCEPT
925 {return iterator(this, __get_pointer());}
926 _LIBCPP_INLINE_VISIBILITY
927 const_iterator begin() const _NOEXCEPT
928 {return const_iterator(this, __get_pointer());}
929 _LIBCPP_INLINE_VISIBILITY
930 iterator end() _NOEXCEPT
931 {return iterator(this, __get_pointer() + size());}
932 _LIBCPP_INLINE_VISIBILITY
933 const_iterator end() const _NOEXCEPT
934 {return const_iterator(this, __get_pointer() + size());}
935#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000936 _LIBCPP_INLINE_VISIBILITY
937 iterator begin() _NOEXCEPT
938 {return iterator(__get_pointer());}
939 _LIBCPP_INLINE_VISIBILITY
940 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000941 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000942 _LIBCPP_INLINE_VISIBILITY
943 iterator end() _NOEXCEPT
944 {return iterator(__get_pointer() + size());}
945 _LIBCPP_INLINE_VISIBILITY
946 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000947 {return const_iterator(__get_pointer() + size());}
Louis Dionneba400782020-10-02 15:02:52 -0400948#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000949 _LIBCPP_INLINE_VISIBILITY
950 reverse_iterator rbegin() _NOEXCEPT
951 {return reverse_iterator(end());}
952 _LIBCPP_INLINE_VISIBILITY
953 const_reverse_iterator rbegin() const _NOEXCEPT
954 {return const_reverse_iterator(end());}
955 _LIBCPP_INLINE_VISIBILITY
956 reverse_iterator rend() _NOEXCEPT
957 {return reverse_iterator(begin());}
958 _LIBCPP_INLINE_VISIBILITY
959 const_reverse_iterator rend() const _NOEXCEPT
960 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000962 _LIBCPP_INLINE_VISIBILITY
963 const_iterator cbegin() const _NOEXCEPT
964 {return begin();}
965 _LIBCPP_INLINE_VISIBILITY
966 const_iterator cend() const _NOEXCEPT
967 {return end();}
968 _LIBCPP_INLINE_VISIBILITY
969 const_reverse_iterator crbegin() const _NOEXCEPT
970 {return rbegin();}
971 _LIBCPP_INLINE_VISIBILITY
972 const_reverse_iterator crend() const _NOEXCEPT
973 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000975 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000977 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
978 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
979 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000980 {return (__is_long() ? __get_long_cap()
981 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982
983 void resize(size_type __n, value_type __c);
984 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
985
Marek Kurdejc9848142020-11-26 10:07:16 +0100986 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000987 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
988
Marek Kurdejc9848142020-11-26 10:07:16 +0100989 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
990 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000991 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100992 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000994 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000995 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
996 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000998 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
999 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000
1001 const_reference at(size_type __n) const;
1002 reference at(size_type __n);
1003
1004 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +00001005
1006 template <class _Tp>
1007 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001008 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001009 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001010 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1011 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001012 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001013 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001014 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001015 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001017#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001019#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020
Howard Hinnantcf823322010-12-17 14:46:43 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001023
1024 template <class _Tp>
1025 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 _EnableIf<
1027 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1028 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001029 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001030 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001031 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001032 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001033
Marshall Clow62953962016-10-03 23:40:48 +00001034 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001035 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001036 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001037 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001038 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1039 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001040 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001041 >
Marshall Clow82513342016-09-24 22:45:42 +00001042 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001043 basic_string& append(const value_type* __s, size_type __n);
1044 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001046
1047 _LIBCPP_INLINE_VISIBILITY
1048 void __append_default_init(size_type __n);
1049
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001050 template <class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001051 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1052 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001054 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001055 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001057 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001058 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001060 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001061 _LIBCPP_INLINE_VISIBILITY
1062 append(_InputIterator __first, _InputIterator __last) {
1063 const basic_string __temp (__first, __last, __alloc());
1064 append(__temp.data(), __temp.size());
1065 return *this;
1066 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001068 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001069 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001071 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001072 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001074 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001075 _LIBCPP_INLINE_VISIBILITY
1076 append(_ForwardIterator __first, _ForwardIterator __last) {
1077 return __append_forward_unsafe(__first, __last);
1078 }
1079
Eric Fiselierfc92be82017-04-19 00:28:44 +00001080#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001083#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084
1085 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001088 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1089 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1090 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1091 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092
Marshall Clowe46031a2018-07-02 18:41:15 +00001093 template <class _Tp>
1094 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001096 <
1097 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1098 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001099 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001100 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001101 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001102 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001103#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001104 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001105 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001106 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001107 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001108#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001109 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001110 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001111 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001112 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001113 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1115 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001116 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001117 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001118 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001119 basic_string& assign(const value_type* __s, size_type __n);
1120 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 basic_string& assign(size_type __n, value_type __c);
1122 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001123 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001124 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001126 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001127 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001129 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 assign(_InputIterator __first, _InputIterator __last);
1131 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001132 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001135 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001136 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001138 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001140#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001143#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144
Howard Hinnantcf823322010-12-17 14:46:43 +00001145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001147
1148 template <class _Tp>
1149 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001150 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001151 <
1152 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1153 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001154 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001155 insert(size_type __pos1, const _Tp& __t)
1156 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1157
Marshall Clow82513342016-09-24 22:45:42 +00001158 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001159 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001160 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001161 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001162 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001163 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001164 >
Marshall Clow82513342016-09-24 22:45:42 +00001165 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001166 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001167 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1168 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1170 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1173 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001174 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001175 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001177 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001178 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001180 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1182 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001183 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001184 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001186 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001187 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001189 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001191#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1194 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001195#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196
1197 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 iterator erase(const_iterator __first, const_iterator __last);
1202
Howard Hinnantcf823322010-12-17 14:46:43 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 basic_string& replace(size_type __pos1, size_type __n1, 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(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 +00001214 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 +00001215 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001216 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001217 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001218 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001219 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001220 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001221 >
Marshall Clow82513342016-09-24 22:45:42 +00001222 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001223 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1224 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001227 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001228
1229 template <class _Tp>
1230 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001231 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001232 <
1233 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1234 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001235 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001236 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1237
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001239 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001241 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001243 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001245 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001246 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001248 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001250 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001251 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001252#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001254 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001256#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257
Howard Hinnantd17880b2013-06-28 16:59:19 +00001258 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1261
Howard Hinnantcf823322010-12-17 14:46:43 +00001262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001263 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001264#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001265 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001266#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001267 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001268 __is_nothrow_swappable<allocator_type>::value);
1269#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Howard Hinnantcf823322010-12-17 14:46:43 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001272 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001273 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001274 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001275#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001276 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001277 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001278#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279
Howard Hinnantcf823322010-12-17 14:46:43 +00001280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001281 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282
Howard Hinnantcf823322010-12-17 14:46:43 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001284 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001285
1286 template <class _Tp>
1287 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001288 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001289 <
1290 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1291 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001292 >
zoecarver1997e0a2021-02-05 11:54:47 -08001293 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001294 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001296 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001297 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298
Howard Hinnantcf823322010-12-17 14:46:43 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001300 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001301
1302 template <class _Tp>
1303 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001304 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001305 <
1306 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1307 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001308 >
zoecarver1997e0a2021-02-05 11:54:47 -08001309 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001310 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001312 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001313 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001314
Howard Hinnantcf823322010-12-17 14:46:43 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001316 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001317
1318 template <class _Tp>
1319 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001320 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001321 <
1322 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1323 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001324 >
zoecarver1997e0a2021-02-05 11:54:47 -08001325 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001326 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001328 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001330 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
Howard Hinnantcf823322010-12-17 14:46:43 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001333 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001334
1335 template <class _Tp>
1336 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001337 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001338 <
1339 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1340 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001341 >
zoecarver1997e0a2021-02-05 11:54:47 -08001342 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001343 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001345 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001347 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348
Howard Hinnantcf823322010-12-17 14:46:43 +00001349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001350 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001351
1352 template <class _Tp>
1353 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001354 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001355 <
1356 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1357 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001358 >
zoecarver1997e0a2021-02-05 11:54:47 -08001359 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001360 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 +00001361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001362 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001363 _LIBCPP_INLINE_VISIBILITY
1364 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1365
1366 _LIBCPP_INLINE_VISIBILITY
1367 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001368
1369 template <class _Tp>
1370 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001371 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001372 <
1373 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1374 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001375 >
zoecarver1997e0a2021-02-05 11:54:47 -08001376 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001377 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 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001379 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001380 _LIBCPP_INLINE_VISIBILITY
1381 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1382
1383 _LIBCPP_INLINE_VISIBILITY
1384 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001385
1386 template <class _Tp>
1387 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001388 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001389 <
1390 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1391 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001392 >
zoecarver1997e0a2021-02-05 11:54:47 -08001393 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001394
1395 template <class _Tp>
1396 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001397 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001398 <
1399 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1400 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001401 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001402 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1403
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001406 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 +00001407
Marshall Clow82513342016-09-24 22:45:42 +00001408 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001409 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001410 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001411 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001412 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001413 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001414 >
Marshall Clow82513342016-09-24 22:45:42 +00001415 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 +00001416 int compare(const value_type* __s) const _NOEXCEPT;
1417 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1418 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419
Marshall Clow18c293b2017-12-04 20:11:38 +00001420#if _LIBCPP_STD_VER > 17
1421 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1422 bool starts_with(__self_view __sv) const _NOEXCEPT
1423 { return __self_view(data(), size()).starts_with(__sv); }
1424
1425 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1426 bool starts_with(value_type __c) const _NOEXCEPT
1427 { return !empty() && _Traits::eq(front(), __c); }
1428
1429 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1430 bool starts_with(const value_type* __s) const _NOEXCEPT
1431 { return starts_with(__self_view(__s)); }
1432
1433 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1434 bool ends_with(__self_view __sv) const _NOEXCEPT
1435 { return __self_view(data(), size()).ends_with( __sv); }
1436
1437 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1438 bool ends_with(value_type __c) const _NOEXCEPT
1439 { return !empty() && _Traits::eq(back(), __c); }
1440
1441 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1442 bool ends_with(const value_type* __s) const _NOEXCEPT
1443 { return ends_with(__self_view(__s)); }
1444#endif
1445
Wim Leflere023c3542021-01-19 14:33:30 -05001446#if _LIBCPP_STD_VER > 20
1447 constexpr _LIBCPP_INLINE_VISIBILITY
1448 bool contains(__self_view __sv) const noexcept
1449 { return __self_view(data(), size()).contains(__sv); }
1450
1451 constexpr _LIBCPP_INLINE_VISIBILITY
1452 bool contains(value_type __c) const noexcept
1453 { return __self_view(data(), size()).contains(__c); }
1454
1455 constexpr _LIBCPP_INLINE_VISIBILITY
1456 bool contains(const value_type* __s) const
1457 { return __self_view(data(), size()).contains(__s); }
1458#endif
1459
Howard Hinnantcf823322010-12-17 14:46:43 +00001460 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001461
Marshall Clowe60a7182018-05-29 17:04:37 +00001462 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001463
Marek Kurdejc9848142020-11-26 10:07:16 +01001464 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1465
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001466 _LIBCPP_INLINE_VISIBILITY
1467 bool __is_long() const _NOEXCEPT
1468 {return bool(__r_.first().__s.__size_ & __short_mask);}
1469
Louis Dionneba400782020-10-02 15:02:52 -04001470#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001471
1472 bool __dereferenceable(const const_iterator* __i) const;
1473 bool __decrementable(const const_iterator* __i) const;
1474 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1475 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1476
Louis Dionneba400782020-10-02 15:02:52 -04001477#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001478
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001480 _LIBCPP_INLINE_VISIBILITY
1481 allocator_type& __alloc() _NOEXCEPT
1482 {return __r_.second();}
1483 _LIBCPP_INLINE_VISIBILITY
1484 const allocator_type& __alloc() const _NOEXCEPT
1485 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001487#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001488
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001490 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001491# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001493# else
1494 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1495# endif
1496
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001497 _LIBCPP_INLINE_VISIBILITY
1498 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001499# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001501# else
1502 {return __r_.first().__s.__size_;}
1503# endif
1504
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001505#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001506
1507 _LIBCPP_INLINE_VISIBILITY
1508 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001509# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001510 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1511# else
1512 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1513# endif
1514
1515 _LIBCPP_INLINE_VISIBILITY
1516 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001517# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001518 {return __r_.first().__s.__size_;}
1519# else
1520 {return __r_.first().__s.__size_ >> 1;}
1521# endif
1522
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001523#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001524
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001525 _LIBCPP_INLINE_VISIBILITY
1526 void __set_long_size(size_type __s) _NOEXCEPT
1527 {__r_.first().__l.__size_ = __s;}
1528 _LIBCPP_INLINE_VISIBILITY
1529 size_type __get_long_size() const _NOEXCEPT
1530 {return __r_.first().__l.__size_;}
1531 _LIBCPP_INLINE_VISIBILITY
1532 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1534
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001535 _LIBCPP_INLINE_VISIBILITY
1536 void __set_long_cap(size_type __s) _NOEXCEPT
1537 {__r_.first().__l.__cap_ = __long_mask | __s;}
1538 _LIBCPP_INLINE_VISIBILITY
1539 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001540 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001542 _LIBCPP_INLINE_VISIBILITY
1543 void __set_long_pointer(pointer __p) _NOEXCEPT
1544 {__r_.first().__l.__data_ = __p;}
1545 _LIBCPP_INLINE_VISIBILITY
1546 pointer __get_long_pointer() _NOEXCEPT
1547 {return __r_.first().__l.__data_;}
1548 _LIBCPP_INLINE_VISIBILITY
1549 const_pointer __get_long_pointer() const _NOEXCEPT
1550 {return __r_.first().__l.__data_;}
1551 _LIBCPP_INLINE_VISIBILITY
1552 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001553 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001554 _LIBCPP_INLINE_VISIBILITY
1555 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001556 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001557 _LIBCPP_INLINE_VISIBILITY
1558 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001560 _LIBCPP_INLINE_VISIBILITY
1561 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1563
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001564 _LIBCPP_INLINE_VISIBILITY
1565 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 {
1567 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1568 for (unsigned __i = 0; __i < __n_words; ++__i)
1569 __a[__i] = 0;
1570 }
1571
1572 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001574 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001575 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001577 static _LIBCPP_INLINE_VISIBILITY
1578 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001579 {
1580 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1581 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1582 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1583 if (__guess == __min_cap) ++__guess;
1584 return __guess;
1585 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001587 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001588 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001589 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001590 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001591 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001593
Martijn Vels5e7c9752020-03-04 17:52:46 -05001594 // Slow path for the (inlined) copy constructor for 'long' strings.
1595 // Always externally instantiated and not inlined.
1596 // Requires that __s is zero terminated.
1597 // The main reason for this function to exist is because for unstable, we
1598 // want to allow inlining of the copy constructor. However, we don't want
1599 // to call the __init() functions as those are marked as inline which may
1600 // result in over-aggressive inlining by the compiler, where our aim is
1601 // to only inline the fast path code directly in the ctor.
1602 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1603
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001605 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001606 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001608 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1609 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 __init(_InputIterator __first, _InputIterator __last);
1611
1612 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001613 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001614 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001616 __is_cpp17_forward_iterator<_ForwardIterator>::value
1617 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618 __init(_ForwardIterator __first, _ForwardIterator __last);
1619
1620 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001621 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1623 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001624 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625
Martijn Vels596e3de2020-02-26 15:55:49 -05001626 // __assign_no_alias is invoked for assignment operations where we
1627 // have proof that the input does not alias the current instance.
1628 // For example, operator=(basic_string) performs a 'self' check.
1629 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001630 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001631
Howard Hinnantcf823322010-12-17 14:46:43 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633 void __erase_to_end(size_type __pos);
1634
Martijn Velsa81fc792020-02-26 13:25:43 -05001635 // __erase_external_with_move is invoked for erase() invocations where
1636 // `n ~= npos`, likely requiring memory moves on the string data.
1637 void __erase_external_with_move(size_type __pos, size_type __n);
1638
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001639 _LIBCPP_INLINE_VISIBILITY
1640 void __copy_assign_alloc(const basic_string& __str)
1641 {__copy_assign_alloc(__str, integral_constant<bool,
1642 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1643
1644 _LIBCPP_INLINE_VISIBILITY
1645 void __copy_assign_alloc(const basic_string& __str, true_type)
1646 {
Marshall Clowf258c202017-01-31 03:40:52 +00001647 if (__alloc() == __str.__alloc())
1648 __alloc() = __str.__alloc();
1649 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001650 {
Marshall Clowf258c202017-01-31 03:40:52 +00001651 if (!__str.__is_long())
1652 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001653 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001654 __alloc() = __str.__alloc();
1655 }
1656 else
1657 {
1658 allocator_type __a = __str.__alloc();
1659 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001660 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001661 __alloc() = _VSTD::move(__a);
1662 __set_long_pointer(__p);
1663 __set_long_cap(__str.__get_long_cap());
1664 __set_long_size(__str.size());
1665 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001666 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001667 }
1668
1669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001670 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001671 {}
1672
Eric Fiselierfc92be82017-04-19 00:28:44 +00001673#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001674 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001675 void __move_assign(basic_string& __str, false_type)
1676 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001678 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001679#if _LIBCPP_STD_VER > 14
1680 _NOEXCEPT;
1681#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001682 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001683#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001684#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001685
1686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001687 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001688 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001689 _NOEXCEPT_(
1690 !__alloc_traits::propagate_on_container_move_assignment::value ||
1691 is_nothrow_move_assignable<allocator_type>::value)
1692 {__move_assign_alloc(__str, integral_constant<bool,
1693 __alloc_traits::propagate_on_container_move_assignment::value>());}
1694
1695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001696 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001697 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1698 {
1699 __alloc() = _VSTD::move(__c.__alloc());
1700 }
1701
1702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001703 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001704 _NOEXCEPT
1705 {}
1706
Martijn Velsda7d94f2020-06-19 14:24:03 -04001707 basic_string& __assign_external(const value_type* __s);
1708 basic_string& __assign_external(const value_type* __s, size_type __n);
1709
1710 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1711 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1712 pointer __p = __is_long()
1713 ? (__set_long_size(__n), __get_long_pointer())
1714 : (__set_short_size(__n), __get_short_pointer());
1715 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1716 traits_type::assign(__p[__n], value_type());
1717 return *this;
1718 }
1719
Howard Hinnantcf823322010-12-17 14:46:43 +00001720 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1721 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722
1723 friend basic_string operator+<>(const basic_string&, const basic_string&);
1724 friend basic_string operator+<>(const value_type*, const basic_string&);
1725 friend basic_string operator+<>(value_type, const basic_string&);
1726 friend basic_string operator+<>(const basic_string&, const value_type*);
1727 friend basic_string operator+<>(const basic_string&, value_type);
1728};
1729
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001730// These declarations must appear before any functions are implicitly used
1731// so that they have the correct visibility specifier.
1732#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1733_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1734_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1735#else
1736_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1737_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1738#endif
1739
1740
Marshall Clowa0563332018-02-08 06:34:03 +00001741#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1742template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001743 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001744 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001745 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1746 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001747 >
1748basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1749 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001750
1751template<class _CharT,
1752 class _Traits,
1753 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001754 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001755 >
1756explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1757 -> basic_string<_CharT, _Traits, _Allocator>;
1758
1759template<class _CharT,
1760 class _Traits,
1761 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001762 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001763 class _Sz = typename allocator_traits<_Allocator>::size_type
1764 >
1765basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1766 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001767#endif
1768
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001770inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771void
1772basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1773{
Louis Dionneba400782020-10-02 15:02:52 -04001774#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001775 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001776#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777}
1778
1779template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001780inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781void
Howard Hinnant28b24882011-12-01 20:21:04 +00001782basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Louis Dionneba400782020-10-02 15:02:52 -04001783#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant28b24882011-12-01 20:21:04 +00001784 __pos
1785#endif
1786 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787{
Louis Dionneba400782020-10-02 15:02:52 -04001788#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001789 __c_node* __c = __get_db()->__find_c_and_lock(this);
1790 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001792 const_pointer __new_last = __get_pointer() + __pos;
1793 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001795 --__p;
1796 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1797 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001799 (*__p)->__c_ = nullptr;
1800 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001801 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001804 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 }
Louis Dionneba400782020-10-02 15:02:52 -04001806#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807}
1808
1809template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001810inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001811basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001812 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001813 : __r_(__default_init_tag(), __default_init_tag())
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 Fiselierbea70602018-11-26 22:51:35 +00001822inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001824#if _LIBCPP_STD_VER <= 14
1825 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1826#else
1827 _NOEXCEPT
1828#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001829: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830{
Louis Dionneba400782020-10-02 15:02:52 -04001831#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001832 __get_db()->__insert_c(this);
1833#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834 __zero();
1835}
1836
1837template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001838void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1839 size_type __sz,
1840 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841{
1842 if (__reserve > max_size())
1843 this->__throw_length_error();
1844 pointer __p;
1845 if (__reserve < __min_cap)
1846 {
1847 __set_short_size(__sz);
1848 __p = __get_short_pointer();
1849 }
1850 else
1851 {
1852 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001853 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854 __set_long_pointer(__p);
1855 __set_long_cap(__cap+1);
1856 __set_long_size(__sz);
1857 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001858 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 traits_type::assign(__p[__sz], value_type());
1860}
1861
1862template <class _CharT, class _Traits, class _Allocator>
1863void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001864basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865{
1866 if (__sz > max_size())
1867 this->__throw_length_error();
1868 pointer __p;
1869 if (__sz < __min_cap)
1870 {
1871 __set_short_size(__sz);
1872 __p = __get_short_pointer();
1873 }
1874 else
1875 {
1876 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001877 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878 __set_long_pointer(__p);
1879 __set_long_cap(__cap+1);
1880 __set_long_size(__sz);
1881 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001882 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883 traits_type::assign(__p[__sz], value_type());
1884}
1885
1886template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001887template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001888basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001889 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001891 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001893#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001894 __get_db()->__insert_c(this);
1895#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896}
1897
1898template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001899inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001900basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001901 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001903 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001905#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001906 __get_db()->__insert_c(this);
1907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908}
1909
1910template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001911inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001912basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001913 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001915 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001917#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001918 __get_db()->__insert_c(this);
1919#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920}
1921
1922template <class _CharT, class _Traits, class _Allocator>
1923basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001924 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
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());
1931
Louis Dionneba400782020-10-02 15:02:52 -04001932#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001933 __get_db()->__insert_c(this);
1934#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935}
1936
1937template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001938basic_string<_CharT, _Traits, _Allocator>::basic_string(
1939 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001940 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941{
1942 if (!__str.__is_long())
1943 __r_.first().__r = __str.__r_.first().__r;
1944 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001945 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1946 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001947#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001948 __get_db()->__insert_c(this);
1949#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950}
1951
Martijn Vels5e7c9752020-03-04 17:52:46 -05001952template <class _CharT, class _Traits, class _Allocator>
1953void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1954 const value_type* __s, size_type __sz) {
1955 pointer __p;
1956 if (__sz < __min_cap) {
1957 __p = __get_short_pointer();
1958 __set_short_size(__sz);
1959 } else {
1960 if (__sz > max_size())
1961 this->__throw_length_error();
1962 size_t __cap = __recommend(__sz);
1963 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1964 __set_long_pointer(__p);
1965 __set_long_cap(__cap + 1);
1966 __set_long_size(__sz);
1967 }
1968 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1969}
1970
Eric Fiselierfc92be82017-04-19 00:28:44 +00001971#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972
1973template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001974inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001975basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001976#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001977 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001978#else
1979 _NOEXCEPT
1980#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001981 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982{
1983 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001984#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001985 __get_db()->__insert_c(this);
1986 if (__is_long())
1987 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988#endif
1989}
1990
1991template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001992inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001994 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995{
Marshall Clowd2d24692014-07-17 15:32:20 +00001996 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001997 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001998 else
1999 {
2000 __r_.first().__r = __str.__r_.first().__r;
2001 __str.__zero();
2002 }
Louis Dionneba400782020-10-02 15:02:52 -04002003#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002004 __get_db()->__insert_c(this);
2005 if (__is_long())
2006 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007#endif
2008}
2009
Eric Fiselierfc92be82017-04-19 00:28:44 +00002010#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011
2012template <class _CharT, class _Traits, class _Allocator>
2013void
2014basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2015{
2016 if (__n > max_size())
2017 this->__throw_length_error();
2018 pointer __p;
2019 if (__n < __min_cap)
2020 {
2021 __set_short_size(__n);
2022 __p = __get_short_pointer();
2023 }
2024 else
2025 {
2026 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002027 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028 __set_long_pointer(__p);
2029 __set_long_cap(__cap+1);
2030 __set_long_size(__n);
2031 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002032 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033 traits_type::assign(__p[__n], value_type());
2034}
2035
2036template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002037inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002038basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002039 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040{
2041 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002042#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002043 __get_db()->__insert_c(this);
2044#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045}
2046
2047template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002048template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002049basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002050 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051{
2052 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002053#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002054 __get_db()->__insert_c(this);
2055#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056}
2057
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002059basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2060 size_type __pos, size_type __n,
2061 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002062 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063{
2064 size_type __str_sz = __str.size();
2065 if (__pos > __str_sz)
2066 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002067 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002068#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002069 __get_db()->__insert_c(this);
2070#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071}
2072
2073template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002074inline
Marshall Clow83445802016-04-07 18:13:41 +00002075basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002076 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002077 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002078{
2079 size_type __str_sz = __str.size();
2080 if (__pos > __str_sz)
2081 this->__throw_out_of_range();
2082 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002083#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002084 __get_db()->__insert_c(this);
2085#endif
2086}
2087
2088template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002089template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002090basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002091 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002092 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002093{
Marshall Clowe46031a2018-07-02 18:41:15 +00002094 __self_view __sv0 = __t;
2095 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002096 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002097#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002098 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002099#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002100}
2101
2102template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002103template <class _Tp, class>
2104basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002105 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002106{
Marshall Clowe46031a2018-07-02 18:41:15 +00002107 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002108 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002109#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002110 __get_db()->__insert_c(this);
2111#endif
2112}
2113
2114template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002115template <class _Tp, class>
2116basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002117 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002118{
Marshall Clowe46031a2018-07-02 18:41:15 +00002119 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002120 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002121#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002122 __get_db()->__insert_c(this);
2123#endif
2124}
2125
2126template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002128_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002130 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2131>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2133{
2134 __zero();
2135#ifndef _LIBCPP_NO_EXCEPTIONS
2136 try
2137 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002138#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139 for (; __first != __last; ++__first)
2140 push_back(*__first);
2141#ifndef _LIBCPP_NO_EXCEPTIONS
2142 }
2143 catch (...)
2144 {
2145 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002146 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147 throw;
2148 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002149#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150}
2151
2152template <class _CharT, class _Traits, class _Allocator>
2153template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002154_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002156 __is_cpp17_forward_iterator<_ForwardIterator>::value
2157>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2159{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002160 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161 if (__sz > max_size())
2162 this->__throw_length_error();
2163 pointer __p;
2164 if (__sz < __min_cap)
2165 {
2166 __set_short_size(__sz);
2167 __p = __get_short_pointer();
2168 }
2169 else
2170 {
2171 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002172 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173 __set_long_pointer(__p);
2174 __set_long_cap(__cap+1);
2175 __set_long_size(__sz);
2176 }
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002177 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178 traits_type::assign(*__p, *__first);
2179 traits_type::assign(*__p, value_type());
2180}
2181
2182template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002183template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002184inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002186 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187{
2188 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002189#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002190 __get_db()->__insert_c(this);
2191#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192}
2193
2194template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002195template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002196inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2198 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002199 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200{
2201 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002202#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002203 __get_db()->__insert_c(this);
2204#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205}
2206
Eric Fiselierfc92be82017-04-19 00:28:44 +00002207#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002208
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002210inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002211basic_string<_CharT, _Traits, _Allocator>::basic_string(
2212 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002213 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214{
2215 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002216#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002217 __get_db()->__insert_c(this);
2218#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219}
2220
2221template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002222inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002223
Eric Fiselier812882b2017-02-17 01:17:10 +00002224basic_string<_CharT, _Traits, _Allocator>::basic_string(
2225 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002226 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227{
2228 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002229#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002230 __get_db()->__insert_c(this);
2231#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232}
2233
Eric Fiselierfc92be82017-04-19 00:28:44 +00002234#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002235
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2238{
Louis Dionneba400782020-10-02 15:02:52 -04002239#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002240 __get_db()->__erase_c(this);
2241#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002243 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244}
2245
2246template <class _CharT, class _Traits, class _Allocator>
2247void
2248basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2249 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002250 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 +00002251{
2252 size_type __ms = max_size();
2253 if (__delta_cap > __ms - __old_cap - 1)
2254 this->__throw_length_error();
2255 pointer __old_p = __get_pointer();
2256 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002257 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002259 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 __invalidate_all_iterators();
2261 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002262 traits_type::copy(_VSTD::__to_address(__p),
2263 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002265 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2267 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002268 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2269 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002271 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272 __set_long_pointer(__p);
2273 __set_long_cap(__cap+1);
2274 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2275 __set_long_size(__old_sz);
2276 traits_type::assign(__p[__old_sz], value_type());
2277}
2278
2279template <class _CharT, class _Traits, class _Allocator>
2280void
2281basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2282 size_type __n_copy, size_type __n_del, size_type __n_add)
2283{
2284 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002285 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286 this->__throw_length_error();
2287 pointer __old_p = __get_pointer();
2288 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002289 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002291 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292 __invalidate_all_iterators();
2293 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002294 traits_type::copy(_VSTD::__to_address(__p),
2295 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2297 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002298 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2299 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002300 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002302 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303 __set_long_pointer(__p);
2304 __set_long_cap(__cap+1);
2305}
2306
2307// assign
2308
2309template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002310template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002311basic_string<_CharT, _Traits, _Allocator>&
2312basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002313 const value_type* __s, size_type __n) {
2314 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2315 if (__n < __cap) {
2316 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2317 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2318 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2319 traits_type::assign(__p[__n], value_type());
2320 __invalidate_iterators_past(__n);
2321 } else {
2322 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2323 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2324 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002325 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002326}
2327
2328template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002330basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2331 const value_type* __s, size_type __n) {
2332 size_type __cap = capacity();
2333 if (__cap >= __n) {
2334 value_type* __p = _VSTD::__to_address(__get_pointer());
2335 traits_type::move(__p, __s, __n);
2336 traits_type::assign(__p[__n], value_type());
2337 __set_size(__n);
2338 __invalidate_iterators_past(__n);
2339 } else {
2340 size_type __sz = size();
2341 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2342 }
2343 return *this;
2344}
2345
2346template <class _CharT, class _Traits, class _Allocator>
2347basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002348basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002350 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002351 return (_LIBCPP_BUILTIN_CONSTANT_P(__n) && __n < __min_cap)
2352 ? __assign_short(__s, __n)
2353 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354}
2355
2356template <class _CharT, class _Traits, class _Allocator>
2357basic_string<_CharT, _Traits, _Allocator>&
2358basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2359{
2360 size_type __cap = capacity();
2361 if (__cap < __n)
2362 {
2363 size_type __sz = size();
2364 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2365 }
2366 else
2367 __invalidate_iterators_past(__n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002368 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002369 traits_type::assign(__p, __n, __c);
2370 traits_type::assign(__p[__n], value_type());
2371 __set_size(__n);
2372 return *this;
2373}
2374
2375template <class _CharT, class _Traits, class _Allocator>
2376basic_string<_CharT, _Traits, _Allocator>&
2377basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2378{
2379 pointer __p;
2380 if (__is_long())
2381 {
2382 __p = __get_long_pointer();
2383 __set_long_size(1);
2384 }
2385 else
2386 {
2387 __p = __get_short_pointer();
2388 __set_short_size(1);
2389 }
2390 traits_type::assign(*__p, __c);
2391 traits_type::assign(*++__p, value_type());
2392 __invalidate_iterators_past(1);
2393 return *this;
2394}
2395
2396template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002397basic_string<_CharT, _Traits, _Allocator>&
2398basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2399{
Martijn Vels596e3de2020-02-26 15:55:49 -05002400 if (this != &__str) {
2401 __copy_assign_alloc(__str);
2402 if (!__is_long()) {
2403 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002404 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002405 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002406 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002407 }
2408 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002409 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002410 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002411 }
2412 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002413}
2414
Eric Fiselierfc92be82017-04-19 00:28:44 +00002415#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002416
2417template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002418inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002419void
2420basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002421 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002422{
2423 if (__alloc() != __str.__alloc())
2424 assign(__str);
2425 else
2426 __move_assign(__str, true_type());
2427}
2428
2429template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002430inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002431void
2432basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002433#if _LIBCPP_STD_VER > 14
2434 _NOEXCEPT
2435#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002436 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002437#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002438{
marshall2ed41622019-11-27 07:13:00 -08002439 if (__is_long()) {
2440 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2441 __get_long_cap());
2442#if _LIBCPP_STD_VER <= 14
2443 if (!is_nothrow_move_assignable<allocator_type>::value) {
2444 __set_short_size(0);
2445 traits_type::assign(__get_short_pointer()[0], value_type());
2446 }
2447#endif
2448 }
2449 __move_assign_alloc(__str);
2450 __r_.first() = __str.__r_.first();
2451 __str.__set_short_size(0);
2452 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002453}
2454
2455template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002456inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002457basic_string<_CharT, _Traits, _Allocator>&
2458basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002459 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002460{
2461 __move_assign(__str, integral_constant<bool,
2462 __alloc_traits::propagate_on_container_move_assignment::value>());
2463 return *this;
2464}
2465
2466#endif
2467
2468template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002470_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002472 __is_exactly_cpp17_input_iterator <_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002473 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002475>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2477{
Marshall Clow958362f2016-09-05 01:54:30 +00002478 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002479 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002480 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481}
2482
2483template <class _CharT, class _Traits, class _Allocator>
2484template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002485_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002487 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002488 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002490>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2492{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002493 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494 size_type __cap = capacity();
2495 if (__cap < __n)
2496 {
2497 size_type __sz = size();
2498 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2499 }
2500 else
2501 __invalidate_iterators_past(__n);
2502 pointer __p = __get_pointer();
2503 for (; __first != __last; ++__first, ++__p)
2504 traits_type::assign(*__p, *__first);
2505 traits_type::assign(*__p, value_type());
2506 __set_size(__n);
2507 return *this;
2508}
2509
2510template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511basic_string<_CharT, _Traits, _Allocator>&
2512basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2513{
2514 size_type __sz = __str.size();
2515 if (__pos > __sz)
2516 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002517 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518}
2519
2520template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002521template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002522_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002523<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002524 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2525 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002526 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002527>
Marshall Clow82513342016-09-24 22:45:42 +00002528basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002529{
Marshall Clow82513342016-09-24 22:45:42 +00002530 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002531 size_type __sz = __sv.size();
2532 if (__pos > __sz)
2533 this->__throw_out_of_range();
2534 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2535}
2536
2537
2538template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002540basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2541 return __assign_external(__s, traits_type::length(__s));
2542}
2543
2544template <class _CharT, class _Traits, class _Allocator>
2545basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002546basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002548 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002549 return _LIBCPP_BUILTIN_CONSTANT_P(*__s)
2550 ? (traits_type::length(__s) < __min_cap
2551 ? __assign_short(__s, traits_type::length(__s))
2552 : __assign_external(__s, traits_type::length(__s)))
2553 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555// append
2556
2557template <class _CharT, class _Traits, class _Allocator>
2558basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002559basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002561 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562 size_type __cap = capacity();
2563 size_type __sz = size();
2564 if (__cap - __sz >= __n)
2565 {
2566 if (__n)
2567 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002568 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569 traits_type::copy(__p + __sz, __s, __n);
2570 __sz += __n;
2571 __set_size(__sz);
2572 traits_type::assign(__p[__sz], value_type());
2573 }
2574 }
2575 else
2576 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2577 return *this;
2578}
2579
2580template <class _CharT, class _Traits, class _Allocator>
2581basic_string<_CharT, _Traits, _Allocator>&
2582basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2583{
2584 if (__n)
2585 {
2586 size_type __cap = capacity();
2587 size_type __sz = size();
2588 if (__cap - __sz < __n)
2589 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2590 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002591 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592 __sz += __n;
2593 __set_size(__sz);
2594 traits_type::assign(__p[__sz], value_type());
2595 }
2596 return *this;
2597}
2598
2599template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002600inline void
2601basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2602{
2603 if (__n)
2604 {
2605 size_type __cap = capacity();
2606 size_type __sz = size();
2607 if (__cap - __sz < __n)
2608 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2609 pointer __p = __get_pointer();
2610 __sz += __n;
2611 __set_size(__sz);
2612 traits_type::assign(__p[__sz], value_type());
2613 }
2614}
2615
2616template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617void
2618basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2619{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002620 bool __is_short = !__is_long();
2621 size_type __cap;
2622 size_type __sz;
2623 if (__is_short)
2624 {
2625 __cap = __min_cap - 1;
2626 __sz = __get_short_size();
2627 }
2628 else
2629 {
2630 __cap = __get_long_cap() - 1;
2631 __sz = __get_long_size();
2632 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002634 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002636 __is_short = !__is_long();
2637 }
2638 pointer __p;
2639 if (__is_short)
2640 {
2641 __p = __get_short_pointer() + __sz;
2642 __set_short_size(__sz+1);
2643 }
2644 else
2645 {
2646 __p = __get_long_pointer() + __sz;
2647 __set_long_size(__sz+1);
2648 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649 traits_type::assign(*__p, __c);
2650 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651}
2652
Marshall Clow6ebbf662016-09-07 03:32:06 +00002653template <class _Tp>
Marshall Clow958362f2016-09-05 01:54:30 +00002654bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2655{
2656 return __first <= __p && __p < __last;
2657}
2658
Marshall Clow6ebbf662016-09-07 03:32:06 +00002659template <class _Tp1, class _Tp2>
Eric Fiselier6003c772016-12-23 23:37:52 +00002660bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clow6ebbf662016-09-07 03:32:06 +00002661{
2662 return false;
2663}
2664
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665template <class _CharT, class _Traits, class _Allocator>
2666template<class _ForwardIterator>
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002667basic_string<_CharT, _Traits, _Allocator>&
2668basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2669 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670{
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002671 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002672 "function requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673 size_type __sz = size();
2674 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002675 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676 if (__n)
2677 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002678 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2679 _CharRef __tmp_ref = *__first;
2680 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002681 {
2682 const basic_string __temp (__first, __last, __alloc());
2683 append(__temp.data(), __temp.size());
2684 }
Louis Dionne173f29e2019-05-29 16:01:36 +00002685 else
Marshall Clow958362f2016-09-05 01:54:30 +00002686 {
2687 if (__cap - __sz < __n)
2688 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2689 pointer __p = __get_pointer() + __sz;
2690 for (; __first != __last; ++__p, ++__first)
2691 traits_type::assign(*__p, *__first);
2692 traits_type::assign(*__p, value_type());
2693 __set_size(__sz + __n);
2694 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 }
2696 return *this;
2697}
2698
2699template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002700inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701basic_string<_CharT, _Traits, _Allocator>&
2702basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2703{
2704 return append(__str.data(), __str.size());
2705}
2706
2707template <class _CharT, class _Traits, class _Allocator>
2708basic_string<_CharT, _Traits, _Allocator>&
2709basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2710{
2711 size_type __sz = __str.size();
2712 if (__pos > __sz)
2713 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002714 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715}
2716
2717template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002718template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002719 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002720 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002721 __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 +00002722 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002723 >
Marshall Clow82513342016-09-24 22:45:42 +00002724basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002725{
Marshall Clow82513342016-09-24 22:45:42 +00002726 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002727 size_type __sz = __sv.size();
2728 if (__pos > __sz)
2729 this->__throw_out_of_range();
2730 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2731}
2732
2733template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002735basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002737 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738 return append(__s, traits_type::length(__s));
2739}
2740
2741// insert
2742
2743template <class _CharT, class _Traits, class _Allocator>
2744basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002745basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002747 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748 size_type __sz = size();
2749 if (__pos > __sz)
2750 this->__throw_out_of_range();
2751 size_type __cap = capacity();
2752 if (__cap - __sz >= __n)
2753 {
2754 if (__n)
2755 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002756 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757 size_type __n_move = __sz - __pos;
2758 if (__n_move != 0)
2759 {
2760 if (__p + __pos <= __s && __s < __p + __sz)
2761 __s += __n;
2762 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2763 }
2764 traits_type::move(__p + __pos, __s, __n);
2765 __sz += __n;
2766 __set_size(__sz);
2767 traits_type::assign(__p[__sz], value_type());
2768 }
2769 }
2770 else
2771 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2772 return *this;
2773}
2774
2775template <class _CharT, class _Traits, class _Allocator>
2776basic_string<_CharT, _Traits, _Allocator>&
2777basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2778{
2779 size_type __sz = size();
2780 if (__pos > __sz)
2781 this->__throw_out_of_range();
2782 if (__n)
2783 {
2784 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002785 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 if (__cap - __sz >= __n)
2787 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002788 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789 size_type __n_move = __sz - __pos;
2790 if (__n_move != 0)
2791 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2792 }
2793 else
2794 {
2795 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002796 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 }
2798 traits_type::assign(__p + __pos, __n, __c);
2799 __sz += __n;
2800 __set_size(__sz);
2801 traits_type::assign(__p[__sz], value_type());
2802 }
2803 return *this;
2804}
2805
2806template <class _CharT, class _Traits, class _Allocator>
2807template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002808_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002810 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002811 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2812 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002813>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2815{
Louis Dionneba400782020-10-02 15:02:52 -04002816#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002817 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2818 "string::insert(iterator, range) called with an iterator not"
2819 " referring to this string");
2820#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002821 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002822 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823}
2824
2825template <class _CharT, class _Traits, class _Allocator>
2826template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002827_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002829 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002830 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002832>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2834{
Louis Dionneba400782020-10-02 15:02:52 -04002835#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002836 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2837 "string::insert(iterator, range) called with an iterator not"
2838 " referring to this string");
2839#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002841 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842 if (__n)
2843 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002844 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2845 _CharRef __tmp_char = *__first;
2846 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002847 {
2848 const basic_string __temp(__first, __last, __alloc());
2849 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2850 }
2851
2852 size_type __sz = size();
2853 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002854 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 if (__cap - __sz >= __n)
2856 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002857 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858 size_type __n_move = __sz - __ip;
2859 if (__n_move != 0)
2860 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2861 }
2862 else
2863 {
2864 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002865 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 }
2867 __sz += __n;
2868 __set_size(__sz);
2869 traits_type::assign(__p[__sz], value_type());
2870 for (__p += __ip; __first != __last; ++__p, ++__first)
2871 traits_type::assign(*__p, *__first);
2872 }
2873 return begin() + __ip;
2874}
2875
2876template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002877inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878basic_string<_CharT, _Traits, _Allocator>&
2879basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2880{
2881 return insert(__pos1, __str.data(), __str.size());
2882}
2883
2884template <class _CharT, class _Traits, class _Allocator>
2885basic_string<_CharT, _Traits, _Allocator>&
2886basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2887 size_type __pos2, size_type __n)
2888{
2889 size_type __str_sz = __str.size();
2890 if (__pos2 > __str_sz)
2891 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002892 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893}
2894
2895template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002896template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002897_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002898<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002899 __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 +00002900 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002901>
Marshall Clow82513342016-09-24 22:45:42 +00002902basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002903 size_type __pos2, size_type __n)
2904{
Marshall Clow82513342016-09-24 22:45:42 +00002905 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002906 size_type __str_sz = __sv.size();
2907 if (__pos2 > __str_sz)
2908 this->__throw_out_of_range();
2909 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2910}
2911
2912template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002914basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002916 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917 return insert(__pos, __s, traits_type::length(__s));
2918}
2919
2920template <class _CharT, class _Traits, class _Allocator>
2921typename basic_string<_CharT, _Traits, _Allocator>::iterator
2922basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2923{
2924 size_type __ip = static_cast<size_type>(__pos - begin());
2925 size_type __sz = size();
2926 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002927 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928 if (__cap == __sz)
2929 {
2930 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002931 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 }
2933 else
2934 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002935 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936 size_type __n_move = __sz - __ip;
2937 if (__n_move != 0)
2938 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2939 }
2940 traits_type::assign(__p[__ip], __c);
2941 traits_type::assign(__p[++__sz], value_type());
2942 __set_size(__sz);
2943 return begin() + static_cast<difference_type>(__ip);
2944}
2945
2946template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002947inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948typename basic_string<_CharT, _Traits, _Allocator>::iterator
2949basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2950{
Louis Dionneba400782020-10-02 15:02:52 -04002951#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002952 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2953 "string::insert(iterator, n, value) called with an iterator not"
2954 " referring to this string");
2955#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956 difference_type __p = __pos - begin();
2957 insert(static_cast<size_type>(__p), __n, __c);
2958 return begin() + __p;
2959}
2960
2961// replace
2962
2963template <class _CharT, class _Traits, class _Allocator>
2964basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002965basic_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 +00002966 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002968 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 size_type __sz = size();
2970 if (__pos > __sz)
2971 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002972 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973 size_type __cap = capacity();
2974 if (__cap - __sz + __n1 >= __n2)
2975 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002976 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977 if (__n1 != __n2)
2978 {
2979 size_type __n_move = __sz - __pos - __n1;
2980 if (__n_move != 0)
2981 {
2982 if (__n1 > __n2)
2983 {
2984 traits_type::move(__p + __pos, __s, __n2);
2985 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2986 goto __finish;
2987 }
2988 if (__p + __pos < __s && __s < __p + __sz)
2989 {
2990 if (__p + __pos + __n1 <= __s)
2991 __s += __n2 - __n1;
2992 else // __p + __pos < __s < __p + __pos + __n1
2993 {
2994 traits_type::move(__p + __pos, __s, __n1);
2995 __pos += __n1;
2996 __s += __n2;
2997 __n2 -= __n1;
2998 __n1 = 0;
2999 }
3000 }
3001 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3002 }
3003 }
3004 traits_type::move(__p + __pos, __s, __n2);
3005__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05003006// __sz += __n2 - __n1; in this and the below function below can cause unsigned
3007// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00003008 __sz += __n2 - __n1;
3009 __set_size(__sz);
3010 __invalidate_iterators_past(__sz);
3011 traits_type::assign(__p[__sz], value_type());
3012 }
3013 else
3014 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3015 return *this;
3016}
3017
3018template <class _CharT, class _Traits, class _Allocator>
3019basic_string<_CharT, _Traits, _Allocator>&
3020basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00003021 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003022{
3023 size_type __sz = size();
3024 if (__pos > __sz)
3025 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003026 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003027 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003028 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029 if (__cap - __sz + __n1 >= __n2)
3030 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003031 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032 if (__n1 != __n2)
3033 {
3034 size_type __n_move = __sz - __pos - __n1;
3035 if (__n_move != 0)
3036 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3037 }
3038 }
3039 else
3040 {
3041 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003042 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043 }
3044 traits_type::assign(__p + __pos, __n2, __c);
3045 __sz += __n2 - __n1;
3046 __set_size(__sz);
3047 __invalidate_iterators_past(__sz);
3048 traits_type::assign(__p[__sz], value_type());
3049 return *this;
3050}
3051
3052template <class _CharT, class _Traits, class _Allocator>
3053template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003054_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003056 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003057 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003058>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003059basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060 _InputIterator __j1, _InputIterator __j2)
3061{
Marshall Clow958362f2016-09-05 01:54:30 +00003062 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003063 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064}
3065
3066template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003067inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068basic_string<_CharT, _Traits, _Allocator>&
3069basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3070{
3071 return replace(__pos1, __n1, __str.data(), __str.size());
3072}
3073
3074template <class _CharT, class _Traits, class _Allocator>
3075basic_string<_CharT, _Traits, _Allocator>&
3076basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3077 size_type __pos2, size_type __n2)
3078{
3079 size_type __str_sz = __str.size();
3080 if (__pos2 > __str_sz)
3081 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003082 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083}
3084
3085template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003086template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003087_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003088<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003089 __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 +00003090 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003091>
Marshall Clow82513342016-09-24 22:45:42 +00003092basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003093 size_type __pos2, size_type __n2)
3094{
Marshall Clow82513342016-09-24 22:45:42 +00003095 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003096 size_type __str_sz = __sv.size();
3097 if (__pos2 > __str_sz)
3098 this->__throw_out_of_range();
3099 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3100}
3101
3102template <class _CharT, class _Traits, class _Allocator>
3103basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003104basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003106 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107 return replace(__pos, __n1, __s, traits_type::length(__s));
3108}
3109
3110template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003111inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003113basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114{
3115 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3116 __str.data(), __str.size());
3117}
3118
3119template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003120inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003122basic_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 +00003123{
3124 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3125}
3126
3127template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003128inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003130basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131{
3132 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3133}
3134
3135template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003136inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003138basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139{
3140 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3141}
3142
3143// erase
3144
Martijn Velsa81fc792020-02-26 13:25:43 -05003145// 'externally instantiated' erase() implementation, called when __n != npos.
3146// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003148void
3149basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3150 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152 if (__n)
3153 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003154 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003155 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003156 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 size_type __n_move = __sz - __pos - __n;
3158 if (__n_move != 0)
3159 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3160 __sz -= __n;
3161 __set_size(__sz);
3162 __invalidate_iterators_past(__sz);
3163 traits_type::assign(__p[__sz], value_type());
3164 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003165}
3166
3167template <class _CharT, class _Traits, class _Allocator>
3168basic_string<_CharT, _Traits, _Allocator>&
3169basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3170 size_type __n) {
3171 if (__pos > size()) this->__throw_out_of_range();
3172 if (__n == npos) {
3173 __erase_to_end(__pos);
3174 } else {
3175 __erase_external_with_move(__pos, __n);
3176 }
3177 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178}
3179
3180template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003181inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182typename basic_string<_CharT, _Traits, _Allocator>::iterator
3183basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3184{
Louis Dionneba400782020-10-02 15:02:52 -04003185#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003186 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3187 "string::erase(iterator) called with an iterator not"
3188 " referring to this string");
3189#endif
3190 _LIBCPP_ASSERT(__pos != end(),
3191 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192 iterator __b = begin();
3193 size_type __r = static_cast<size_type>(__pos - __b);
3194 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003195 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196}
3197
3198template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003199inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200typename basic_string<_CharT, _Traits, _Allocator>::iterator
3201basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3202{
Louis Dionneba400782020-10-02 15:02:52 -04003203#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003204 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3205 "string::erase(iterator, iterator) called with an iterator not"
3206 " referring to this string");
3207#endif
3208 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209 iterator __b = begin();
3210 size_type __r = static_cast<size_type>(__first - __b);
3211 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003212 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213}
3214
3215template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003216inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217void
3218basic_string<_CharT, _Traits, _Allocator>::pop_back()
3219{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003220 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221 size_type __sz;
3222 if (__is_long())
3223 {
3224 __sz = __get_long_size() - 1;
3225 __set_long_size(__sz);
3226 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3227 }
3228 else
3229 {
3230 __sz = __get_short_size() - 1;
3231 __set_short_size(__sz);
3232 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3233 }
3234 __invalidate_iterators_past(__sz);
3235}
3236
3237template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003238inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003240basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241{
3242 __invalidate_all_iterators();
3243 if (__is_long())
3244 {
3245 traits_type::assign(*__get_long_pointer(), value_type());
3246 __set_long_size(0);
3247 }
3248 else
3249 {
3250 traits_type::assign(*__get_short_pointer(), value_type());
3251 __set_short_size(0);
3252 }
3253}
3254
3255template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003256inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257void
3258basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3259{
3260 if (__is_long())
3261 {
3262 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3263 __set_long_size(__pos);
3264 }
3265 else
3266 {
3267 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3268 __set_short_size(__pos);
3269 }
3270 __invalidate_iterators_past(__pos);
3271}
3272
3273template <class _CharT, class _Traits, class _Allocator>
3274void
3275basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3276{
3277 size_type __sz = size();
3278 if (__n > __sz)
3279 append(__n - __sz, __c);
3280 else
3281 __erase_to_end(__n);
3282}
3283
3284template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003285inline void
3286basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3287{
3288 size_type __sz = size();
3289 if (__n > __sz) {
3290 __append_default_init(__n - __sz);
3291 } else
3292 __erase_to_end(__n);
3293}
3294
3295template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003296inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003298basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003300 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003301#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003302 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003304 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305#endif
3306}
3307
3308template <class _CharT, class _Traits, class _Allocator>
3309void
Marek Kurdejc9848142020-11-26 10:07:16 +01003310basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311{
Marek Kurdejc9848142020-11-26 10:07:16 +01003312 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003313 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003314
3315#if _LIBCPP_STD_VER > 17
3316 // Reserve never shrinks as of C++20.
3317 if (__requested_capacity <= capacity()) return;
3318#endif
3319
3320 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3321 __target_capacity = __recommend(__target_capacity);
3322 if (__target_capacity == capacity()) return;
3323
3324 __shrink_or_extend(__target_capacity);
3325}
3326
3327template <class _CharT, class _Traits, class _Allocator>
3328void
3329basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3330{
3331 size_type __target_capacity = __recommend(size());
3332 if (__target_capacity == capacity()) return;
3333
3334 __shrink_or_extend(__target_capacity);
3335}
3336
3337template <class _CharT, class _Traits, class _Allocator>
3338void
3339basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3340{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341 size_type __cap = capacity();
3342 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003343
3344 pointer __new_data, __p;
3345 bool __was_long, __now_long;
3346 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003348 __was_long = true;
3349 __now_long = false;
3350 __new_data = __get_short_pointer();
3351 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003353 else
3354 {
3355 if (__target_capacity > __cap)
3356 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3357 else
3358 {
3359 #ifndef _LIBCPP_NO_EXCEPTIONS
3360 try
3361 {
3362 #endif // _LIBCPP_NO_EXCEPTIONS
3363 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3364 #ifndef _LIBCPP_NO_EXCEPTIONS
3365 }
3366 catch (...)
3367 {
3368 return;
3369 }
3370 #else // _LIBCPP_NO_EXCEPTIONS
3371 if (__new_data == nullptr)
3372 return;
3373 #endif // _LIBCPP_NO_EXCEPTIONS
3374 }
3375 __now_long = true;
3376 __was_long = __is_long();
3377 __p = __get_pointer();
3378 }
3379 traits_type::copy(_VSTD::__to_address(__new_data),
3380 _VSTD::__to_address(__p), size()+1);
3381 if (__was_long)
3382 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3383 if (__now_long)
3384 {
3385 __set_long_cap(__target_capacity+1);
3386 __set_long_size(__sz);
3387 __set_long_pointer(__new_data);
3388 }
3389 else
3390 __set_short_size(__sz);
3391 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392}
3393
3394template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003395inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003397basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003399 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400 return *(data() + __pos);
3401}
3402
3403template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003404inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003406basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003408 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409 return *(__get_pointer() + __pos);
3410}
3411
3412template <class _CharT, class _Traits, class _Allocator>
3413typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3414basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3415{
3416 if (__n >= size())
3417 this->__throw_out_of_range();
3418 return (*this)[__n];
3419}
3420
3421template <class _CharT, class _Traits, class _Allocator>
3422typename basic_string<_CharT, _Traits, _Allocator>::reference
3423basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3424{
3425 if (__n >= size())
3426 this->__throw_out_of_range();
3427 return (*this)[__n];
3428}
3429
3430template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003431inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003433basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003435 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436 return *__get_pointer();
3437}
3438
3439template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003440inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003442basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003444 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445 return *data();
3446}
3447
3448template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003449inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003451basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003453 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454 return *(__get_pointer() + size() - 1);
3455}
3456
3457template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003458inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003460basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003461{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003462 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463 return *(data() + size() - 1);
3464}
3465
3466template <class _CharT, class _Traits, class _Allocator>
3467typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003468basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469{
3470 size_type __sz = size();
3471 if (__pos > __sz)
3472 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003473 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474 traits_type::copy(__s, data() + __pos, __rlen);
3475 return __rlen;
3476}
3477
3478template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003479inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480basic_string<_CharT, _Traits, _Allocator>
3481basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3482{
3483 return basic_string(*this, __pos, __n, __alloc());
3484}
3485
3486template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003487inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488void
3489basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003490#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003491 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003492#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003493 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003494 __is_nothrow_swappable<allocator_type>::value)
3495#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496{
Louis Dionneba400782020-10-02 15:02:52 -04003497#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003498 if (!__is_long())
3499 __get_db()->__invalidate_all(this);
3500 if (!__str.__is_long())
3501 __get_db()->__invalidate_all(&__str);
3502 __get_db()->swap(this, &__str);
3503#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003504 _LIBCPP_ASSERT(
3505 __alloc_traits::propagate_on_container_swap::value ||
3506 __alloc_traits::is_always_equal::value ||
3507 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003508 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003509 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510}
3511
3512// find
3513
3514template <class _Traits>
3515struct _LIBCPP_HIDDEN __traits_eq
3516{
3517 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003518 _LIBCPP_INLINE_VISIBILITY
3519 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3520 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521};
3522
3523template<class _CharT, class _Traits, class _Allocator>
3524typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003525basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003526 size_type __pos,
3527 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003529 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003530 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003531 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532}
3533
3534template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003535inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003537basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3538 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003540 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003541 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542}
3543
3544template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003545template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003546_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003547<
3548 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3549 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003550>
Marshall Clowe46031a2018-07-02 18:41:15 +00003551basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003552 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003553{
Marshall Clowe46031a2018-07-02 18:41:15 +00003554 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003555 return __str_find<value_type, size_type, traits_type, npos>
3556 (data(), size(), __sv.data(), __pos, __sv.size());
3557}
3558
3559template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003560inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003561typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003562basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003563 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003565 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003566 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003567 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568}
3569
3570template<class _CharT, class _Traits, class _Allocator>
3571typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003572basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3573 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003575 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003576 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577}
3578
3579// rfind
3580
3581template<class _CharT, class _Traits, class _Allocator>
3582typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003583basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003584 size_type __pos,
3585 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003587 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003588 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003589 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590}
3591
3592template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003593inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003595basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3596 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003598 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003599 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600}
3601
3602template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003603template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003604_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003605<
3606 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3607 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003608>
Marshall Clowe46031a2018-07-02 18:41:15 +00003609basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003610 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003611{
Marshall Clowe46031a2018-07-02 18:41:15 +00003612 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003613 return __str_rfind<value_type, size_type, traits_type, npos>
3614 (data(), size(), __sv.data(), __pos, __sv.size());
3615}
3616
3617template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003618inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003619typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003620basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003621 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003623 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003624 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003625 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626}
3627
3628template<class _CharT, class _Traits, class _Allocator>
3629typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003630basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3631 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003633 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003634 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635}
3636
3637// find_first_of
3638
3639template<class _CharT, class _Traits, class _Allocator>
3640typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003641basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003642 size_type __pos,
3643 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003645 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003646 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003647 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648}
3649
3650template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003651inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003653basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3654 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003656 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003657 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658}
3659
3660template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003661template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003662_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003663<
3664 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3665 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003666>
Marshall Clowe46031a2018-07-02 18:41:15 +00003667basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003668 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003669{
Marshall Clowe46031a2018-07-02 18:41:15 +00003670 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003671 return __str_find_first_of<value_type, size_type, traits_type, npos>
3672 (data(), size(), __sv.data(), __pos, __sv.size());
3673}
3674
3675template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003676inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003677typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003678basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003679 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003681 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003682 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003683 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684}
3685
3686template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003687inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003689basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3690 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691{
3692 return find(__c, __pos);
3693}
3694
3695// find_last_of
3696
3697template<class _CharT, class _Traits, class _Allocator>
3698typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003699basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003700 size_type __pos,
3701 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003702{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003703 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003704 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003705 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706}
3707
3708template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003709inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003710typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003711basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3712 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003714 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003715 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716}
3717
3718template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003719template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003720_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003721<
3722 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3723 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003724>
Marshall Clowe46031a2018-07-02 18:41:15 +00003725basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003726 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003727{
Marshall Clowe46031a2018-07-02 18:41:15 +00003728 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003729 return __str_find_last_of<value_type, size_type, traits_type, npos>
3730 (data(), size(), __sv.data(), __pos, __sv.size());
3731}
3732
3733template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003734inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003735typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003736basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003737 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003739 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003740 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003741 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003742}
3743
3744template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003745inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003746typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003747basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3748 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749{
3750 return rfind(__c, __pos);
3751}
3752
3753// find_first_not_of
3754
3755template<class _CharT, class _Traits, class _Allocator>
3756typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003757basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003758 size_type __pos,
3759 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003760{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003761 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003762 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003763 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764}
3765
3766template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003767inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003769basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3770 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003772 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003773 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774}
3775
3776template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003777template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003778_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003779<
3780 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3781 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003782>
Marshall Clowe46031a2018-07-02 18:41:15 +00003783basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003784 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003785{
Marshall Clowe46031a2018-07-02 18:41:15 +00003786 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003787 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3788 (data(), size(), __sv.data(), __pos, __sv.size());
3789}
3790
3791template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003792inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003793typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003794basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003795 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003797 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003798 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003799 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003800}
3801
3802template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003803inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003805basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3806 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003808 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003809 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810}
3811
3812// find_last_not_of
3813
3814template<class _CharT, class _Traits, class _Allocator>
3815typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003816basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003817 size_type __pos,
3818 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003820 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003821 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003822 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823}
3824
3825template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003826inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003828basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3829 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003831 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003832 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003833}
3834
3835template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003836template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003837_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003838<
3839 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3840 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003841>
Marshall Clowe46031a2018-07-02 18:41:15 +00003842basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003843 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003844{
Marshall Clowe46031a2018-07-02 18:41:15 +00003845 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003846 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3847 (data(), size(), __sv.data(), __pos, __sv.size());
3848}
3849
3850template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003851inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003852typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003853basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003854 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003856 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003857 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003858 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859}
3860
3861template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003862inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003864basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3865 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003867 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003868 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869}
3870
3871// compare
3872
3873template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003874template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003875_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003876<
3877 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3878 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003879>
zoecarver1997e0a2021-02-05 11:54:47 -08003880basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881{
Marshall Clowe46031a2018-07-02 18:41:15 +00003882 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003883 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003884 size_t __rhs_sz = __sv.size();
3885 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003886 _VSTD::min(__lhs_sz, __rhs_sz));
3887 if (__result != 0)
3888 return __result;
3889 if (__lhs_sz < __rhs_sz)
3890 return -1;
3891 if (__lhs_sz > __rhs_sz)
3892 return 1;
3893 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894}
3895
3896template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003897inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003899basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003901 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902}
3903
3904template <class _CharT, class _Traits, class _Allocator>
3905int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003906basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3907 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003908 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003909 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003910{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003911 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912 size_type __sz = size();
3913 if (__pos1 > __sz || __n2 == npos)
3914 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003915 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3916 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917 if (__r == 0)
3918 {
3919 if (__rlen < __n2)
3920 __r = -1;
3921 else if (__rlen > __n2)
3922 __r = 1;
3923 }
3924 return __r;
3925}
3926
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003927template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003928template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003929_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003930<
3931 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3932 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003933>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003934basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3935 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003936 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003937{
Marshall Clowe46031a2018-07-02 18:41:15 +00003938 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003939 return compare(__pos1, __n1, __sv.data(), __sv.size());
3940}
3941
3942template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003943inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003944int
3945basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3946 size_type __n1,
3947 const basic_string& __str) const
3948{
3949 return compare(__pos1, __n1, __str.data(), __str.size());
3950}
3951
3952template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003953template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003954_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003955<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003956 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3957 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003958 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003959>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003960basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3961 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003962 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003963 size_type __pos2,
3964 size_type __n2) const
3965{
Marshall Clow82513342016-09-24 22:45:42 +00003966 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003967 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3968}
3969
3970template <class _CharT, class _Traits, class _Allocator>
3971int
3972basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3973 size_type __n1,
3974 const basic_string& __str,
3975 size_type __pos2,
3976 size_type __n2) const
3977{
3978 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3979}
3980
3981template <class _CharT, class _Traits, class _Allocator>
3982int
3983basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3984{
3985 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3986 return compare(0, npos, __s, traits_type::length(__s));
3987}
3988
3989template <class _CharT, class _Traits, class _Allocator>
3990int
3991basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3992 size_type __n1,
3993 const value_type* __s) const
3994{
3995 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3996 return compare(__pos1, __n1, __s, traits_type::length(__s));
3997}
3998
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999// __invariants
4000
4001template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004002inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003bool
4004basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4005{
4006 if (size() > capacity())
4007 return false;
4008 if (capacity() < __min_cap - 1)
4009 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004010 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004012 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013 return false;
4014 return true;
4015}
4016
Vedant Kumar55e007e2018-03-08 21:15:26 +00004017// __clear_and_shrink
4018
4019template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004020inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004021void
Marshall Clowe60a7182018-05-29 17:04:37 +00004022basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004023{
4024 clear();
4025 if(__is_long())
4026 {
4027 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4028 __set_long_cap(0);
4029 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004030 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004031 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004032}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004033
Howard Hinnantc51e1022010-05-11 19:42:16 +00004034// operator==
4035
4036template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004037inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038bool
4039operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004040 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004042 size_t __lhs_sz = __lhs.size();
4043 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4044 __rhs.data(),
4045 __lhs_sz) == 0;
4046}
4047
4048template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004049inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004050bool
4051operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4052 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4053{
4054 size_t __lhs_sz = __lhs.size();
4055 if (__lhs_sz != __rhs.size())
4056 return false;
4057 const char* __lp = __lhs.data();
4058 const char* __rp = __rhs.data();
4059 if (__lhs.__is_long())
4060 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4061 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4062 if (*__lp != *__rp)
4063 return false;
4064 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065}
4066
4067template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004068inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004070operator==(const _CharT* __lhs,
4071 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004073 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4074 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4075 size_t __lhs_len = _Traits::length(__lhs);
4076 if (__lhs_len != __rhs.size()) return false;
4077 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078}
4079
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004081inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004083operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4084 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004086 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4087 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4088 size_t __rhs_len = _Traits::length(__rhs);
4089 if (__rhs_len != __lhs.size()) return false;
4090 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091}
4092
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004093template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004094inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095bool
4096operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004097 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098{
4099 return !(__lhs == __rhs);
4100}
4101
4102template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004103inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004105operator!=(const _CharT* __lhs,
4106 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107{
4108 return !(__lhs == __rhs);
4109}
4110
4111template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004112inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004114operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4115 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116{
4117 return !(__lhs == __rhs);
4118}
4119
4120// operator<
4121
4122template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004123inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124bool
4125operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004126 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004128 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129}
4130
4131template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004132inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004134operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4135 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004137 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138}
4139
4140template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004141inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004143operator< (const _CharT* __lhs,
4144 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145{
4146 return __rhs.compare(__lhs) > 0;
4147}
4148
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149// operator>
4150
4151template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004152inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153bool
4154operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004155 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156{
4157 return __rhs < __lhs;
4158}
4159
4160template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004161inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004163operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4164 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165{
4166 return __rhs < __lhs;
4167}
4168
4169template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004170inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004172operator> (const _CharT* __lhs,
4173 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004174{
4175 return __rhs < __lhs;
4176}
4177
4178// operator<=
4179
4180template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004181inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182bool
4183operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004184 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185{
4186 return !(__rhs < __lhs);
4187}
4188
4189template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004190inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004192operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4193 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194{
4195 return !(__rhs < __lhs);
4196}
4197
4198template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004199inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004201operator<=(const _CharT* __lhs,
4202 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203{
4204 return !(__rhs < __lhs);
4205}
4206
4207// operator>=
4208
4209template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004210inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211bool
4212operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004213 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214{
4215 return !(__lhs < __rhs);
4216}
4217
4218template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004219inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004221operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4222 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223{
4224 return !(__lhs < __rhs);
4225}
4226
4227template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004230operator>=(const _CharT* __lhs,
4231 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004232{
4233 return !(__lhs < __rhs);
4234}
4235
4236// operator +
4237
4238template<class _CharT, class _Traits, class _Allocator>
4239basic_string<_CharT, _Traits, _Allocator>
4240operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4241 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4242{
4243 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4244 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4245 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4246 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4247 __r.append(__rhs.data(), __rhs_sz);
4248 return __r;
4249}
4250
4251template<class _CharT, class _Traits, class _Allocator>
4252basic_string<_CharT, _Traits, _Allocator>
4253operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4254{
4255 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4256 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4257 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4258 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4259 __r.append(__rhs.data(), __rhs_sz);
4260 return __r;
4261}
4262
4263template<class _CharT, class _Traits, class _Allocator>
4264basic_string<_CharT, _Traits, _Allocator>
4265operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4266{
4267 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4268 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4269 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4270 __r.append(__rhs.data(), __rhs_sz);
4271 return __r;
4272}
4273
4274template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004275inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276basic_string<_CharT, _Traits, _Allocator>
4277operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4278{
4279 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4280 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4281 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4282 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4283 __r.append(__rhs, __rhs_sz);
4284 return __r;
4285}
4286
4287template<class _CharT, class _Traits, class _Allocator>
4288basic_string<_CharT, _Traits, _Allocator>
4289operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4290{
4291 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4292 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4293 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4294 __r.push_back(__rhs);
4295 return __r;
4296}
4297
Eric Fiselierfc92be82017-04-19 00:28:44 +00004298#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299
4300template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302basic_string<_CharT, _Traits, _Allocator>
4303operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4304{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004305 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306}
4307
4308template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310basic_string<_CharT, _Traits, _Allocator>
4311operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4312{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004313 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314}
4315
4316template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004317inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318basic_string<_CharT, _Traits, _Allocator>
4319operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4320{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004321 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322}
4323
4324template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004325inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326basic_string<_CharT, _Traits, _Allocator>
4327operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4328{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004329 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330}
4331
4332template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004333inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334basic_string<_CharT, _Traits, _Allocator>
4335operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4336{
4337 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004338 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339}
4340
4341template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343basic_string<_CharT, _Traits, _Allocator>
4344operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4345{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004346 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347}
4348
4349template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004350inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351basic_string<_CharT, _Traits, _Allocator>
4352operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4353{
4354 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004355 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004356}
4357
Eric Fiselierfc92be82017-04-19 00:28:44 +00004358#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359
4360// swap
4361
4362template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004365swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004366 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4367 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368{
4369 __lhs.swap(__rhs);
4370}
4371
Bruce Mitchener170d8972020-11-24 12:53:53 -05004372_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4373_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4374_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4375_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4376_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004377
Bruce Mitchener170d8972020-11-24 12:53:53 -05004378_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4379_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4380_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004381
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004382_LIBCPP_FUNC_VIS string to_string(int __val);
4383_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4384_LIBCPP_FUNC_VIS string to_string(long __val);
4385_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4386_LIBCPP_FUNC_VIS string to_string(long long __val);
4387_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4388_LIBCPP_FUNC_VIS string to_string(float __val);
4389_LIBCPP_FUNC_VIS string to_string(double __val);
4390_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004391
Bruce Mitchener170d8972020-11-24 12:53:53 -05004392_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4393_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4394_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4395_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4396_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004397
Bruce Mitchener170d8972020-11-24 12:53:53 -05004398_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4399_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4400_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004401
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004402_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4403_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4404_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4405_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4406_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4407_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4408_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4409_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4410_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004411
Howard Hinnantc51e1022010-05-11 19:42:16 +00004412template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004413_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004414const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4415 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416
Marshall Clow851b9ec2019-05-20 21:56:51 +00004417template <class _CharT, class _Allocator>
4418struct _LIBCPP_TEMPLATE_VIS
4419 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4420 : public unary_function<
4421 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422{
4423 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004424 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4425 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004426};
4427
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428
Howard Hinnantdc095972011-07-18 15:51:59 +00004429template<class _CharT, class _Traits, class _Allocator>
4430basic_ostream<_CharT, _Traits>&
4431operator<<(basic_ostream<_CharT, _Traits>& __os,
4432 const basic_string<_CharT, _Traits, _Allocator>& __str);
4433
4434template<class _CharT, class _Traits, class _Allocator>
4435basic_istream<_CharT, _Traits>&
4436operator>>(basic_istream<_CharT, _Traits>& __is,
4437 basic_string<_CharT, _Traits, _Allocator>& __str);
4438
4439template<class _CharT, class _Traits, class _Allocator>
4440basic_istream<_CharT, _Traits>&
4441getline(basic_istream<_CharT, _Traits>& __is,
4442 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4443
4444template<class _CharT, class _Traits, class _Allocator>
4445inline _LIBCPP_INLINE_VISIBILITY
4446basic_istream<_CharT, _Traits>&
4447getline(basic_istream<_CharT, _Traits>& __is,
4448 basic_string<_CharT, _Traits, _Allocator>& __str);
4449
Eric Fiselierfc92be82017-04-19 00:28:44 +00004450#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004451
4452template<class _CharT, class _Traits, class _Allocator>
4453inline _LIBCPP_INLINE_VISIBILITY
4454basic_istream<_CharT, _Traits>&
4455getline(basic_istream<_CharT, _Traits>&& __is,
4456 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4457
4458template<class _CharT, class _Traits, class _Allocator>
4459inline _LIBCPP_INLINE_VISIBILITY
4460basic_istream<_CharT, _Traits>&
4461getline(basic_istream<_CharT, _Traits>&& __is,
4462 basic_string<_CharT, _Traits, _Allocator>& __str);
4463
Eric Fiselierfc92be82017-04-19 00:28:44 +00004464#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004465
Marshall Clow29b53f22018-12-14 18:49:35 +00004466#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004467template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004468inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004469 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4470 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4471 auto __old_size = __str.size();
4472 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4473 return __old_size - __str.size();
4474}
Marshall Clow29b53f22018-12-14 18:49:35 +00004475
Marek Kurdeja98b1412020-05-02 13:58:03 +02004476template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004477inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004478 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4479 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4480 _Predicate __pred) {
4481 auto __old_size = __str.size();
4482 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4483 __str.end());
4484 return __old_size - __str.size();
4485}
Marshall Clow29b53f22018-12-14 18:49:35 +00004486#endif
4487
Louis Dionneba400782020-10-02 15:02:52 -04004488#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004489
4490template<class _CharT, class _Traits, class _Allocator>
4491bool
4492basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4493{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004494 return this->data() <= _VSTD::__to_address(__i->base()) &&
4495 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004496}
4497
4498template<class _CharT, class _Traits, class _Allocator>
4499bool
4500basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4501{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004502 return this->data() < _VSTD::__to_address(__i->base()) &&
4503 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004504}
4505
4506template<class _CharT, class _Traits, class _Allocator>
4507bool
4508basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4509{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004510 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004511 return this->data() <= __p && __p <= this->data() + this->size();
4512}
4513
4514template<class _CharT, class _Traits, class _Allocator>
4515bool
4516basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4517{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004518 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004519 return this->data() <= __p && __p < this->data() + this->size();
4520}
4521
Louis Dionneba400782020-10-02 15:02:52 -04004522#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004523
Louis Dionne173f29e2019-05-29 16:01:36 +00004524#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004525// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004526inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004527{
4528 inline namespace string_literals
4529 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004530 inline _LIBCPP_INLINE_VISIBILITY
4531 basic_string<char> operator "" s( const char *__str, size_t __len )
4532 {
4533 return basic_string<char> (__str, __len);
4534 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004535
Howard Hinnant5c167562013-08-07 19:39:48 +00004536 inline _LIBCPP_INLINE_VISIBILITY
4537 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4538 {
4539 return basic_string<wchar_t> (__str, __len);
4540 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004541
Marshall Clow8732fed2018-12-11 04:35:44 +00004542#ifndef _LIBCPP_NO_HAS_CHAR8_T
4543 inline _LIBCPP_INLINE_VISIBILITY
4544 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4545 {
4546 return basic_string<char8_t> (__str, __len);
4547 }
4548#endif
4549
Howard Hinnant5c167562013-08-07 19:39:48 +00004550 inline _LIBCPP_INLINE_VISIBILITY
4551 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4552 {
4553 return basic_string<char16_t> (__str, __len);
4554 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004555
Howard Hinnant5c167562013-08-07 19:39:48 +00004556 inline _LIBCPP_INLINE_VISIBILITY
4557 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4558 {
4559 return basic_string<char32_t> (__str, __len);
4560 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004561 }
4562}
4563#endif
4564
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565_LIBCPP_END_NAMESPACE_STD
4566
Eric Fiselierf4433a32017-05-31 22:07:49 +00004567_LIBCPP_POP_MACROS
4568
Howard Hinnantc51e1022010-05-11 19:42:16 +00004569#endif // _LIBCPP_STRING