blob: 8f0229ceb2447dd1cb241c24f183879fc9d1c2e9 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Marek Kurdejc9848142020-11-26 10:07:16 +0100161 void reserve(size_type res_arg);
162 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000164 void clear() noexcept;
165 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166
167 const_reference operator[](size_type pos) const;
168 reference operator[](size_type pos);
169
170 const_reference at(size_type n) const;
171 reference at(size_type n);
172
173 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000174 template <class T>
175 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000176 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177 basic_string& operator+=(value_type c);
178 basic_string& operator+=(initializer_list<value_type>);
179
180 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000181 template <class T>
182 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000183 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000184 template <class T>
185 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000186 basic_string& append(const value_type* s, size_type n);
187 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000189 template<class InputIterator>
190 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(initializer_list<value_type>);
192
193 void push_back(value_type c);
194 void pop_back();
195 reference front();
196 const_reference front() const;
197 reference back();
198 const_reference back() const;
199
200 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000201 template <class T>
202 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000203 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000204 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000205 template <class T>
206 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000207 basic_string& assign(const value_type* s, size_type n);
208 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000210 template<class InputIterator>
211 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(initializer_list<value_type>);
213
214 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000215 template <class T>
216 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000217 basic_string& insert(size_type pos1, const basic_string& str,
218 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000219 template <class T>
220 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000221 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000222 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 basic_string& insert(size_type pos, size_type n, value_type c);
224 iterator insert(const_iterator p, value_type c);
225 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000226 template<class InputIterator>
227 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 iterator insert(const_iterator p, initializer_list<value_type>);
229
230 basic_string& erase(size_type pos = 0, size_type n = npos);
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
234 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000235 template <class T>
236 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000238 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000239 template <class T>
240 basic_string& replace(size_type pos1, size_type n1, const T& t,
241 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000242 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
243 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000245 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000246 template <class T>
247 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
249 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000250 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000251 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000252 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
253 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254
Howard Hinnantd17880b2013-06-28 16:59:19 +0000255 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256 basic_string substr(size_type pos = 0, size_type n = npos) const;
257
Howard Hinnant3e276872011-06-03 18:40:47 +0000258 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000259 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
260 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261
Howard Hinnantd17880b2013-06-28 16:59:19 +0000262 const value_type* c_str() const noexcept;
263 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000264 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000266 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000268 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000269 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800270 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 +0000271 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
272 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000273 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000275 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000276 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800277 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 +0000278 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
279 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000280 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000282 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000283 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800284 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 +0000285 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
286 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000287 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000289 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000290 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800291 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 +0000292 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
293 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000294 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000296 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000297 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800298 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 +0000299 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
300 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000301 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000303 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000304 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800305 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 +0000306 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
307 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000308 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000310 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000311 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800312 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
315 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000317 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000318 template <class T>
319 int compare(size_type pos1, size_type n1, const T& t,
320 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000321 int compare(const value_type* s) const noexcept;
322 int compare(size_type pos1, size_type n1, const value_type* s) const;
323 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324
Marek Kurdej24b4c512021-01-07 12:29:04 +0100325 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
326 bool starts_with(charT c) const noexcept; // C++20
327 bool starts_with(const charT* s) const; // C++20
328 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool ends_with(charT c) const noexcept; // C++20
330 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000331
Wim Leflere023c3542021-01-19 14:33:30 -0500332 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
333 constexpr bool contains(charT c) const noexcept; // C++2b
334 constexpr bool contains(const charT* s) const; // C++2b
335
Howard Hinnantc51e1022010-05-11 19:42:16 +0000336 bool __invariants() const;
337};
338
Marshall Clowa0563332018-02-08 06:34:03 +0000339template<class InputIterator,
340 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
341basic_string(InputIterator, InputIterator, Allocator = Allocator())
342 -> basic_string<typename iterator_traits<InputIterator>::value_type,
343 char_traits<typename iterator_traits<InputIterator>::value_type>,
344 Allocator>; // C++17
345
Howard Hinnantc51e1022010-05-11 19:42:16 +0000346template<class charT, class traits, class Allocator>
347basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000348operator+(const basic_string<charT, traits, Allocator>& lhs,
349 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350
351template<class charT, class traits, class Allocator>
352basic_string<charT, traits, Allocator>
353operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
354
355template<class charT, class traits, class Allocator>
356basic_string<charT, traits, Allocator>
357operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
358
359template<class charT, class traits, class Allocator>
360basic_string<charT, traits, Allocator>
361operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
362
363template<class charT, class traits, class Allocator>
364basic_string<charT, traits, Allocator>
365operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
366
367template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000368bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000369 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370
371template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000377template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000378bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000379 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380
381template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000388bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000389 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390
391template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000398bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000399 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400
401template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000408bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000409 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410
411template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000418bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000419 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420
421template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000428void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000429 basic_string<charT, traits, Allocator>& rhs)
430 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431
432template<class charT, class traits, class Allocator>
433basic_istream<charT, traits>&
434operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
435
436template<class charT, class traits, class Allocator>
437basic_ostream<charT, traits>&
438operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
439
440template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000441basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000442getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
443 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444
445template<class charT, class traits, class Allocator>
446basic_istream<charT, traits>&
447getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
448
Marshall Clow29b53f22018-12-14 18:49:35 +0000449template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200450typename basic_string<charT, traits, Allocator>::size_type
451erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456typedef basic_string<char> string;
457typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100458typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000459typedef basic_string<char16_t> u16string;
460typedef basic_string<char32_t> u32string;
461
Bruce Mitchener170d8972020-11-24 12:53:53 -0500462int stoi (const string& str, size_t* idx = nullptr, int base = 10);
463long stol (const string& str, size_t* idx = nullptr, int base = 10);
464unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
465long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
466unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000467
Bruce Mitchener170d8972020-11-24 12:53:53 -0500468float stof (const string& str, size_t* idx = nullptr);
469double stod (const string& str, size_t* idx = nullptr);
470long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000471
472string to_string(int val);
473string to_string(unsigned val);
474string to_string(long val);
475string to_string(unsigned long val);
476string to_string(long long val);
477string to_string(unsigned long long val);
478string to_string(float val);
479string to_string(double val);
480string to_string(long double val);
481
Bruce Mitchener170d8972020-11-24 12:53:53 -0500482int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
483long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
484unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
485long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
486unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000487
Bruce Mitchener170d8972020-11-24 12:53:53 -0500488float stof (const wstring& str, size_t* idx = nullptr);
489double stod (const wstring& str, size_t* idx = nullptr);
490long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000491
492wstring to_wstring(int val);
493wstring to_wstring(unsigned val);
494wstring to_wstring(long val);
495wstring to_wstring(unsigned long val);
496wstring to_wstring(long long val);
497wstring to_wstring(unsigned long long val);
498wstring to_wstring(float val);
499wstring to_wstring(double val);
500wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501
502template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100503template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504template <> struct hash<u16string>;
505template <> struct hash<u32string>;
506template <> struct hash<wstring>;
507
Marshall Clowcba751f2013-07-23 17:05:24 +0000508basic_string<char> operator "" s( const char *str, size_t len ); // C++14
509basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100510basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
512basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
513
Howard Hinnantc51e1022010-05-11 19:42:16 +0000514} // std
515
516*/
517
518#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400519#include <__debug>
520#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400521#include <__iterator/wrap_iter.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522#include <algorithm>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400523#include <compare>
524#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400525#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400526#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400527#include <initializer_list>
528#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530#include <memory>
531#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400532#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400534#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000535#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000536
Louis Dionne89258142021-08-23 15:32:36 -0400537#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
538# include <cwchar>
539#endif
540
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400541#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
542# include <cstdint>
543#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000544
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000545#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000547#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548
Eric Fiselierf4433a32017-05-31 22:07:49 +0000549_LIBCPP_PUSH_MACROS
550#include <__undef_macros>
551
552
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553_LIBCPP_BEGIN_NAMESPACE_STD
554
555// fpos
556
557template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000558class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559{
560private:
561 _StateT __st_;
562 streamoff __off_;
563public:
564 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
565
566 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
567
568 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
569 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
570
571 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
572 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
573 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
574 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
575};
576
577template <class _StateT>
578inline _LIBCPP_INLINE_VISIBILITY
579streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
580 {return streamoff(__x) - streamoff(__y);}
581
582template <class _StateT>
583inline _LIBCPP_INLINE_VISIBILITY
584bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
585 {return streamoff(__x) == streamoff(__y);}
586
587template <class _StateT>
588inline _LIBCPP_INLINE_VISIBILITY
589bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
590 {return streamoff(__x) != streamoff(__y);}
591
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592// basic_string
593
594template<class _CharT, class _Traits, class _Allocator>
595basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000596operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
597 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598
599template<class _CharT, class _Traits, class _Allocator>
600basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000601operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602
603template<class _CharT, class _Traits, class _Allocator>
604basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000605operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606
607template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000608inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000610operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611
612template<class _CharT, class _Traits, class _Allocator>
613basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000614operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615
Shoaib Meenaiea363712017-07-29 02:54:41 +0000616_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
617
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400619struct __basic_string_common;
620
621template <>
622struct __basic_string_common<true> {
623 // Both are defined in string.cpp
624 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
625 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000626};
627
Marshall Clow039b2f02016-01-13 21:54:34 +0000628template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400629struct __string_is_trivial_iterator : public false_type {};
630
631template <class _Tp>
632struct __string_is_trivial_iterator<_Tp*>
633 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000634
Louis Dionne173f29e2019-05-29 16:01:36 +0000635template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400636struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
637 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000638
Marshall Clow82513342016-09-24 22:45:42 +0000639template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500640struct __can_be_converted_to_string_view : public _BoolConstant<
641 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
642 !is_convertible<const _Tp&, const _CharT*>::value
643 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000644
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000645#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000646
647template <class _CharT, size_t = sizeof(_CharT)>
648struct __padding
649{
650 unsigned char __xx[sizeof(_CharT)-1];
651};
652
653template <class _CharT>
654struct __padding<_CharT, 1>
655{
656};
657
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400658#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000659
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400660#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800661typedef basic_string<char8_t> u8string;
662#endif
663
664#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
665typedef basic_string<char16_t> u16string;
666typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400667#endif
Richard Smith256954d2020-11-11 17:12:18 -0800668
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000669template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800670class
671 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400672#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800673 _LIBCPP_PREFERRED_NAME(u8string)
674#endif
675#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
676 _LIBCPP_PREFERRED_NAME(u16string)
677 _LIBCPP_PREFERRED_NAME(u32string)
678#endif
679 basic_string
Louis Dionneb6aabd92021-08-19 12:21:06 -0400680 : private __basic_string_common<true> // This base class is historical, but it needs to remain for ABI compatibility
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681{
682public:
683 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000684 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000686 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000688 typedef allocator_traits<allocator_type> __alloc_traits;
689 typedef typename __alloc_traits::size_type size_type;
690 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000691 typedef value_type& reference;
692 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000693 typedef typename __alloc_traits::pointer pointer;
694 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695
Marshall Clow79f33542018-03-21 00:36:05 +0000696 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
697 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
698 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
699 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000700 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000701 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000702 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000703
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704 typedef __wrap_iter<pointer> iterator;
705 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000706 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
707 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708
709private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000710
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000711#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000712
713 struct __long
714 {
715 pointer __data_;
716 size_type __size_;
717 size_type __cap_;
718 };
719
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000720#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000721 static const size_type __short_mask = 0x01;
722 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000723#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000724 static const size_type __short_mask = 0x80;
725 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400726#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000727
728 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
729 (sizeof(__long) - 1)/sizeof(value_type) : 2};
730
731 struct __short
732 {
733 value_type __data_[__min_cap];
734 struct
735 : __padding<value_type>
736 {
737 unsigned char __size_;
738 };
739 };
740
741#else
742
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743 struct __long
744 {
745 size_type __cap_;
746 size_type __size_;
747 pointer __data_;
748 };
749
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000750#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000751 static const size_type __short_mask = 0x80;
752 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000753#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000754 static const size_type __short_mask = 0x01;
755 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400756#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
759 (sizeof(__long) - 1)/sizeof(value_type) : 2};
760
761 struct __short
762 {
763 union
764 {
765 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000766 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 };
768 value_type __data_[__min_cap];
769 };
770
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400771#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000772
Howard Hinnant8ea98242013-08-23 17:37:05 +0000773 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774
Howard Hinnant8ea98242013-08-23 17:37:05 +0000775 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776
777 struct __raw
778 {
779 size_type __words[__n_words];
780 };
781
782 struct __rep
783 {
784 union
785 {
786 __long __l;
787 __short __s;
788 __raw __r;
789 };
790 };
791
792 __compressed_pair<__rep, allocator_type> __r_;
793
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200795 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796 static const size_type npos = -1;
797
Howard Hinnant3e276872011-06-03 18:40:47 +0000798 _LIBCPP_INLINE_VISIBILITY basic_string()
799 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000800
801 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
802#if _LIBCPP_STD_VER <= 14
803 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
804#else
805 _NOEXCEPT;
806#endif
807
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808 basic_string(const basic_string& __str);
809 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000810
Eric Fiselierfc92be82017-04-19 00:28:44 +0000811#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000813 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000814#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000815 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000816#else
817 _NOEXCEPT;
818#endif
819
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400822#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000823
Louis Dionne9ce598d2021-09-08 09:14:43 -0400824 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000825 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500826 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000827 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
828 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400829# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000830 __get_db()->__insert_c(this);
831# endif
832 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000833
Louis Dionne9ce598d2021-09-08 09:14:43 -0400834 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000835 _LIBCPP_INLINE_VISIBILITY
836 basic_string(const _CharT* __s, const _Allocator& __a);
837
Marek Kurdej90d79712021-07-27 16:16:21 +0200838#if _LIBCPP_STD_VER > 20
839 basic_string(nullptr_t) = delete;
840#endif
841
Howard Hinnantcf823322010-12-17 14:46:43 +0000842 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000843 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000844 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000845 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000846 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000847 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000848
Louis Dionne9ce598d2021-09-08 09:14:43 -0400849 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000850 _LIBCPP_INLINE_VISIBILITY
851 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
852
Marshall Clow83445802016-04-07 18:13:41 +0000853 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000854 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000855 _LIBCPP_INLINE_VISIBILITY
856 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000857 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000858
Louis Dionne9ce598d2021-09-08 09:14:43 -0400859 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000860 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000861 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500862 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000863
Louis Dionne9ce598d2021-09-08 09:14:43 -0400864 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500865 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000866 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
867 explicit basic_string(const _Tp& __t);
868
Louis Dionne9ce598d2021-09-08 09:14:43 -0400869 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000870 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
871 explicit basic_string(const _Tp& __t, const allocator_type& __a);
872
Louis Dionne9ce598d2021-09-08 09:14:43 -0400873 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400876 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000879#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000880 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000881 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000882 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000883 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400884#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000886 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000888 _LIBCPP_INLINE_VISIBILITY
889 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
890
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000891 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000892
Louis Dionne9ce598d2021-09-08 09:14:43 -0400893 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000894 basic_string& operator=(const _Tp& __t)
895 {__self_view __sv = __t; return assign(__sv);}
896
Eric Fiselierfc92be82017-04-19 00:28:44 +0000897#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000899 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000900 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000901 _LIBCPP_INLINE_VISIBILITY
902 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000904 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200905#if _LIBCPP_STD_VER > 20
906 basic_string& operator=(nullptr_t) = delete;
907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909
Louis Dionneba400782020-10-02 15:02:52 -0400910#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000911 _LIBCPP_INLINE_VISIBILITY
912 iterator begin() _NOEXCEPT
913 {return iterator(this, __get_pointer());}
914 _LIBCPP_INLINE_VISIBILITY
915 const_iterator begin() const _NOEXCEPT
916 {return const_iterator(this, __get_pointer());}
917 _LIBCPP_INLINE_VISIBILITY
918 iterator end() _NOEXCEPT
919 {return iterator(this, __get_pointer() + size());}
920 _LIBCPP_INLINE_VISIBILITY
921 const_iterator end() const _NOEXCEPT
922 {return const_iterator(this, __get_pointer() + size());}
923#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000924 _LIBCPP_INLINE_VISIBILITY
925 iterator begin() _NOEXCEPT
926 {return iterator(__get_pointer());}
927 _LIBCPP_INLINE_VISIBILITY
928 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000929 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000930 _LIBCPP_INLINE_VISIBILITY
931 iterator end() _NOEXCEPT
932 {return iterator(__get_pointer() + size());}
933 _LIBCPP_INLINE_VISIBILITY
934 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000935 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400936#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000937 _LIBCPP_INLINE_VISIBILITY
938 reverse_iterator rbegin() _NOEXCEPT
939 {return reverse_iterator(end());}
940 _LIBCPP_INLINE_VISIBILITY
941 const_reverse_iterator rbegin() const _NOEXCEPT
942 {return const_reverse_iterator(end());}
943 _LIBCPP_INLINE_VISIBILITY
944 reverse_iterator rend() _NOEXCEPT
945 {return reverse_iterator(begin());}
946 _LIBCPP_INLINE_VISIBILITY
947 const_reverse_iterator rend() const _NOEXCEPT
948 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000950 _LIBCPP_INLINE_VISIBILITY
951 const_iterator cbegin() const _NOEXCEPT
952 {return begin();}
953 _LIBCPP_INLINE_VISIBILITY
954 const_iterator cend() const _NOEXCEPT
955 {return end();}
956 _LIBCPP_INLINE_VISIBILITY
957 const_reverse_iterator crbegin() const _NOEXCEPT
958 {return rbegin();}
959 _LIBCPP_INLINE_VISIBILITY
960 const_reverse_iterator crend() const _NOEXCEPT
961 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000963 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000965 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
966 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
967 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000968 {return (__is_long() ? __get_long_cap()
969 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970
971 void resize(size_type __n, value_type __c);
972 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
973
Marek Kurdejc9848142020-11-26 10:07:16 +0100974 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000975 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
976
Marek Kurdejc9848142020-11-26 10:07:16 +0100977 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
978 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000979 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100980 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000982 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000983 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
984 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000986 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
987 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988
989 const_reference at(size_type __n) const;
990 reference at(size_type __n);
991
992 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000993
994 template <class _Tp>
995 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400996 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000997 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500998 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
999 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001000 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001001 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001002 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001003 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001005#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001007#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008
Howard Hinnantcf823322010-12-17 14:46:43 +00001009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001011
1012 template <class _Tp>
1013 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001014 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001015 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1016 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001017 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001018 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001019 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001020 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001021
Marshall Clow62953962016-10-03 23:40:48 +00001022 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001023 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001024 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001025 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1027 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001028 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001029 >
Marshall Clow82513342016-09-24 22:45:42 +00001030 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001031 basic_string& append(const value_type* __s, size_type __n);
1032 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001034
1035 _LIBCPP_INLINE_VISIBILITY
1036 void __append_default_init(size_type __n);
1037
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001039 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001040 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001042 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001044 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001045 _LIBCPP_INLINE_VISIBILITY
1046 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001047 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001048 append(__temp.data(), __temp.size());
1049 return *this;
1050 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001052 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001053 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001055 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001057 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001058 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001059 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001060
Eric Fiselierfc92be82017-04-19 00:28:44 +00001061#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001064#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065
1066 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001069 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1070 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1071 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1072 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073
Marshall Clowe46031a2018-07-02 18:41:15 +00001074 template <class _Tp>
1075 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001076 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001077 <
1078 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1079 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001080 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001081 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001082 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001083 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001084#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001085 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001086 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001087 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001088 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001089#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001090 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001091 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001092 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001093 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001094 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1096 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001097 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001098 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001099 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001100 basic_string& assign(const value_type* __s, size_type __n);
1101 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 basic_string& assign(size_type __n, value_type __c);
1103 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001104 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001105 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001107 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001109 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 assign(_InputIterator __first, _InputIterator __last);
1111 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001112 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001113 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001115 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001117 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001119#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001122#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123
Howard Hinnantcf823322010-12-17 14:46:43 +00001124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001126
1127 template <class _Tp>
1128 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001129 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001130 <
1131 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1132 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001134 insert(size_type __pos1, const _Tp& __t)
1135 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1136
Marshall Clow82513342016-09-24 22:45:42 +00001137 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001138 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001139 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001140 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001141 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001142 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001143 >
Marshall Clow82513342016-09-24 22:45:42 +00001144 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001145 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001146 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1147 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1149 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1152 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001153 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001154 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001156 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001158 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1160 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001161 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001162 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001164 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001166 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001168#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1171 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001172#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173
1174 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 iterator erase(const_iterator __first, const_iterator __last);
1179
Howard Hinnantcf823322010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001182
1183 template <class _Tp>
1184 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001185 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001186 <
1187 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1188 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001189 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001190 replace(size_type __pos1, size_type __n1, const _Tp& __t) { __self_view __sv = __t; return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001191 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
Marshall Clow82513342016-09-24 22:45:42 +00001192 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001193 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001194 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001195 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001196 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001197 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001198 >
Marshall Clow82513342016-09-24 22:45:42 +00001199 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001200 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1201 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001204 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001205
1206 template <class _Tp>
1207 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001208 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001209 <
1210 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1211 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001212 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001213 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1214
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001216 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001218 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001220 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001222 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001223 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001225 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001227 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001228 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001229#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001231 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001233#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234
Howard Hinnantd17880b2013-06-28 16:59:19 +00001235 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1238
Howard Hinnantcf823322010-12-17 14:46:43 +00001239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001240 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001241#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001242 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001243#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001244 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001245 __is_nothrow_swappable<allocator_type>::value);
1246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247
Howard Hinnantcf823322010-12-17 14:46:43 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001249 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001250 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001251 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001252#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001253 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001254 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001255#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256
Howard Hinnantcf823322010-12-17 14:46:43 +00001257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001258 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259
Howard Hinnantcf823322010-12-17 14:46:43 +00001260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001261 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001262
1263 template <class _Tp>
1264 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001265 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001266 <
1267 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1268 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001269 >
zoecarver1997e0a2021-02-05 11:54:47 -08001270 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001271 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001273 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001274 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Howard Hinnantcf823322010-12-17 14:46:43 +00001276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001277 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001278
1279 template <class _Tp>
1280 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001281 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001282 <
1283 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1284 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001285 >
zoecarver1997e0a2021-02-05 11:54:47 -08001286 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001287 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001289 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001290 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291
Howard Hinnantcf823322010-12-17 14:46:43 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001293 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001294
1295 template <class _Tp>
1296 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001297 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001298 <
1299 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1300 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001301 >
zoecarver1997e0a2021-02-05 11:54:47 -08001302 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001303 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001305 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001307 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308
Howard Hinnantcf823322010-12-17 14:46:43 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001310 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001311
1312 template <class _Tp>
1313 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001314 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001315 <
1316 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1317 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001318 >
zoecarver1997e0a2021-02-05 11:54:47 -08001319 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001320 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001322 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001324 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
Howard Hinnantcf823322010-12-17 14:46:43 +00001326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001327 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001328
1329 template <class _Tp>
1330 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001331 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001332 <
1333 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1334 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001335 >
zoecarver1997e0a2021-02-05 11:54:47 -08001336 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001337 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001339 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001340 _LIBCPP_INLINE_VISIBILITY
1341 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1342
1343 _LIBCPP_INLINE_VISIBILITY
1344 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001345
1346 template <class _Tp>
1347 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001348 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001349 <
1350 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1351 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001352 >
zoecarver1997e0a2021-02-05 11:54:47 -08001353 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001354 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001356 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001357 _LIBCPP_INLINE_VISIBILITY
1358 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1359
1360 _LIBCPP_INLINE_VISIBILITY
1361 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001362
1363 template <class _Tp>
1364 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001365 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001366 <
1367 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1368 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001369 >
zoecarver1997e0a2021-02-05 11:54:47 -08001370 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001371
1372 template <class _Tp>
1373 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001374 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001375 <
1376 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1377 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001378 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001379 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1380
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001383 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001384
Marshall Clow82513342016-09-24 22:45:42 +00001385 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001386 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001387 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001388 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001389 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001390 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001391 >
Marshall Clow82513342016-09-24 22:45:42 +00001392 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001393 int compare(const value_type* __s) const _NOEXCEPT;
1394 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1395 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396
Marshall Clow18c293b2017-12-04 20:11:38 +00001397#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001398 constexpr _LIBCPP_INLINE_VISIBILITY
1399 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001400 { return __self_view(data(), size()).starts_with(__sv); }
1401
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001402 constexpr _LIBCPP_INLINE_VISIBILITY
1403 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001404 { return !empty() && _Traits::eq(front(), __c); }
1405
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001406 constexpr _LIBCPP_INLINE_VISIBILITY
1407 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001408 { return starts_with(__self_view(__s)); }
1409
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001410 constexpr _LIBCPP_INLINE_VISIBILITY
1411 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001412 { return __self_view(data(), size()).ends_with( __sv); }
1413
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001414 constexpr _LIBCPP_INLINE_VISIBILITY
1415 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001416 { return !empty() && _Traits::eq(back(), __c); }
1417
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001418 constexpr _LIBCPP_INLINE_VISIBILITY
1419 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001420 { return ends_with(__self_view(__s)); }
1421#endif
1422
Wim Leflere023c3542021-01-19 14:33:30 -05001423#if _LIBCPP_STD_VER > 20
1424 constexpr _LIBCPP_INLINE_VISIBILITY
1425 bool contains(__self_view __sv) const noexcept
1426 { return __self_view(data(), size()).contains(__sv); }
1427
1428 constexpr _LIBCPP_INLINE_VISIBILITY
1429 bool contains(value_type __c) const noexcept
1430 { return __self_view(data(), size()).contains(__c); }
1431
1432 constexpr _LIBCPP_INLINE_VISIBILITY
1433 bool contains(const value_type* __s) const
1434 { return __self_view(data(), size()).contains(__s); }
1435#endif
1436
Howard Hinnantcf823322010-12-17 14:46:43 +00001437 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001438
Marshall Clowe60a7182018-05-29 17:04:37 +00001439 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001440
Marek Kurdejc9848142020-11-26 10:07:16 +01001441 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1442
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001443 _LIBCPP_INLINE_VISIBILITY
1444 bool __is_long() const _NOEXCEPT
1445 {return bool(__r_.first().__s.__size_ & __short_mask);}
1446
Louis Dionneba400782020-10-02 15:02:52 -04001447#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001448
1449 bool __dereferenceable(const const_iterator* __i) const;
1450 bool __decrementable(const const_iterator* __i) const;
1451 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1452 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1453
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001454#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001455
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001457 _LIBCPP_INLINE_VISIBILITY
1458 allocator_type& __alloc() _NOEXCEPT
1459 {return __r_.second();}
1460 _LIBCPP_INLINE_VISIBILITY
1461 const allocator_type& __alloc() const _NOEXCEPT
1462 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001464#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001465
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001467 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001468# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001470# else
1471 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1472# endif
1473
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001474 _LIBCPP_INLINE_VISIBILITY
1475 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001476# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001478# else
1479 {return __r_.first().__s.__size_;}
1480# endif
1481
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001482#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001483
1484 _LIBCPP_INLINE_VISIBILITY
1485 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001486# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001487 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1488# else
1489 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1490# endif
1491
1492 _LIBCPP_INLINE_VISIBILITY
1493 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001494# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001495 {return __r_.first().__s.__size_;}
1496# else
1497 {return __r_.first().__s.__size_ >> 1;}
1498# endif
1499
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001500#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001501
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001502 _LIBCPP_INLINE_VISIBILITY
1503 void __set_long_size(size_type __s) _NOEXCEPT
1504 {__r_.first().__l.__size_ = __s;}
1505 _LIBCPP_INLINE_VISIBILITY
1506 size_type __get_long_size() const _NOEXCEPT
1507 {return __r_.first().__l.__size_;}
1508 _LIBCPP_INLINE_VISIBILITY
1509 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1511
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 void __set_long_cap(size_type __s) _NOEXCEPT
1514 {__r_.first().__l.__cap_ = __long_mask | __s;}
1515 _LIBCPP_INLINE_VISIBILITY
1516 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001517 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __set_long_pointer(pointer __p) _NOEXCEPT
1521 {__r_.first().__l.__data_ = __p;}
1522 _LIBCPP_INLINE_VISIBILITY
1523 pointer __get_long_pointer() _NOEXCEPT
1524 {return __r_.first().__l.__data_;}
1525 _LIBCPP_INLINE_VISIBILITY
1526 const_pointer __get_long_pointer() const _NOEXCEPT
1527 {return __r_.first().__l.__data_;}
1528 _LIBCPP_INLINE_VISIBILITY
1529 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001530 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001531 _LIBCPP_INLINE_VISIBILITY
1532 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001533 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001534 _LIBCPP_INLINE_VISIBILITY
1535 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001537 _LIBCPP_INLINE_VISIBILITY
1538 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1540
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001541 _LIBCPP_INLINE_VISIBILITY
1542 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 {
1544 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1545 for (unsigned __i = 0; __i < __n_words; ++__i)
1546 __a[__i] = 0;
1547 }
1548
1549 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001551 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001552 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001554 static _LIBCPP_INLINE_VISIBILITY
1555 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001556 {
1557 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1558 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1559 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1560 if (__guess == __min_cap) ++__guess;
1561 return __guess;
1562 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001564 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001565 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001566 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001567 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001568 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001570
Martijn Vels5e7c9752020-03-04 17:52:46 -05001571 // Slow path for the (inlined) copy constructor for 'long' strings.
1572 // Always externally instantiated and not inlined.
1573 // Requires that __s is zero terminated.
1574 // The main reason for this function to exist is because for unstable, we
1575 // want to allow inlining of the copy constructor. However, we don't want
1576 // to call the __init() functions as those are marked as inline which may
1577 // result in over-aggressive inlining by the compiler, where our aim is
1578 // to only inline the fast path code directly in the ctor.
1579 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1580
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001582 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001583 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001585 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1586 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 __init(_InputIterator __first, _InputIterator __last);
1588
1589 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001590 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001591 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001593 __is_cpp17_forward_iterator<_ForwardIterator>::value
1594 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 __init(_ForwardIterator __first, _ForwardIterator __last);
1596
1597 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001598 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1600 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001601 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602
Martijn Vels596e3de2020-02-26 15:55:49 -05001603 // __assign_no_alias is invoked for assignment operations where we
1604 // have proof that the input does not alias the current instance.
1605 // For example, operator=(basic_string) performs a 'self' check.
1606 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001607 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001608
Howard Hinnantcf823322010-12-17 14:46:43 +00001609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 void __erase_to_end(size_type __pos);
1611
Martijn Velsa81fc792020-02-26 13:25:43 -05001612 // __erase_external_with_move is invoked for erase() invocations where
1613 // `n ~= npos`, likely requiring memory moves on the string data.
1614 void __erase_external_with_move(size_type __pos, size_type __n);
1615
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001616 _LIBCPP_INLINE_VISIBILITY
1617 void __copy_assign_alloc(const basic_string& __str)
1618 {__copy_assign_alloc(__str, integral_constant<bool,
1619 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1620
1621 _LIBCPP_INLINE_VISIBILITY
1622 void __copy_assign_alloc(const basic_string& __str, true_type)
1623 {
Marshall Clowf258c202017-01-31 03:40:52 +00001624 if (__alloc() == __str.__alloc())
1625 __alloc() = __str.__alloc();
1626 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001627 {
Marshall Clowf258c202017-01-31 03:40:52 +00001628 if (!__str.__is_long())
1629 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001630 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001631 __alloc() = __str.__alloc();
1632 }
1633 else
1634 {
1635 allocator_type __a = __str.__alloc();
1636 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001637 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001638 __alloc() = _VSTD::move(__a);
1639 __set_long_pointer(__p);
1640 __set_long_cap(__str.__get_long_cap());
1641 __set_long_size(__str.size());
1642 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001643 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001644 }
1645
1646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001647 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001648 {}
1649
Eric Fiselierfc92be82017-04-19 00:28:44 +00001650#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001651 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001652 void __move_assign(basic_string& __str, false_type)
1653 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001655 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001656#if _LIBCPP_STD_VER > 14
1657 _NOEXCEPT;
1658#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001659 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001660#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001661#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001662
1663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001664 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001665 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001666 _NOEXCEPT_(
1667 !__alloc_traits::propagate_on_container_move_assignment::value ||
1668 is_nothrow_move_assignable<allocator_type>::value)
1669 {__move_assign_alloc(__str, integral_constant<bool,
1670 __alloc_traits::propagate_on_container_move_assignment::value>());}
1671
1672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001673 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001674 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1675 {
1676 __alloc() = _VSTD::move(__c.__alloc());
1677 }
1678
1679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001680 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001681 _NOEXCEPT
1682 {}
1683
Martijn Velsda7d94f2020-06-19 14:24:03 -04001684 basic_string& __assign_external(const value_type* __s);
1685 basic_string& __assign_external(const value_type* __s, size_type __n);
1686
1687 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1688 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1689 pointer __p = __is_long()
1690 ? (__set_long_size(__n), __get_long_pointer())
1691 : (__set_short_size(__n), __get_short_pointer());
1692 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1693 traits_type::assign(__p[__n], value_type());
1694 return *this;
1695 }
1696
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001697 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1698 __set_size(__newsz);
1699 __invalidate_iterators_past(__newsz);
1700 traits_type::assign(__p[__newsz], value_type());
1701 return *this;
1702 }
1703
Howard Hinnantcf823322010-12-17 14:46:43 +00001704 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1705 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001707 template<class _Tp>
1708 _LIBCPP_INLINE_VISIBILITY
1709 bool __addr_in_range(_Tp&& __t) const {
1710 const volatile void *__p = _VSTD::addressof(__t);
1711 return data() <= __p && __p <= data() + size();
1712 }
1713
Louis Dionned24191c2021-08-19 12:39:16 -04001714 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1715 void __throw_length_error() const {
1716#ifndef _LIBCPP_NO_EXCEPTIONS
1717 __basic_string_common<true>::__throw_length_error();
1718#else
1719 _VSTD::abort();
1720#endif
1721 }
1722
1723 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1724 void __throw_out_of_range() const {
1725#ifndef _LIBCPP_NO_EXCEPTIONS
1726 __basic_string_common<true>::__throw_out_of_range();
1727#else
1728 _VSTD::abort();
1729#endif
1730 }
1731
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732 friend basic_string operator+<>(const basic_string&, const basic_string&);
1733 friend basic_string operator+<>(const value_type*, const basic_string&);
1734 friend basic_string operator+<>(value_type, const basic_string&);
1735 friend basic_string operator+<>(const basic_string&, const value_type*);
1736 friend basic_string operator+<>(const basic_string&, value_type);
1737};
1738
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001739// These declarations must appear before any functions are implicitly used
1740// so that they have the correct visibility specifier.
1741#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001742 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1743# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1744 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1745# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001746#else
Louis Dionne89258142021-08-23 15:32:36 -04001747 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1748# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1749 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1750# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001751#endif
1752
1753
Louis Dionned59f8a52021-08-17 11:59:07 -04001754#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001755template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001756 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001757 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001758 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1759 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001760 >
1761basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1762 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001763
1764template<class _CharT,
1765 class _Traits,
1766 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001767 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001768 >
1769explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1770 -> basic_string<_CharT, _Traits, _Allocator>;
1771
1772template<class _CharT,
1773 class _Traits,
1774 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001775 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001776 class _Sz = typename allocator_traits<_Allocator>::size_type
1777 >
1778basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1779 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001780#endif
1781
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001783inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784void
1785basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1786{
Louis Dionneba400782020-10-02 15:02:52 -04001787#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001788 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001789#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790}
1791
1792template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001793inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001795basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796{
Louis Dionneba400782020-10-02 15:02:52 -04001797#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001798 __c_node* __c = __get_db()->__find_c_and_lock(this);
1799 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001801 const_pointer __new_last = __get_pointer() + __pos;
1802 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001804 --__p;
1805 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1806 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001808 (*__p)->__c_ = nullptr;
1809 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001810 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001813 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001815#else
1816 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001817#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818}
1819
1820template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001821inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001822basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001823 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001824 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825{
Louis Dionneba400782020-10-02 15:02:52 -04001826#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001827 __get_db()->__insert_c(this);
1828#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 __zero();
1830}
1831
1832template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001833inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001835#if _LIBCPP_STD_VER <= 14
1836 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1837#else
1838 _NOEXCEPT
1839#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001840: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841{
Louis Dionneba400782020-10-02 15:02:52 -04001842#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001843 __get_db()->__insert_c(this);
1844#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 __zero();
1846}
1847
1848template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001849void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1850 size_type __sz,
1851 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852{
1853 if (__reserve > max_size())
1854 this->__throw_length_error();
1855 pointer __p;
1856 if (__reserve < __min_cap)
1857 {
1858 __set_short_size(__sz);
1859 __p = __get_short_pointer();
1860 }
1861 else
1862 {
1863 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001864 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 __set_long_pointer(__p);
1866 __set_long_cap(__cap+1);
1867 __set_long_size(__sz);
1868 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001869 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 traits_type::assign(__p[__sz], value_type());
1871}
1872
1873template <class _CharT, class _Traits, class _Allocator>
1874void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001875basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876{
1877 if (__sz > max_size())
1878 this->__throw_length_error();
1879 pointer __p;
1880 if (__sz < __min_cap)
1881 {
1882 __set_short_size(__sz);
1883 __p = __get_short_pointer();
1884 }
1885 else
1886 {
1887 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001888 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889 __set_long_pointer(__p);
1890 __set_long_cap(__cap+1);
1891 __set_long_size(__sz);
1892 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001893 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 traits_type::assign(__p[__sz], value_type());
1895}
1896
1897template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001898template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001899basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001900 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001902 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001904#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001905 __get_db()->__insert_c(this);
1906#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907}
1908
1909template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001910inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001911basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001912 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001914 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001916#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001917 __get_db()->__insert_c(this);
1918#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919}
1920
1921template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001922inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001923basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001924 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001926 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001928#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001929 __get_db()->__insert_c(this);
1930#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931}
1932
1933template <class _CharT, class _Traits, class _Allocator>
1934basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001935 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936{
1937 if (!__str.__is_long())
1938 __r_.first().__r = __str.__r_.first().__r;
1939 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001940 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1941 __str.__get_long_size());
1942
Louis Dionneba400782020-10-02 15:02:52 -04001943#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001944 __get_db()->__insert_c(this);
1945#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946}
1947
1948template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001949basic_string<_CharT, _Traits, _Allocator>::basic_string(
1950 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001951 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952{
1953 if (!__str.__is_long())
1954 __r_.first().__r = __str.__r_.first().__r;
1955 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001956 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1957 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001958#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001959 __get_db()->__insert_c(this);
1960#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961}
1962
Martijn Vels5e7c9752020-03-04 17:52:46 -05001963template <class _CharT, class _Traits, class _Allocator>
1964void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1965 const value_type* __s, size_type __sz) {
1966 pointer __p;
1967 if (__sz < __min_cap) {
1968 __p = __get_short_pointer();
1969 __set_short_size(__sz);
1970 } else {
1971 if (__sz > max_size())
1972 this->__throw_length_error();
1973 size_t __cap = __recommend(__sz);
1974 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1975 __set_long_pointer(__p);
1976 __set_long_cap(__cap + 1);
1977 __set_long_size(__sz);
1978 }
1979 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1980}
1981
Eric Fiselierfc92be82017-04-19 00:28:44 +00001982#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983
1984template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001985inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001986basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001987#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001988 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001989#else
1990 _NOEXCEPT
1991#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001992 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993{
1994 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001995#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001996 __get_db()->__insert_c(this);
1997 if (__is_long())
1998 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999#endif
2000}
2001
2002template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002003inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002005 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006{
Marshall Clowd2d24692014-07-17 15:32:20 +00002007 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002008 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002009 else
2010 {
2011 __r_.first().__r = __str.__r_.first().__r;
2012 __str.__zero();
2013 }
Louis Dionneba400782020-10-02 15:02:52 -04002014#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002015 __get_db()->__insert_c(this);
2016 if (__is_long())
2017 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018#endif
2019}
2020
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002021#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022
2023template <class _CharT, class _Traits, class _Allocator>
2024void
2025basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2026{
2027 if (__n > max_size())
2028 this->__throw_length_error();
2029 pointer __p;
2030 if (__n < __min_cap)
2031 {
2032 __set_short_size(__n);
2033 __p = __get_short_pointer();
2034 }
2035 else
2036 {
2037 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002038 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039 __set_long_pointer(__p);
2040 __set_long_cap(__cap+1);
2041 __set_long_size(__n);
2042 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002043 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044 traits_type::assign(__p[__n], value_type());
2045}
2046
2047template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002048inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002049basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002050 : __r_(__default_init_tag(), __default_init_tag())
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
2058template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002059template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002060basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002061 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062{
2063 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002064#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002065 __get_db()->__insert_c(this);
2066#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067}
2068
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002070basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2071 size_type __pos, size_type __n,
2072 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002073 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074{
2075 size_type __str_sz = __str.size();
2076 if (__pos > __str_sz)
2077 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002078 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002079#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002080 __get_db()->__insert_c(this);
2081#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082}
2083
2084template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002085inline
Marshall Clow83445802016-04-07 18:13:41 +00002086basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002087 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002088 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002089{
2090 size_type __str_sz = __str.size();
2091 if (__pos > __str_sz)
2092 this->__throw_out_of_range();
2093 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002094#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002095 __get_db()->__insert_c(this);
2096#endif
2097}
2098
2099template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002100template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002101basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002102 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002103 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002104{
Marshall Clowe46031a2018-07-02 18:41:15 +00002105 __self_view __sv0 = __t;
2106 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002107 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002108#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002109 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002110#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002111}
2112
2113template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002114template <class _Tp, class>
2115basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002116 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002117{
Marshall Clowe46031a2018-07-02 18:41:15 +00002118 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002119 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002120#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002121 __get_db()->__insert_c(this);
2122#endif
2123}
2124
2125template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002126template <class _Tp, class>
2127basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002128 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002129{
Marshall Clowe46031a2018-07-02 18:41:15 +00002130 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002131 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002132#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002133 __get_db()->__insert_c(this);
2134#endif
2135}
2136
2137template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002139__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002141 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2142>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2144{
2145 __zero();
2146#ifndef _LIBCPP_NO_EXCEPTIONS
2147 try
2148 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002149#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 for (; __first != __last; ++__first)
2151 push_back(*__first);
2152#ifndef _LIBCPP_NO_EXCEPTIONS
2153 }
2154 catch (...)
2155 {
2156 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002157 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158 throw;
2159 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002160#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161}
2162
2163template <class _CharT, class _Traits, class _Allocator>
2164template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002165__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002167 __is_cpp17_forward_iterator<_ForwardIterator>::value
2168>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2170{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002171 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 if (__sz > max_size())
2173 this->__throw_length_error();
2174 pointer __p;
2175 if (__sz < __min_cap)
2176 {
2177 __set_short_size(__sz);
2178 __p = __get_short_pointer();
2179 }
2180 else
2181 {
2182 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002183 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 __set_long_pointer(__p);
2185 __set_long_cap(__cap+1);
2186 __set_long_size(__sz);
2187 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002188
2189#ifndef _LIBCPP_NO_EXCEPTIONS
2190 try
2191 {
2192#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002193 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 traits_type::assign(*__p, *__first);
2195 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002196#ifndef _LIBCPP_NO_EXCEPTIONS
2197 }
2198 catch (...)
2199 {
2200 if (__is_long())
2201 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2202 throw;
2203 }
2204#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205}
2206
2207template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002208template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002209inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002211 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212{
2213 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002214#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002215 __get_db()->__insert_c(this);
2216#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217}
2218
2219template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002220template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002221inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2223 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002224 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225{
2226 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002227#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002228 __get_db()->__insert_c(this);
2229#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230}
2231
Eric Fiselierfc92be82017-04-19 00:28:44 +00002232#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002233
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002235inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002236basic_string<_CharT, _Traits, _Allocator>::basic_string(
2237 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002238 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239{
2240 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002241#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002242 __get_db()->__insert_c(this);
2243#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244}
2245
2246template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002247inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002248
Eric Fiselier812882b2017-02-17 01:17:10 +00002249basic_string<_CharT, _Traits, _Allocator>::basic_string(
2250 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002251 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252{
2253 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002254#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002255 __get_db()->__insert_c(this);
2256#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257}
2258
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002259#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002260
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2263{
Louis Dionneba400782020-10-02 15:02:52 -04002264#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002265 __get_db()->__erase_c(this);
2266#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002268 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269}
2270
2271template <class _CharT, class _Traits, class _Allocator>
2272void
2273basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2274 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002275 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 +00002276{
2277 size_type __ms = max_size();
2278 if (__delta_cap > __ms - __old_cap - 1)
2279 this->__throw_length_error();
2280 pointer __old_p = __get_pointer();
2281 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002282 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002284 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285 __invalidate_all_iterators();
2286 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002287 traits_type::copy(_VSTD::__to_address(__p),
2288 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002290 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2292 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002293 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2294 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002296 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 __set_long_pointer(__p);
2298 __set_long_cap(__cap+1);
2299 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2300 __set_long_size(__old_sz);
2301 traits_type::assign(__p[__old_sz], value_type());
2302}
2303
2304template <class _CharT, class _Traits, class _Allocator>
2305void
2306basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2307 size_type __n_copy, size_type __n_del, size_type __n_add)
2308{
2309 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002310 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311 this->__throw_length_error();
2312 pointer __old_p = __get_pointer();
2313 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002314 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002316 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317 __invalidate_all_iterators();
2318 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002319 traits_type::copy(_VSTD::__to_address(__p),
2320 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2322 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002323 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2324 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002325 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002327 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328 __set_long_pointer(__p);
2329 __set_long_cap(__cap+1);
2330}
2331
2332// assign
2333
2334template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002335template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002336basic_string<_CharT, _Traits, _Allocator>&
2337basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002338 const value_type* __s, size_type __n) {
2339 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2340 if (__n < __cap) {
2341 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2342 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2343 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2344 traits_type::assign(__p[__n], value_type());
2345 __invalidate_iterators_past(__n);
2346 } else {
2347 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2348 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2349 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002350 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002351}
2352
2353template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002355basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2356 const value_type* __s, size_type __n) {
2357 size_type __cap = capacity();
2358 if (__cap >= __n) {
2359 value_type* __p = _VSTD::__to_address(__get_pointer());
2360 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002361 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002362 } else {
2363 size_type __sz = size();
2364 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002365 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002366 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002367}
2368
2369template <class _CharT, class _Traits, class _Allocator>
2370basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002371basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002373 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002374 return (__builtin_constant_p(__n) && __n < __min_cap)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002375 ? __assign_short(__s, __n)
2376 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002377}
2378
2379template <class _CharT, class _Traits, class _Allocator>
2380basic_string<_CharT, _Traits, _Allocator>&
2381basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2382{
2383 size_type __cap = capacity();
2384 if (__cap < __n)
2385 {
2386 size_type __sz = size();
2387 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2388 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002389 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002391 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392}
2393
2394template <class _CharT, class _Traits, class _Allocator>
2395basic_string<_CharT, _Traits, _Allocator>&
2396basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2397{
2398 pointer __p;
2399 if (__is_long())
2400 {
2401 __p = __get_long_pointer();
2402 __set_long_size(1);
2403 }
2404 else
2405 {
2406 __p = __get_short_pointer();
2407 __set_short_size(1);
2408 }
2409 traits_type::assign(*__p, __c);
2410 traits_type::assign(*++__p, value_type());
2411 __invalidate_iterators_past(1);
2412 return *this;
2413}
2414
2415template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002416basic_string<_CharT, _Traits, _Allocator>&
2417basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2418{
Martijn Vels596e3de2020-02-26 15:55:49 -05002419 if (this != &__str) {
2420 __copy_assign_alloc(__str);
2421 if (!__is_long()) {
2422 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002423 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002424 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002425 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002426 }
2427 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002428 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002429 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002430 }
2431 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002432}
2433
Eric Fiselierfc92be82017-04-19 00:28:44 +00002434#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002435
2436template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002437inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002438void
2439basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002440 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002441{
2442 if (__alloc() != __str.__alloc())
2443 assign(__str);
2444 else
2445 __move_assign(__str, true_type());
2446}
2447
2448template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002449inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002450void
2451basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002452#if _LIBCPP_STD_VER > 14
2453 _NOEXCEPT
2454#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002455 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002456#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002457{
marshall2ed41622019-11-27 07:13:00 -08002458 if (__is_long()) {
2459 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2460 __get_long_cap());
2461#if _LIBCPP_STD_VER <= 14
2462 if (!is_nothrow_move_assignable<allocator_type>::value) {
2463 __set_short_size(0);
2464 traits_type::assign(__get_short_pointer()[0], value_type());
2465 }
2466#endif
2467 }
2468 __move_assign_alloc(__str);
2469 __r_.first() = __str.__r_.first();
2470 __str.__set_short_size(0);
2471 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002472}
2473
2474template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002475inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002476basic_string<_CharT, _Traits, _Allocator>&
2477basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002478 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002479{
2480 __move_assign(__str, integral_constant<bool,
2481 __alloc_traits::propagate_on_container_move_assignment::value>());
2482 return *this;
2483}
2484
2485#endif
2486
2487template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002489__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002491 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002493>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2495{
Marshall Clow958362f2016-09-05 01:54:30 +00002496 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002497 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002498 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499}
2500
2501template <class _CharT, class _Traits, class _Allocator>
2502template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002503__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002505 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002507>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002508basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2509{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002511 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2512 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2513
2514 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2515 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002517 if (__cap < __n)
2518 {
2519 size_type __sz = size();
2520 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2521 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002522 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002523 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002524 traits_type::assign(*__p, *__first);
2525 traits_type::assign(*__p, value_type());
2526 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002527 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528 }
2529 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002530 {
2531 const basic_string __temp(__first, __last, __alloc());
2532 assign(__temp.data(), __temp.size());
2533 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534 return *this;
2535}
2536
2537template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538basic_string<_CharT, _Traits, _Allocator>&
2539basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2540{
2541 size_type __sz = __str.size();
2542 if (__pos > __sz)
2543 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002544 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545}
2546
2547template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002548template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002549__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002550<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002551 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2552 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002553 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002554>
Marshall Clow82513342016-09-24 22:45:42 +00002555basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002556{
Marshall Clow82513342016-09-24 22:45:42 +00002557 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002558 size_type __sz = __sv.size();
2559 if (__pos > __sz)
2560 this->__throw_out_of_range();
2561 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2562}
2563
2564
2565template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002567basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2568 return __assign_external(__s, traits_type::length(__s));
2569}
2570
2571template <class _CharT, class _Traits, class _Allocator>
2572basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002573basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002575 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002576 return __builtin_constant_p(*__s)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002577 ? (traits_type::length(__s) < __min_cap
2578 ? __assign_short(__s, traits_type::length(__s))
2579 : __assign_external(__s, traits_type::length(__s)))
2580 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582// append
2583
2584template <class _CharT, class _Traits, class _Allocator>
2585basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002586basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002588 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589 size_type __cap = capacity();
2590 size_type __sz = size();
2591 if (__cap - __sz >= __n)
2592 {
2593 if (__n)
2594 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002595 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596 traits_type::copy(__p + __sz, __s, __n);
2597 __sz += __n;
2598 __set_size(__sz);
2599 traits_type::assign(__p[__sz], value_type());
2600 }
2601 }
2602 else
2603 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2604 return *this;
2605}
2606
2607template <class _CharT, class _Traits, class _Allocator>
2608basic_string<_CharT, _Traits, _Allocator>&
2609basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2610{
2611 if (__n)
2612 {
2613 size_type __cap = capacity();
2614 size_type __sz = size();
2615 if (__cap - __sz < __n)
2616 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2617 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002618 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619 __sz += __n;
2620 __set_size(__sz);
2621 traits_type::assign(__p[__sz], value_type());
2622 }
2623 return *this;
2624}
2625
2626template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002627inline void
2628basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2629{
2630 if (__n)
2631 {
2632 size_type __cap = capacity();
2633 size_type __sz = size();
2634 if (__cap - __sz < __n)
2635 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2636 pointer __p = __get_pointer();
2637 __sz += __n;
2638 __set_size(__sz);
2639 traits_type::assign(__p[__sz], value_type());
2640 }
2641}
2642
2643template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644void
2645basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2646{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002647 bool __is_short = !__is_long();
2648 size_type __cap;
2649 size_type __sz;
2650 if (__is_short)
2651 {
2652 __cap = __min_cap - 1;
2653 __sz = __get_short_size();
2654 }
2655 else
2656 {
2657 __cap = __get_long_cap() - 1;
2658 __sz = __get_long_size();
2659 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002661 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002662 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002663 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002664 }
2665 pointer __p;
2666 if (__is_short)
2667 {
2668 __p = __get_short_pointer() + __sz;
2669 __set_short_size(__sz+1);
2670 }
2671 else
2672 {
2673 __p = __get_long_pointer() + __sz;
2674 __set_long_size(__sz+1);
2675 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676 traits_type::assign(*__p, __c);
2677 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678}
2679
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680template <class _CharT, class _Traits, class _Allocator>
2681template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002682__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002683<
2684 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2685 basic_string<_CharT, _Traits, _Allocator>&
2686>
2687basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002688 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689{
2690 size_type __sz = size();
2691 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002692 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693 if (__n)
2694 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002695 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2696 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002697 {
2698 if (__cap - __sz < __n)
2699 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2700 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002701 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002702 traits_type::assign(*__p, *__first);
2703 traits_type::assign(*__p, value_type());
2704 __set_size(__sz + __n);
2705 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002706 else
2707 {
2708 const basic_string __temp(__first, __last, __alloc());
2709 append(__temp.data(), __temp.size());
2710 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711 }
2712 return *this;
2713}
2714
2715template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002716inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717basic_string<_CharT, _Traits, _Allocator>&
2718basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2719{
2720 return append(__str.data(), __str.size());
2721}
2722
2723template <class _CharT, class _Traits, class _Allocator>
2724basic_string<_CharT, _Traits, _Allocator>&
2725basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2726{
2727 size_type __sz = __str.size();
2728 if (__pos > __sz)
2729 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002730 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731}
2732
2733template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002734template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002735 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002736 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002737 __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 +00002738 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002739 >
Marshall Clow82513342016-09-24 22:45:42 +00002740basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002741{
Marshall Clow82513342016-09-24 22:45:42 +00002742 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002743 size_type __sz = __sv.size();
2744 if (__pos > __sz)
2745 this->__throw_out_of_range();
2746 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2747}
2748
2749template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002751basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002753 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 return append(__s, traits_type::length(__s));
2755}
2756
2757// insert
2758
2759template <class _CharT, class _Traits, class _Allocator>
2760basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002761basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002763 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 size_type __sz = size();
2765 if (__pos > __sz)
2766 this->__throw_out_of_range();
2767 size_type __cap = capacity();
2768 if (__cap - __sz >= __n)
2769 {
2770 if (__n)
2771 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002772 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773 size_type __n_move = __sz - __pos;
2774 if (__n_move != 0)
2775 {
2776 if (__p + __pos <= __s && __s < __p + __sz)
2777 __s += __n;
2778 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2779 }
2780 traits_type::move(__p + __pos, __s, __n);
2781 __sz += __n;
2782 __set_size(__sz);
2783 traits_type::assign(__p[__sz], value_type());
2784 }
2785 }
2786 else
2787 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2788 return *this;
2789}
2790
2791template <class _CharT, class _Traits, class _Allocator>
2792basic_string<_CharT, _Traits, _Allocator>&
2793basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2794{
2795 size_type __sz = size();
2796 if (__pos > __sz)
2797 this->__throw_out_of_range();
2798 if (__n)
2799 {
2800 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002801 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 if (__cap - __sz >= __n)
2803 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002804 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805 size_type __n_move = __sz - __pos;
2806 if (__n_move != 0)
2807 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2808 }
2809 else
2810 {
2811 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002812 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 }
2814 traits_type::assign(__p + __pos, __n, __c);
2815 __sz += __n;
2816 __set_size(__sz);
2817 traits_type::assign(__p[__sz], value_type());
2818 }
2819 return *this;
2820}
2821
2822template <class _CharT, class _Traits, class _Allocator>
2823template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002824__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002826 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002827 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002828>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2830{
Louis Dionneba400782020-10-02 15:02:52 -04002831#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002832 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2833 "string::insert(iterator, range) called with an iterator not"
2834 " referring to this string");
2835#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002836 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002837 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838}
2839
2840template <class _CharT, class _Traits, class _Allocator>
2841template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002842__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002844 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002846>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2848{
Louis Dionneba400782020-10-02 15:02:52 -04002849#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002850 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2851 "string::insert(iterator, range) called with an iterator not"
2852 " referring to this string");
2853#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002855 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 if (__n)
2857 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002858 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2859 !__addr_in_range(*__first))
2860 {
2861 size_type __sz = size();
2862 size_type __cap = capacity();
2863 value_type* __p;
2864 if (__cap - __sz >= __n)
2865 {
2866 __p = _VSTD::__to_address(__get_pointer());
2867 size_type __n_move = __sz - __ip;
2868 if (__n_move != 0)
2869 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2870 }
2871 else
2872 {
2873 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2874 __p = _VSTD::__to_address(__get_long_pointer());
2875 }
2876 __sz += __n;
2877 __set_size(__sz);
2878 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002879 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002880 traits_type::assign(*__p, *__first);
2881 }
2882 else
Marshall Clow958362f2016-09-05 01:54:30 +00002883 {
2884 const basic_string __temp(__first, __last, __alloc());
2885 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2886 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887 }
2888 return begin() + __ip;
2889}
2890
2891template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002892inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893basic_string<_CharT, _Traits, _Allocator>&
2894basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2895{
2896 return insert(__pos1, __str.data(), __str.size());
2897}
2898
2899template <class _CharT, class _Traits, class _Allocator>
2900basic_string<_CharT, _Traits, _Allocator>&
2901basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2902 size_type __pos2, size_type __n)
2903{
2904 size_type __str_sz = __str.size();
2905 if (__pos2 > __str_sz)
2906 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002907 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908}
2909
2910template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002911template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002912__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002913<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002914 __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 +00002915 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002916>
Marshall Clow82513342016-09-24 22:45:42 +00002917basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002918 size_type __pos2, size_type __n)
2919{
Marshall Clow82513342016-09-24 22:45:42 +00002920 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002921 size_type __str_sz = __sv.size();
2922 if (__pos2 > __str_sz)
2923 this->__throw_out_of_range();
2924 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2925}
2926
2927template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002929basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002931 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 return insert(__pos, __s, traits_type::length(__s));
2933}
2934
2935template <class _CharT, class _Traits, class _Allocator>
2936typename basic_string<_CharT, _Traits, _Allocator>::iterator
2937basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2938{
2939 size_type __ip = static_cast<size_type>(__pos - begin());
2940 size_type __sz = size();
2941 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002942 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 if (__cap == __sz)
2944 {
2945 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002946 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 }
2948 else
2949 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002950 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951 size_type __n_move = __sz - __ip;
2952 if (__n_move != 0)
2953 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2954 }
2955 traits_type::assign(__p[__ip], __c);
2956 traits_type::assign(__p[++__sz], value_type());
2957 __set_size(__sz);
2958 return begin() + static_cast<difference_type>(__ip);
2959}
2960
2961template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002962inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963typename basic_string<_CharT, _Traits, _Allocator>::iterator
2964basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2965{
Louis Dionneba400782020-10-02 15:02:52 -04002966#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002967 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2968 "string::insert(iterator, n, value) called with an iterator not"
2969 " referring to this string");
2970#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 difference_type __p = __pos - begin();
2972 insert(static_cast<size_type>(__p), __n, __c);
2973 return begin() + __p;
2974}
2975
2976// replace
2977
2978template <class _CharT, class _Traits, class _Allocator>
2979basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002980basic_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 +00002981 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002983 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984 size_type __sz = size();
2985 if (__pos > __sz)
2986 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002987 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002988 size_type __cap = capacity();
2989 if (__cap - __sz + __n1 >= __n2)
2990 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002991 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992 if (__n1 != __n2)
2993 {
2994 size_type __n_move = __sz - __pos - __n1;
2995 if (__n_move != 0)
2996 {
2997 if (__n1 > __n2)
2998 {
2999 traits_type::move(__p + __pos, __s, __n2);
3000 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003001 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002 }
3003 if (__p + __pos < __s && __s < __p + __sz)
3004 {
3005 if (__p + __pos + __n1 <= __s)
3006 __s += __n2 - __n1;
3007 else // __p + __pos < __s < __p + __pos + __n1
3008 {
3009 traits_type::move(__p + __pos, __s, __n1);
3010 __pos += __n1;
3011 __s += __n2;
3012 __n2 -= __n1;
3013 __n1 = 0;
3014 }
3015 }
3016 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3017 }
3018 }
3019 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003020 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021 }
3022 else
3023 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3024 return *this;
3025}
3026
3027template <class _CharT, class _Traits, class _Allocator>
3028basic_string<_CharT, _Traits, _Allocator>&
3029basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3030{
3031 size_type __sz = size();
3032 if (__pos > __sz)
3033 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003034 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003036 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037 if (__cap - __sz + __n1 >= __n2)
3038 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003039 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 if (__n1 != __n2)
3041 {
3042 size_type __n_move = __sz - __pos - __n1;
3043 if (__n_move != 0)
3044 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3045 }
3046 }
3047 else
3048 {
3049 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003050 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051 }
3052 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003053 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054}
3055
3056template <class _CharT, class _Traits, class _Allocator>
3057template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003058__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003060 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003062>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003063basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064 _InputIterator __j1, _InputIterator __j2)
3065{
Marshall Clow958362f2016-09-05 01:54:30 +00003066 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003067 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068}
3069
3070template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003071inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072basic_string<_CharT, _Traits, _Allocator>&
3073basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3074{
3075 return replace(__pos1, __n1, __str.data(), __str.size());
3076}
3077
3078template <class _CharT, class _Traits, class _Allocator>
3079basic_string<_CharT, _Traits, _Allocator>&
3080basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3081 size_type __pos2, size_type __n2)
3082{
3083 size_type __str_sz = __str.size();
3084 if (__pos2 > __str_sz)
3085 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003086 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087}
3088
3089template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003090template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003091__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003092<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003093 __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 +00003094 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003095>
Marshall Clow82513342016-09-24 22:45:42 +00003096basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003097 size_type __pos2, size_type __n2)
3098{
Marshall Clow82513342016-09-24 22:45:42 +00003099 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003100 size_type __str_sz = __sv.size();
3101 if (__pos2 > __str_sz)
3102 this->__throw_out_of_range();
3103 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3104}
3105
3106template <class _CharT, class _Traits, class _Allocator>
3107basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003108basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003110 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111 return replace(__pos, __n1, __s, traits_type::length(__s));
3112}
3113
3114template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003115inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003117basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118{
3119 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3120 __str.data(), __str.size());
3121}
3122
3123template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003124inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003126basic_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 +00003127{
3128 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3129}
3130
3131template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003132inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003134basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135{
3136 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3137}
3138
3139template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003140inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003142basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143{
3144 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3145}
3146
3147// erase
3148
Martijn Velsa81fc792020-02-26 13:25:43 -05003149// 'externally instantiated' erase() implementation, called when __n != npos.
3150// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003152void
3153basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3154 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003156 if (__n)
3157 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003158 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003159 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003160 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161 size_type __n_move = __sz - __pos - __n;
3162 if (__n_move != 0)
3163 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003164 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003166}
3167
3168template <class _CharT, class _Traits, class _Allocator>
3169basic_string<_CharT, _Traits, _Allocator>&
3170basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3171 size_type __n) {
3172 if (__pos > size()) this->__throw_out_of_range();
3173 if (__n == npos) {
3174 __erase_to_end(__pos);
3175 } else {
3176 __erase_external_with_move(__pos, __n);
3177 }
3178 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179}
3180
3181template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003182inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183typename basic_string<_CharT, _Traits, _Allocator>::iterator
3184basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3185{
Louis Dionneba400782020-10-02 15:02:52 -04003186#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003187 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3188 "string::erase(iterator) called with an iterator not"
3189 " referring to this string");
3190#endif
3191 _LIBCPP_ASSERT(__pos != end(),
3192 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193 iterator __b = begin();
3194 size_type __r = static_cast<size_type>(__pos - __b);
3195 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003196 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197}
3198
3199template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003200inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201typename basic_string<_CharT, _Traits, _Allocator>::iterator
3202basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3203{
Louis Dionneba400782020-10-02 15:02:52 -04003204#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003205 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3206 "string::erase(iterator, iterator) called with an iterator not"
3207 " referring to this string");
3208#endif
3209 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210 iterator __b = begin();
3211 size_type __r = static_cast<size_type>(__first - __b);
3212 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003213 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214}
3215
3216template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003217inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218void
3219basic_string<_CharT, _Traits, _Allocator>::pop_back()
3220{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003221 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003222 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223}
3224
3225template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003226inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003228basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229{
3230 __invalidate_all_iterators();
3231 if (__is_long())
3232 {
3233 traits_type::assign(*__get_long_pointer(), value_type());
3234 __set_long_size(0);
3235 }
3236 else
3237 {
3238 traits_type::assign(*__get_short_pointer(), value_type());
3239 __set_short_size(0);
3240 }
3241}
3242
3243template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003244inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245void
3246basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3247{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003248 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249}
3250
3251template <class _CharT, class _Traits, class _Allocator>
3252void
3253basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3254{
3255 size_type __sz = size();
3256 if (__n > __sz)
3257 append(__n - __sz, __c);
3258 else
3259 __erase_to_end(__n);
3260}
3261
3262template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003263inline void
3264basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3265{
3266 size_type __sz = size();
3267 if (__n > __sz) {
3268 __append_default_init(__n - __sz);
3269 } else
3270 __erase_to_end(__n);
3271}
3272
3273template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003274inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003276basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003278 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003279#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003280 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003282 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003283#endif
3284}
3285
3286template <class _CharT, class _Traits, class _Allocator>
3287void
Marek Kurdejc9848142020-11-26 10:07:16 +01003288basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289{
Marek Kurdejc9848142020-11-26 10:07:16 +01003290 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003292
3293#if _LIBCPP_STD_VER > 17
3294 // Reserve never shrinks as of C++20.
3295 if (__requested_capacity <= capacity()) return;
3296#endif
3297
3298 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3299 __target_capacity = __recommend(__target_capacity);
3300 if (__target_capacity == capacity()) return;
3301
3302 __shrink_or_extend(__target_capacity);
3303}
3304
3305template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003306inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003307void
3308basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3309{
3310 size_type __target_capacity = __recommend(size());
3311 if (__target_capacity == capacity()) return;
3312
3313 __shrink_or_extend(__target_capacity);
3314}
3315
3316template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003317inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003318void
3319basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3320{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003321 size_type __cap = capacity();
3322 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003323
3324 pointer __new_data, __p;
3325 bool __was_long, __now_long;
3326 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003328 __was_long = true;
3329 __now_long = false;
3330 __new_data = __get_short_pointer();
3331 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003333 else
3334 {
3335 if (__target_capacity > __cap)
3336 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3337 else
3338 {
3339 #ifndef _LIBCPP_NO_EXCEPTIONS
3340 try
3341 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003342 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003343 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3344 #ifndef _LIBCPP_NO_EXCEPTIONS
3345 }
3346 catch (...)
3347 {
3348 return;
3349 }
3350 #else // _LIBCPP_NO_EXCEPTIONS
3351 if (__new_data == nullptr)
3352 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003353 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003354 }
3355 __now_long = true;
3356 __was_long = __is_long();
3357 __p = __get_pointer();
3358 }
3359 traits_type::copy(_VSTD::__to_address(__new_data),
3360 _VSTD::__to_address(__p), size()+1);
3361 if (__was_long)
3362 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3363 if (__now_long)
3364 {
3365 __set_long_cap(__target_capacity+1);
3366 __set_long_size(__sz);
3367 __set_long_pointer(__new_data);
3368 }
3369 else
3370 __set_short_size(__sz);
3371 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372}
3373
3374template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003375inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003377basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003379 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380 return *(data() + __pos);
3381}
3382
3383template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003384inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003386basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003388 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389 return *(__get_pointer() + __pos);
3390}
3391
3392template <class _CharT, class _Traits, class _Allocator>
3393typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3394basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3395{
3396 if (__n >= size())
3397 this->__throw_out_of_range();
3398 return (*this)[__n];
3399}
3400
3401template <class _CharT, class _Traits, class _Allocator>
3402typename basic_string<_CharT, _Traits, _Allocator>::reference
3403basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3404{
3405 if (__n >= size())
3406 this->__throw_out_of_range();
3407 return (*this)[__n];
3408}
3409
3410template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003411inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003413basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003415 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416 return *__get_pointer();
3417}
3418
3419template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003420inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003421typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003422basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003424 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425 return *data();
3426}
3427
3428template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003429inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003431basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003433 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434 return *(__get_pointer() + size() - 1);
3435}
3436
3437template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003438inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003439typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003440basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003442 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443 return *(data() + size() - 1);
3444}
3445
3446template <class _CharT, class _Traits, class _Allocator>
3447typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003448basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449{
3450 size_type __sz = size();
3451 if (__pos > __sz)
3452 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003453 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454 traits_type::copy(__s, data() + __pos, __rlen);
3455 return __rlen;
3456}
3457
3458template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003459inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460basic_string<_CharT, _Traits, _Allocator>
3461basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3462{
3463 return basic_string(*this, __pos, __n, __alloc());
3464}
3465
3466template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003467inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468void
3469basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003470#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003471 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003472#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003473 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003474 __is_nothrow_swappable<allocator_type>::value)
3475#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476{
Louis Dionneba400782020-10-02 15:02:52 -04003477#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003478 if (!__is_long())
3479 __get_db()->__invalidate_all(this);
3480 if (!__str.__is_long())
3481 __get_db()->__invalidate_all(&__str);
3482 __get_db()->swap(this, &__str);
3483#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003484 _LIBCPP_ASSERT(
3485 __alloc_traits::propagate_on_container_swap::value ||
3486 __alloc_traits::is_always_equal::value ||
3487 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003488 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003489 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490}
3491
3492// find
3493
3494template <class _Traits>
3495struct _LIBCPP_HIDDEN __traits_eq
3496{
3497 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003498 _LIBCPP_INLINE_VISIBILITY
3499 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3500 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501};
3502
3503template<class _CharT, class _Traits, class _Allocator>
3504typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003505basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003506 size_type __pos,
3507 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003509 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003510 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003511 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512}
3513
3514template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003515inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003517basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3518 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003520 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003521 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522}
3523
3524template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003525template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003526__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003527<
3528 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3529 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003530>
Marshall Clowe46031a2018-07-02 18:41:15 +00003531basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003532 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003533{
Marshall Clowe46031a2018-07-02 18:41:15 +00003534 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003535 return __str_find<value_type, size_type, traits_type, npos>
3536 (data(), size(), __sv.data(), __pos, __sv.size());
3537}
3538
3539template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003540inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003541typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003542basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003543 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003545 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003546 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003547 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548}
3549
3550template<class _CharT, class _Traits, class _Allocator>
3551typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003552basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3553 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003555 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003556 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557}
3558
3559// rfind
3560
3561template<class _CharT, class _Traits, class _Allocator>
3562typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003563basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003564 size_type __pos,
3565 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003567 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003568 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003569 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570}
3571
3572template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003573inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003575basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3576 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003578 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003579 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580}
3581
3582template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003583template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003584__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003585<
3586 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3587 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003588>
Marshall Clowe46031a2018-07-02 18:41:15 +00003589basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003590 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003591{
Marshall Clowe46031a2018-07-02 18:41:15 +00003592 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003593 return __str_rfind<value_type, size_type, traits_type, npos>
3594 (data(), size(), __sv.data(), __pos, __sv.size());
3595}
3596
3597template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003598inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003599typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003600basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003601 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003603 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003604 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003605 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606}
3607
3608template<class _CharT, class _Traits, class _Allocator>
3609typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003610basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3611 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003613 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003614 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615}
3616
3617// find_first_of
3618
3619template<class _CharT, class _Traits, class _Allocator>
3620typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003621basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003622 size_type __pos,
3623 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003625 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003626 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003627 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628}
3629
3630template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003631inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003633basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3634 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003636 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003637 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638}
3639
3640template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003641template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003642__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003643<
3644 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3645 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003646>
Marshall Clowe46031a2018-07-02 18:41:15 +00003647basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003648 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003649{
Marshall Clowe46031a2018-07-02 18:41:15 +00003650 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003651 return __str_find_first_of<value_type, size_type, traits_type, npos>
3652 (data(), size(), __sv.data(), __pos, __sv.size());
3653}
3654
3655template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003656inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003657typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003658basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003659 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003661 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003662 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003663 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664}
3665
3666template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003667inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003669basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3670 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671{
3672 return find(__c, __pos);
3673}
3674
3675// find_last_of
3676
3677template<class _CharT, class _Traits, class _Allocator>
3678typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003679basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003680 size_type __pos,
3681 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003683 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003684 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003685 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686}
3687
3688template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003689inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003691basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3692 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003695 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696}
3697
3698template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003699template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003700__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003701<
3702 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3703 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003704>
Marshall Clowe46031a2018-07-02 18:41:15 +00003705basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003706 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003707{
Marshall Clowe46031a2018-07-02 18:41:15 +00003708 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003709 return __str_find_last_of<value_type, size_type, traits_type, npos>
3710 (data(), size(), __sv.data(), __pos, __sv.size());
3711}
3712
3713template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003714inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003715typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003716basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003717 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003719 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003720 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003721 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722}
3723
3724template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003725inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003727basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3728 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729{
3730 return rfind(__c, __pos);
3731}
3732
3733// find_first_not_of
3734
3735template<class _CharT, class _Traits, class _Allocator>
3736typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003737basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003738 size_type __pos,
3739 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003741 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003742 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003743 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744}
3745
3746template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003747inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003749basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3750 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003752 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003753 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754}
3755
3756template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003757template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003758__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003759<
3760 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3761 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003762>
Marshall Clowe46031a2018-07-02 18:41:15 +00003763basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003764 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003765{
Marshall Clowe46031a2018-07-02 18:41:15 +00003766 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003767 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3768 (data(), size(), __sv.data(), __pos, __sv.size());
3769}
3770
3771template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003772inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003773typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003774basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003775 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003777 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003778 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003779 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780}
3781
3782template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003783inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003785basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3786 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003788 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003789 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790}
3791
3792// find_last_not_of
3793
3794template<class _CharT, class _Traits, class _Allocator>
3795typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003796basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003797 size_type __pos,
3798 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003800 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003801 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003802 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803}
3804
3805template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003806inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003808basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3809 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003811 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003812 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813}
3814
3815template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003816template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003817__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003818<
3819 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3820 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003821>
Marshall Clowe46031a2018-07-02 18:41:15 +00003822basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003823 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003824{
Marshall Clowe46031a2018-07-02 18:41:15 +00003825 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003826 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3827 (data(), size(), __sv.data(), __pos, __sv.size());
3828}
3829
3830template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003831inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003832typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003833basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003834 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003836 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003837 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003838 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839}
3840
3841template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003842inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003844basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3845 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003847 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003848 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849}
3850
3851// compare
3852
3853template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003854template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003855__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003856<
3857 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3858 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003859>
zoecarver1997e0a2021-02-05 11:54:47 -08003860basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861{
Marshall Clowe46031a2018-07-02 18:41:15 +00003862 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003863 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003864 size_t __rhs_sz = __sv.size();
3865 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003866 _VSTD::min(__lhs_sz, __rhs_sz));
3867 if (__result != 0)
3868 return __result;
3869 if (__lhs_sz < __rhs_sz)
3870 return -1;
3871 if (__lhs_sz > __rhs_sz)
3872 return 1;
3873 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874}
3875
3876template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003877inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003879basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003880{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003881 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882}
3883
3884template <class _CharT, class _Traits, class _Allocator>
3885int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003886basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3887 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003888 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003889 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003890{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003891 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892 size_type __sz = size();
3893 if (__pos1 > __sz || __n2 == npos)
3894 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003895 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3896 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003897 if (__r == 0)
3898 {
3899 if (__rlen < __n2)
3900 __r = -1;
3901 else if (__rlen > __n2)
3902 __r = 1;
3903 }
3904 return __r;
3905}
3906
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003907template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003908template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003909__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003910<
3911 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3912 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003913>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003914basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3915 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003916 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003917{
Marshall Clowe46031a2018-07-02 18:41:15 +00003918 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003919 return compare(__pos1, __n1, __sv.data(), __sv.size());
3920}
3921
3922template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003923inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003924int
3925basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3926 size_type __n1,
3927 const basic_string& __str) const
3928{
3929 return compare(__pos1, __n1, __str.data(), __str.size());
3930}
3931
3932template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003933template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003934__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003935<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003936 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3937 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003938 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003939>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003940basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3941 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003942 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003943 size_type __pos2,
3944 size_type __n2) const
3945{
Marshall Clow82513342016-09-24 22:45:42 +00003946 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003947 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3948}
3949
3950template <class _CharT, class _Traits, class _Allocator>
3951int
3952basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3953 size_type __n1,
3954 const basic_string& __str,
3955 size_type __pos2,
3956 size_type __n2) const
3957{
3958 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3959}
3960
3961template <class _CharT, class _Traits, class _Allocator>
3962int
3963basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3964{
3965 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3966 return compare(0, npos, __s, traits_type::length(__s));
3967}
3968
3969template <class _CharT, class _Traits, class _Allocator>
3970int
3971basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3972 size_type __n1,
3973 const value_type* __s) const
3974{
3975 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3976 return compare(__pos1, __n1, __s, traits_type::length(__s));
3977}
3978
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979// __invariants
3980
3981template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003982inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983bool
3984basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3985{
3986 if (size() > capacity())
3987 return false;
3988 if (capacity() < __min_cap - 1)
3989 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003990 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003992 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993 return false;
3994 return true;
3995}
3996
Vedant Kumar55e007e2018-03-08 21:15:26 +00003997// __clear_and_shrink
3998
3999template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004000inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004001void
Marshall Clowe60a7182018-05-29 17:04:37 +00004002basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004003{
4004 clear();
4005 if(__is_long())
4006 {
4007 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4008 __set_long_cap(0);
4009 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004010 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004011 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004012}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004013
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014// operator==
4015
4016template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004017inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018bool
4019operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004020 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004021{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004022 size_t __lhs_sz = __lhs.size();
4023 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4024 __rhs.data(),
4025 __lhs_sz) == 0;
4026}
4027
4028template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004029inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004030bool
4031operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4032 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4033{
4034 size_t __lhs_sz = __lhs.size();
4035 if (__lhs_sz != __rhs.size())
4036 return false;
4037 const char* __lp = __lhs.data();
4038 const char* __rp = __rhs.data();
4039 if (__lhs.__is_long())
4040 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4041 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4042 if (*__lp != *__rp)
4043 return false;
4044 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045}
4046
4047template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004048inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004050operator==(const _CharT* __lhs,
4051 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004053 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4054 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4055 size_t __lhs_len = _Traits::length(__lhs);
4056 if (__lhs_len != __rhs.size()) return false;
4057 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058}
4059
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004061inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004063operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4064 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004066 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4067 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4068 size_t __rhs_len = _Traits::length(__rhs);
4069 if (__rhs_len != __lhs.size()) return false;
4070 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071}
4072
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004073template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004074inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075bool
4076operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004077 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078{
4079 return !(__lhs == __rhs);
4080}
4081
4082template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004083inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004085operator!=(const _CharT* __lhs,
4086 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087{
4088 return !(__lhs == __rhs);
4089}
4090
4091template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004092inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004094operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4095 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096{
4097 return !(__lhs == __rhs);
4098}
4099
4100// operator<
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
4105operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004106 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004108 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109}
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{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004117 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118}
4119
4120template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004121inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004123operator< (const _CharT* __lhs,
4124 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125{
4126 return __rhs.compare(__lhs) > 0;
4127}
4128
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129// operator>
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
4134operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004135 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136{
4137 return __rhs < __lhs;
4138}
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 basic_string<_CharT, _Traits, _Allocator>& __lhs,
4144 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145{
4146 return __rhs < __lhs;
4147}
4148
4149template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004152operator> (const _CharT* __lhs,
4153 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154{
4155 return __rhs < __lhs;
4156}
4157
4158// operator<=
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
4163operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004164 const basic_string<_CharT, _Traits, _Allocator>& __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 basic_string<_CharT, _Traits, _Allocator>& __lhs,
4173 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004174{
4175 return !(__rhs < __lhs);
4176}
4177
4178template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004179inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004181operator<=(const _CharT* __lhs,
4182 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183{
4184 return !(__rhs < __lhs);
4185}
4186
4187// operator>=
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
4192operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004193 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194{
4195 return !(__lhs < __rhs);
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 basic_string<_CharT, _Traits, _Allocator>& __lhs,
4202 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203{
4204 return !(__lhs < __rhs);
4205}
4206
4207template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004208inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004210operator>=(const _CharT* __lhs,
4211 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212{
4213 return !(__lhs < __rhs);
4214}
4215
4216// operator +
4217
4218template<class _CharT, class _Traits, class _Allocator>
4219basic_string<_CharT, _Traits, _Allocator>
4220operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4221 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4222{
4223 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4224 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4225 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4226 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4227 __r.append(__rhs.data(), __rhs_sz);
4228 return __r;
4229}
4230
4231template<class _CharT, class _Traits, class _Allocator>
4232basic_string<_CharT, _Traits, _Allocator>
4233operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4234{
4235 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4236 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4237 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4238 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4239 __r.append(__rhs.data(), __rhs_sz);
4240 return __r;
4241}
4242
4243template<class _CharT, class _Traits, class _Allocator>
4244basic_string<_CharT, _Traits, _Allocator>
4245operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4246{
4247 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4248 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4249 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4250 __r.append(__rhs.data(), __rhs_sz);
4251 return __r;
4252}
4253
4254template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004255inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256basic_string<_CharT, _Traits, _Allocator>
4257operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4258{
4259 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4260 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4261 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4262 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4263 __r.append(__rhs, __rhs_sz);
4264 return __r;
4265}
4266
4267template<class _CharT, class _Traits, class _Allocator>
4268basic_string<_CharT, _Traits, _Allocator>
4269operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4270{
4271 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4272 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4273 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4274 __r.push_back(__rhs);
4275 return __r;
4276}
4277
Eric Fiselierfc92be82017-04-19 00:28:44 +00004278#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279
4280template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004281inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282basic_string<_CharT, _Traits, _Allocator>
4283operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4284{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004285 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286}
4287
4288template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004289inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290basic_string<_CharT, _Traits, _Allocator>
4291operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4292{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004293 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294}
4295
4296template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004297inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004298basic_string<_CharT, _Traits, _Allocator>
4299operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4300{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004301 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302}
4303
4304template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306basic_string<_CharT, _Traits, _Allocator>
4307operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4308{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004309 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310}
4311
4312template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004313inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314basic_string<_CharT, _Traits, _Allocator>
4315operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4316{
4317 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004318 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004319}
4320
4321template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004322inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323basic_string<_CharT, _Traits, _Allocator>
4324operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4325{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004326 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327}
4328
4329template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004330inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331basic_string<_CharT, _Traits, _Allocator>
4332operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4333{
4334 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004335 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004336}
4337
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004338#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339
4340// swap
4341
4342template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004343inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004345swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004346 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4347 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348{
4349 __lhs.swap(__rhs);
4350}
4351
Bruce Mitchener170d8972020-11-24 12:53:53 -05004352_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4353_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4354_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4355_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4356_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004357
Bruce Mitchener170d8972020-11-24 12:53:53 -05004358_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4359_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4360_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004361
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004362_LIBCPP_FUNC_VIS string to_string(int __val);
4363_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4364_LIBCPP_FUNC_VIS string to_string(long __val);
4365_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4366_LIBCPP_FUNC_VIS string to_string(long long __val);
4367_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4368_LIBCPP_FUNC_VIS string to_string(float __val);
4369_LIBCPP_FUNC_VIS string to_string(double __val);
4370_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004371
Louis Dionne89258142021-08-23 15:32:36 -04004372#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004373_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4374_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4375_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4376_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4377_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004378
Bruce Mitchener170d8972020-11-24 12:53:53 -05004379_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4380_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4381_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004382
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004383_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4384_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4385_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4386_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4387_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4388_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4389_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4390_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4391_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004392#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004393
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004395_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004396const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4397 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004398
Marshall Clow851b9ec2019-05-20 21:56:51 +00004399template <class _CharT, class _Allocator>
4400struct _LIBCPP_TEMPLATE_VIS
4401 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4402 : public unary_function<
4403 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404{
4405 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004406 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4407 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004408};
4409
Howard Hinnantc51e1022010-05-11 19:42:16 +00004410
Howard Hinnantdc095972011-07-18 15:51:59 +00004411template<class _CharT, class _Traits, class _Allocator>
4412basic_ostream<_CharT, _Traits>&
4413operator<<(basic_ostream<_CharT, _Traits>& __os,
4414 const basic_string<_CharT, _Traits, _Allocator>& __str);
4415
4416template<class _CharT, class _Traits, class _Allocator>
4417basic_istream<_CharT, _Traits>&
4418operator>>(basic_istream<_CharT, _Traits>& __is,
4419 basic_string<_CharT, _Traits, _Allocator>& __str);
4420
4421template<class _CharT, class _Traits, class _Allocator>
4422basic_istream<_CharT, _Traits>&
4423getline(basic_istream<_CharT, _Traits>& __is,
4424 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4425
4426template<class _CharT, class _Traits, class _Allocator>
4427inline _LIBCPP_INLINE_VISIBILITY
4428basic_istream<_CharT, _Traits>&
4429getline(basic_istream<_CharT, _Traits>& __is,
4430 basic_string<_CharT, _Traits, _Allocator>& __str);
4431
Howard Hinnantdc095972011-07-18 15:51:59 +00004432template<class _CharT, class _Traits, class _Allocator>
4433inline _LIBCPP_INLINE_VISIBILITY
4434basic_istream<_CharT, _Traits>&
4435getline(basic_istream<_CharT, _Traits>&& __is,
4436 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4437
4438template<class _CharT, class _Traits, class _Allocator>
4439inline _LIBCPP_INLINE_VISIBILITY
4440basic_istream<_CharT, _Traits>&
4441getline(basic_istream<_CharT, _Traits>&& __is,
4442 basic_string<_CharT, _Traits, _Allocator>& __str);
4443
Marshall Clow29b53f22018-12-14 18:49:35 +00004444#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004445template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004446inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004447 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4448 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4449 auto __old_size = __str.size();
4450 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4451 return __old_size - __str.size();
4452}
Marshall Clow29b53f22018-12-14 18:49:35 +00004453
Marek Kurdeja98b1412020-05-02 13:58:03 +02004454template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004455inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004456 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4457 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4458 _Predicate __pred) {
4459 auto __old_size = __str.size();
4460 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4461 __str.end());
4462 return __old_size - __str.size();
4463}
Marshall Clow29b53f22018-12-14 18:49:35 +00004464#endif
4465
Louis Dionneba400782020-10-02 15:02:52 -04004466#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004467
4468template<class _CharT, class _Traits, class _Allocator>
4469bool
4470basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4471{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004472 return this->data() <= _VSTD::__to_address(__i->base()) &&
4473 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004474}
4475
4476template<class _CharT, class _Traits, class _Allocator>
4477bool
4478basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4479{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004480 return this->data() < _VSTD::__to_address(__i->base()) &&
4481 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004482}
4483
4484template<class _CharT, class _Traits, class _Allocator>
4485bool
4486basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4487{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004488 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004489 return this->data() <= __p && __p <= this->data() + this->size();
4490}
4491
4492template<class _CharT, class _Traits, class _Allocator>
4493bool
4494basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4495{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004496 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004497 return this->data() <= __p && __p < this->data() + this->size();
4498}
4499
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004500#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004501
Louis Dionne173f29e2019-05-29 16:01:36 +00004502#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004503// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004504inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004505{
4506 inline namespace string_literals
4507 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004508 inline _LIBCPP_INLINE_VISIBILITY
4509 basic_string<char> operator "" s( const char *__str, size_t __len )
4510 {
4511 return basic_string<char> (__str, __len);
4512 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004513
Louis Dionne89258142021-08-23 15:32:36 -04004514#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004515 inline _LIBCPP_INLINE_VISIBILITY
4516 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4517 {
4518 return basic_string<wchar_t> (__str, __len);
4519 }
Louis Dionne89258142021-08-23 15:32:36 -04004520#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004521
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004522#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004523 inline _LIBCPP_INLINE_VISIBILITY
4524 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4525 {
4526 return basic_string<char8_t> (__str, __len);
4527 }
4528#endif
4529
Howard Hinnant5c167562013-08-07 19:39:48 +00004530 inline _LIBCPP_INLINE_VISIBILITY
4531 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4532 {
4533 return basic_string<char16_t> (__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<char32_t> operator "" s( const char32_t *__str, size_t __len )
4538 {
4539 return basic_string<char32_t> (__str, __len);
4540 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004541 } // namespace string_literals
4542} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004543#endif
4544
Howard Hinnantc51e1022010-05-11 19:42:16 +00004545_LIBCPP_END_NAMESPACE_STD
4546
Eric Fiselierf4433a32017-05-31 22:07:49 +00004547_LIBCPP_POP_MACROS
4548
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004549#endif // _LIBCPP_STRING