blob: 543234e40fbdaae57bc284a018ea30dc14ca1816 [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>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000616class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617{
618protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000619 _LIBCPP_NORETURN void __throw_length_error() const;
620 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621};
622
623template <bool __b>
624void
625__basic_string_common<__b>::__throw_length_error() const
626{
Marshall Clow8fea1612016-08-25 15:09:01 +0000627 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000628}
629
630template <bool __b>
631void
632__basic_string_common<__b>::__throw_out_of_range() const
633{
Marshall Clow8fea1612016-08-25 15:09:01 +0000634 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635}
636
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000637_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638
Marshall Clow039b2f02016-01-13 21:54:34 +0000639template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400640struct __string_is_trivial_iterator : public false_type {};
641
642template <class _Tp>
643struct __string_is_trivial_iterator<_Tp*>
644 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000645
Louis Dionne173f29e2019-05-29 16:01:36 +0000646template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400647struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
648 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000649
Marshall Clow82513342016-09-24 22:45:42 +0000650template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500651struct __can_be_converted_to_string_view : public _BoolConstant<
652 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
653 !is_convertible<const _Tp&, const _CharT*>::value
654 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000655
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000656#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000657
658template <class _CharT, size_t = sizeof(_CharT)>
659struct __padding
660{
661 unsigned char __xx[sizeof(_CharT)-1];
662};
663
664template <class _CharT>
665struct __padding<_CharT, 1>
666{
667};
668
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400669#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000670
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400671#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800672typedef basic_string<char8_t> u8string;
673#endif
674
675#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
676typedef basic_string<char16_t> u16string;
677typedef basic_string<char32_t> u32string;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400678#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Richard Smith256954d2020-11-11 17:12:18 -0800679
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000680template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800681class
682 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400683#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800684 _LIBCPP_PREFERRED_NAME(u8string)
685#endif
686#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
687 _LIBCPP_PREFERRED_NAME(u16string)
688 _LIBCPP_PREFERRED_NAME(u32string)
689#endif
690 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000691 : private __basic_string_common<true>
692{
693public:
694 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000695 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000697 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000699 typedef allocator_traits<allocator_type> __alloc_traits;
700 typedef typename __alloc_traits::size_type size_type;
701 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000702 typedef value_type& reference;
703 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000704 typedef typename __alloc_traits::pointer pointer;
705 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706
Marshall Clow79f33542018-03-21 00:36:05 +0000707 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
708 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
709 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
710 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000711 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000712 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000713 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000714
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715 typedef __wrap_iter<pointer> iterator;
716 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000717 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
718 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719
720private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000721
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000722#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000723
724 struct __long
725 {
726 pointer __data_;
727 size_type __size_;
728 size_type __cap_;
729 };
730
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000731#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000732 static const size_type __short_mask = 0x01;
733 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000734#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000735 static const size_type __short_mask = 0x80;
736 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400737#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000738
739 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
740 (sizeof(__long) - 1)/sizeof(value_type) : 2};
741
742 struct __short
743 {
744 value_type __data_[__min_cap];
745 struct
746 : __padding<value_type>
747 {
748 unsigned char __size_;
749 };
750 };
751
752#else
753
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754 struct __long
755 {
756 size_type __cap_;
757 size_type __size_;
758 pointer __data_;
759 };
760
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000761#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000762 static const size_type __short_mask = 0x80;
763 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000764#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000765 static const size_type __short_mask = 0x01;
766 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400767#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
770 (sizeof(__long) - 1)/sizeof(value_type) : 2};
771
772 struct __short
773 {
774 union
775 {
776 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000777 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778 };
779 value_type __data_[__min_cap];
780 };
781
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400782#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000783
Howard Hinnant8ea98242013-08-23 17:37:05 +0000784 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785
Howard Hinnant8ea98242013-08-23 17:37:05 +0000786 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787
788 struct __raw
789 {
790 size_type __words[__n_words];
791 };
792
793 struct __rep
794 {
795 union
796 {
797 __long __l;
798 __short __s;
799 __raw __r;
800 };
801 };
802
803 __compressed_pair<__rep, allocator_type> __r_;
804
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200806 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807 static const size_type npos = -1;
808
Howard Hinnant3e276872011-06-03 18:40:47 +0000809 _LIBCPP_INLINE_VISIBILITY basic_string()
810 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000811
812 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
813#if _LIBCPP_STD_VER <= 14
814 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
815#else
816 _NOEXCEPT;
817#endif
818
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819 basic_string(const basic_string& __str);
820 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000821
Eric Fiselierfc92be82017-04-19 00:28:44 +0000822#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000824 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000825#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000826 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000827#else
828 _NOEXCEPT;
829#endif
830
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400833#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000834
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500835 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000836 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500837 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000838 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
839 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400840# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000841 __get_db()->__insert_c(this);
842# endif
843 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000844
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500845 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000846 _LIBCPP_INLINE_VISIBILITY
847 basic_string(const _CharT* __s, const _Allocator& __a);
848
Marek Kurdej90d79712021-07-27 16:16:21 +0200849#if _LIBCPP_STD_VER > 20
850 basic_string(nullptr_t) = delete;
851#endif
852
Howard Hinnantcf823322010-12-17 14:46:43 +0000853 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000854 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000855 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000856 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000857 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000858 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000859
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500860 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000861 _LIBCPP_INLINE_VISIBILITY
862 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
863
Marshall Clow83445802016-04-07 18:13:41 +0000864 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000865 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000866 _LIBCPP_INLINE_VISIBILITY
867 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000868 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000869
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500870 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000871 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000872 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500873 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000874
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500875 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
876 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000877 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
878 explicit basic_string(const _Tp& __t);
879
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500880 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000881 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
882 explicit basic_string(const _Tp& __t, const allocator_type& __a);
883
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500884 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500887 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000890#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000891 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000892 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000893 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000894 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400895#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000897 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000899 _LIBCPP_INLINE_VISIBILITY
900 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
901
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000902 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000903
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500904 template <class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000905 basic_string& operator=(const _Tp& __t)
906 {__self_view __sv = __t; return assign(__sv);}
907
Eric Fiselierfc92be82017-04-19 00:28:44 +0000908#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000910 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000911 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000912 _LIBCPP_INLINE_VISIBILITY
913 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000915 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200916#if _LIBCPP_STD_VER > 20
917 basic_string& operator=(nullptr_t) = delete;
918#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920
Louis Dionneba400782020-10-02 15:02:52 -0400921#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000922 _LIBCPP_INLINE_VISIBILITY
923 iterator begin() _NOEXCEPT
924 {return iterator(this, __get_pointer());}
925 _LIBCPP_INLINE_VISIBILITY
926 const_iterator begin() const _NOEXCEPT
927 {return const_iterator(this, __get_pointer());}
928 _LIBCPP_INLINE_VISIBILITY
929 iterator end() _NOEXCEPT
930 {return iterator(this, __get_pointer() + size());}
931 _LIBCPP_INLINE_VISIBILITY
932 const_iterator end() const _NOEXCEPT
933 {return const_iterator(this, __get_pointer() + size());}
934#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000935 _LIBCPP_INLINE_VISIBILITY
936 iterator begin() _NOEXCEPT
937 {return iterator(__get_pointer());}
938 _LIBCPP_INLINE_VISIBILITY
939 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000940 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000941 _LIBCPP_INLINE_VISIBILITY
942 iterator end() _NOEXCEPT
943 {return iterator(__get_pointer() + size());}
944 _LIBCPP_INLINE_VISIBILITY
945 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000946 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400947#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000948 _LIBCPP_INLINE_VISIBILITY
949 reverse_iterator rbegin() _NOEXCEPT
950 {return reverse_iterator(end());}
951 _LIBCPP_INLINE_VISIBILITY
952 const_reverse_iterator rbegin() const _NOEXCEPT
953 {return const_reverse_iterator(end());}
954 _LIBCPP_INLINE_VISIBILITY
955 reverse_iterator rend() _NOEXCEPT
956 {return reverse_iterator(begin());}
957 _LIBCPP_INLINE_VISIBILITY
958 const_reverse_iterator rend() const _NOEXCEPT
959 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000961 _LIBCPP_INLINE_VISIBILITY
962 const_iterator cbegin() const _NOEXCEPT
963 {return begin();}
964 _LIBCPP_INLINE_VISIBILITY
965 const_iterator cend() const _NOEXCEPT
966 {return end();}
967 _LIBCPP_INLINE_VISIBILITY
968 const_reverse_iterator crbegin() const _NOEXCEPT
969 {return rbegin();}
970 _LIBCPP_INLINE_VISIBILITY
971 const_reverse_iterator crend() const _NOEXCEPT
972 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000974 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000976 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
977 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
978 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000979 {return (__is_long() ? __get_long_cap()
980 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981
982 void resize(size_type __n, value_type __c);
983 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
984
Marek Kurdejc9848142020-11-26 10:07:16 +0100985 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000986 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
987
Marek Kurdejc9848142020-11-26 10:07:16 +0100988 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
989 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000990 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100991 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000993 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000994 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
995 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000997 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
998 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999
1000 const_reference at(size_type __n) const;
1001 reference at(size_type __n);
1002
1003 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +00001004
1005 template <class _Tp>
1006 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001007 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001008 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001009 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1010 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001011 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001012 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001013 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001014 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001016#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001018#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019
Howard Hinnantcf823322010-12-17 14:46:43 +00001020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001022
1023 template <class _Tp>
1024 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001025 _EnableIf<
1026 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1027 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001028 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001029 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001030 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001031 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001032
Marshall Clow62953962016-10-03 23:40:48 +00001033 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001034 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001035 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001036 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001037 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1038 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001039 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001040 >
Marshall Clow82513342016-09-24 22:45:42 +00001041 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001042 basic_string& append(const value_type* __s, size_type __n);
1043 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001045
1046 _LIBCPP_INLINE_VISIBILITY
1047 void __append_default_init(size_type __n);
1048
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001050 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001051 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001053 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001055 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001056 _LIBCPP_INLINE_VISIBILITY
1057 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001058 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001059 append(__temp.data(), __temp.size());
1060 return *this;
1061 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001063 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001064 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001066 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001068 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001069 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001070 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001071
Eric Fiselierfc92be82017-04-19 00:28:44 +00001072#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001075#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076
1077 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001080 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1081 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1082 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1083 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084
Marshall Clowe46031a2018-07-02 18:41:15 +00001085 template <class _Tp>
1086 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001087 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001088 <
1089 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1090 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001091 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001092 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001093 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001094 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001095#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001096 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001097 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001098 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001099 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001100#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001101 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001102 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001103 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001104 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001105 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1107 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001108 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001109 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001110 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001111 basic_string& assign(const value_type* __s, size_type __n);
1112 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113 basic_string& assign(size_type __n, value_type __c);
1114 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001115 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001116 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001118 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001120 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 assign(_InputIterator __first, _InputIterator __last);
1122 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001123 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001124 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001126 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001128 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001130#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001133#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134
Howard Hinnantcf823322010-12-17 14:46:43 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001137
1138 template <class _Tp>
1139 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001140 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001141 <
1142 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1143 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001144 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001145 insert(size_type __pos1, const _Tp& __t)
1146 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1147
Marshall Clow82513342016-09-24 22:45:42 +00001148 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001149 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001150 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001151 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001152 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001153 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001154 >
Marshall Clow82513342016-09-24 22:45:42 +00001155 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001156 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001157 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1158 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1160 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1163 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001164 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001165 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001167 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001169 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1171 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001172 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001173 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001175 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001177 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001179#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1182 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001183#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184
1185 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 iterator erase(const_iterator __first, const_iterator __last);
1190
Howard Hinnantcf823322010-12-17 14:46:43 +00001191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001193
1194 template <class _Tp>
1195 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001196 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001197 <
1198 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1199 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001200 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001201 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 +00001202 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 +00001203 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001204 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001205 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001206 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001207 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001208 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001209 >
Marshall Clow82513342016-09-24 22:45:42 +00001210 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001211 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1212 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001215 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001216
1217 template <class _Tp>
1218 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001219 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001220 <
1221 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1222 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001223 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001224 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1225
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001227 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001229 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001231 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001233 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001234 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001236 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001238 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001239 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001240#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001242 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001244#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
Howard Hinnantd17880b2013-06-28 16:59:19 +00001246 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1249
Howard Hinnantcf823322010-12-17 14:46:43 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001251 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001252#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001253 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001254#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001255 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001256 __is_nothrow_swappable<allocator_type>::value);
1257#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
Howard Hinnantcf823322010-12-17 14:46:43 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001260 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001261 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001262 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001263#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001264 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001265 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001266#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267
Howard Hinnantcf823322010-12-17 14:46:43 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001269 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Howard Hinnantcf823322010-12-17 14:46:43 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001272 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001273
1274 template <class _Tp>
1275 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001276 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001277 <
1278 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1279 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001280 >
zoecarver1997e0a2021-02-05 11:54:47 -08001281 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001282 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001284 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001285 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286
Howard Hinnantcf823322010-12-17 14:46:43 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001288 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001289
1290 template <class _Tp>
1291 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001292 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001293 <
1294 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1295 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001296 >
zoecarver1997e0a2021-02-05 11:54:47 -08001297 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001298 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001301 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302
Howard Hinnantcf823322010-12-17 14:46:43 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001304 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001305
1306 template <class _Tp>
1307 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001308 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001309 <
1310 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1311 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001312 >
zoecarver1997e0a2021-02-05 11:54:47 -08001313 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001314 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001318 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319
Howard Hinnantcf823322010-12-17 14:46:43 +00001320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001321 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001322
1323 template <class _Tp>
1324 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001325 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001326 <
1327 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1328 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001329 >
zoecarver1997e0a2021-02-05 11:54:47 -08001330 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001331 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001335 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336
Howard Hinnantcf823322010-12-17 14:46:43 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001338 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001339
1340 template <class _Tp>
1341 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001342 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001343 <
1344 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1345 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001346 >
zoecarver1997e0a2021-02-05 11:54:47 -08001347 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001348 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 +00001349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001350 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001351 _LIBCPP_INLINE_VISIBILITY
1352 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1353
1354 _LIBCPP_INLINE_VISIBILITY
1355 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001356
1357 template <class _Tp>
1358 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001359 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001360 <
1361 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1362 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001363 >
zoecarver1997e0a2021-02-05 11:54:47 -08001364 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001365 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 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001367 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001368 _LIBCPP_INLINE_VISIBILITY
1369 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1370
1371 _LIBCPP_INLINE_VISIBILITY
1372 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001373
1374 template <class _Tp>
1375 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001376 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001377 <
1378 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1379 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001380 >
zoecarver1997e0a2021-02-05 11:54:47 -08001381 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001382
1383 template <class _Tp>
1384 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001385 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001386 <
1387 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1388 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001389 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001390 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1391
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001394 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 +00001395
Marshall Clow82513342016-09-24 22:45:42 +00001396 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001397 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001398 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001399 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001400 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001401 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001402 >
Marshall Clow82513342016-09-24 22:45:42 +00001403 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 +00001404 int compare(const value_type* __s) const _NOEXCEPT;
1405 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1406 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407
Marshall Clow18c293b2017-12-04 20:11:38 +00001408#if _LIBCPP_STD_VER > 17
1409 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1410 bool starts_with(__self_view __sv) const _NOEXCEPT
1411 { return __self_view(data(), size()).starts_with(__sv); }
1412
1413 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1414 bool starts_with(value_type __c) const _NOEXCEPT
1415 { return !empty() && _Traits::eq(front(), __c); }
1416
1417 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1418 bool starts_with(const value_type* __s) const _NOEXCEPT
1419 { return starts_with(__self_view(__s)); }
1420
1421 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1422 bool ends_with(__self_view __sv) const _NOEXCEPT
1423 { return __self_view(data(), size()).ends_with( __sv); }
1424
1425 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1426 bool ends_with(value_type __c) const _NOEXCEPT
1427 { return !empty() && _Traits::eq(back(), __c); }
1428
1429 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1430 bool ends_with(const value_type* __s) const _NOEXCEPT
1431 { return ends_with(__self_view(__s)); }
1432#endif
1433
Wim Leflere023c3542021-01-19 14:33:30 -05001434#if _LIBCPP_STD_VER > 20
1435 constexpr _LIBCPP_INLINE_VISIBILITY
1436 bool contains(__self_view __sv) const noexcept
1437 { return __self_view(data(), size()).contains(__sv); }
1438
1439 constexpr _LIBCPP_INLINE_VISIBILITY
1440 bool contains(value_type __c) const noexcept
1441 { return __self_view(data(), size()).contains(__c); }
1442
1443 constexpr _LIBCPP_INLINE_VISIBILITY
1444 bool contains(const value_type* __s) const
1445 { return __self_view(data(), size()).contains(__s); }
1446#endif
1447
Howard Hinnantcf823322010-12-17 14:46:43 +00001448 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001449
Marshall Clowe60a7182018-05-29 17:04:37 +00001450 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001451
Marek Kurdejc9848142020-11-26 10:07:16 +01001452 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1453
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001454 _LIBCPP_INLINE_VISIBILITY
1455 bool __is_long() const _NOEXCEPT
1456 {return bool(__r_.first().__s.__size_ & __short_mask);}
1457
Louis Dionneba400782020-10-02 15:02:52 -04001458#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001459
1460 bool __dereferenceable(const const_iterator* __i) const;
1461 bool __decrementable(const const_iterator* __i) const;
1462 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1463 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1464
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001465#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001466
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001468 _LIBCPP_INLINE_VISIBILITY
1469 allocator_type& __alloc() _NOEXCEPT
1470 {return __r_.second();}
1471 _LIBCPP_INLINE_VISIBILITY
1472 const allocator_type& __alloc() const _NOEXCEPT
1473 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001475#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001476
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001478 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001479# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001481# else
1482 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1483# endif
1484
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001485 _LIBCPP_INLINE_VISIBILITY
1486 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001487# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001489# else
1490 {return __r_.first().__s.__size_;}
1491# endif
1492
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001493#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001494
1495 _LIBCPP_INLINE_VISIBILITY
1496 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001497# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001498 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1499# else
1500 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1501# endif
1502
1503 _LIBCPP_INLINE_VISIBILITY
1504 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001505# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001506 {return __r_.first().__s.__size_;}
1507# else
1508 {return __r_.first().__s.__size_ >> 1;}
1509# endif
1510
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001511#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001512
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001513 _LIBCPP_INLINE_VISIBILITY
1514 void __set_long_size(size_type __s) _NOEXCEPT
1515 {__r_.first().__l.__size_ = __s;}
1516 _LIBCPP_INLINE_VISIBILITY
1517 size_type __get_long_size() const _NOEXCEPT
1518 {return __r_.first().__l.__size_;}
1519 _LIBCPP_INLINE_VISIBILITY
1520 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1522
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001523 _LIBCPP_INLINE_VISIBILITY
1524 void __set_long_cap(size_type __s) _NOEXCEPT
1525 {__r_.first().__l.__cap_ = __long_mask | __s;}
1526 _LIBCPP_INLINE_VISIBILITY
1527 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001528 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001530 _LIBCPP_INLINE_VISIBILITY
1531 void __set_long_pointer(pointer __p) _NOEXCEPT
1532 {__r_.first().__l.__data_ = __p;}
1533 _LIBCPP_INLINE_VISIBILITY
1534 pointer __get_long_pointer() _NOEXCEPT
1535 {return __r_.first().__l.__data_;}
1536 _LIBCPP_INLINE_VISIBILITY
1537 const_pointer __get_long_pointer() const _NOEXCEPT
1538 {return __r_.first().__l.__data_;}
1539 _LIBCPP_INLINE_VISIBILITY
1540 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001541 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001542 _LIBCPP_INLINE_VISIBILITY
1543 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001544 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001545 _LIBCPP_INLINE_VISIBILITY
1546 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001548 _LIBCPP_INLINE_VISIBILITY
1549 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1551
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001552 _LIBCPP_INLINE_VISIBILITY
1553 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 {
1555 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1556 for (unsigned __i = 0; __i < __n_words; ++__i)
1557 __a[__i] = 0;
1558 }
1559
1560 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001562 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001563 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001565 static _LIBCPP_INLINE_VISIBILITY
1566 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001567 {
1568 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1569 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1570 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1571 if (__guess == __min_cap) ++__guess;
1572 return __guess;
1573 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001575 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001576 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001577 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001578 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001579 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001581
Martijn Vels5e7c9752020-03-04 17:52:46 -05001582 // Slow path for the (inlined) copy constructor for 'long' strings.
1583 // Always externally instantiated and not inlined.
1584 // Requires that __s is zero terminated.
1585 // The main reason for this function to exist is because for unstable, we
1586 // want to allow inlining of the copy constructor. However, we don't want
1587 // to call the __init() functions as those are marked as inline which may
1588 // result in over-aggressive inlining by the compiler, where our aim is
1589 // to only inline the fast path code directly in the ctor.
1590 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1591
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001593 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001594 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001596 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1597 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 __init(_InputIterator __first, _InputIterator __last);
1599
1600 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001601 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001602 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001604 __is_cpp17_forward_iterator<_ForwardIterator>::value
1605 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606 __init(_ForwardIterator __first, _ForwardIterator __last);
1607
1608 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001609 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1611 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001612 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613
Martijn Vels596e3de2020-02-26 15:55:49 -05001614 // __assign_no_alias is invoked for assignment operations where we
1615 // have proof that the input does not alias the current instance.
1616 // For example, operator=(basic_string) performs a 'self' check.
1617 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001618 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001619
Howard Hinnantcf823322010-12-17 14:46:43 +00001620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621 void __erase_to_end(size_type __pos);
1622
Martijn Velsa81fc792020-02-26 13:25:43 -05001623 // __erase_external_with_move is invoked for erase() invocations where
1624 // `n ~= npos`, likely requiring memory moves on the string data.
1625 void __erase_external_with_move(size_type __pos, size_type __n);
1626
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001627 _LIBCPP_INLINE_VISIBILITY
1628 void __copy_assign_alloc(const basic_string& __str)
1629 {__copy_assign_alloc(__str, integral_constant<bool,
1630 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1631
1632 _LIBCPP_INLINE_VISIBILITY
1633 void __copy_assign_alloc(const basic_string& __str, true_type)
1634 {
Marshall Clowf258c202017-01-31 03:40:52 +00001635 if (__alloc() == __str.__alloc())
1636 __alloc() = __str.__alloc();
1637 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001638 {
Marshall Clowf258c202017-01-31 03:40:52 +00001639 if (!__str.__is_long())
1640 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001641 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001642 __alloc() = __str.__alloc();
1643 }
1644 else
1645 {
1646 allocator_type __a = __str.__alloc();
1647 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001648 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001649 __alloc() = _VSTD::move(__a);
1650 __set_long_pointer(__p);
1651 __set_long_cap(__str.__get_long_cap());
1652 __set_long_size(__str.size());
1653 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001654 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001655 }
1656
1657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001658 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001659 {}
1660
Eric Fiselierfc92be82017-04-19 00:28:44 +00001661#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001662 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001663 void __move_assign(basic_string& __str, false_type)
1664 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001666 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001667#if _LIBCPP_STD_VER > 14
1668 _NOEXCEPT;
1669#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001670 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001671#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001672#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001673
1674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001675 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001676 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001677 _NOEXCEPT_(
1678 !__alloc_traits::propagate_on_container_move_assignment::value ||
1679 is_nothrow_move_assignable<allocator_type>::value)
1680 {__move_assign_alloc(__str, integral_constant<bool,
1681 __alloc_traits::propagate_on_container_move_assignment::value>());}
1682
1683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001684 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001685 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1686 {
1687 __alloc() = _VSTD::move(__c.__alloc());
1688 }
1689
1690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001691 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001692 _NOEXCEPT
1693 {}
1694
Martijn Velsda7d94f2020-06-19 14:24:03 -04001695 basic_string& __assign_external(const value_type* __s);
1696 basic_string& __assign_external(const value_type* __s, size_type __n);
1697
1698 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1699 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1700 pointer __p = __is_long()
1701 ? (__set_long_size(__n), __get_long_pointer())
1702 : (__set_short_size(__n), __get_short_pointer());
1703 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1704 traits_type::assign(__p[__n], value_type());
1705 return *this;
1706 }
1707
Howard Hinnantcf823322010-12-17 14:46:43 +00001708 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1709 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001711 template<class _Tp>
1712 _LIBCPP_INLINE_VISIBILITY
1713 bool __addr_in_range(_Tp&& __t) const {
1714 const volatile void *__p = _VSTD::addressof(__t);
1715 return data() <= __p && __p <= data() + size();
1716 }
1717
Louis Dionned24191c2021-08-19 12:39:16 -04001718 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1719 void __throw_length_error() const {
1720#ifndef _LIBCPP_NO_EXCEPTIONS
1721 __basic_string_common<true>::__throw_length_error();
1722#else
1723 _VSTD::abort();
1724#endif
1725 }
1726
1727 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1728 void __throw_out_of_range() const {
1729#ifndef _LIBCPP_NO_EXCEPTIONS
1730 __basic_string_common<true>::__throw_out_of_range();
1731#else
1732 _VSTD::abort();
1733#endif
1734 }
1735
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 friend basic_string operator+<>(const basic_string&, const basic_string&);
1737 friend basic_string operator+<>(const value_type*, const basic_string&);
1738 friend basic_string operator+<>(value_type, const basic_string&);
1739 friend basic_string operator+<>(const basic_string&, const value_type*);
1740 friend basic_string operator+<>(const basic_string&, value_type);
1741};
1742
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001743// These declarations must appear before any functions are implicitly used
1744// so that they have the correct visibility specifier.
1745#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1746_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1747_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1748#else
1749_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1750_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1751#endif
1752
1753
Louis Dionned59f8a52021-08-17 11:59:07 -04001754#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001755template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001756 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001757 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001758 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1759 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001760 >
1761basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1762 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001763
1764template<class _CharT,
1765 class _Traits,
1766 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001767 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001768 >
1769explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1770 -> basic_string<_CharT, _Traits, _Allocator>;
1771
1772template<class _CharT,
1773 class _Traits,
1774 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001775 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001776 class _Sz = typename allocator_traits<_Allocator>::size_type
1777 >
1778basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1779 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001780#endif
1781
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001783inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784void
1785basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1786{
Louis Dionneba400782020-10-02 15:02:52 -04001787#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001788 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001789#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790}
1791
1792template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001793inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001795basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796{
Louis Dionneba400782020-10-02 15:02:52 -04001797#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001798 __c_node* __c = __get_db()->__find_c_and_lock(this);
1799 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001801 const_pointer __new_last = __get_pointer() + __pos;
1802 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001804 --__p;
1805 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1806 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001808 (*__p)->__c_ = nullptr;
1809 if (--__c->end_ != __p)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001810 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001813 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001815#else
1816 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001817#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818}
1819
1820template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001821inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001822basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001823 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001824 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825{
Louis Dionneba400782020-10-02 15:02:52 -04001826#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001827 __get_db()->__insert_c(this);
1828#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 __zero();
1830}
1831
1832template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001833inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001835#if _LIBCPP_STD_VER <= 14
1836 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1837#else
1838 _NOEXCEPT
1839#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001840: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841{
Louis Dionneba400782020-10-02 15:02:52 -04001842#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001843 __get_db()->__insert_c(this);
1844#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 __zero();
1846}
1847
1848template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001849void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1850 size_type __sz,
1851 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852{
1853 if (__reserve > max_size())
1854 this->__throw_length_error();
1855 pointer __p;
1856 if (__reserve < __min_cap)
1857 {
1858 __set_short_size(__sz);
1859 __p = __get_short_pointer();
1860 }
1861 else
1862 {
1863 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001864 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 __set_long_pointer(__p);
1866 __set_long_cap(__cap+1);
1867 __set_long_size(__sz);
1868 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001869 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 traits_type::assign(__p[__sz], value_type());
1871}
1872
1873template <class _CharT, class _Traits, class _Allocator>
1874void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001875basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876{
1877 if (__sz > max_size())
1878 this->__throw_length_error();
1879 pointer __p;
1880 if (__sz < __min_cap)
1881 {
1882 __set_short_size(__sz);
1883 __p = __get_short_pointer();
1884 }
1885 else
1886 {
1887 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001888 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889 __set_long_pointer(__p);
1890 __set_long_cap(__cap+1);
1891 __set_long_size(__sz);
1892 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001893 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 traits_type::assign(__p[__sz], value_type());
1895}
1896
1897template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001898template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001899basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001900 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001902 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001904#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001905 __get_db()->__insert_c(this);
1906#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907}
1908
1909template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001910inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001911basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001912 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001914 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001916#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001917 __get_db()->__insert_c(this);
1918#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919}
1920
1921template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001922inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001923basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001924 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001926 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001928#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001929 __get_db()->__insert_c(this);
1930#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931}
1932
1933template <class _CharT, class _Traits, class _Allocator>
1934basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001935 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936{
1937 if (!__str.__is_long())
1938 __r_.first().__r = __str.__r_.first().__r;
1939 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001940 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1941 __str.__get_long_size());
1942
Louis Dionneba400782020-10-02 15:02:52 -04001943#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001944 __get_db()->__insert_c(this);
1945#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946}
1947
1948template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001949basic_string<_CharT, _Traits, _Allocator>::basic_string(
1950 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001951 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952{
1953 if (!__str.__is_long())
1954 __r_.first().__r = __str.__r_.first().__r;
1955 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001956 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1957 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001958#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001959 __get_db()->__insert_c(this);
1960#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961}
1962
Martijn Vels5e7c9752020-03-04 17:52:46 -05001963template <class _CharT, class _Traits, class _Allocator>
1964void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1965 const value_type* __s, size_type __sz) {
1966 pointer __p;
1967 if (__sz < __min_cap) {
1968 __p = __get_short_pointer();
1969 __set_short_size(__sz);
1970 } else {
1971 if (__sz > max_size())
1972 this->__throw_length_error();
1973 size_t __cap = __recommend(__sz);
1974 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1975 __set_long_pointer(__p);
1976 __set_long_cap(__cap + 1);
1977 __set_long_size(__sz);
1978 }
1979 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1980}
1981
Eric Fiselierfc92be82017-04-19 00:28:44 +00001982#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983
1984template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001985inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001986basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001987#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001988 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001989#else
1990 _NOEXCEPT
1991#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001992 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993{
1994 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001995#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001996 __get_db()->__insert_c(this);
1997 if (__is_long())
1998 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999#endif
2000}
2001
2002template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002003inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002005 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006{
Marshall Clowd2d24692014-07-17 15:32:20 +00002007 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002008 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002009 else
2010 {
2011 __r_.first().__r = __str.__r_.first().__r;
2012 __str.__zero();
2013 }
Louis Dionneba400782020-10-02 15:02:52 -04002014#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002015 __get_db()->__insert_c(this);
2016 if (__is_long())
2017 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018#endif
2019}
2020
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002021#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022
2023template <class _CharT, class _Traits, class _Allocator>
2024void
2025basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2026{
2027 if (__n > max_size())
2028 this->__throw_length_error();
2029 pointer __p;
2030 if (__n < __min_cap)
2031 {
2032 __set_short_size(__n);
2033 __p = __get_short_pointer();
2034 }
2035 else
2036 {
2037 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002038 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039 __set_long_pointer(__p);
2040 __set_long_cap(__cap+1);
2041 __set_long_size(__n);
2042 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002043 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044 traits_type::assign(__p[__n], value_type());
2045}
2046
2047template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002048inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002049basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002050 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051{
2052 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002053#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002054 __get_db()->__insert_c(this);
2055#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056}
2057
2058template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002059template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002060basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002061 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062{
2063 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002064#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002065 __get_db()->__insert_c(this);
2066#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067}
2068
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002070basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2071 size_type __pos, size_type __n,
2072 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002073 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074{
2075 size_type __str_sz = __str.size();
2076 if (__pos > __str_sz)
2077 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002078 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002079#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002080 __get_db()->__insert_c(this);
2081#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082}
2083
2084template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002085inline
Marshall Clow83445802016-04-07 18:13:41 +00002086basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002087 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002088 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002089{
2090 size_type __str_sz = __str.size();
2091 if (__pos > __str_sz)
2092 this->__throw_out_of_range();
2093 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002094#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002095 __get_db()->__insert_c(this);
2096#endif
2097}
2098
2099template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002100template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002101basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002102 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002103 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002104{
Marshall Clowe46031a2018-07-02 18:41:15 +00002105 __self_view __sv0 = __t;
2106 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002107 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002108#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002109 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002110#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002111}
2112
2113template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002114template <class _Tp, class>
2115basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002116 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002117{
Marshall Clowe46031a2018-07-02 18:41:15 +00002118 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002119 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002120#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002121 __get_db()->__insert_c(this);
2122#endif
2123}
2124
2125template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002126template <class _Tp, class>
2127basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002128 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002129{
Marshall Clowe46031a2018-07-02 18:41:15 +00002130 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002131 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002132#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002133 __get_db()->__insert_c(this);
2134#endif
2135}
2136
2137template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002139_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002141 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2142>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2144{
2145 __zero();
2146#ifndef _LIBCPP_NO_EXCEPTIONS
2147 try
2148 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002149#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 for (; __first != __last; ++__first)
2151 push_back(*__first);
2152#ifndef _LIBCPP_NO_EXCEPTIONS
2153 }
2154 catch (...)
2155 {
2156 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002157 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158 throw;
2159 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002160#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161}
2162
2163template <class _CharT, class _Traits, class _Allocator>
2164template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002165_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002167 __is_cpp17_forward_iterator<_ForwardIterator>::value
2168>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2170{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002171 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 if (__sz > max_size())
2173 this->__throw_length_error();
2174 pointer __p;
2175 if (__sz < __min_cap)
2176 {
2177 __set_short_size(__sz);
2178 __p = __get_short_pointer();
2179 }
2180 else
2181 {
2182 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002183 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 __set_long_pointer(__p);
2185 __set_long_cap(__cap+1);
2186 __set_long_size(__sz);
2187 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002188
2189#ifndef _LIBCPP_NO_EXCEPTIONS
2190 try
2191 {
2192#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002193 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 traits_type::assign(*__p, *__first);
2195 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002196#ifndef _LIBCPP_NO_EXCEPTIONS
2197 }
2198 catch (...)
2199 {
2200 if (__is_long())
2201 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2202 throw;
2203 }
2204#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205}
2206
2207template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002208template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002209inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002211 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212{
2213 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002214#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002215 __get_db()->__insert_c(this);
2216#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217}
2218
2219template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002220template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002221inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2223 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002224 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225{
2226 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002227#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002228 __get_db()->__insert_c(this);
2229#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230}
2231
Eric Fiselierfc92be82017-04-19 00:28:44 +00002232#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002233
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002235inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002236basic_string<_CharT, _Traits, _Allocator>::basic_string(
2237 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002238 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239{
2240 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002241#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002242 __get_db()->__insert_c(this);
2243#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244}
2245
2246template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002247inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002248
Eric Fiselier812882b2017-02-17 01:17:10 +00002249basic_string<_CharT, _Traits, _Allocator>::basic_string(
2250 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002251 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252{
2253 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002254#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002255 __get_db()->__insert_c(this);
2256#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257}
2258
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002259#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002260
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2263{
Louis Dionneba400782020-10-02 15:02:52 -04002264#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002265 __get_db()->__erase_c(this);
2266#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002268 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269}
2270
2271template <class _CharT, class _Traits, class _Allocator>
2272void
2273basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2274 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002275 size_type __n_copy, size_type __n_del, size_type __n_add, const value_type* __p_new_stuff)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276{
2277 size_type __ms = max_size();
2278 if (__delta_cap > __ms - __old_cap - 1)
2279 this->__throw_length_error();
2280 pointer __old_p = __get_pointer();
2281 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002282 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002284 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285 __invalidate_all_iterators();
2286 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002287 traits_type::copy(_VSTD::__to_address(__p),
2288 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002290 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2292 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002293 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2294 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002296 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 __set_long_pointer(__p);
2298 __set_long_cap(__cap+1);
2299 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2300 __set_long_size(__old_sz);
2301 traits_type::assign(__p[__old_sz], value_type());
2302}
2303
2304template <class _CharT, class _Traits, class _Allocator>
2305void
2306basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2307 size_type __n_copy, size_type __n_del, size_type __n_add)
2308{
2309 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002310 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311 this->__throw_length_error();
2312 pointer __old_p = __get_pointer();
2313 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002314 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002316 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317 __invalidate_all_iterators();
2318 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002319 traits_type::copy(_VSTD::__to_address(__p),
2320 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2322 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002323 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2324 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002325 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002327 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328 __set_long_pointer(__p);
2329 __set_long_cap(__cap+1);
2330}
2331
2332// assign
2333
2334template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002335template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002336basic_string<_CharT, _Traits, _Allocator>&
2337basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002338 const value_type* __s, size_type __n) {
2339 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2340 if (__n < __cap) {
2341 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2342 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2343 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2344 traits_type::assign(__p[__n], value_type());
2345 __invalidate_iterators_past(__n);
2346 } else {
2347 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2348 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2349 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002350 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002351}
2352
2353template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002355basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2356 const value_type* __s, size_type __n) {
2357 size_type __cap = capacity();
2358 if (__cap >= __n) {
2359 value_type* __p = _VSTD::__to_address(__get_pointer());
2360 traits_type::move(__p, __s, __n);
2361 traits_type::assign(__p[__n], value_type());
2362 __set_size(__n);
2363 __invalidate_iterators_past(__n);
2364 } else {
2365 size_type __sz = size();
2366 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2367 }
2368 return *this;
2369}
2370
2371template <class _CharT, class _Traits, class _Allocator>
2372basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002373basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002375 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002376 return (_LIBCPP_BUILTIN_CONSTANT_P(__n) && __n < __min_cap)
2377 ? __assign_short(__s, __n)
2378 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379}
2380
2381template <class _CharT, class _Traits, class _Allocator>
2382basic_string<_CharT, _Traits, _Allocator>&
2383basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2384{
2385 size_type __cap = capacity();
2386 if (__cap < __n)
2387 {
2388 size_type __sz = size();
2389 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2390 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002391 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392 traits_type::assign(__p, __n, __c);
2393 traits_type::assign(__p[__n], value_type());
2394 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002395 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396 return *this;
2397}
2398
2399template <class _CharT, class _Traits, class _Allocator>
2400basic_string<_CharT, _Traits, _Allocator>&
2401basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2402{
2403 pointer __p;
2404 if (__is_long())
2405 {
2406 __p = __get_long_pointer();
2407 __set_long_size(1);
2408 }
2409 else
2410 {
2411 __p = __get_short_pointer();
2412 __set_short_size(1);
2413 }
2414 traits_type::assign(*__p, __c);
2415 traits_type::assign(*++__p, value_type());
2416 __invalidate_iterators_past(1);
2417 return *this;
2418}
2419
2420template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002421basic_string<_CharT, _Traits, _Allocator>&
2422basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2423{
Martijn Vels596e3de2020-02-26 15:55:49 -05002424 if (this != &__str) {
2425 __copy_assign_alloc(__str);
2426 if (!__is_long()) {
2427 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002428 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002429 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002430 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002431 }
2432 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002433 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002434 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002435 }
2436 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002437}
2438
Eric Fiselierfc92be82017-04-19 00:28:44 +00002439#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002440
2441template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002442inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002443void
2444basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002445 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002446{
2447 if (__alloc() != __str.__alloc())
2448 assign(__str);
2449 else
2450 __move_assign(__str, true_type());
2451}
2452
2453template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002454inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002455void
2456basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002457#if _LIBCPP_STD_VER > 14
2458 _NOEXCEPT
2459#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002460 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002461#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002462{
marshall2ed41622019-11-27 07:13:00 -08002463 if (__is_long()) {
2464 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2465 __get_long_cap());
2466#if _LIBCPP_STD_VER <= 14
2467 if (!is_nothrow_move_assignable<allocator_type>::value) {
2468 __set_short_size(0);
2469 traits_type::assign(__get_short_pointer()[0], value_type());
2470 }
2471#endif
2472 }
2473 __move_assign_alloc(__str);
2474 __r_.first() = __str.__r_.first();
2475 __str.__set_short_size(0);
2476 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002477}
2478
2479template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002480inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002481basic_string<_CharT, _Traits, _Allocator>&
2482basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002483 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002484{
2485 __move_assign(__str, integral_constant<bool,
2486 __alloc_traits::propagate_on_container_move_assignment::value>());
2487 return *this;
2488}
2489
2490#endif
2491
2492template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002494_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002496 __is_exactly_cpp17_input_iterator<_InputIterator>::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(_InputIterator __first, _InputIterator __last)
2500{
Marshall Clow958362f2016-09-05 01:54:30 +00002501 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002502 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002503 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504}
2505
2506template <class _CharT, class _Traits, class _Allocator>
2507template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002508_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002510 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002512>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002513basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2514{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002516 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2517 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2518
2519 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2520 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002522 if (__cap < __n)
2523 {
2524 size_type __sz = size();
2525 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2526 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002527 pointer __p = __get_pointer();
2528 for (; __first != __last; ++__first, ++__p)
2529 traits_type::assign(*__p, *__first);
2530 traits_type::assign(*__p, value_type());
2531 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002532 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533 }
2534 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002535 {
2536 const basic_string __temp(__first, __last, __alloc());
2537 assign(__temp.data(), __temp.size());
2538 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539 return *this;
2540}
2541
2542template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543basic_string<_CharT, _Traits, _Allocator>&
2544basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2545{
2546 size_type __sz = __str.size();
2547 if (__pos > __sz)
2548 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002549 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550}
2551
2552template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002553template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002554_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002555<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002556 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2557 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002558 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002559>
Marshall Clow82513342016-09-24 22:45:42 +00002560basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002561{
Marshall Clow82513342016-09-24 22:45:42 +00002562 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002563 size_type __sz = __sv.size();
2564 if (__pos > __sz)
2565 this->__throw_out_of_range();
2566 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2567}
2568
2569
2570template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002572basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2573 return __assign_external(__s, traits_type::length(__s));
2574}
2575
2576template <class _CharT, class _Traits, class _Allocator>
2577basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002578basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002580 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002581 return _LIBCPP_BUILTIN_CONSTANT_P(*__s)
2582 ? (traits_type::length(__s) < __min_cap
2583 ? __assign_short(__s, traits_type::length(__s))
2584 : __assign_external(__s, traits_type::length(__s)))
2585 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587// append
2588
2589template <class _CharT, class _Traits, class _Allocator>
2590basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002591basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002593 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594 size_type __cap = capacity();
2595 size_type __sz = size();
2596 if (__cap - __sz >= __n)
2597 {
2598 if (__n)
2599 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002600 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601 traits_type::copy(__p + __sz, __s, __n);
2602 __sz += __n;
2603 __set_size(__sz);
2604 traits_type::assign(__p[__sz], value_type());
2605 }
2606 }
2607 else
2608 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2609 return *this;
2610}
2611
2612template <class _CharT, class _Traits, class _Allocator>
2613basic_string<_CharT, _Traits, _Allocator>&
2614basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2615{
2616 if (__n)
2617 {
2618 size_type __cap = capacity();
2619 size_type __sz = size();
2620 if (__cap - __sz < __n)
2621 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2622 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002623 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 __sz += __n;
2625 __set_size(__sz);
2626 traits_type::assign(__p[__sz], value_type());
2627 }
2628 return *this;
2629}
2630
2631template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002632inline void
2633basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2634{
2635 if (__n)
2636 {
2637 size_type __cap = capacity();
2638 size_type __sz = size();
2639 if (__cap - __sz < __n)
2640 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2641 pointer __p = __get_pointer();
2642 __sz += __n;
2643 __set_size(__sz);
2644 traits_type::assign(__p[__sz], value_type());
2645 }
2646}
2647
2648template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649void
2650basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2651{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002652 bool __is_short = !__is_long();
2653 size_type __cap;
2654 size_type __sz;
2655 if (__is_short)
2656 {
2657 __cap = __min_cap - 1;
2658 __sz = __get_short_size();
2659 }
2660 else
2661 {
2662 __cap = __get_long_cap() - 1;
2663 __sz = __get_long_size();
2664 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002666 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002668 __is_short = !__is_long();
2669 }
2670 pointer __p;
2671 if (__is_short)
2672 {
2673 __p = __get_short_pointer() + __sz;
2674 __set_short_size(__sz+1);
2675 }
2676 else
2677 {
2678 __p = __get_long_pointer() + __sz;
2679 __set_long_size(__sz+1);
2680 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681 traits_type::assign(*__p, __c);
2682 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683}
2684
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685template <class _CharT, class _Traits, class _Allocator>
2686template<class _ForwardIterator>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002687_EnableIf
2688<
2689 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2690 basic_string<_CharT, _Traits, _Allocator>&
2691>
2692basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002693 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694{
2695 size_type __sz = size();
2696 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002697 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 if (__n)
2699 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002700 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2701 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002702 {
2703 if (__cap - __sz < __n)
2704 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2705 pointer __p = __get_pointer() + __sz;
2706 for (; __first != __last; ++__p, ++__first)
2707 traits_type::assign(*__p, *__first);
2708 traits_type::assign(*__p, value_type());
2709 __set_size(__sz + __n);
2710 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002711 else
2712 {
2713 const basic_string __temp(__first, __last, __alloc());
2714 append(__temp.data(), __temp.size());
2715 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716 }
2717 return *this;
2718}
2719
2720template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002721inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722basic_string<_CharT, _Traits, _Allocator>&
2723basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2724{
2725 return append(__str.data(), __str.size());
2726}
2727
2728template <class _CharT, class _Traits, class _Allocator>
2729basic_string<_CharT, _Traits, _Allocator>&
2730basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2731{
2732 size_type __sz = __str.size();
2733 if (__pos > __sz)
2734 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002735 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736}
2737
2738template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002739template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002740 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002741 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002742 __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 +00002743 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002744 >
Marshall Clow82513342016-09-24 22:45:42 +00002745basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002746{
Marshall Clow82513342016-09-24 22:45:42 +00002747 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002748 size_type __sz = __sv.size();
2749 if (__pos > __sz)
2750 this->__throw_out_of_range();
2751 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2752}
2753
2754template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002756basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002758 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759 return append(__s, traits_type::length(__s));
2760}
2761
2762// insert
2763
2764template <class _CharT, class _Traits, class _Allocator>
2765basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002766basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002768 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769 size_type __sz = size();
2770 if (__pos > __sz)
2771 this->__throw_out_of_range();
2772 size_type __cap = capacity();
2773 if (__cap - __sz >= __n)
2774 {
2775 if (__n)
2776 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002777 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 size_type __n_move = __sz - __pos;
2779 if (__n_move != 0)
2780 {
2781 if (__p + __pos <= __s && __s < __p + __sz)
2782 __s += __n;
2783 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2784 }
2785 traits_type::move(__p + __pos, __s, __n);
2786 __sz += __n;
2787 __set_size(__sz);
2788 traits_type::assign(__p[__sz], value_type());
2789 }
2790 }
2791 else
2792 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2793 return *this;
2794}
2795
2796template <class _CharT, class _Traits, class _Allocator>
2797basic_string<_CharT, _Traits, _Allocator>&
2798basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2799{
2800 size_type __sz = size();
2801 if (__pos > __sz)
2802 this->__throw_out_of_range();
2803 if (__n)
2804 {
2805 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002806 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 if (__cap - __sz >= __n)
2808 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002809 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810 size_type __n_move = __sz - __pos;
2811 if (__n_move != 0)
2812 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2813 }
2814 else
2815 {
2816 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002817 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818 }
2819 traits_type::assign(__p + __pos, __n, __c);
2820 __sz += __n;
2821 __set_size(__sz);
2822 traits_type::assign(__p[__sz], value_type());
2823 }
2824 return *this;
2825}
2826
2827template <class _CharT, class _Traits, class _Allocator>
2828template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002829_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002831 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002832 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002833>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2835{
Louis Dionneba400782020-10-02 15:02:52 -04002836#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002837 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2838 "string::insert(iterator, range) called with an iterator not"
2839 " referring to this string");
2840#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002841 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002842 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843}
2844
2845template <class _CharT, class _Traits, class _Allocator>
2846template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002847_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002849 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002851>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2853{
Louis Dionneba400782020-10-02 15:02:52 -04002854#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002855 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2856 "string::insert(iterator, range) called with an iterator not"
2857 " referring to this string");
2858#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002860 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861 if (__n)
2862 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002863 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2864 !__addr_in_range(*__first))
2865 {
2866 size_type __sz = size();
2867 size_type __cap = capacity();
2868 value_type* __p;
2869 if (__cap - __sz >= __n)
2870 {
2871 __p = _VSTD::__to_address(__get_pointer());
2872 size_type __n_move = __sz - __ip;
2873 if (__n_move != 0)
2874 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2875 }
2876 else
2877 {
2878 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2879 __p = _VSTD::__to_address(__get_long_pointer());
2880 }
2881 __sz += __n;
2882 __set_size(__sz);
2883 traits_type::assign(__p[__sz], value_type());
2884 for (__p += __ip; __first != __last; ++__p, ++__first)
2885 traits_type::assign(*__p, *__first);
2886 }
2887 else
Marshall Clow958362f2016-09-05 01:54:30 +00002888 {
2889 const basic_string __temp(__first, __last, __alloc());
2890 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2891 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 }
2893 return begin() + __ip;
2894}
2895
2896template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002897inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898basic_string<_CharT, _Traits, _Allocator>&
2899basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2900{
2901 return insert(__pos1, __str.data(), __str.size());
2902}
2903
2904template <class _CharT, class _Traits, class _Allocator>
2905basic_string<_CharT, _Traits, _Allocator>&
2906basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2907 size_type __pos2, size_type __n)
2908{
2909 size_type __str_sz = __str.size();
2910 if (__pos2 > __str_sz)
2911 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002912 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913}
2914
2915template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002916template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002917_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002918<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002919 __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 +00002920 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002921>
Marshall Clow82513342016-09-24 22:45:42 +00002922basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002923 size_type __pos2, size_type __n)
2924{
Marshall Clow82513342016-09-24 22:45:42 +00002925 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002926 size_type __str_sz = __sv.size();
2927 if (__pos2 > __str_sz)
2928 this->__throw_out_of_range();
2929 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2930}
2931
2932template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002934basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002936 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937 return insert(__pos, __s, traits_type::length(__s));
2938}
2939
2940template <class _CharT, class _Traits, class _Allocator>
2941typename basic_string<_CharT, _Traits, _Allocator>::iterator
2942basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2943{
2944 size_type __ip = static_cast<size_type>(__pos - begin());
2945 size_type __sz = size();
2946 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002947 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948 if (__cap == __sz)
2949 {
2950 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002951 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952 }
2953 else
2954 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002955 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956 size_type __n_move = __sz - __ip;
2957 if (__n_move != 0)
2958 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2959 }
2960 traits_type::assign(__p[__ip], __c);
2961 traits_type::assign(__p[++__sz], value_type());
2962 __set_size(__sz);
2963 return begin() + static_cast<difference_type>(__ip);
2964}
2965
2966template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002967inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968typename basic_string<_CharT, _Traits, _Allocator>::iterator
2969basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2970{
Louis Dionneba400782020-10-02 15:02:52 -04002971#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002972 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2973 "string::insert(iterator, n, value) called with an iterator not"
2974 " referring to this string");
2975#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 difference_type __p = __pos - begin();
2977 insert(static_cast<size_type>(__p), __n, __c);
2978 return begin() + __p;
2979}
2980
2981// replace
2982
2983template <class _CharT, class _Traits, class _Allocator>
2984basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002985basic_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 +00002986 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002988 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989 size_type __sz = size();
2990 if (__pos > __sz)
2991 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002992 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993 size_type __cap = capacity();
2994 if (__cap - __sz + __n1 >= __n2)
2995 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002996 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997 if (__n1 != __n2)
2998 {
2999 size_type __n_move = __sz - __pos - __n1;
3000 if (__n_move != 0)
3001 {
3002 if (__n1 > __n2)
3003 {
3004 traits_type::move(__p + __pos, __s, __n2);
3005 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3006 goto __finish;
3007 }
3008 if (__p + __pos < __s && __s < __p + __sz)
3009 {
3010 if (__p + __pos + __n1 <= __s)
3011 __s += __n2 - __n1;
3012 else // __p + __pos < __s < __p + __pos + __n1
3013 {
3014 traits_type::move(__p + __pos, __s, __n1);
3015 __pos += __n1;
3016 __s += __n2;
3017 __n2 -= __n1;
3018 __n1 = 0;
3019 }
3020 }
3021 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3022 }
3023 }
3024 traits_type::move(__p + __pos, __s, __n2);
3025__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05003026// __sz += __n2 - __n1; in this and the below function below can cause unsigned
3027// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028 __sz += __n2 - __n1;
3029 __set_size(__sz);
3030 __invalidate_iterators_past(__sz);
3031 traits_type::assign(__p[__sz], value_type());
3032 }
3033 else
3034 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3035 return *this;
3036}
3037
3038template <class _CharT, class _Traits, class _Allocator>
3039basic_string<_CharT, _Traits, _Allocator>&
3040basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00003041 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042{
3043 size_type __sz = size();
3044 if (__pos > __sz)
3045 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003046 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003048 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 if (__cap - __sz + __n1 >= __n2)
3050 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003051 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052 if (__n1 != __n2)
3053 {
3054 size_type __n_move = __sz - __pos - __n1;
3055 if (__n_move != 0)
3056 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3057 }
3058 }
3059 else
3060 {
3061 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003062 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063 }
3064 traits_type::assign(__p + __pos, __n2, __c);
3065 __sz += __n2 - __n1;
3066 __set_size(__sz);
3067 __invalidate_iterators_past(__sz);
3068 traits_type::assign(__p[__sz], value_type());
3069 return *this;
3070}
3071
3072template <class _CharT, class _Traits, class _Allocator>
3073template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003074_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003076 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003078>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003079basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080 _InputIterator __j1, _InputIterator __j2)
3081{
Marshall Clow958362f2016-09-05 01:54:30 +00003082 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003083 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084}
3085
3086template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003087inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088basic_string<_CharT, _Traits, _Allocator>&
3089basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3090{
3091 return replace(__pos1, __n1, __str.data(), __str.size());
3092}
3093
3094template <class _CharT, class _Traits, class _Allocator>
3095basic_string<_CharT, _Traits, _Allocator>&
3096basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3097 size_type __pos2, size_type __n2)
3098{
3099 size_type __str_sz = __str.size();
3100 if (__pos2 > __str_sz)
3101 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003102 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103}
3104
3105template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003106template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003107_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003108<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003109 __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 +00003110 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003111>
Marshall Clow82513342016-09-24 22:45:42 +00003112basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003113 size_type __pos2, size_type __n2)
3114{
Marshall Clow82513342016-09-24 22:45:42 +00003115 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003116 size_type __str_sz = __sv.size();
3117 if (__pos2 > __str_sz)
3118 this->__throw_out_of_range();
3119 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3120}
3121
3122template <class _CharT, class _Traits, class _Allocator>
3123basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003124basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003126 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127 return replace(__pos, __n1, __s, traits_type::length(__s));
3128}
3129
3130template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003131inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003133basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134{
3135 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3136 __str.data(), __str.size());
3137}
3138
3139template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003140inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003142basic_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 +00003143{
3144 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3145}
3146
3147template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003148inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003150basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151{
3152 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3153}
3154
3155template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003156inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003158basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159{
3160 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3161}
3162
3163// erase
3164
Martijn Velsa81fc792020-02-26 13:25:43 -05003165// 'externally instantiated' erase() implementation, called when __n != npos.
3166// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003168void
3169basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3170 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172 if (__n)
3173 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003174 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003175 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003176 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177 size_type __n_move = __sz - __pos - __n;
3178 if (__n_move != 0)
3179 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3180 __sz -= __n;
3181 __set_size(__sz);
3182 __invalidate_iterators_past(__sz);
3183 traits_type::assign(__p[__sz], value_type());
3184 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003185}
3186
3187template <class _CharT, class _Traits, class _Allocator>
3188basic_string<_CharT, _Traits, _Allocator>&
3189basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3190 size_type __n) {
3191 if (__pos > size()) this->__throw_out_of_range();
3192 if (__n == npos) {
3193 __erase_to_end(__pos);
3194 } else {
3195 __erase_external_with_move(__pos, __n);
3196 }
3197 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198}
3199
3200template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003201inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202typename basic_string<_CharT, _Traits, _Allocator>::iterator
3203basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3204{
Louis Dionneba400782020-10-02 15:02:52 -04003205#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003206 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3207 "string::erase(iterator) called with an iterator not"
3208 " referring to this string");
3209#endif
3210 _LIBCPP_ASSERT(__pos != end(),
3211 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212 iterator __b = begin();
3213 size_type __r = static_cast<size_type>(__pos - __b);
3214 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003215 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216}
3217
3218template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003219inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220typename basic_string<_CharT, _Traits, _Allocator>::iterator
3221basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3222{
Louis Dionneba400782020-10-02 15:02:52 -04003223#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003224 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3225 "string::erase(iterator, iterator) called with an iterator not"
3226 " referring to this string");
3227#endif
3228 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 iterator __b = begin();
3230 size_type __r = static_cast<size_type>(__first - __b);
3231 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003232 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233}
3234
3235template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003236inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237void
3238basic_string<_CharT, _Traits, _Allocator>::pop_back()
3239{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003240 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241 size_type __sz;
3242 if (__is_long())
3243 {
3244 __sz = __get_long_size() - 1;
3245 __set_long_size(__sz);
3246 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3247 }
3248 else
3249 {
3250 __sz = __get_short_size() - 1;
3251 __set_short_size(__sz);
3252 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3253 }
3254 __invalidate_iterators_past(__sz);
3255}
3256
3257template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003258inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003260basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261{
3262 __invalidate_all_iterators();
3263 if (__is_long())
3264 {
3265 traits_type::assign(*__get_long_pointer(), value_type());
3266 __set_long_size(0);
3267 }
3268 else
3269 {
3270 traits_type::assign(*__get_short_pointer(), value_type());
3271 __set_short_size(0);
3272 }
3273}
3274
3275template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003276inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277void
3278basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3279{
3280 if (__is_long())
3281 {
3282 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3283 __set_long_size(__pos);
3284 }
3285 else
3286 {
3287 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3288 __set_short_size(__pos);
3289 }
3290 __invalidate_iterators_past(__pos);
3291}
3292
3293template <class _CharT, class _Traits, class _Allocator>
3294void
3295basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3296{
3297 size_type __sz = size();
3298 if (__n > __sz)
3299 append(__n - __sz, __c);
3300 else
3301 __erase_to_end(__n);
3302}
3303
3304template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003305inline void
3306basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3307{
3308 size_type __sz = size();
3309 if (__n > __sz) {
3310 __append_default_init(__n - __sz);
3311 } else
3312 __erase_to_end(__n);
3313}
3314
3315template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003316inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003317typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003318basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003320 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003321#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003322 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003324 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325#endif
3326}
3327
3328template <class _CharT, class _Traits, class _Allocator>
3329void
Marek Kurdejc9848142020-11-26 10:07:16 +01003330basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331{
Marek Kurdejc9848142020-11-26 10:07:16 +01003332 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003334
3335#if _LIBCPP_STD_VER > 17
3336 // Reserve never shrinks as of C++20.
3337 if (__requested_capacity <= capacity()) return;
3338#endif
3339
3340 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3341 __target_capacity = __recommend(__target_capacity);
3342 if (__target_capacity == capacity()) return;
3343
3344 __shrink_or_extend(__target_capacity);
3345}
3346
3347template <class _CharT, class _Traits, class _Allocator>
3348void
3349basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3350{
3351 size_type __target_capacity = __recommend(size());
3352 if (__target_capacity == capacity()) return;
3353
3354 __shrink_or_extend(__target_capacity);
3355}
3356
3357template <class _CharT, class _Traits, class _Allocator>
3358void
3359basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3360{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361 size_type __cap = capacity();
3362 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003363
3364 pointer __new_data, __p;
3365 bool __was_long, __now_long;
3366 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003368 __was_long = true;
3369 __now_long = false;
3370 __new_data = __get_short_pointer();
3371 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003373 else
3374 {
3375 if (__target_capacity > __cap)
3376 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3377 else
3378 {
3379 #ifndef _LIBCPP_NO_EXCEPTIONS
3380 try
3381 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003382 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003383 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3384 #ifndef _LIBCPP_NO_EXCEPTIONS
3385 }
3386 catch (...)
3387 {
3388 return;
3389 }
3390 #else // _LIBCPP_NO_EXCEPTIONS
3391 if (__new_data == nullptr)
3392 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003393 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003394 }
3395 __now_long = true;
3396 __was_long = __is_long();
3397 __p = __get_pointer();
3398 }
3399 traits_type::copy(_VSTD::__to_address(__new_data),
3400 _VSTD::__to_address(__p), size()+1);
3401 if (__was_long)
3402 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3403 if (__now_long)
3404 {
3405 __set_long_cap(__target_capacity+1);
3406 __set_long_size(__sz);
3407 __set_long_pointer(__new_data);
3408 }
3409 else
3410 __set_short_size(__sz);
3411 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412}
3413
3414template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003415inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003417basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003419 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420 return *(data() + __pos);
3421}
3422
3423template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003424inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003426basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003428 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003429 return *(__get_pointer() + __pos);
3430}
3431
3432template <class _CharT, class _Traits, class _Allocator>
3433typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3434basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3435{
3436 if (__n >= size())
3437 this->__throw_out_of_range();
3438 return (*this)[__n];
3439}
3440
3441template <class _CharT, class _Traits, class _Allocator>
3442typename basic_string<_CharT, _Traits, _Allocator>::reference
3443basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3444{
3445 if (__n >= size())
3446 this->__throw_out_of_range();
3447 return (*this)[__n];
3448}
3449
3450template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003451inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003453basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003455 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456 return *__get_pointer();
3457}
3458
3459template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003460inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003461typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003462basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003464 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465 return *data();
3466}
3467
3468template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003469inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003470typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003471basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003473 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474 return *(__get_pointer() + size() - 1);
3475}
3476
3477template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003478inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003480basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003482 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483 return *(data() + size() - 1);
3484}
3485
3486template <class _CharT, class _Traits, class _Allocator>
3487typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003488basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489{
3490 size_type __sz = size();
3491 if (__pos > __sz)
3492 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003493 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494 traits_type::copy(__s, data() + __pos, __rlen);
3495 return __rlen;
3496}
3497
3498template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003499inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500basic_string<_CharT, _Traits, _Allocator>
3501basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3502{
3503 return basic_string(*this, __pos, __n, __alloc());
3504}
3505
3506template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003507inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508void
3509basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003510#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003511 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003512#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003513 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003514 __is_nothrow_swappable<allocator_type>::value)
3515#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516{
Louis Dionneba400782020-10-02 15:02:52 -04003517#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003518 if (!__is_long())
3519 __get_db()->__invalidate_all(this);
3520 if (!__str.__is_long())
3521 __get_db()->__invalidate_all(&__str);
3522 __get_db()->swap(this, &__str);
3523#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003524 _LIBCPP_ASSERT(
3525 __alloc_traits::propagate_on_container_swap::value ||
3526 __alloc_traits::is_always_equal::value ||
3527 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003528 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003529 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530}
3531
3532// find
3533
3534template <class _Traits>
3535struct _LIBCPP_HIDDEN __traits_eq
3536{
3537 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003538 _LIBCPP_INLINE_VISIBILITY
3539 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3540 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541};
3542
3543template<class _CharT, class _Traits, class _Allocator>
3544typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003545basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003546 size_type __pos,
3547 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003549 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003550 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003551 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552}
3553
3554template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003555inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003557basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3558 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003560 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003561 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562}
3563
3564template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003565template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003566_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003567<
3568 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3569 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003570>
Marshall Clowe46031a2018-07-02 18:41:15 +00003571basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003572 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003573{
Marshall Clowe46031a2018-07-02 18:41:15 +00003574 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003575 return __str_find<value_type, size_type, traits_type, npos>
3576 (data(), size(), __sv.data(), __pos, __sv.size());
3577}
3578
3579template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003580inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003581typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003582basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003583 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003585 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003586 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003587 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588}
3589
3590template<class _CharT, class _Traits, class _Allocator>
3591typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003592basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3593 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003595 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003596 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597}
3598
3599// rfind
3600
3601template<class _CharT, class _Traits, class _Allocator>
3602typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003603basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003604 size_type __pos,
3605 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003607 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003608 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003609 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610}
3611
3612template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003613inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003615basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3616 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003618 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003619 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620}
3621
3622template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003623template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003624_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003625<
3626 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3627 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003628>
Marshall Clowe46031a2018-07-02 18:41:15 +00003629basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003630 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003631{
Marshall Clowe46031a2018-07-02 18:41:15 +00003632 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003633 return __str_rfind<value_type, size_type, traits_type, npos>
3634 (data(), size(), __sv.data(), __pos, __sv.size());
3635}
3636
3637template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003638inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003639typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003640basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003641 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003643 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003644 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003645 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646}
3647
3648template<class _CharT, class _Traits, class _Allocator>
3649typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003650basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3651 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003653 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003654 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655}
3656
3657// find_first_of
3658
3659template<class _CharT, class _Traits, class _Allocator>
3660typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003661basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003662 size_type __pos,
3663 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003665 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003666 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003667 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668}
3669
3670template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003671inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003673basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3674 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003676 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003677 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678}
3679
3680template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003681template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003682_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003683<
3684 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3685 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003686>
Marshall Clowe46031a2018-07-02 18:41:15 +00003687basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003688 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003689{
Marshall Clowe46031a2018-07-02 18:41:15 +00003690 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003691 return __str_find_first_of<value_type, size_type, traits_type, npos>
3692 (data(), size(), __sv.data(), __pos, __sv.size());
3693}
3694
3695template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003696inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003697typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003698basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003699 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003701 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003703 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704}
3705
3706template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003707inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003709basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3710 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711{
3712 return find(__c, __pos);
3713}
3714
3715// find_last_of
3716
3717template<class _CharT, class _Traits, class _Allocator>
3718typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003719basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003720 size_type __pos,
3721 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003723 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003724 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003725 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726}
3727
3728template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003729inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003731basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3732 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003734 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003735 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736}
3737
3738template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003739template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003740_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003741<
3742 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3743 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003744>
Marshall Clowe46031a2018-07-02 18:41:15 +00003745basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003746 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003747{
Marshall Clowe46031a2018-07-02 18:41:15 +00003748 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003749 return __str_find_last_of<value_type, size_type, traits_type, npos>
3750 (data(), size(), __sv.data(), __pos, __sv.size());
3751}
3752
3753template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003754inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003755typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003756basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003757 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003759 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003760 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003761 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762}
3763
3764template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003765inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003767basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3768 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769{
3770 return rfind(__c, __pos);
3771}
3772
3773// find_first_not_of
3774
3775template<class _CharT, class _Traits, class _Allocator>
3776typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003777basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003778 size_type __pos,
3779 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003781 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003782 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003783 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784}
3785
3786template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003787inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003789basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3790 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003792 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003793 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794}
3795
3796template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003797template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003798_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003799<
3800 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3801 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003802>
Marshall Clowe46031a2018-07-02 18:41:15 +00003803basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003804 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003805{
Marshall Clowe46031a2018-07-02 18:41:15 +00003806 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003807 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3808 (data(), size(), __sv.data(), __pos, __sv.size());
3809}
3810
3811template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003812inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003813typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003814basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003815 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003817 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003818 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003819 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820}
3821
3822template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003823inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003825basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3826 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003828 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003829 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830}
3831
3832// find_last_not_of
3833
3834template<class _CharT, class _Traits, class _Allocator>
3835typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003836basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003837 size_type __pos,
3838 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003840 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003841 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003842 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843}
3844
3845template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003846inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003848basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3849 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003850{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003851 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003852 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853}
3854
3855template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003856template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003857_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003858<
3859 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3860 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003861>
Marshall Clowe46031a2018-07-02 18:41:15 +00003862basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003863 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003864{
Marshall Clowe46031a2018-07-02 18:41:15 +00003865 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003866 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3867 (data(), size(), __sv.data(), __pos, __sv.size());
3868}
3869
3870template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003871inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003872typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003873basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003874 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003876 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003877 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003878 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879}
3880
3881template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003882inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003883typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003884basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3885 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003887 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003888 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889}
3890
3891// compare
3892
3893template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003894template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003895_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003896<
3897 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3898 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003899>
zoecarver1997e0a2021-02-05 11:54:47 -08003900basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901{
Marshall Clowe46031a2018-07-02 18:41:15 +00003902 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003903 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003904 size_t __rhs_sz = __sv.size();
3905 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003906 _VSTD::min(__lhs_sz, __rhs_sz));
3907 if (__result != 0)
3908 return __result;
3909 if (__lhs_sz < __rhs_sz)
3910 return -1;
3911 if (__lhs_sz > __rhs_sz)
3912 return 1;
3913 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003914}
3915
3916template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003917inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003919basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003921 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003922}
3923
3924template <class _CharT, class _Traits, class _Allocator>
3925int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003926basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3927 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003928 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003929 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003931 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932 size_type __sz = size();
3933 if (__pos1 > __sz || __n2 == npos)
3934 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003935 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3936 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937 if (__r == 0)
3938 {
3939 if (__rlen < __n2)
3940 __r = -1;
3941 else if (__rlen > __n2)
3942 __r = 1;
3943 }
3944 return __r;
3945}
3946
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003947template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003948template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003949_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003950<
3951 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3952 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003953>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003954basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3955 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003956 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003957{
Marshall Clowe46031a2018-07-02 18:41:15 +00003958 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003959 return compare(__pos1, __n1, __sv.data(), __sv.size());
3960}
3961
3962template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003963inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003964int
3965basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3966 size_type __n1,
3967 const basic_string& __str) const
3968{
3969 return compare(__pos1, __n1, __str.data(), __str.size());
3970}
3971
3972template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003973template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003974_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003975<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003976 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3977 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003978 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003979>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003980basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3981 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003982 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003983 size_type __pos2,
3984 size_type __n2) const
3985{
Marshall Clow82513342016-09-24 22:45:42 +00003986 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003987 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3988}
3989
3990template <class _CharT, class _Traits, class _Allocator>
3991int
3992basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3993 size_type __n1,
3994 const basic_string& __str,
3995 size_type __pos2,
3996 size_type __n2) const
3997{
3998 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3999}
4000
4001template <class _CharT, class _Traits, class _Allocator>
4002int
4003basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4004{
4005 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4006 return compare(0, npos, __s, traits_type::length(__s));
4007}
4008
4009template <class _CharT, class _Traits, class _Allocator>
4010int
4011basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4012 size_type __n1,
4013 const value_type* __s) const
4014{
4015 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4016 return compare(__pos1, __n1, __s, traits_type::length(__s));
4017}
4018
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019// __invariants
4020
4021template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004022inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023bool
4024basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4025{
4026 if (size() > capacity())
4027 return false;
4028 if (capacity() < __min_cap - 1)
4029 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004030 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004032 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033 return false;
4034 return true;
4035}
4036
Vedant Kumar55e007e2018-03-08 21:15:26 +00004037// __clear_and_shrink
4038
4039template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004040inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004041void
Marshall Clowe60a7182018-05-29 17:04:37 +00004042basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004043{
4044 clear();
4045 if(__is_long())
4046 {
4047 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4048 __set_long_cap(0);
4049 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004050 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004051 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004052}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004053
Howard Hinnantc51e1022010-05-11 19:42:16 +00004054// operator==
4055
4056template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004057inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058bool
4059operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004060 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004062 size_t __lhs_sz = __lhs.size();
4063 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4064 __rhs.data(),
4065 __lhs_sz) == 0;
4066}
4067
4068template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004069inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004070bool
4071operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4072 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4073{
4074 size_t __lhs_sz = __lhs.size();
4075 if (__lhs_sz != __rhs.size())
4076 return false;
4077 const char* __lp = __lhs.data();
4078 const char* __rp = __rhs.data();
4079 if (__lhs.__is_long())
4080 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4081 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4082 if (*__lp != *__rp)
4083 return false;
4084 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085}
4086
4087template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004090operator==(const _CharT* __lhs,
4091 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004093 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4094 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4095 size_t __lhs_len = _Traits::length(__lhs);
4096 if (__lhs_len != __rhs.size()) return false;
4097 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098}
4099
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004103operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4104 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004106 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4107 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4108 size_t __rhs_len = _Traits::length(__rhs);
4109 if (__rhs_len != __lhs.size()) return false;
4110 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111}
4112
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004113template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004114inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115bool
4116operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004117 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118{
4119 return !(__lhs == __rhs);
4120}
4121
4122template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004123inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004125operator!=(const _CharT* __lhs,
4126 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127{
4128 return !(__lhs == __rhs);
4129}
4130
4131template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004132inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004134operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4135 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136{
4137 return !(__lhs == __rhs);
4138}
4139
4140// operator<
4141
4142template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004143inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144bool
4145operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004146 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004148 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149}
4150
4151template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004152inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004154operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4155 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004157 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158}
4159
4160template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004161inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004163operator< (const _CharT* __lhs,
4164 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165{
4166 return __rhs.compare(__lhs) > 0;
4167}
4168
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169// operator>
4170
4171template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004172inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173bool
4174operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004175 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176{
4177 return __rhs < __lhs;
4178}
4179
4180template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004181inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004183operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4184 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185{
4186 return __rhs < __lhs;
4187}
4188
4189template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004190inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004192operator> (const _CharT* __lhs,
4193 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194{
4195 return __rhs < __lhs;
4196}
4197
4198// operator<=
4199
4200template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004201inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202bool
4203operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004204 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205{
4206 return !(__rhs < __lhs);
4207}
4208
4209template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004210inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004212operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4213 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214{
4215 return !(__rhs < __lhs);
4216}
4217
4218template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004219inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004221operator<=(const _CharT* __lhs,
4222 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223{
4224 return !(__rhs < __lhs);
4225}
4226
4227// operator>=
4228
4229template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004230inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231bool
4232operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004233 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234{
4235 return !(__lhs < __rhs);
4236}
4237
4238template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004239inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004240bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004241operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4242 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243{
4244 return !(__lhs < __rhs);
4245}
4246
4247template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004250operator>=(const _CharT* __lhs,
4251 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252{
4253 return !(__lhs < __rhs);
4254}
4255
4256// operator +
4257
4258template<class _CharT, class _Traits, class _Allocator>
4259basic_string<_CharT, _Traits, _Allocator>
4260operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4261 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4262{
4263 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4264 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4265 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4266 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4267 __r.append(__rhs.data(), __rhs_sz);
4268 return __r;
4269}
4270
4271template<class _CharT, class _Traits, class _Allocator>
4272basic_string<_CharT, _Traits, _Allocator>
4273operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4274{
4275 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4276 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4277 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4278 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4279 __r.append(__rhs.data(), __rhs_sz);
4280 return __r;
4281}
4282
4283template<class _CharT, class _Traits, class _Allocator>
4284basic_string<_CharT, _Traits, _Allocator>
4285operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4286{
4287 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4288 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4289 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4290 __r.append(__rhs.data(), __rhs_sz);
4291 return __r;
4292}
4293
4294template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004295inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296basic_string<_CharT, _Traits, _Allocator>
4297operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4298{
4299 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4300 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4301 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4302 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4303 __r.append(__rhs, __rhs_sz);
4304 return __r;
4305}
4306
4307template<class _CharT, class _Traits, class _Allocator>
4308basic_string<_CharT, _Traits, _Allocator>
4309operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4310{
4311 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4312 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4313 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4314 __r.push_back(__rhs);
4315 return __r;
4316}
4317
Eric Fiselierfc92be82017-04-19 00:28:44 +00004318#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004319
4320template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004321inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322basic_string<_CharT, _Traits, _Allocator>
4323operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4324{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004325 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326}
4327
4328template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004329inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330basic_string<_CharT, _Traits, _Allocator>
4331operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4332{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004333 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334}
4335
4336template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004337inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338basic_string<_CharT, _Traits, _Allocator>
4339operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4340{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004341 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342}
4343
4344template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004345inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004346basic_string<_CharT, _Traits, _Allocator>
4347operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4348{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004349 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350}
4351
4352template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004353inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354basic_string<_CharT, _Traits, _Allocator>
4355operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4356{
4357 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004358 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359}
4360
4361template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004362inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363basic_string<_CharT, _Traits, _Allocator>
4364operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4365{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004366 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367}
4368
4369template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004370inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371basic_string<_CharT, _Traits, _Allocator>
4372operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4373{
4374 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004375 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376}
4377
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004378#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379
4380// swap
4381
4382template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004383inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004385swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004386 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4387 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388{
4389 __lhs.swap(__rhs);
4390}
4391
Bruce Mitchener170d8972020-11-24 12:53:53 -05004392_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4393_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4394_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4395_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4396_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004397
Bruce Mitchener170d8972020-11-24 12:53:53 -05004398_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4399_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4400_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004401
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004402_LIBCPP_FUNC_VIS string to_string(int __val);
4403_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4404_LIBCPP_FUNC_VIS string to_string(long __val);
4405_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4406_LIBCPP_FUNC_VIS string to_string(long long __val);
4407_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4408_LIBCPP_FUNC_VIS string to_string(float __val);
4409_LIBCPP_FUNC_VIS string to_string(double __val);
4410_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004411
Bruce Mitchener170d8972020-11-24 12:53:53 -05004412_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4413_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4414_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4415_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4416_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004417
Bruce Mitchener170d8972020-11-24 12:53:53 -05004418_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4419_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4420_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004421
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004422_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4423_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4424_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4425_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4426_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4427_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4428_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4429_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4430_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004431
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004433_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004434const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4435 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436
Marshall Clow851b9ec2019-05-20 21:56:51 +00004437template <class _CharT, class _Allocator>
4438struct _LIBCPP_TEMPLATE_VIS
4439 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4440 : public unary_function<
4441 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442{
4443 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004444 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4445 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446};
4447
Howard Hinnantc51e1022010-05-11 19:42:16 +00004448
Howard Hinnantdc095972011-07-18 15:51:59 +00004449template<class _CharT, class _Traits, class _Allocator>
4450basic_ostream<_CharT, _Traits>&
4451operator<<(basic_ostream<_CharT, _Traits>& __os,
4452 const basic_string<_CharT, _Traits, _Allocator>& __str);
4453
4454template<class _CharT, class _Traits, class _Allocator>
4455basic_istream<_CharT, _Traits>&
4456operator>>(basic_istream<_CharT, _Traits>& __is,
4457 basic_string<_CharT, _Traits, _Allocator>& __str);
4458
4459template<class _CharT, class _Traits, class _Allocator>
4460basic_istream<_CharT, _Traits>&
4461getline(basic_istream<_CharT, _Traits>& __is,
4462 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4463
4464template<class _CharT, class _Traits, class _Allocator>
4465inline _LIBCPP_INLINE_VISIBILITY
4466basic_istream<_CharT, _Traits>&
4467getline(basic_istream<_CharT, _Traits>& __is,
4468 basic_string<_CharT, _Traits, _Allocator>& __str);
4469
Howard Hinnantdc095972011-07-18 15:51:59 +00004470template<class _CharT, class _Traits, class _Allocator>
4471inline _LIBCPP_INLINE_VISIBILITY
4472basic_istream<_CharT, _Traits>&
4473getline(basic_istream<_CharT, _Traits>&& __is,
4474 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4475
4476template<class _CharT, class _Traits, class _Allocator>
4477inline _LIBCPP_INLINE_VISIBILITY
4478basic_istream<_CharT, _Traits>&
4479getline(basic_istream<_CharT, _Traits>&& __is,
4480 basic_string<_CharT, _Traits, _Allocator>& __str);
4481
Marshall Clow29b53f22018-12-14 18:49:35 +00004482#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004483template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004484inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004485 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4486 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4487 auto __old_size = __str.size();
4488 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4489 return __old_size - __str.size();
4490}
Marshall Clow29b53f22018-12-14 18:49:35 +00004491
Marek Kurdeja98b1412020-05-02 13:58:03 +02004492template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004493inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004494 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4495 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4496 _Predicate __pred) {
4497 auto __old_size = __str.size();
4498 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4499 __str.end());
4500 return __old_size - __str.size();
4501}
Marshall Clow29b53f22018-12-14 18:49:35 +00004502#endif
4503
Louis Dionneba400782020-10-02 15:02:52 -04004504#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004505
4506template<class _CharT, class _Traits, class _Allocator>
4507bool
4508basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4509{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004510 return this->data() <= _VSTD::__to_address(__i->base()) &&
4511 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004512}
4513
4514template<class _CharT, class _Traits, class _Allocator>
4515bool
4516basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4517{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004518 return this->data() < _VSTD::__to_address(__i->base()) &&
4519 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004520}
4521
4522template<class _CharT, class _Traits, class _Allocator>
4523bool
4524basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4525{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004526 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004527 return this->data() <= __p && __p <= this->data() + this->size();
4528}
4529
4530template<class _CharT, class _Traits, class _Allocator>
4531bool
4532basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4533{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004534 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004535 return this->data() <= __p && __p < this->data() + this->size();
4536}
4537
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004538#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004539
Louis Dionne173f29e2019-05-29 16:01:36 +00004540#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004541// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004542inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004543{
4544 inline namespace string_literals
4545 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004546 inline _LIBCPP_INLINE_VISIBILITY
4547 basic_string<char> operator "" s( const char *__str, size_t __len )
4548 {
4549 return basic_string<char> (__str, __len);
4550 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004551
Howard Hinnant5c167562013-08-07 19:39:48 +00004552 inline _LIBCPP_INLINE_VISIBILITY
4553 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4554 {
4555 return basic_string<wchar_t> (__str, __len);
4556 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004557
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004558#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004559 inline _LIBCPP_INLINE_VISIBILITY
4560 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4561 {
4562 return basic_string<char8_t> (__str, __len);
4563 }
4564#endif
4565
Howard Hinnant5c167562013-08-07 19:39:48 +00004566 inline _LIBCPP_INLINE_VISIBILITY
4567 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4568 {
4569 return basic_string<char16_t> (__str, __len);
4570 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004571
Howard Hinnant5c167562013-08-07 19:39:48 +00004572 inline _LIBCPP_INLINE_VISIBILITY
4573 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4574 {
4575 return basic_string<char32_t> (__str, __len);
4576 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004577 }
4578}
4579#endif
4580
Howard Hinnantc51e1022010-05-11 19:42:16 +00004581_LIBCPP_END_NAMESPACE_STD
4582
Eric Fiselierf4433a32017-05-31 22:07:49 +00004583_LIBCPP_POP_MACROS
4584
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004585#endif // _LIBCPP_STRING