blob: c6a799dbc7e89a3b5c8199cde10b20ea2a60f09a [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- string -----------------------------------===//
3//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
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>
527#include <cwchar>
528#include <initializer_list>
529#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531#include <memory>
532#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400533#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400535#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000536#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000537
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400538#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
539# include <cstdint>
540#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000541
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000542#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000544#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545
Eric Fiselierf4433a32017-05-31 22:07:49 +0000546_LIBCPP_PUSH_MACROS
547#include <__undef_macros>
548
549
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550_LIBCPP_BEGIN_NAMESPACE_STD
551
552// fpos
553
554template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000555class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556{
557private:
558 _StateT __st_;
559 streamoff __off_;
560public:
561 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
562
563 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
564
565 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
566 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
567
568 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
569 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
570 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
571 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
572};
573
574template <class _StateT>
575inline _LIBCPP_INLINE_VISIBILITY
576streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
577 {return streamoff(__x) - streamoff(__y);}
578
579template <class _StateT>
580inline _LIBCPP_INLINE_VISIBILITY
581bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
582 {return streamoff(__x) == streamoff(__y);}
583
584template <class _StateT>
585inline _LIBCPP_INLINE_VISIBILITY
586bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
587 {return streamoff(__x) != streamoff(__y);}
588
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589// basic_string
590
591template<class _CharT, class _Traits, class _Allocator>
592basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000593operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
594 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
596template<class _CharT, class _Traits, class _Allocator>
597basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000598operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
600template<class _CharT, class _Traits, class _Allocator>
601basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000602operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603
604template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000605inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000607operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608
609template<class _CharT, class _Traits, class _Allocator>
610basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000611operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612
Shoaib Meenaiea363712017-07-29 02:54:41 +0000613_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
614
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400616struct __basic_string_common;
617
618template <>
619struct __basic_string_common<true> {
620 // Both are defined in string.cpp
621 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
622 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623};
624
Marshall Clow039b2f02016-01-13 21:54:34 +0000625template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400626struct __string_is_trivial_iterator : public false_type {};
627
628template <class _Tp>
629struct __string_is_trivial_iterator<_Tp*>
630 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000631
Louis Dionne173f29e2019-05-29 16:01:36 +0000632template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400633struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
634 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000635
Marshall Clow82513342016-09-24 22:45:42 +0000636template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500637struct __can_be_converted_to_string_view : public _BoolConstant<
638 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
639 !is_convertible<const _Tp&, const _CharT*>::value
640 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000641
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000642#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000643
644template <class _CharT, size_t = sizeof(_CharT)>
645struct __padding
646{
647 unsigned char __xx[sizeof(_CharT)-1];
648};
649
650template <class _CharT>
651struct __padding<_CharT, 1>
652{
653};
654
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400655#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000656
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400657#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800658typedef basic_string<char8_t> u8string;
659#endif
660
661#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
662typedef basic_string<char16_t> u16string;
663typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400664#endif
Richard Smith256954d2020-11-11 17:12:18 -0800665
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000666template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800667class
668 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400669#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800670 _LIBCPP_PREFERRED_NAME(u8string)
671#endif
672#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
673 _LIBCPP_PREFERRED_NAME(u16string)
674 _LIBCPP_PREFERRED_NAME(u32string)
675#endif
676 basic_string
Louis Dionneb6aabd92021-08-19 12:21:06 -0400677 : 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 +0000678{
679public:
680 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000681 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000683 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000685 typedef allocator_traits<allocator_type> __alloc_traits;
686 typedef typename __alloc_traits::size_type size_type;
687 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000688 typedef value_type& reference;
689 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000690 typedef typename __alloc_traits::pointer pointer;
691 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692
Marshall Clow79f33542018-03-21 00:36:05 +0000693 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
694 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
695 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
696 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000697 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000698 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000699 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000700
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701 typedef __wrap_iter<pointer> iterator;
702 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000703 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
704 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705
706private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000707
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000708#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000709
710 struct __long
711 {
712 pointer __data_;
713 size_type __size_;
714 size_type __cap_;
715 };
716
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000717#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000718 static const size_type __short_mask = 0x01;
719 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000720#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000721 static const size_type __short_mask = 0x80;
722 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400723#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000724
725 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
726 (sizeof(__long) - 1)/sizeof(value_type) : 2};
727
728 struct __short
729 {
730 value_type __data_[__min_cap];
731 struct
732 : __padding<value_type>
733 {
734 unsigned char __size_;
735 };
736 };
737
738#else
739
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 struct __long
741 {
742 size_type __cap_;
743 size_type __size_;
744 pointer __data_;
745 };
746
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000747#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000748 static const size_type __short_mask = 0x80;
749 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000750#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000751 static const size_type __short_mask = 0x01;
752 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400753#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
756 (sizeof(__long) - 1)/sizeof(value_type) : 2};
757
758 struct __short
759 {
760 union
761 {
762 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000763 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 };
765 value_type __data_[__min_cap];
766 };
767
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400768#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000769
Howard Hinnant8ea98242013-08-23 17:37:05 +0000770 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771
Howard Hinnant8ea98242013-08-23 17:37:05 +0000772 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773
774 struct __raw
775 {
776 size_type __words[__n_words];
777 };
778
779 struct __rep
780 {
781 union
782 {
783 __long __l;
784 __short __s;
785 __raw __r;
786 };
787 };
788
789 __compressed_pair<__rep, allocator_type> __r_;
790
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200792 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793 static const size_type npos = -1;
794
Howard Hinnant3e276872011-06-03 18:40:47 +0000795 _LIBCPP_INLINE_VISIBILITY basic_string()
796 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000797
798 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
799#if _LIBCPP_STD_VER <= 14
800 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
801#else
802 _NOEXCEPT;
803#endif
804
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805 basic_string(const basic_string& __str);
806 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000807
Eric Fiselierfc92be82017-04-19 00:28:44 +0000808#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000810 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000811#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000812 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000813#else
814 _NOEXCEPT;
815#endif
816
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400819#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000820
Louis Dionne9ce598d2021-09-08 09:14:43 -0400821 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000822 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500823 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000824 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
825 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400826# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000827 __get_db()->__insert_c(this);
828# endif
829 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000830
Louis Dionne9ce598d2021-09-08 09:14:43 -0400831 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000832 _LIBCPP_INLINE_VISIBILITY
833 basic_string(const _CharT* __s, const _Allocator& __a);
834
Marek Kurdej90d79712021-07-27 16:16:21 +0200835#if _LIBCPP_STD_VER > 20
836 basic_string(nullptr_t) = delete;
837#endif
838
Howard Hinnantcf823322010-12-17 14:46:43 +0000839 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000840 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000841 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000842 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000843 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000844 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000845
Louis Dionne9ce598d2021-09-08 09:14:43 -0400846 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000847 _LIBCPP_INLINE_VISIBILITY
848 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
849
Marshall Clow83445802016-04-07 18:13:41 +0000850 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000851 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000852 _LIBCPP_INLINE_VISIBILITY
853 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000854 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000855
Louis Dionne9ce598d2021-09-08 09:14:43 -0400856 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 +0000857 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000858 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500859 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000860
Louis Dionne9ce598d2021-09-08 09:14:43 -0400861 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500862 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000863 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
864 explicit basic_string(const _Tp& __t);
865
Louis Dionne9ce598d2021-09-08 09:14:43 -0400866 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 +0000867 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
868 explicit basic_string(const _Tp& __t, const allocator_type& __a);
869
Louis Dionne9ce598d2021-09-08 09:14:43 -0400870 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872 basic_string(_InputIterator __first, _InputIterator __last);
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, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000876#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000877 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000878 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000879 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000880 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400881#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000883 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000885 _LIBCPP_INLINE_VISIBILITY
886 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
887
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000888 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000889
Louis Dionne9ce598d2021-09-08 09:14:43 -0400890 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 +0000891 basic_string& operator=(const _Tp& __t)
892 {__self_view __sv = __t; return assign(__sv);}
893
Eric Fiselierfc92be82017-04-19 00:28:44 +0000894#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000896 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000897 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000898 _LIBCPP_INLINE_VISIBILITY
899 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000901 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200902#if _LIBCPP_STD_VER > 20
903 basic_string& operator=(nullptr_t) = delete;
904#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906
Louis Dionneba400782020-10-02 15:02:52 -0400907#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000908 _LIBCPP_INLINE_VISIBILITY
909 iterator begin() _NOEXCEPT
910 {return iterator(this, __get_pointer());}
911 _LIBCPP_INLINE_VISIBILITY
912 const_iterator begin() const _NOEXCEPT
913 {return const_iterator(this, __get_pointer());}
914 _LIBCPP_INLINE_VISIBILITY
915 iterator end() _NOEXCEPT
916 {return iterator(this, __get_pointer() + size());}
917 _LIBCPP_INLINE_VISIBILITY
918 const_iterator end() const _NOEXCEPT
919 {return const_iterator(this, __get_pointer() + size());}
920#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000921 _LIBCPP_INLINE_VISIBILITY
922 iterator begin() _NOEXCEPT
923 {return iterator(__get_pointer());}
924 _LIBCPP_INLINE_VISIBILITY
925 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000926 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000927 _LIBCPP_INLINE_VISIBILITY
928 iterator end() _NOEXCEPT
929 {return iterator(__get_pointer() + size());}
930 _LIBCPP_INLINE_VISIBILITY
931 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000932 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400933#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000934 _LIBCPP_INLINE_VISIBILITY
935 reverse_iterator rbegin() _NOEXCEPT
936 {return reverse_iterator(end());}
937 _LIBCPP_INLINE_VISIBILITY
938 const_reverse_iterator rbegin() const _NOEXCEPT
939 {return const_reverse_iterator(end());}
940 _LIBCPP_INLINE_VISIBILITY
941 reverse_iterator rend() _NOEXCEPT
942 {return reverse_iterator(begin());}
943 _LIBCPP_INLINE_VISIBILITY
944 const_reverse_iterator rend() const _NOEXCEPT
945 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000947 _LIBCPP_INLINE_VISIBILITY
948 const_iterator cbegin() const _NOEXCEPT
949 {return begin();}
950 _LIBCPP_INLINE_VISIBILITY
951 const_iterator cend() const _NOEXCEPT
952 {return end();}
953 _LIBCPP_INLINE_VISIBILITY
954 const_reverse_iterator crbegin() const _NOEXCEPT
955 {return rbegin();}
956 _LIBCPP_INLINE_VISIBILITY
957 const_reverse_iterator crend() const _NOEXCEPT
958 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000960 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000962 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
963 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
964 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000965 {return (__is_long() ? __get_long_cap()
966 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967
968 void resize(size_type __n, value_type __c);
969 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
970
Marek Kurdejc9848142020-11-26 10:07:16 +0100971 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000972 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
973
Marek Kurdejc9848142020-11-26 10:07:16 +0100974 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
975 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000976 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100977 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000979 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000980 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
981 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000983 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
984 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
986 const_reference at(size_type __n) const;
987 reference at(size_type __n);
988
989 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000990
991 template <class _Tp>
992 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400993 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000994 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500995 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
996 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000997 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500998 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000999 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001000 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001002#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001004#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005
Howard Hinnantcf823322010-12-17 14:46:43 +00001006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001008
1009 template <class _Tp>
1010 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001011 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001012 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1013 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001014 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001015 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001016 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001017 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001018
Marshall Clow62953962016-10-03 23:40:48 +00001019 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001020 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001021 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001022 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001023 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1024 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001025 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 >
Marshall Clow82513342016-09-24 22:45:42 +00001027 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001028 basic_string& append(const value_type* __s, size_type __n);
1029 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001031
1032 _LIBCPP_INLINE_VISIBILITY
1033 void __append_default_init(size_type __n);
1034
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001036 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001037 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001039 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001041 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001042 _LIBCPP_INLINE_VISIBILITY
1043 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001044 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001045 append(__temp.data(), __temp.size());
1046 return *this;
1047 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001049 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001050 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001052 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001054 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001055 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001056 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001057
Eric Fiselierfc92be82017-04-19 00:28:44 +00001058#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001061#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062
1063 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001066 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1067 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1068 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1069 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070
Marshall Clowe46031a2018-07-02 18:41:15 +00001071 template <class _Tp>
1072 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001073 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001074 <
1075 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1076 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001077 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001078 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001079 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001080 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001081#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001082 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001083 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001084 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001085 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001086#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001087 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001088 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001089 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001090 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001091 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001092 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1093 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001094 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001096 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001097 basic_string& assign(const value_type* __s, size_type __n);
1098 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 basic_string& assign(size_type __n, value_type __c);
1100 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001101 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001102 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001104 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107 assign(_InputIterator __first, _InputIterator __last);
1108 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001109 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001110 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001112 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001116#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001119#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120
Howard Hinnantcf823322010-12-17 14:46:43 +00001121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001123
1124 template <class _Tp>
1125 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001126 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001127 <
1128 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1129 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001130 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001131 insert(size_type __pos1, const _Tp& __t)
1132 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1133
Marshall Clow82513342016-09-24 22:45:42 +00001134 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001135 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001136 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001137 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001138 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001139 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001140 >
Marshall Clow82513342016-09-24 22:45:42 +00001141 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001142 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001143 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1144 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1146 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1149 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001150 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001151 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001153 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001155 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1157 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001158 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001159 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001161 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001163 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001165#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1168 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001169#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170
1171 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 iterator erase(const_iterator __first, const_iterator __last);
1176
Howard Hinnantcf823322010-12-17 14:46:43 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001179
1180 template <class _Tp>
1181 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001182 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001183 <
1184 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1185 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001186 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001187 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 +00001188 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 +00001189 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001190 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001191 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001192 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001193 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001194 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001195 >
Marshall Clow82513342016-09-24 22:45:42 +00001196 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001197 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1198 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001201 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001202
1203 template <class _Tp>
1204 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001205 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001206 <
1207 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1208 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001209 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001210 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1211
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001213 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001215 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001217 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001219 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001220 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001222 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001224 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001225 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001226#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001228 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001230#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231
Howard Hinnantd17880b2013-06-28 16:59:19 +00001232 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1235
Howard Hinnantcf823322010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001237 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001238#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001239 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001240#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001241 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001242 __is_nothrow_swappable<allocator_type>::value);
1243#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244
Howard Hinnantcf823322010-12-17 14:46:43 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001246 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001247 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001248 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001249#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001250 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001251 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001252#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253
Howard Hinnantcf823322010-12-17 14:46:43 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001255 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
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 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001259
1260 template <class _Tp>
1261 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001262 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001263 <
1264 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1265 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001266 >
zoecarver1997e0a2021-02-05 11:54:47 -08001267 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001268 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001270 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001271 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272
Howard Hinnantcf823322010-12-17 14:46:43 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001274 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001275
1276 template <class _Tp>
1277 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001278 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001279 <
1280 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1281 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001282 >
zoecarver1997e0a2021-02-05 11:54:47 -08001283 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001284 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001286 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001287 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288
Howard Hinnantcf823322010-12-17 14:46:43 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001290 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001291
1292 template <class _Tp>
1293 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001294 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001295 <
1296 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1297 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001298 >
zoecarver1997e0a2021-02-05 11:54:47 -08001299 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001302 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001304 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305
Howard Hinnantcf823322010-12-17 14:46:43 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001307 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001308
1309 template <class _Tp>
1310 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001311 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001312 <
1313 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1314 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001315 >
zoecarver1997e0a2021-02-05 11:54:47 -08001316 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001317 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001319 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001321 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322
Howard Hinnantcf823322010-12-17 14:46:43 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001324 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001325
1326 template <class _Tp>
1327 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001328 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001329 <
1330 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1331 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001332 >
zoecarver1997e0a2021-02-05 11:54:47 -08001333 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001334 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 +00001335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001336 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001337 _LIBCPP_INLINE_VISIBILITY
1338 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1339
1340 _LIBCPP_INLINE_VISIBILITY
1341 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001342
1343 template <class _Tp>
1344 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001345 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001346 <
1347 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1348 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001349 >
zoecarver1997e0a2021-02-05 11:54:47 -08001350 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001351 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 +00001352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001353 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001354 _LIBCPP_INLINE_VISIBILITY
1355 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1356
1357 _LIBCPP_INLINE_VISIBILITY
1358 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001359
1360 template <class _Tp>
1361 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001362 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001363 <
1364 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1365 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001366 >
zoecarver1997e0a2021-02-05 11:54:47 -08001367 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001368
1369 template <class _Tp>
1370 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001371 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001372 <
1373 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1374 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001375 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001376 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1377
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001380 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 +00001381
Marshall Clow82513342016-09-24 22:45:42 +00001382 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001383 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001384 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001385 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001386 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001387 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001388 >
Marshall Clow82513342016-09-24 22:45:42 +00001389 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 +00001390 int compare(const value_type* __s) const _NOEXCEPT;
1391 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1392 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393
Marshall Clow18c293b2017-12-04 20:11:38 +00001394#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001395 constexpr _LIBCPP_INLINE_VISIBILITY
1396 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001397 { return __self_view(data(), size()).starts_with(__sv); }
1398
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001399 constexpr _LIBCPP_INLINE_VISIBILITY
1400 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001401 { return !empty() && _Traits::eq(front(), __c); }
1402
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001403 constexpr _LIBCPP_INLINE_VISIBILITY
1404 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001405 { return starts_with(__self_view(__s)); }
1406
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001407 constexpr _LIBCPP_INLINE_VISIBILITY
1408 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001409 { return __self_view(data(), size()).ends_with( __sv); }
1410
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001411 constexpr _LIBCPP_INLINE_VISIBILITY
1412 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001413 { return !empty() && _Traits::eq(back(), __c); }
1414
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001415 constexpr _LIBCPP_INLINE_VISIBILITY
1416 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001417 { return ends_with(__self_view(__s)); }
1418#endif
1419
Wim Leflere023c3542021-01-19 14:33:30 -05001420#if _LIBCPP_STD_VER > 20
1421 constexpr _LIBCPP_INLINE_VISIBILITY
1422 bool contains(__self_view __sv) const noexcept
1423 { return __self_view(data(), size()).contains(__sv); }
1424
1425 constexpr _LIBCPP_INLINE_VISIBILITY
1426 bool contains(value_type __c) const noexcept
1427 { return __self_view(data(), size()).contains(__c); }
1428
1429 constexpr _LIBCPP_INLINE_VISIBILITY
1430 bool contains(const value_type* __s) const
1431 { return __self_view(data(), size()).contains(__s); }
1432#endif
1433
Howard Hinnantcf823322010-12-17 14:46:43 +00001434 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001435
Marshall Clowe60a7182018-05-29 17:04:37 +00001436 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001437
Marek Kurdejc9848142020-11-26 10:07:16 +01001438 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1439
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001440 _LIBCPP_INLINE_VISIBILITY
1441 bool __is_long() const _NOEXCEPT
1442 {return bool(__r_.first().__s.__size_ & __short_mask);}
1443
Louis Dionneba400782020-10-02 15:02:52 -04001444#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001445
1446 bool __dereferenceable(const const_iterator* __i) const;
1447 bool __decrementable(const const_iterator* __i) const;
1448 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1449 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1450
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001451#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001452
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001454 _LIBCPP_INLINE_VISIBILITY
1455 allocator_type& __alloc() _NOEXCEPT
1456 {return __r_.second();}
1457 _LIBCPP_INLINE_VISIBILITY
1458 const allocator_type& __alloc() const _NOEXCEPT
1459 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001461#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001462
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001464 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001465# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001467# else
1468 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1469# endif
1470
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001471 _LIBCPP_INLINE_VISIBILITY
1472 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001473# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001475# else
1476 {return __r_.first().__s.__size_;}
1477# endif
1478
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001479#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001480
1481 _LIBCPP_INLINE_VISIBILITY
1482 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001483# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001484 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1485# else
1486 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1487# endif
1488
1489 _LIBCPP_INLINE_VISIBILITY
1490 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001491# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001492 {return __r_.first().__s.__size_;}
1493# else
1494 {return __r_.first().__s.__size_ >> 1;}
1495# endif
1496
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001497#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001498
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001499 _LIBCPP_INLINE_VISIBILITY
1500 void __set_long_size(size_type __s) _NOEXCEPT
1501 {__r_.first().__l.__size_ = __s;}
1502 _LIBCPP_INLINE_VISIBILITY
1503 size_type __get_long_size() const _NOEXCEPT
1504 {return __r_.first().__l.__size_;}
1505 _LIBCPP_INLINE_VISIBILITY
1506 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1508
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001509 _LIBCPP_INLINE_VISIBILITY
1510 void __set_long_cap(size_type __s) _NOEXCEPT
1511 {__r_.first().__l.__cap_ = __long_mask | __s;}
1512 _LIBCPP_INLINE_VISIBILITY
1513 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001514 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001516 _LIBCPP_INLINE_VISIBILITY
1517 void __set_long_pointer(pointer __p) _NOEXCEPT
1518 {__r_.first().__l.__data_ = __p;}
1519 _LIBCPP_INLINE_VISIBILITY
1520 pointer __get_long_pointer() _NOEXCEPT
1521 {return __r_.first().__l.__data_;}
1522 _LIBCPP_INLINE_VISIBILITY
1523 const_pointer __get_long_pointer() const _NOEXCEPT
1524 {return __r_.first().__l.__data_;}
1525 _LIBCPP_INLINE_VISIBILITY
1526 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001527 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001528 _LIBCPP_INLINE_VISIBILITY
1529 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001530 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001531 _LIBCPP_INLINE_VISIBILITY
1532 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001534 _LIBCPP_INLINE_VISIBILITY
1535 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1537
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001538 _LIBCPP_INLINE_VISIBILITY
1539 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 {
1541 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1542 for (unsigned __i = 0; __i < __n_words; ++__i)
1543 __a[__i] = 0;
1544 }
1545
1546 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001548 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001549 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001551 static _LIBCPP_INLINE_VISIBILITY
1552 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001553 {
1554 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1555 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1556 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1557 if (__guess == __min_cap) ++__guess;
1558 return __guess;
1559 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001561 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001562 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001563 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001564 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001565 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001567
Martijn Vels5e7c9752020-03-04 17:52:46 -05001568 // Slow path for the (inlined) copy constructor for 'long' strings.
1569 // Always externally instantiated and not inlined.
1570 // Requires that __s is zero terminated.
1571 // The main reason for this function to exist is because for unstable, we
1572 // want to allow inlining of the copy constructor. However, we don't want
1573 // to call the __init() functions as those are marked as inline which may
1574 // result in over-aggressive inlining by the compiler, where our aim is
1575 // to only inline the fast path code directly in the ctor.
1576 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1577
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001579 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001580 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001582 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1583 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 __init(_InputIterator __first, _InputIterator __last);
1585
1586 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001587 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001588 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001590 __is_cpp17_forward_iterator<_ForwardIterator>::value
1591 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 __init(_ForwardIterator __first, _ForwardIterator __last);
1593
1594 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001595 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1597 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001598 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599
Martijn Vels596e3de2020-02-26 15:55:49 -05001600 // __assign_no_alias is invoked for assignment operations where we
1601 // have proof that the input does not alias the current instance.
1602 // For example, operator=(basic_string) performs a 'self' check.
1603 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001604 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001605
Howard Hinnantcf823322010-12-17 14:46:43 +00001606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 void __erase_to_end(size_type __pos);
1608
Martijn Velsa81fc792020-02-26 13:25:43 -05001609 // __erase_external_with_move is invoked for erase() invocations where
1610 // `n ~= npos`, likely requiring memory moves on the string data.
1611 void __erase_external_with_move(size_type __pos, size_type __n);
1612
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001613 _LIBCPP_INLINE_VISIBILITY
1614 void __copy_assign_alloc(const basic_string& __str)
1615 {__copy_assign_alloc(__str, integral_constant<bool,
1616 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1617
1618 _LIBCPP_INLINE_VISIBILITY
1619 void __copy_assign_alloc(const basic_string& __str, true_type)
1620 {
Marshall Clowf258c202017-01-31 03:40:52 +00001621 if (__alloc() == __str.__alloc())
1622 __alloc() = __str.__alloc();
1623 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001624 {
Marshall Clowf258c202017-01-31 03:40:52 +00001625 if (!__str.__is_long())
1626 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001627 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001628 __alloc() = __str.__alloc();
1629 }
1630 else
1631 {
1632 allocator_type __a = __str.__alloc();
1633 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001634 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001635 __alloc() = _VSTD::move(__a);
1636 __set_long_pointer(__p);
1637 __set_long_cap(__str.__get_long_cap());
1638 __set_long_size(__str.size());
1639 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001640 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001641 }
1642
1643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001644 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001645 {}
1646
Eric Fiselierfc92be82017-04-19 00:28:44 +00001647#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001648 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001649 void __move_assign(basic_string& __str, false_type)
1650 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001652 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001653#if _LIBCPP_STD_VER > 14
1654 _NOEXCEPT;
1655#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001656 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001657#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001658#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001659
1660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001661 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001662 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001663 _NOEXCEPT_(
1664 !__alloc_traits::propagate_on_container_move_assignment::value ||
1665 is_nothrow_move_assignable<allocator_type>::value)
1666 {__move_assign_alloc(__str, integral_constant<bool,
1667 __alloc_traits::propagate_on_container_move_assignment::value>());}
1668
1669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001670 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001671 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1672 {
1673 __alloc() = _VSTD::move(__c.__alloc());
1674 }
1675
1676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001677 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001678 _NOEXCEPT
1679 {}
1680
Martijn Velsda7d94f2020-06-19 14:24:03 -04001681 basic_string& __assign_external(const value_type* __s);
1682 basic_string& __assign_external(const value_type* __s, size_type __n);
1683
1684 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1685 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1686 pointer __p = __is_long()
1687 ? (__set_long_size(__n), __get_long_pointer())
1688 : (__set_short_size(__n), __get_short_pointer());
1689 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1690 traits_type::assign(__p[__n], value_type());
1691 return *this;
1692 }
1693
Howard Hinnantcf823322010-12-17 14:46:43 +00001694 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1695 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001697 template<class _Tp>
1698 _LIBCPP_INLINE_VISIBILITY
1699 bool __addr_in_range(_Tp&& __t) const {
1700 const volatile void *__p = _VSTD::addressof(__t);
1701 return data() <= __p && __p <= data() + size();
1702 }
1703
Louis Dionned24191c2021-08-19 12:39:16 -04001704 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1705 void __throw_length_error() const {
1706#ifndef _LIBCPP_NO_EXCEPTIONS
1707 __basic_string_common<true>::__throw_length_error();
1708#else
1709 _VSTD::abort();
1710#endif
1711 }
1712
1713 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1714 void __throw_out_of_range() const {
1715#ifndef _LIBCPP_NO_EXCEPTIONS
1716 __basic_string_common<true>::__throw_out_of_range();
1717#else
1718 _VSTD::abort();
1719#endif
1720 }
1721
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 friend basic_string operator+<>(const basic_string&, const basic_string&);
1723 friend basic_string operator+<>(const value_type*, const basic_string&);
1724 friend basic_string operator+<>(value_type, const basic_string&);
1725 friend basic_string operator+<>(const basic_string&, const value_type*);
1726 friend basic_string operator+<>(const basic_string&, value_type);
1727};
1728
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001729// These declarations must appear before any functions are implicitly used
1730// so that they have the correct visibility specifier.
1731#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1732_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1733_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1734#else
1735_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1736_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1737#endif
1738
1739
Louis Dionned59f8a52021-08-17 11:59:07 -04001740#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001741template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001742 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001743 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001744 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1745 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001746 >
1747basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1748 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001749
1750template<class _CharT,
1751 class _Traits,
1752 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001753 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001754 >
1755explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1756 -> basic_string<_CharT, _Traits, _Allocator>;
1757
1758template<class _CharT,
1759 class _Traits,
1760 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001761 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001762 class _Sz = typename allocator_traits<_Allocator>::size_type
1763 >
1764basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1765 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001766#endif
1767
Howard Hinnantc51e1022010-05-11 19:42:16 +00001768template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001769inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770void
1771basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1772{
Louis Dionneba400782020-10-02 15:02:52 -04001773#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001774 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001775#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776}
1777
1778template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001779inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001780void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001781basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782{
Louis Dionneba400782020-10-02 15:02:52 -04001783#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001784 __c_node* __c = __get_db()->__find_c_and_lock(this);
1785 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001787 const_pointer __new_last = __get_pointer() + __pos;
1788 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001790 --__p;
1791 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1792 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001794 (*__p)->__c_ = nullptr;
1795 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001796 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001799 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001801#else
1802 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001803#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804}
1805
1806template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001807inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001808basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001809 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001810 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811{
Louis Dionneba400782020-10-02 15:02:52 -04001812#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001813 __get_db()->__insert_c(this);
1814#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 __zero();
1816}
1817
1818template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001819inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001821#if _LIBCPP_STD_VER <= 14
1822 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1823#else
1824 _NOEXCEPT
1825#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001826: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827{
Louis Dionneba400782020-10-02 15:02:52 -04001828#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001829 __get_db()->__insert_c(this);
1830#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 __zero();
1832}
1833
1834template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001835void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1836 size_type __sz,
1837 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838{
1839 if (__reserve > max_size())
1840 this->__throw_length_error();
1841 pointer __p;
1842 if (__reserve < __min_cap)
1843 {
1844 __set_short_size(__sz);
1845 __p = __get_short_pointer();
1846 }
1847 else
1848 {
1849 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001850 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 __set_long_pointer(__p);
1852 __set_long_cap(__cap+1);
1853 __set_long_size(__sz);
1854 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001855 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856 traits_type::assign(__p[__sz], value_type());
1857}
1858
1859template <class _CharT, class _Traits, class _Allocator>
1860void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001861basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862{
1863 if (__sz > max_size())
1864 this->__throw_length_error();
1865 pointer __p;
1866 if (__sz < __min_cap)
1867 {
1868 __set_short_size(__sz);
1869 __p = __get_short_pointer();
1870 }
1871 else
1872 {
1873 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001874 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 __set_long_pointer(__p);
1876 __set_long_cap(__cap+1);
1877 __set_long_size(__sz);
1878 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001879 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880 traits_type::assign(__p[__sz], value_type());
1881}
1882
1883template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001884template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001885basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001886 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001888 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001890#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001891 __get_db()->__insert_c(this);
1892#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893}
1894
1895template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001896inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001897basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001898 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001900 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001902#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001903 __get_db()->__insert_c(this);
1904#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905}
1906
1907template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001908inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001909basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001910 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001912 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001914#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001915 __get_db()->__insert_c(this);
1916#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917}
1918
1919template <class _CharT, class _Traits, class _Allocator>
1920basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001921 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922{
1923 if (!__str.__is_long())
1924 __r_.first().__r = __str.__r_.first().__r;
1925 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001926 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1927 __str.__get_long_size());
1928
Louis Dionneba400782020-10-02 15:02:52 -04001929#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001930 __get_db()->__insert_c(this);
1931#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932}
1933
1934template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001935basic_string<_CharT, _Traits, _Allocator>::basic_string(
1936 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001937 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938{
1939 if (!__str.__is_long())
1940 __r_.first().__r = __str.__r_.first().__r;
1941 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001942 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1943 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001944#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001945 __get_db()->__insert_c(this);
1946#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947}
1948
Martijn Vels5e7c9752020-03-04 17:52:46 -05001949template <class _CharT, class _Traits, class _Allocator>
1950void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1951 const value_type* __s, size_type __sz) {
1952 pointer __p;
1953 if (__sz < __min_cap) {
1954 __p = __get_short_pointer();
1955 __set_short_size(__sz);
1956 } else {
1957 if (__sz > max_size())
1958 this->__throw_length_error();
1959 size_t __cap = __recommend(__sz);
1960 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1961 __set_long_pointer(__p);
1962 __set_long_cap(__cap + 1);
1963 __set_long_size(__sz);
1964 }
1965 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1966}
1967
Eric Fiselierfc92be82017-04-19 00:28:44 +00001968#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969
1970template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001971inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001972basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001973#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001974 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001975#else
1976 _NOEXCEPT
1977#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001978 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979{
1980 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001981#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001982 __get_db()->__insert_c(this);
1983 if (__is_long())
1984 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985#endif
1986}
1987
1988template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001989inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001991 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992{
Marshall Clowd2d24692014-07-17 15:32:20 +00001993 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001994 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001995 else
1996 {
1997 __r_.first().__r = __str.__r_.first().__r;
1998 __str.__zero();
1999 }
Louis Dionneba400782020-10-02 15:02:52 -04002000#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002001 __get_db()->__insert_c(this);
2002 if (__is_long())
2003 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004#endif
2005}
2006
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002007#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008
2009template <class _CharT, class _Traits, class _Allocator>
2010void
2011basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2012{
2013 if (__n > max_size())
2014 this->__throw_length_error();
2015 pointer __p;
2016 if (__n < __min_cap)
2017 {
2018 __set_short_size(__n);
2019 __p = __get_short_pointer();
2020 }
2021 else
2022 {
2023 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002024 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025 __set_long_pointer(__p);
2026 __set_long_cap(__cap+1);
2027 __set_long_size(__n);
2028 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002029 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030 traits_type::assign(__p[__n], value_type());
2031}
2032
2033template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002034inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002035basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002036 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037{
2038 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002039#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002040 __get_db()->__insert_c(this);
2041#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042}
2043
2044template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002045template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002046basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002047 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002048{
2049 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002050#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002051 __get_db()->__insert_c(this);
2052#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053}
2054
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002056basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2057 size_type __pos, size_type __n,
2058 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002059 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060{
2061 size_type __str_sz = __str.size();
2062 if (__pos > __str_sz)
2063 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002064 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002065#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002066 __get_db()->__insert_c(this);
2067#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068}
2069
2070template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002071inline
Marshall Clow83445802016-04-07 18:13:41 +00002072basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002073 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002074 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002075{
2076 size_type __str_sz = __str.size();
2077 if (__pos > __str_sz)
2078 this->__throw_out_of_range();
2079 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002080#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002081 __get_db()->__insert_c(this);
2082#endif
2083}
2084
2085template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002086template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002087basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002088 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002089 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002090{
Marshall Clowe46031a2018-07-02 18:41:15 +00002091 __self_view __sv0 = __t;
2092 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002093 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002094#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002095 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002096#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002097}
2098
2099template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002100template <class _Tp, class>
2101basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002102 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002103{
Marshall Clowe46031a2018-07-02 18:41:15 +00002104 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002105 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002106#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002107 __get_db()->__insert_c(this);
2108#endif
2109}
2110
2111template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002112template <class _Tp, class>
2113basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002114 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002115{
Marshall Clowe46031a2018-07-02 18:41:15 +00002116 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002117 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002118#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002119 __get_db()->__insert_c(this);
2120#endif
2121}
2122
2123template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002125__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002127 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2128>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2130{
2131 __zero();
2132#ifndef _LIBCPP_NO_EXCEPTIONS
2133 try
2134 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002135#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136 for (; __first != __last; ++__first)
2137 push_back(*__first);
2138#ifndef _LIBCPP_NO_EXCEPTIONS
2139 }
2140 catch (...)
2141 {
2142 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002143 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144 throw;
2145 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002146#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147}
2148
2149template <class _CharT, class _Traits, class _Allocator>
2150template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002151__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002153 __is_cpp17_forward_iterator<_ForwardIterator>::value
2154>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2156{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002157 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158 if (__sz > max_size())
2159 this->__throw_length_error();
2160 pointer __p;
2161 if (__sz < __min_cap)
2162 {
2163 __set_short_size(__sz);
2164 __p = __get_short_pointer();
2165 }
2166 else
2167 {
2168 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002169 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170 __set_long_pointer(__p);
2171 __set_long_cap(__cap+1);
2172 __set_long_size(__sz);
2173 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002174
2175#ifndef _LIBCPP_NO_EXCEPTIONS
2176 try
2177 {
2178#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002179 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 traits_type::assign(*__p, *__first);
2181 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002182#ifndef _LIBCPP_NO_EXCEPTIONS
2183 }
2184 catch (...)
2185 {
2186 if (__is_long())
2187 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2188 throw;
2189 }
2190#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191}
2192
2193template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002194template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002195inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002197 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198{
2199 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002200#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002201 __get_db()->__insert_c(this);
2202#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203}
2204
2205template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002206template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002207inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2209 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002210 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211{
2212 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002213#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002214 __get_db()->__insert_c(this);
2215#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216}
2217
Eric Fiselierfc92be82017-04-19 00:28:44 +00002218#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002219
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002221inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002222basic_string<_CharT, _Traits, _Allocator>::basic_string(
2223 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002224 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225{
2226 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002227#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002228 __get_db()->__insert_c(this);
2229#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230}
2231
2232template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002233inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002234
Eric Fiselier812882b2017-02-17 01:17:10 +00002235basic_string<_CharT, _Traits, _Allocator>::basic_string(
2236 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002237 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238{
2239 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002240#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002241 __get_db()->__insert_c(this);
2242#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243}
2244
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002245#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002246
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2249{
Louis Dionneba400782020-10-02 15:02:52 -04002250#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002251 __get_db()->__erase_c(this);
2252#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002254 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255}
2256
2257template <class _CharT, class _Traits, class _Allocator>
2258void
2259basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2260 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002261 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 +00002262{
2263 size_type __ms = max_size();
2264 if (__delta_cap > __ms - __old_cap - 1)
2265 this->__throw_length_error();
2266 pointer __old_p = __get_pointer();
2267 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002268 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002270 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271 __invalidate_all_iterators();
2272 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002273 traits_type::copy(_VSTD::__to_address(__p),
2274 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002276 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2278 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002279 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2280 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002282 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 __set_long_pointer(__p);
2284 __set_long_cap(__cap+1);
2285 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2286 __set_long_size(__old_sz);
2287 traits_type::assign(__p[__old_sz], value_type());
2288}
2289
2290template <class _CharT, class _Traits, class _Allocator>
2291void
2292basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2293 size_type __n_copy, size_type __n_del, size_type __n_add)
2294{
2295 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002296 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 this->__throw_length_error();
2298 pointer __old_p = __get_pointer();
2299 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002300 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002302 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303 __invalidate_all_iterators();
2304 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002305 traits_type::copy(_VSTD::__to_address(__p),
2306 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2308 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002309 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2310 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002311 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002313 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314 __set_long_pointer(__p);
2315 __set_long_cap(__cap+1);
2316}
2317
2318// assign
2319
2320template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002321template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002322basic_string<_CharT, _Traits, _Allocator>&
2323basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002324 const value_type* __s, size_type __n) {
2325 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2326 if (__n < __cap) {
2327 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2328 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2329 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2330 traits_type::assign(__p[__n], value_type());
2331 __invalidate_iterators_past(__n);
2332 } else {
2333 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2334 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2335 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002336 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002337}
2338
2339template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002341basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2342 const value_type* __s, size_type __n) {
2343 size_type __cap = capacity();
2344 if (__cap >= __n) {
2345 value_type* __p = _VSTD::__to_address(__get_pointer());
2346 traits_type::move(__p, __s, __n);
2347 traits_type::assign(__p[__n], value_type());
2348 __set_size(__n);
2349 __invalidate_iterators_past(__n);
2350 } else {
2351 size_type __sz = size();
2352 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2353 }
2354 return *this;
2355}
2356
2357template <class _CharT, class _Traits, class _Allocator>
2358basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002359basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002361 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002362 return (__builtin_constant_p(__n) && __n < __min_cap)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002363 ? __assign_short(__s, __n)
2364 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365}
2366
2367template <class _CharT, class _Traits, class _Allocator>
2368basic_string<_CharT, _Traits, _Allocator>&
2369basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2370{
2371 size_type __cap = capacity();
2372 if (__cap < __n)
2373 {
2374 size_type __sz = size();
2375 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2376 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002377 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002378 traits_type::assign(__p, __n, __c);
2379 traits_type::assign(__p[__n], value_type());
2380 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002381 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382 return *this;
2383}
2384
2385template <class _CharT, class _Traits, class _Allocator>
2386basic_string<_CharT, _Traits, _Allocator>&
2387basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2388{
2389 pointer __p;
2390 if (__is_long())
2391 {
2392 __p = __get_long_pointer();
2393 __set_long_size(1);
2394 }
2395 else
2396 {
2397 __p = __get_short_pointer();
2398 __set_short_size(1);
2399 }
2400 traits_type::assign(*__p, __c);
2401 traits_type::assign(*++__p, value_type());
2402 __invalidate_iterators_past(1);
2403 return *this;
2404}
2405
2406template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002407basic_string<_CharT, _Traits, _Allocator>&
2408basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2409{
Martijn Vels596e3de2020-02-26 15:55:49 -05002410 if (this != &__str) {
2411 __copy_assign_alloc(__str);
2412 if (!__is_long()) {
2413 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002414 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002415 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002416 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002417 }
2418 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002419 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002420 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002421 }
2422 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002423}
2424
Eric Fiselierfc92be82017-04-19 00:28:44 +00002425#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002426
2427template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002428inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002429void
2430basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002431 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002432{
2433 if (__alloc() != __str.__alloc())
2434 assign(__str);
2435 else
2436 __move_assign(__str, true_type());
2437}
2438
2439template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002440inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002441void
2442basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002443#if _LIBCPP_STD_VER > 14
2444 _NOEXCEPT
2445#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002446 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002447#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002448{
marshall2ed41622019-11-27 07:13:00 -08002449 if (__is_long()) {
2450 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2451 __get_long_cap());
2452#if _LIBCPP_STD_VER <= 14
2453 if (!is_nothrow_move_assignable<allocator_type>::value) {
2454 __set_short_size(0);
2455 traits_type::assign(__get_short_pointer()[0], value_type());
2456 }
2457#endif
2458 }
2459 __move_assign_alloc(__str);
2460 __r_.first() = __str.__r_.first();
2461 __str.__set_short_size(0);
2462 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002463}
2464
2465template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002466inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002467basic_string<_CharT, _Traits, _Allocator>&
2468basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002469 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002470{
2471 __move_assign(__str, integral_constant<bool,
2472 __alloc_traits::propagate_on_container_move_assignment::value>());
2473 return *this;
2474}
2475
2476#endif
2477
2478template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002480__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002482 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002484>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2486{
Marshall Clow958362f2016-09-05 01:54:30 +00002487 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002488 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002489 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490}
2491
2492template <class _CharT, class _Traits, class _Allocator>
2493template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002494__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002496 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002498>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2500{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002501 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002502 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2503 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2504
2505 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2506 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002508 if (__cap < __n)
2509 {
2510 size_type __sz = size();
2511 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2512 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002513 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002514 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002515 traits_type::assign(*__p, *__first);
2516 traits_type::assign(*__p, value_type());
2517 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002518 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519 }
2520 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002521 {
2522 const basic_string __temp(__first, __last, __alloc());
2523 assign(__temp.data(), __temp.size());
2524 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525 return *this;
2526}
2527
2528template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529basic_string<_CharT, _Traits, _Allocator>&
2530basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2531{
2532 size_type __sz = __str.size();
2533 if (__pos > __sz)
2534 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002535 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536}
2537
2538template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002539template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002540__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002541<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002542 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2543 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002544 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002545>
Marshall Clow82513342016-09-24 22:45:42 +00002546basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002547{
Marshall Clow82513342016-09-24 22:45:42 +00002548 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002549 size_type __sz = __sv.size();
2550 if (__pos > __sz)
2551 this->__throw_out_of_range();
2552 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2553}
2554
2555
2556template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002558basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2559 return __assign_external(__s, traits_type::length(__s));
2560}
2561
2562template <class _CharT, class _Traits, class _Allocator>
2563basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002564basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002566 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002567 return __builtin_constant_p(*__s)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002568 ? (traits_type::length(__s) < __min_cap
2569 ? __assign_short(__s, traits_type::length(__s))
2570 : __assign_external(__s, traits_type::length(__s)))
2571 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573// append
2574
2575template <class _CharT, class _Traits, class _Allocator>
2576basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002577basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002579 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580 size_type __cap = capacity();
2581 size_type __sz = size();
2582 if (__cap - __sz >= __n)
2583 {
2584 if (__n)
2585 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002586 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587 traits_type::copy(__p + __sz, __s, __n);
2588 __sz += __n;
2589 __set_size(__sz);
2590 traits_type::assign(__p[__sz], value_type());
2591 }
2592 }
2593 else
2594 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2595 return *this;
2596}
2597
2598template <class _CharT, class _Traits, class _Allocator>
2599basic_string<_CharT, _Traits, _Allocator>&
2600basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2601{
2602 if (__n)
2603 {
2604 size_type __cap = capacity();
2605 size_type __sz = size();
2606 if (__cap - __sz < __n)
2607 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2608 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002609 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610 __sz += __n;
2611 __set_size(__sz);
2612 traits_type::assign(__p[__sz], value_type());
2613 }
2614 return *this;
2615}
2616
2617template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002618inline void
2619basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2620{
2621 if (__n)
2622 {
2623 size_type __cap = capacity();
2624 size_type __sz = size();
2625 if (__cap - __sz < __n)
2626 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2627 pointer __p = __get_pointer();
2628 __sz += __n;
2629 __set_size(__sz);
2630 traits_type::assign(__p[__sz], value_type());
2631 }
2632}
2633
2634template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635void
2636basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2637{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002638 bool __is_short = !__is_long();
2639 size_type __cap;
2640 size_type __sz;
2641 if (__is_short)
2642 {
2643 __cap = __min_cap - 1;
2644 __sz = __get_short_size();
2645 }
2646 else
2647 {
2648 __cap = __get_long_cap() - 1;
2649 __sz = __get_long_size();
2650 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002652 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002654 __is_short = !__is_long();
2655 }
2656 pointer __p;
2657 if (__is_short)
2658 {
2659 __p = __get_short_pointer() + __sz;
2660 __set_short_size(__sz+1);
2661 }
2662 else
2663 {
2664 __p = __get_long_pointer() + __sz;
2665 __set_long_size(__sz+1);
2666 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667 traits_type::assign(*__p, __c);
2668 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669}
2670
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671template <class _CharT, class _Traits, class _Allocator>
2672template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002673__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002674<
2675 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2676 basic_string<_CharT, _Traits, _Allocator>&
2677>
2678basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002679 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680{
2681 size_type __sz = size();
2682 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002683 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684 if (__n)
2685 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002686 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2687 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002688 {
2689 if (__cap - __sz < __n)
2690 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2691 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002692 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002693 traits_type::assign(*__p, *__first);
2694 traits_type::assign(*__p, value_type());
2695 __set_size(__sz + __n);
2696 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002697 else
2698 {
2699 const basic_string __temp(__first, __last, __alloc());
2700 append(__temp.data(), __temp.size());
2701 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702 }
2703 return *this;
2704}
2705
2706template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002707inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708basic_string<_CharT, _Traits, _Allocator>&
2709basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2710{
2711 return append(__str.data(), __str.size());
2712}
2713
2714template <class _CharT, class _Traits, class _Allocator>
2715basic_string<_CharT, _Traits, _Allocator>&
2716basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2717{
2718 size_type __sz = __str.size();
2719 if (__pos > __sz)
2720 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002721 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722}
2723
2724template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002725template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002726 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002727 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002728 __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 +00002729 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002730 >
Marshall Clow82513342016-09-24 22:45:42 +00002731basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002732{
Marshall Clow82513342016-09-24 22:45:42 +00002733 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002734 size_type __sz = __sv.size();
2735 if (__pos > __sz)
2736 this->__throw_out_of_range();
2737 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2738}
2739
2740template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002742basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002744 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745 return append(__s, traits_type::length(__s));
2746}
2747
2748// insert
2749
2750template <class _CharT, class _Traits, class _Allocator>
2751basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002752basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002754 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755 size_type __sz = size();
2756 if (__pos > __sz)
2757 this->__throw_out_of_range();
2758 size_type __cap = capacity();
2759 if (__cap - __sz >= __n)
2760 {
2761 if (__n)
2762 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002763 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 size_type __n_move = __sz - __pos;
2765 if (__n_move != 0)
2766 {
2767 if (__p + __pos <= __s && __s < __p + __sz)
2768 __s += __n;
2769 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2770 }
2771 traits_type::move(__p + __pos, __s, __n);
2772 __sz += __n;
2773 __set_size(__sz);
2774 traits_type::assign(__p[__sz], value_type());
2775 }
2776 }
2777 else
2778 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2779 return *this;
2780}
2781
2782template <class _CharT, class _Traits, class _Allocator>
2783basic_string<_CharT, _Traits, _Allocator>&
2784basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2785{
2786 size_type __sz = size();
2787 if (__pos > __sz)
2788 this->__throw_out_of_range();
2789 if (__n)
2790 {
2791 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002792 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 if (__cap - __sz >= __n)
2794 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002795 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796 size_type __n_move = __sz - __pos;
2797 if (__n_move != 0)
2798 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2799 }
2800 else
2801 {
2802 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002803 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804 }
2805 traits_type::assign(__p + __pos, __n, __c);
2806 __sz += __n;
2807 __set_size(__sz);
2808 traits_type::assign(__p[__sz], value_type());
2809 }
2810 return *this;
2811}
2812
2813template <class _CharT, class _Traits, class _Allocator>
2814template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002815__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002817 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002818 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002819>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2821{
Louis Dionneba400782020-10-02 15:02:52 -04002822#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002823 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2824 "string::insert(iterator, range) called with an iterator not"
2825 " referring to this string");
2826#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002827 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002828 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829}
2830
2831template <class _CharT, class _Traits, class _Allocator>
2832template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002833__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002835 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002837>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2839{
Louis Dionneba400782020-10-02 15:02:52 -04002840#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002841 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2842 "string::insert(iterator, range) called with an iterator not"
2843 " referring to this string");
2844#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002846 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847 if (__n)
2848 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002849 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2850 !__addr_in_range(*__first))
2851 {
2852 size_type __sz = size();
2853 size_type __cap = capacity();
2854 value_type* __p;
2855 if (__cap - __sz >= __n)
2856 {
2857 __p = _VSTD::__to_address(__get_pointer());
2858 size_type __n_move = __sz - __ip;
2859 if (__n_move != 0)
2860 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2861 }
2862 else
2863 {
2864 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2865 __p = _VSTD::__to_address(__get_long_pointer());
2866 }
2867 __sz += __n;
2868 __set_size(__sz);
2869 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002870 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002871 traits_type::assign(*__p, *__first);
2872 }
2873 else
Marshall Clow958362f2016-09-05 01:54:30 +00002874 {
2875 const basic_string __temp(__first, __last, __alloc());
2876 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2877 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 }
2879 return begin() + __ip;
2880}
2881
2882template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002883inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884basic_string<_CharT, _Traits, _Allocator>&
2885basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2886{
2887 return insert(__pos1, __str.data(), __str.size());
2888}
2889
2890template <class _CharT, class _Traits, class _Allocator>
2891basic_string<_CharT, _Traits, _Allocator>&
2892basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2893 size_type __pos2, size_type __n)
2894{
2895 size_type __str_sz = __str.size();
2896 if (__pos2 > __str_sz)
2897 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002898 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899}
2900
2901template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002902template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002903__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002904<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002905 __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 +00002906 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002907>
Marshall Clow82513342016-09-24 22:45:42 +00002908basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002909 size_type __pos2, size_type __n)
2910{
Marshall Clow82513342016-09-24 22:45:42 +00002911 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002912 size_type __str_sz = __sv.size();
2913 if (__pos2 > __str_sz)
2914 this->__throw_out_of_range();
2915 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2916}
2917
2918template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002920basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002922 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 return insert(__pos, __s, traits_type::length(__s));
2924}
2925
2926template <class _CharT, class _Traits, class _Allocator>
2927typename basic_string<_CharT, _Traits, _Allocator>::iterator
2928basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2929{
2930 size_type __ip = static_cast<size_type>(__pos - begin());
2931 size_type __sz = size();
2932 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002933 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934 if (__cap == __sz)
2935 {
2936 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002937 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938 }
2939 else
2940 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002941 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942 size_type __n_move = __sz - __ip;
2943 if (__n_move != 0)
2944 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2945 }
2946 traits_type::assign(__p[__ip], __c);
2947 traits_type::assign(__p[++__sz], value_type());
2948 __set_size(__sz);
2949 return begin() + static_cast<difference_type>(__ip);
2950}
2951
2952template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002953inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954typename basic_string<_CharT, _Traits, _Allocator>::iterator
2955basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2956{
Louis Dionneba400782020-10-02 15:02:52 -04002957#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002958 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2959 "string::insert(iterator, n, value) called with an iterator not"
2960 " referring to this string");
2961#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 difference_type __p = __pos - begin();
2963 insert(static_cast<size_type>(__p), __n, __c);
2964 return begin() + __p;
2965}
2966
2967// replace
2968
2969template <class _CharT, class _Traits, class _Allocator>
2970basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002971basic_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 +00002972 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002974 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975 size_type __sz = size();
2976 if (__pos > __sz)
2977 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002978 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979 size_type __cap = capacity();
2980 if (__cap - __sz + __n1 >= __n2)
2981 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002982 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983 if (__n1 != __n2)
2984 {
2985 size_type __n_move = __sz - __pos - __n1;
2986 if (__n_move != 0)
2987 {
2988 if (__n1 > __n2)
2989 {
2990 traits_type::move(__p + __pos, __s, __n2);
2991 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2992 goto __finish;
2993 }
2994 if (__p + __pos < __s && __s < __p + __sz)
2995 {
2996 if (__p + __pos + __n1 <= __s)
2997 __s += __n2 - __n1;
2998 else // __p + __pos < __s < __p + __pos + __n1
2999 {
3000 traits_type::move(__p + __pos, __s, __n1);
3001 __pos += __n1;
3002 __s += __n2;
3003 __n2 -= __n1;
3004 __n1 = 0;
3005 }
3006 }
3007 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3008 }
3009 }
3010 traits_type::move(__p + __pos, __s, __n2);
3011__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05003012// __sz += __n2 - __n1; in this and the below function below can cause unsigned
3013// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014 __sz += __n2 - __n1;
3015 __set_size(__sz);
3016 __invalidate_iterators_past(__sz);
3017 traits_type::assign(__p[__sz], value_type());
3018 }
3019 else
3020 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3021 return *this;
3022}
3023
3024template <class _CharT, class _Traits, class _Allocator>
3025basic_string<_CharT, _Traits, _Allocator>&
3026basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00003027 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028{
3029 size_type __sz = size();
3030 if (__pos > __sz)
3031 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003032 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003034 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 if (__cap - __sz + __n1 >= __n2)
3036 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003037 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038 if (__n1 != __n2)
3039 {
3040 size_type __n_move = __sz - __pos - __n1;
3041 if (__n_move != 0)
3042 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3043 }
3044 }
3045 else
3046 {
3047 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003048 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 }
3050 traits_type::assign(__p + __pos, __n2, __c);
3051 __sz += __n2 - __n1;
3052 __set_size(__sz);
3053 __invalidate_iterators_past(__sz);
3054 traits_type::assign(__p[__sz], value_type());
3055 return *this;
3056}
3057
3058template <class _CharT, class _Traits, class _Allocator>
3059template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003060__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003062 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003064>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003065basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066 _InputIterator __j1, _InputIterator __j2)
3067{
Marshall Clow958362f2016-09-05 01:54:30 +00003068 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003069 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070}
3071
3072template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003073inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074basic_string<_CharT, _Traits, _Allocator>&
3075basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3076{
3077 return replace(__pos1, __n1, __str.data(), __str.size());
3078}
3079
3080template <class _CharT, class _Traits, class _Allocator>
3081basic_string<_CharT, _Traits, _Allocator>&
3082basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3083 size_type __pos2, size_type __n2)
3084{
3085 size_type __str_sz = __str.size();
3086 if (__pos2 > __str_sz)
3087 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003088 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089}
3090
3091template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003092template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003093__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003094<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003095 __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 +00003096 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003097>
Marshall Clow82513342016-09-24 22:45:42 +00003098basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003099 size_type __pos2, size_type __n2)
3100{
Marshall Clow82513342016-09-24 22:45:42 +00003101 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003102 size_type __str_sz = __sv.size();
3103 if (__pos2 > __str_sz)
3104 this->__throw_out_of_range();
3105 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3106}
3107
3108template <class _CharT, class _Traits, class _Allocator>
3109basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003110basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003112 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 return replace(__pos, __n1, __s, traits_type::length(__s));
3114}
3115
3116template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003117inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003119basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120{
3121 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3122 __str.data(), __str.size());
3123}
3124
3125template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003126inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003128basic_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 +00003129{
3130 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3131}
3132
3133template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003134inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003136basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137{
3138 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3139}
3140
3141template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003142inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003144basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145{
3146 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3147}
3148
3149// erase
3150
Martijn Velsa81fc792020-02-26 13:25:43 -05003151// 'externally instantiated' erase() implementation, called when __n != npos.
3152// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003154void
3155basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3156 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158 if (__n)
3159 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003160 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003161 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003162 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 size_type __n_move = __sz - __pos - __n;
3164 if (__n_move != 0)
3165 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3166 __sz -= __n;
3167 __set_size(__sz);
3168 __invalidate_iterators_past(__sz);
3169 traits_type::assign(__p[__sz], value_type());
3170 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003171}
3172
3173template <class _CharT, class _Traits, class _Allocator>
3174basic_string<_CharT, _Traits, _Allocator>&
3175basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3176 size_type __n) {
3177 if (__pos > size()) this->__throw_out_of_range();
3178 if (__n == npos) {
3179 __erase_to_end(__pos);
3180 } else {
3181 __erase_external_with_move(__pos, __n);
3182 }
3183 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184}
3185
3186template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003187inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188typename basic_string<_CharT, _Traits, _Allocator>::iterator
3189basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3190{
Louis Dionneba400782020-10-02 15:02:52 -04003191#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003192 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3193 "string::erase(iterator) called with an iterator not"
3194 " referring to this string");
3195#endif
3196 _LIBCPP_ASSERT(__pos != end(),
3197 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198 iterator __b = begin();
3199 size_type __r = static_cast<size_type>(__pos - __b);
3200 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003201 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202}
3203
3204template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003205inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206typename basic_string<_CharT, _Traits, _Allocator>::iterator
3207basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3208{
Louis Dionneba400782020-10-02 15:02:52 -04003209#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003210 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3211 "string::erase(iterator, iterator) called with an iterator not"
3212 " referring to this string");
3213#endif
3214 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215 iterator __b = begin();
3216 size_type __r = static_cast<size_type>(__first - __b);
3217 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003218 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219}
3220
3221template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003222inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223void
3224basic_string<_CharT, _Traits, _Allocator>::pop_back()
3225{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003226 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227 size_type __sz;
3228 if (__is_long())
3229 {
3230 __sz = __get_long_size() - 1;
3231 __set_long_size(__sz);
3232 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3233 }
3234 else
3235 {
3236 __sz = __get_short_size() - 1;
3237 __set_short_size(__sz);
3238 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3239 }
3240 __invalidate_iterators_past(__sz);
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003246basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247{
3248 __invalidate_all_iterators();
3249 if (__is_long())
3250 {
3251 traits_type::assign(*__get_long_pointer(), value_type());
3252 __set_long_size(0);
3253 }
3254 else
3255 {
3256 traits_type::assign(*__get_short_pointer(), value_type());
3257 __set_short_size(0);
3258 }
3259}
3260
3261template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003262inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263void
3264basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3265{
3266 if (__is_long())
3267 {
3268 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3269 __set_long_size(__pos);
3270 }
3271 else
3272 {
3273 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3274 __set_short_size(__pos);
3275 }
3276 __invalidate_iterators_past(__pos);
3277}
3278
3279template <class _CharT, class _Traits, class _Allocator>
3280void
3281basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3282{
3283 size_type __sz = size();
3284 if (__n > __sz)
3285 append(__n - __sz, __c);
3286 else
3287 __erase_to_end(__n);
3288}
3289
3290template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003291inline void
3292basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3293{
3294 size_type __sz = size();
3295 if (__n > __sz) {
3296 __append_default_init(__n - __sz);
3297 } else
3298 __erase_to_end(__n);
3299}
3300
3301template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003302inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003304basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003306 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003307#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003308 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003310 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311#endif
3312}
3313
3314template <class _CharT, class _Traits, class _Allocator>
3315void
Marek Kurdejc9848142020-11-26 10:07:16 +01003316basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003317{
Marek Kurdejc9848142020-11-26 10:07:16 +01003318 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003320
3321#if _LIBCPP_STD_VER > 17
3322 // Reserve never shrinks as of C++20.
3323 if (__requested_capacity <= capacity()) return;
3324#endif
3325
3326 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3327 __target_capacity = __recommend(__target_capacity);
3328 if (__target_capacity == capacity()) return;
3329
3330 __shrink_or_extend(__target_capacity);
3331}
3332
3333template <class _CharT, class _Traits, class _Allocator>
3334void
3335basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3336{
3337 size_type __target_capacity = __recommend(size());
3338 if (__target_capacity == capacity()) return;
3339
3340 __shrink_or_extend(__target_capacity);
3341}
3342
3343template <class _CharT, class _Traits, class _Allocator>
3344void
3345basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3346{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347 size_type __cap = capacity();
3348 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003349
3350 pointer __new_data, __p;
3351 bool __was_long, __now_long;
3352 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003354 __was_long = true;
3355 __now_long = false;
3356 __new_data = __get_short_pointer();
3357 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003359 else
3360 {
3361 if (__target_capacity > __cap)
3362 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3363 else
3364 {
3365 #ifndef _LIBCPP_NO_EXCEPTIONS
3366 try
3367 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003368 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003369 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3370 #ifndef _LIBCPP_NO_EXCEPTIONS
3371 }
3372 catch (...)
3373 {
3374 return;
3375 }
3376 #else // _LIBCPP_NO_EXCEPTIONS
3377 if (__new_data == nullptr)
3378 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003379 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003380 }
3381 __now_long = true;
3382 __was_long = __is_long();
3383 __p = __get_pointer();
3384 }
3385 traits_type::copy(_VSTD::__to_address(__new_data),
3386 _VSTD::__to_address(__p), size()+1);
3387 if (__was_long)
3388 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3389 if (__now_long)
3390 {
3391 __set_long_cap(__target_capacity+1);
3392 __set_long_size(__sz);
3393 __set_long_pointer(__new_data);
3394 }
3395 else
3396 __set_short_size(__sz);
3397 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398}
3399
3400template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003401inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003402typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003403basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003405 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003406 return *(data() + __pos);
3407}
3408
3409template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003410inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003412basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003414 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415 return *(__get_pointer() + __pos);
3416}
3417
3418template <class _CharT, class _Traits, class _Allocator>
3419typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3420basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3421{
3422 if (__n >= size())
3423 this->__throw_out_of_range();
3424 return (*this)[__n];
3425}
3426
3427template <class _CharT, class _Traits, class _Allocator>
3428typename basic_string<_CharT, _Traits, _Allocator>::reference
3429basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3430{
3431 if (__n >= size())
3432 this->__throw_out_of_range();
3433 return (*this)[__n];
3434}
3435
3436template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003437inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003439basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003441 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442 return *__get_pointer();
3443}
3444
3445template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003446inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003448basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003450 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003451 return *data();
3452}
3453
3454template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003455inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003457basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003459 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460 return *(__get_pointer() + size() - 1);
3461}
3462
3463template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003464inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003466basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003467{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003468 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469 return *(data() + size() - 1);
3470}
3471
3472template <class _CharT, class _Traits, class _Allocator>
3473typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003474basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475{
3476 size_type __sz = size();
3477 if (__pos > __sz)
3478 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003479 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480 traits_type::copy(__s, data() + __pos, __rlen);
3481 return __rlen;
3482}
3483
3484template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003485inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486basic_string<_CharT, _Traits, _Allocator>
3487basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3488{
3489 return basic_string(*this, __pos, __n, __alloc());
3490}
3491
3492template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003493inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494void
3495basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003496#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003497 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003498#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003499 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003500 __is_nothrow_swappable<allocator_type>::value)
3501#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502{
Louis Dionneba400782020-10-02 15:02:52 -04003503#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003504 if (!__is_long())
3505 __get_db()->__invalidate_all(this);
3506 if (!__str.__is_long())
3507 __get_db()->__invalidate_all(&__str);
3508 __get_db()->swap(this, &__str);
3509#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003510 _LIBCPP_ASSERT(
3511 __alloc_traits::propagate_on_container_swap::value ||
3512 __alloc_traits::is_always_equal::value ||
3513 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003514 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003515 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516}
3517
3518// find
3519
3520template <class _Traits>
3521struct _LIBCPP_HIDDEN __traits_eq
3522{
3523 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003524 _LIBCPP_INLINE_VISIBILITY
3525 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3526 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527};
3528
3529template<class _CharT, class _Traits, class _Allocator>
3530typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003531basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003532 size_type __pos,
3533 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003535 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003536 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003537 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538}
3539
3540template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003541inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003543basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3544 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548}
3549
3550template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003551template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003552__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003553<
3554 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3555 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003556>
Marshall Clowe46031a2018-07-02 18:41:15 +00003557basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003558 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003559{
Marshall Clowe46031a2018-07-02 18:41:15 +00003560 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003561 return __str_find<value_type, size_type, traits_type, npos>
3562 (data(), size(), __sv.data(), __pos, __sv.size());
3563}
3564
3565template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003566inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003567typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003568basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003569 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003571 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003572 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003573 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574}
3575
3576template<class _CharT, class _Traits, class _Allocator>
3577typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003578basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3579 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003581 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003582 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583}
3584
3585// rfind
3586
3587template<class _CharT, class _Traits, class _Allocator>
3588typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003589basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003590 size_type __pos,
3591 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003593 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003594 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003595 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003596}
3597
3598template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003599inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003601basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3602 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606}
3607
3608template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003609template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003610__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003611<
3612 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3613 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003614>
Marshall Clowe46031a2018-07-02 18:41:15 +00003615basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003616 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003617{
Marshall Clowe46031a2018-07-02 18:41:15 +00003618 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003619 return __str_rfind<value_type, size_type, traits_type, npos>
3620 (data(), size(), __sv.data(), __pos, __sv.size());
3621}
3622
3623template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003624inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003625typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003626basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003627 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003629 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003630 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003631 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632}
3633
3634template<class _CharT, class _Traits, class _Allocator>
3635typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003636basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3637 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003639 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003640 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641}
3642
3643// find_first_of
3644
3645template<class _CharT, class _Traits, class _Allocator>
3646typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003647basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003648 size_type __pos,
3649 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003651 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003652 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003653 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654}
3655
3656template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003657inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003659basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3660 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664}
3665
3666template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003667template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003668__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003669<
3670 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3671 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003672>
Marshall Clowe46031a2018-07-02 18:41:15 +00003673basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003674 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003675{
Marshall Clowe46031a2018-07-02 18:41:15 +00003676 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003677 return __str_find_first_of<value_type, size_type, traits_type, npos>
3678 (data(), size(), __sv.data(), __pos, __sv.size());
3679}
3680
3681template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003682inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003683typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003684basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003685 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003687 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003688 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003689 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690}
3691
3692template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003693inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003695basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3696 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697{
3698 return find(__c, __pos);
3699}
3700
3701// find_last_of
3702
3703template<class _CharT, class _Traits, class _Allocator>
3704typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003705basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003706 size_type __pos,
3707 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003709 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003710 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003711 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003712}
3713
3714template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003715inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003717basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3718 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722}
3723
3724template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003725template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003726__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003727<
3728 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3729 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003730>
Marshall Clowe46031a2018-07-02 18:41:15 +00003731basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003732 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003733{
Marshall Clowe46031a2018-07-02 18:41:15 +00003734 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003735 return __str_find_last_of<value_type, size_type, traits_type, npos>
3736 (data(), size(), __sv.data(), __pos, __sv.size());
3737}
3738
3739template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003740inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003741typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003742basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003743 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003745 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003746 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003747 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748}
3749
3750template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003751inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003752typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003753basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3754 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755{
3756 return rfind(__c, __pos);
3757}
3758
3759// find_first_not_of
3760
3761template<class _CharT, class _Traits, class _Allocator>
3762typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003763basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003764 size_type __pos,
3765 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003767 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003768 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003769 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770}
3771
3772template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003773inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003775basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3776 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780}
3781
3782template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003783template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003784__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003785<
3786 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3787 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003788>
Marshall Clowe46031a2018-07-02 18:41:15 +00003789basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003790 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003791{
Marshall Clowe46031a2018-07-02 18:41:15 +00003792 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003793 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3794 (data(), size(), __sv.data(), __pos, __sv.size());
3795}
3796
3797template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003798inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003799typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003800basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003801 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003803 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003804 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003805 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806}
3807
3808template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003809inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003811basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3812 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003814 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003815 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816}
3817
3818// find_last_not_of
3819
3820template<class _CharT, class _Traits, class _Allocator>
3821typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003822basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003823 size_type __pos,
3824 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003826 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003827 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003828 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003829}
3830
3831template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003832inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003833typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003834basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3835 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839}
3840
3841template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003842template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003843__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003844<
3845 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3846 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003847>
Marshall Clowe46031a2018-07-02 18:41:15 +00003848basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003849 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003850{
Marshall Clowe46031a2018-07-02 18:41:15 +00003851 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003852 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3853 (data(), size(), __sv.data(), __pos, __sv.size());
3854}
3855
3856template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003857inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003858typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003859basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003860 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003862 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003863 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003864 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865}
3866
3867template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003868inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003870basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3871 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003873 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003874 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875}
3876
3877// compare
3878
3879template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003880template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003881__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003882<
3883 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3884 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003885>
zoecarver1997e0a2021-02-05 11:54:47 -08003886basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887{
Marshall Clowe46031a2018-07-02 18:41:15 +00003888 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003889 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003890 size_t __rhs_sz = __sv.size();
3891 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003892 _VSTD::min(__lhs_sz, __rhs_sz));
3893 if (__result != 0)
3894 return __result;
3895 if (__lhs_sz < __rhs_sz)
3896 return -1;
3897 if (__lhs_sz > __rhs_sz)
3898 return 1;
3899 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900}
3901
3902template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003903inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003905basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003906{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003907 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908}
3909
3910template <class _CharT, class _Traits, class _Allocator>
3911int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003912basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3913 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003914 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003915 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003916{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003917 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918 size_type __sz = size();
3919 if (__pos1 > __sz || __n2 == npos)
3920 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003921 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3922 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003923 if (__r == 0)
3924 {
3925 if (__rlen < __n2)
3926 __r = -1;
3927 else if (__rlen > __n2)
3928 __r = 1;
3929 }
3930 return __r;
3931}
3932
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003933template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003934template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003935__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003936<
3937 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3938 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 Clowe46031a2018-07-02 18:41:15 +00003942 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003943{
Marshall Clowe46031a2018-07-02 18:41:15 +00003944 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003945 return compare(__pos1, __n1, __sv.data(), __sv.size());
3946}
3947
3948template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003949inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003950int
3951basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3952 size_type __n1,
3953 const basic_string& __str) const
3954{
3955 return compare(__pos1, __n1, __str.data(), __str.size());
3956}
3957
3958template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003959template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003960__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003961<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003962 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3963 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003964 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003965>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003966basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3967 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003968 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003969 size_type __pos2,
3970 size_type __n2) const
3971{
Marshall Clow82513342016-09-24 22:45:42 +00003972 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003973 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3974}
3975
3976template <class _CharT, class _Traits, class _Allocator>
3977int
3978basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3979 size_type __n1,
3980 const basic_string& __str,
3981 size_type __pos2,
3982 size_type __n2) const
3983{
3984 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3985}
3986
3987template <class _CharT, class _Traits, class _Allocator>
3988int
3989basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3990{
3991 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3992 return compare(0, npos, __s, traits_type::length(__s));
3993}
3994
3995template <class _CharT, class _Traits, class _Allocator>
3996int
3997basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3998 size_type __n1,
3999 const value_type* __s) const
4000{
4001 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4002 return compare(__pos1, __n1, __s, traits_type::length(__s));
4003}
4004
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005// __invariants
4006
4007template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004008inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009bool
4010basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4011{
4012 if (size() > capacity())
4013 return false;
4014 if (capacity() < __min_cap - 1)
4015 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004016 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004018 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019 return false;
4020 return true;
4021}
4022
Vedant Kumar55e007e2018-03-08 21:15:26 +00004023// __clear_and_shrink
4024
4025template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004026inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004027void
Marshall Clowe60a7182018-05-29 17:04:37 +00004028basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004029{
4030 clear();
4031 if(__is_long())
4032 {
4033 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4034 __set_long_cap(0);
4035 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004036 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004037 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004038}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004039
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040// operator==
4041
4042template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044bool
4045operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004046 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004048 size_t __lhs_sz = __lhs.size();
4049 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4050 __rhs.data(),
4051 __lhs_sz) == 0;
4052}
4053
4054template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004055inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004056bool
4057operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4058 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4059{
4060 size_t __lhs_sz = __lhs.size();
4061 if (__lhs_sz != __rhs.size())
4062 return false;
4063 const char* __lp = __lhs.data();
4064 const char* __rp = __rhs.data();
4065 if (__lhs.__is_long())
4066 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4067 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4068 if (*__lp != *__rp)
4069 return false;
4070 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071}
4072
4073template<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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004076operator==(const _CharT* __lhs,
4077 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004079 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4080 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4081 size_t __lhs_len = _Traits::length(__lhs);
4082 if (__lhs_len != __rhs.size()) return false;
4083 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084}
4085
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004087inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004089operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4090 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004092 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4093 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4094 size_t __rhs_len = _Traits::length(__rhs);
4095 if (__rhs_len != __lhs.size()) return false;
4096 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097}
4098
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004099template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004100inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101bool
4102operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004103 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104{
4105 return !(__lhs == __rhs);
4106}
4107
4108template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004109inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004111operator!=(const _CharT* __lhs,
4112 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113{
4114 return !(__lhs == __rhs);
4115}
4116
4117template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004118inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004120operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4121 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122{
4123 return !(__lhs == __rhs);
4124}
4125
4126// operator<
4127
4128template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004129inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130bool
4131operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004132 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004134 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135}
4136
4137template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004138inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004140operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4141 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004143 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144}
4145
4146template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004147inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004148bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004149operator< (const _CharT* __lhs,
4150 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151{
4152 return __rhs.compare(__lhs) > 0;
4153}
4154
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155// operator>
4156
4157template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004158inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159bool
4160operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004161 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162{
4163 return __rhs < __lhs;
4164}
4165
4166template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004167inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004169operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4170 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171{
4172 return __rhs < __lhs;
4173}
4174
4175template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004176inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004178operator> (const _CharT* __lhs,
4179 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180{
4181 return __rhs < __lhs;
4182}
4183
4184// operator<=
4185
4186template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004187inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188bool
4189operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004190 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191{
4192 return !(__rhs < __lhs);
4193}
4194
4195template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004196inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004198operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4199 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200{
4201 return !(__rhs < __lhs);
4202}
4203
4204template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004205inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004207operator<=(const _CharT* __lhs,
4208 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209{
4210 return !(__rhs < __lhs);
4211}
4212
4213// operator>=
4214
4215template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004216inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217bool
4218operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004219 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220{
4221 return !(__lhs < __rhs);
4222}
4223
4224template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004227operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4228 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229{
4230 return !(__lhs < __rhs);
4231}
4232
4233template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004234inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004236operator>=(const _CharT* __lhs,
4237 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238{
4239 return !(__lhs < __rhs);
4240}
4241
4242// operator +
4243
4244template<class _CharT, class _Traits, class _Allocator>
4245basic_string<_CharT, _Traits, _Allocator>
4246operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4247 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4248{
4249 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4250 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4251 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4252 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4253 __r.append(__rhs.data(), __rhs_sz);
4254 return __r;
4255}
4256
4257template<class _CharT, class _Traits, class _Allocator>
4258basic_string<_CharT, _Traits, _Allocator>
4259operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4260{
4261 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4262 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4263 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4264 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4265 __r.append(__rhs.data(), __rhs_sz);
4266 return __r;
4267}
4268
4269template<class _CharT, class _Traits, class _Allocator>
4270basic_string<_CharT, _Traits, _Allocator>
4271operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4272{
4273 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4274 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4275 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4276 __r.append(__rhs.data(), __rhs_sz);
4277 return __r;
4278}
4279
4280template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004281inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282basic_string<_CharT, _Traits, _Allocator>
4283operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4284{
4285 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4286 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4287 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4288 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4289 __r.append(__rhs, __rhs_sz);
4290 return __r;
4291}
4292
4293template<class _CharT, class _Traits, class _Allocator>
4294basic_string<_CharT, _Traits, _Allocator>
4295operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4296{
4297 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4298 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4299 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4300 __r.push_back(__rhs);
4301 return __r;
4302}
4303
Eric Fiselierfc92be82017-04-19 00:28:44 +00004304#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305
4306template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004307inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308basic_string<_CharT, _Traits, _Allocator>
4309operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4310{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004311 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004312}
4313
4314template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004315inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004316basic_string<_CharT, _Traits, _Allocator>
4317operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4318{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004319 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004320}
4321
4322template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004323inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324basic_string<_CharT, _Traits, _Allocator>
4325operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4326{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004327 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004328}
4329
4330template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004331inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004332basic_string<_CharT, _Traits, _Allocator>
4333operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4334{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004335 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004336}
4337
4338template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004339inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340basic_string<_CharT, _Traits, _Allocator>
4341operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4342{
4343 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004344 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345}
4346
4347template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004348inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349basic_string<_CharT, _Traits, _Allocator>
4350operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4351{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004352 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004353}
4354
4355template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004356inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357basic_string<_CharT, _Traits, _Allocator>
4358operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4359{
4360 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004361 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004362}
4363
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004364#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365
4366// swap
4367
4368template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004369inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004370void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004371swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004372 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4373 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004374{
4375 __lhs.swap(__rhs);
4376}
4377
Bruce Mitchener170d8972020-11-24 12:53:53 -05004378_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4379_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4380_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4381_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4382_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004383
Bruce Mitchener170d8972020-11-24 12:53:53 -05004384_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4385_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4386_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004387
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004388_LIBCPP_FUNC_VIS string to_string(int __val);
4389_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4390_LIBCPP_FUNC_VIS string to_string(long __val);
4391_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4392_LIBCPP_FUNC_VIS string to_string(long long __val);
4393_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4394_LIBCPP_FUNC_VIS string to_string(float __val);
4395_LIBCPP_FUNC_VIS string to_string(double __val);
4396_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004397
Bruce Mitchener170d8972020-11-24 12:53:53 -05004398_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4399_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4400_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4401_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4402_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004403
Bruce Mitchener170d8972020-11-24 12:53:53 -05004404_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4405_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4406_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004407
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004408_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4409_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4410_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4411_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4412_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4413_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4414_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4415_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4416_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004417
Howard Hinnantc51e1022010-05-11 19:42:16 +00004418template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004419_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004420const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4421 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422
Marshall Clow851b9ec2019-05-20 21:56:51 +00004423template <class _CharT, class _Allocator>
4424struct _LIBCPP_TEMPLATE_VIS
4425 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4426 : public unary_function<
4427 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428{
4429 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004430 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4431 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432};
4433
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434
Howard Hinnantdc095972011-07-18 15:51:59 +00004435template<class _CharT, class _Traits, class _Allocator>
4436basic_ostream<_CharT, _Traits>&
4437operator<<(basic_ostream<_CharT, _Traits>& __os,
4438 const basic_string<_CharT, _Traits, _Allocator>& __str);
4439
4440template<class _CharT, class _Traits, class _Allocator>
4441basic_istream<_CharT, _Traits>&
4442operator>>(basic_istream<_CharT, _Traits>& __is,
4443 basic_string<_CharT, _Traits, _Allocator>& __str);
4444
4445template<class _CharT, class _Traits, class _Allocator>
4446basic_istream<_CharT, _Traits>&
4447getline(basic_istream<_CharT, _Traits>& __is,
4448 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4449
4450template<class _CharT, class _Traits, class _Allocator>
4451inline _LIBCPP_INLINE_VISIBILITY
4452basic_istream<_CharT, _Traits>&
4453getline(basic_istream<_CharT, _Traits>& __is,
4454 basic_string<_CharT, _Traits, _Allocator>& __str);
4455
Howard Hinnantdc095972011-07-18 15:51:59 +00004456template<class _CharT, class _Traits, class _Allocator>
4457inline _LIBCPP_INLINE_VISIBILITY
4458basic_istream<_CharT, _Traits>&
4459getline(basic_istream<_CharT, _Traits>&& __is,
4460 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4461
4462template<class _CharT, class _Traits, class _Allocator>
4463inline _LIBCPP_INLINE_VISIBILITY
4464basic_istream<_CharT, _Traits>&
4465getline(basic_istream<_CharT, _Traits>&& __is,
4466 basic_string<_CharT, _Traits, _Allocator>& __str);
4467
Marshall Clow29b53f22018-12-14 18:49:35 +00004468#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004469template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004470inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004471 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4472 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4473 auto __old_size = __str.size();
4474 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4475 return __old_size - __str.size();
4476}
Marshall Clow29b53f22018-12-14 18:49:35 +00004477
Marek Kurdeja98b1412020-05-02 13:58:03 +02004478template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004479inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004480 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4481 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4482 _Predicate __pred) {
4483 auto __old_size = __str.size();
4484 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4485 __str.end());
4486 return __old_size - __str.size();
4487}
Marshall Clow29b53f22018-12-14 18:49:35 +00004488#endif
4489
Louis Dionneba400782020-10-02 15:02:52 -04004490#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004491
4492template<class _CharT, class _Traits, class _Allocator>
4493bool
4494basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4495{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004496 return this->data() <= _VSTD::__to_address(__i->base()) &&
4497 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004498}
4499
4500template<class _CharT, class _Traits, class _Allocator>
4501bool
4502basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4503{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004504 return this->data() < _VSTD::__to_address(__i->base()) &&
4505 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004506}
4507
4508template<class _CharT, class _Traits, class _Allocator>
4509bool
4510basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4511{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004512 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004513 return this->data() <= __p && __p <= this->data() + this->size();
4514}
4515
4516template<class _CharT, class _Traits, class _Allocator>
4517bool
4518basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4519{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004520 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004521 return this->data() <= __p && __p < this->data() + this->size();
4522}
4523
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004524#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004525
Louis Dionne173f29e2019-05-29 16:01:36 +00004526#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004527// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004528inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004529{
4530 inline namespace string_literals
4531 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004532 inline _LIBCPP_INLINE_VISIBILITY
4533 basic_string<char> operator "" s( const char *__str, size_t __len )
4534 {
4535 return basic_string<char> (__str, __len);
4536 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004537
Howard Hinnant5c167562013-08-07 19:39:48 +00004538 inline _LIBCPP_INLINE_VISIBILITY
4539 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4540 {
4541 return basic_string<wchar_t> (__str, __len);
4542 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004543
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004544#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004545 inline _LIBCPP_INLINE_VISIBILITY
4546 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4547 {
4548 return basic_string<char8_t> (__str, __len);
4549 }
4550#endif
4551
Howard Hinnant5c167562013-08-07 19:39:48 +00004552 inline _LIBCPP_INLINE_VISIBILITY
4553 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4554 {
4555 return basic_string<char16_t> (__str, __len);
4556 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004557
Howard Hinnant5c167562013-08-07 19:39:48 +00004558 inline _LIBCPP_INLINE_VISIBILITY
4559 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4560 {
4561 return basic_string<char32_t> (__str, __len);
4562 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004563 }
4564}
4565#endif
4566
Howard Hinnantc51e1022010-05-11 19:42:16 +00004567_LIBCPP_END_NAMESPACE_STD
4568
Eric Fiselierf4433a32017-05-31 22:07:49 +00004569_LIBCPP_POP_MACROS
4570
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004571#endif // _LIBCPP_STRING