blob: c3a213091ef11e09035e961b59d6df99e46e51b9 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
Nikolas Klauser80b3cf32022-04-27 10:11:44 +020098 noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20
99 explicit basic_string(const allocator_type& a); // constexpr since C++20
100 basic_string(const basic_string& str); // constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200102 noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200104 const allocator_type& a = allocator_type()); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200106 const Allocator& a = Allocator()); // constexpr since C++20
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17, constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17, constexpr since C++20
111 basic_string(const value_type* s, const allocator_type& a = allocator_type()); // constexpr since C++20
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type()); // constexpr since C++20
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type()); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200117 const allocator_type& a = allocator_type()); // constexpr since C++20
118 basic_string(initializer_list<value_type>, const Allocator& = Allocator()); // constexpr since C++20
119 basic_string(const basic_string&, const Allocator&); // constexpr since C++20
120 basic_string(basic_string&&, const Allocator&); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200122 ~basic_string(); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200124 operator basic_string_view<charT, traits>() const noexcept; // constexpr since C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000125
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200126 basic_string& operator=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200128 basic_string& operator=(const T& t); // C++17, constexpr since C++20
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 ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200132 allocator_type::is_always_equal::value ); // C++17, constexpr since C++20
133 basic_string& operator=(const value_type* s); // constexpr since C++20
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200135 basic_string& operator=(value_type c); // constexpr since C++20
136 basic_string& operator=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200138 iterator begin() noexcept; // constexpr since C++20
139 const_iterator begin() const noexcept; // constexpr since C++20
140 iterator end() noexcept; // constexpr since C++20
141 const_iterator end() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200143 reverse_iterator rbegin() noexcept; // constexpr since C++20
144 const_reverse_iterator rbegin() const noexcept; // constexpr since C++20
145 reverse_iterator rend() noexcept; // constexpr since C++20
146 const_reverse_iterator rend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200148 const_iterator cbegin() const noexcept; // constexpr since C++20
149 const_iterator cend() const noexcept; // constexpr since C++20
150 const_reverse_iterator crbegin() const noexcept; // constexpr since C++20
151 const_reverse_iterator crend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200153 size_type size() const noexcept; // constexpr since C++20
154 size_type length() const noexcept; // constexpr since C++20
155 size_type max_size() const noexcept; // constexpr since C++20
156 size_type capacity() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200158 void resize(size_type n, value_type c); // constexpr since C++20
159 void resize(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200164 void reserve(size_type res_arg); // constexpr since C++20
Marek Kurdejc9848142020-11-26 10:07:16 +0100165 void reserve(); // deprecated in C++20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200166 void shrink_to_fit(); // constexpr since C++20
167 void clear() noexcept; // constexpr since C++20
168 bool empty() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200170 const_reference operator[](size_type pos) const; // constexpr since C++20
171 reference operator[](size_type pos); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200173 const_reference at(size_type n) const; // constexpr since C++20
174 reference at(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200176 basic_string& operator+=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200178 basic_string& operator+=(const T& t); // C++17, constexpr since C++20
179 basic_string& operator+=(const value_type* s); // constexpr since C++20
180 basic_string& operator+=(value_type c); // constexpr since C++20
181 basic_string& operator+=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200183 basic_string& append(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200185 basic_string& append(const T& t); // C++17, constexpr since C++20
186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
189 basic_string& append(const value_type* s, size_type n); // constexpr since C++20
190 basic_string& append(const value_type* s); // constexpr since C++20
191 basic_string& append(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200193 basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
194 basic_string& append(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200196 void push_back(value_type c); // constexpr since C++20
197 void pop_back(); // constexpr since C++20
198 reference front(); // constexpr since C++20
199 const_reference front() const; // constexpr since C++20
200 reference back(); // constexpr since C++20
201 const_reference back() const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200203 basic_string& assign(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200205 basic_string& assign(const T& t); // C++17, constexpr since C++20
206 basic_string& assign(basic_string&& str); // constexpr since C++20
207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
210 basic_string& assign(const value_type* s, size_type n); // constexpr since C++20
211 basic_string& assign(const value_type* s); // constexpr since C++20
212 basic_string& assign(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200214 basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
215 basic_string& assign(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200217 basic_string& insert(size_type pos1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200219 basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200221 size_type pos2, size_type n); // constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
225 basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
226 basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
227 iterator insert(const_iterator p, value_type c); // constexpr since C++20
228 iterator insert(const_iterator p, size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200230 iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20
231 iterator insert(const_iterator p, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200233 basic_string& erase(size_type pos = 0, size_type n = npos); // constexpr since C++20
234 iterator erase(const_iterator position); // constexpr since C++20
235 iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17, constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200241 size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200244 size_type pos2, size_type n); // C++17, constexpr since C++20
245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
246 basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17, constexpr since C++20
251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s); // constexpr since C++20
253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200258 size_type copy(value_type* s, size_type n, size_type pos = 0) const; // constexpr since C++20
259 basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200263 allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200265 const value_type* c_str() const noexcept; // constexpr since C++20
266 const value_type* data() const noexcept; // constexpr since C++20
267 value_type* data() noexcept; // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200269 allocator_type get_allocator() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200271 size_type find(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
275 size_type find(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
276 size_type find(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
283 size_type rfind(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension, constexpr since C++20
295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200313 int compare(const basic_string& str) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
316 int compare(size_type pos1, size_type n1, const basic_string& str) const; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17, constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200320 size_type pos2, size_type n2=npos) const; // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200323 size_type pos2, size_type n2=npos) const; // C++17, constexpr since C++20
324 int compare(const value_type* s) const noexcept; // constexpr since C++20
325 int compare(size_type pos1, size_type n1, const value_type* s) const; // constexpr since C++20
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200328 constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 constexpr bool starts_with(charT c) const noexcept; // C++20
330 constexpr bool starts_with(const charT* s) const; // C++20
331 constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 constexpr bool ends_with(charT c) const noexcept; // C++20
333 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338};
339
Marshall Clowa0563332018-02-08 06:34:03 +0000340template<class InputIterator,
341 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
342basic_string(InputIterator, InputIterator, Allocator = Allocator())
343 -> basic_string<typename iterator_traits<InputIterator>::value_type,
344 char_traits<typename iterator_traits<InputIterator>::value_type>,
345 Allocator>; // C++17
346
Howard Hinnantc51e1022010-05-11 19:42:16 +0000347template<class charT, class traits, class Allocator>
348basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000349operator+(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200350 const basic_string<charT, traits, Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351
352template<class charT, class traits, class Allocator>
353basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200354operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000355
356template<class charT, class traits, class Allocator>
357basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200358operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000359
360template<class charT, class traits, class Allocator>
361basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200362operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
364template<class charT, class traits, class Allocator>
365basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200366operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367
368template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000369bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200370 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200373bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374
375template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200376bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000378template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000379bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200380 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200383bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200386bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387
388template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000389bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200390 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200393bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394
395template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200396bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397
398template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000399bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200400 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200403bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200406bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407
408template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000409bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200410 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
412template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200413bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414
415template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200416bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417
418template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000419bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200420 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200423bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424
425template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200426bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427
428template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000429void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000430 basic_string<charT, traits, Allocator>& rhs)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200431 noexcept(noexcept(lhs.swap(rhs))); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432
433template<class charT, class traits, class Allocator>
434basic_istream<charT, traits>&
435operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
436
437template<class charT, class traits, class Allocator>
438basic_ostream<charT, traits>&
439operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
440
441template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000442basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000443getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
444 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445
446template<class charT, class traits, class Allocator>
447basic_istream<charT, traits>&
448getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
449
Marshall Clow29b53f22018-12-14 18:49:35 +0000450template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200451typename basic_string<charT, traits, Allocator>::size_type
452erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000453template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200454typename basic_string<charT, traits, Allocator>::size_type
455erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000456
Howard Hinnantc51e1022010-05-11 19:42:16 +0000457typedef basic_string<char> string;
458typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100459typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000460typedef basic_string<char16_t> u16string;
461typedef basic_string<char32_t> u32string;
462
Bruce Mitchener170d8972020-11-24 12:53:53 -0500463int stoi (const string& str, size_t* idx = nullptr, int base = 10);
464long stol (const string& str, size_t* idx = nullptr, int base = 10);
465unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
466long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000468
Bruce Mitchener170d8972020-11-24 12:53:53 -0500469float stof (const string& str, size_t* idx = nullptr);
470double stod (const string& str, size_t* idx = nullptr);
471long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000472
473string to_string(int val);
474string to_string(unsigned val);
475string to_string(long val);
476string to_string(unsigned long val);
477string to_string(long long val);
478string to_string(unsigned long long val);
479string to_string(float val);
480string to_string(double val);
481string to_string(long double val);
482
Bruce Mitchener170d8972020-11-24 12:53:53 -0500483int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
484long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
485unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
486long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000488
Bruce Mitchener170d8972020-11-24 12:53:53 -0500489float stof (const wstring& str, size_t* idx = nullptr);
490double stod (const wstring& str, size_t* idx = nullptr);
491long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000492
493wstring to_wstring(int val);
494wstring to_wstring(unsigned val);
495wstring to_wstring(long val);
496wstring to_wstring(unsigned long val);
497wstring to_wstring(long long val);
498wstring to_wstring(unsigned long long val);
499wstring to_wstring(float val);
500wstring to_wstring(double val);
501wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502
503template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100504template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000505template <> struct hash<u16string>;
506template <> struct hash<u32string>;
507template <> struct hash<wstring>;
508
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200509basic_string<char> operator "" s( const char *str, size_t len ); // C++14, constexpr since C++20
510basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14, constexpr since C++20
511constexpr basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
512basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14, constexpr since C++20
513basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14, constexpr since C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515} // std
516
517*/
518
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100519#include <__algorithm/max.h>
520#include <__algorithm/min.h>
521#include <__algorithm/remove.h>
522#include <__algorithm/remove_if.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400523#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400525#include <__debug>
Mark de Weverdf3e0d42021-09-26 15:47:42 +0200526#include <__format/enable_insertable.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200527#include <__functional/hash.h>
Nikolas Klauser24540d32022-05-21 00:45:51 +0200528#include <__functional/unary_function.h>
Nikolas Klauser80840272022-02-04 13:04:33 +0100529#include <__ios/fpos.h>
Nikolas Klauserfb1b0672022-06-10 19:53:10 +0200530#include <__iterator/distance.h>
531#include <__iterator/iterator_traits.h>
532#include <__iterator/reverse_iterator.h>
Louis Dionne77249522021-06-11 09:55:11 -0400533#include <__iterator/wrap_iter.h>
Nikolas Klauserc513eba2022-04-09 09:41:19 +0200534#include <__memory/allocate_at_least.h>
Nikolas Klauser35080b42022-07-26 16:13:56 +0200535#include <__memory/swap_allocator.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200536#include <__string/char_traits.h>
537#include <__string/extern_template_lists.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100538#include <__utility/auto_cast.h>
539#include <__utility/move.h>
540#include <__utility/swap.h>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200541#include <__utility/unreachable.h>
542#include <climits>
Louis Dionne44962c32022-06-13 11:21:16 -0400543#include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400544#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400545#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400546#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400547#include <iosfwd>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200548#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549#include <memory>
550#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400551#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000553#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000554
Louis Dionne89258142021-08-23 15:32:36 -0400555#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100556# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400557#endif
558
Louis Dionne22355cc2022-06-27 15:53:41 -0400559#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
560# include <algorithm>
561# include <functional>
562# include <iterator>
563# include <new>
564# include <typeinfo>
565# include <utility>
566# include <vector>
567#endif
568
Nikolas Klausera0e0edb2022-06-16 22:43:46 +0200569// standard-mandated includes
570
571// [iterator.range]
572#include <__iterator/access.h>
573#include <__iterator/data.h>
574#include <__iterator/empty.h>
575#include <__iterator/reverse_access.h>
576#include <__iterator/size.h>
577
578// [string.syn]
579#include <compare>
580#include <initializer_list>
581
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000582#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500583# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000584#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585
Eric Fiselierf4433a32017-05-31 22:07:49 +0000586_LIBCPP_PUSH_MACROS
587#include <__undef_macros>
588
589
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590_LIBCPP_BEGIN_NAMESPACE_STD
591
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592// basic_string
593
594template<class _CharT, class _Traits, class _Allocator>
595basic_string<_CharT, _Traits, _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200596_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant944510a2011-06-14 19:58:17 +0000597operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
598 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
600template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200601_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000603operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604
605template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200606_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000608operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609
610template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200611inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000613operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614
615template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200616_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000618operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619
Louis Dionnedc496ec2021-06-08 17:25:08 -0400620extern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Shoaib Meenaiea363712017-07-29 02:54:41 +0000621
Marshall Clow039b2f02016-01-13 21:54:34 +0000622template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400623struct __string_is_trivial_iterator : public false_type {};
624
625template <class _Tp>
626struct __string_is_trivial_iterator<_Tp*>
627 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000628
Louis Dionne173f29e2019-05-29 16:01:36 +0000629template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400630struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
631 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000632
Marshall Clow82513342016-09-24 22:45:42 +0000633template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500634struct __can_be_converted_to_string_view : public _BoolConstant<
635 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
636 !is_convertible<const _Tp&, const _CharT*>::value
637 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000638
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400639#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800640typedef basic_string<char8_t> u8string;
641#endif
Richard Smith256954d2020-11-11 17:12:18 -0800642typedef basic_string<char16_t> u16string;
643typedef basic_string<char32_t> u32string;
Richard Smith256954d2020-11-11 17:12:18 -0800644
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200645struct __uninitialized_size_tag {};
646
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000647template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800648class
649 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400650#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800651 _LIBCPP_PREFERRED_NAME(u8string)
652#endif
Richard Smith256954d2020-11-11 17:12:18 -0800653 _LIBCPP_PREFERRED_NAME(u16string)
654 _LIBCPP_PREFERRED_NAME(u32string)
Richard Smith256954d2020-11-11 17:12:18 -0800655 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656{
657public:
658 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000659 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000661 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000663 typedef allocator_traits<allocator_type> __alloc_traits;
664 typedef typename __alloc_traits::size_type size_type;
665 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000666 typedef value_type& reference;
667 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000668 typedef typename __alloc_traits::pointer pointer;
669 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670
Marshall Clow79f33542018-03-21 00:36:05 +0000671 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
672 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
673 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
674 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000675 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000676 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000677 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000678
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679 typedef __wrap_iter<pointer> iterator;
680 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200681 typedef std::reverse_iterator<iterator> reverse_iterator;
682 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000683
684private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200685 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000686
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000687#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000688
689 struct __long
690 {
691 pointer __data_;
692 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200693 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
694 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000695 };
696
Howard Hinnant68bf1812013-04-30 21:44:48 +0000697 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
698 (sizeof(__long) - 1)/sizeof(value_type) : 2};
699
700 struct __short
701 {
702 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200703 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200704 unsigned char __size_ : 7;
705 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000706 };
707
Nikolas Klauser62060be2022-04-20 10:52:04 +0200708// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200709// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200710//
711// If the LSB is used to store the short-flag in the short string representation,
712// we have to multiply the size by two when it is stored and divide it by two when
713// it is loaded to make sure that we always store an even number. In the long string
714// representation, we can ignore this because we can assume that we always allocate
715// an even amount of value_types.
716//
717// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
718// This does not impact the short string representation, since we never need the MSB
719// for representing the size of a short string anyway.
720
721#ifdef _LIBCPP_BIG_ENDIAN
722 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000723#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200724 static const size_type __endian_factor = 1;
725#endif
726
727#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
728
729#ifdef _LIBCPP_BIG_ENDIAN
730 static const size_type __endian_factor = 1;
731#else
732 static const size_type __endian_factor = 2;
733#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000734
Xing Xue38b9fc42022-06-24 17:25:15 -0400735 // Attribute 'packed' is used to keep the layout compatible with the
736 // previous definition that did not use bit fields. This is because on
737 // some platforms bit fields have a default size rather than the actual
738 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739 struct __long
740 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400741 struct _LIBCPP_PACKED {
742 size_type __is_long_ : 1;
743 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
744 };
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 size_type __size_;
746 pointer __data_;
747 };
748
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
750 (sizeof(__long) - 1)/sizeof(value_type) : 2};
751
752 struct __short
753 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400754 struct _LIBCPP_PACKED {
755 unsigned char __is_long_ : 1;
756 unsigned char __size_ : 7;
757 };
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200758 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759 value_type __data_[__min_cap];
760 };
761
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400762#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000763
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200764 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
765
Howard Hinnant8ea98242013-08-23 17:37:05 +0000766 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767
Howard Hinnant8ea98242013-08-23 17:37:05 +0000768 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769
770 struct __raw
771 {
772 size_type __words[__n_words];
773 };
774
775 struct __rep
776 {
777 union
778 {
779 __long __l;
780 __short __s;
781 __raw __r;
782 };
783 };
784
785 __compressed_pair<__rep, allocator_type> __r_;
786
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200787 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
788 // don't initialize the characters. The contents of the string, including the null terminator, must be
789 // initialized separately.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200790 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
791 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200792 : __r_(__default_init_tag(), __a) {
793 if (__size > max_size())
794 __throw_length_error();
795 if (__fits_in_sso(__size)) {
796 __zero();
797 __set_short_size(__size);
798 } else {
799 auto __capacity = __recommend(__size) + 1;
800 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200801 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200802 __set_long_cap(__capacity);
803 __set_long_pointer(__allocation);
804 __set_long_size(__size);
805 }
806 std::__debug_db_insert_c(this);
807 }
808
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200810 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811 static const size_type npos = -1;
812
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200813 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string()
Howard Hinnant3e276872011-06-03 18:40:47 +0000814 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000815
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200816 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000817#if _LIBCPP_STD_VER <= 14
818 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
819#else
820 _NOEXCEPT;
821#endif
822
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200823 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string(const basic_string& __str);
824 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000825
Eric Fiselierfc92be82017-04-19 00:28:44 +0000826#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200827 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +0000828 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000829#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000830 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000831#else
832 _NOEXCEPT;
833#endif
834
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200835 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400837#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000838
Louis Dionne9ce598d2021-09-08 09:14:43 -0400839 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200840 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500841 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000842 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
843 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200844 std::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000845 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000846
Louis Dionne9ce598d2021-09-08 09:14:43 -0400847 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200848 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000849 basic_string(const _CharT* __s, const _Allocator& __a);
850
Marek Kurdej90d79712021-07-27 16:16:21 +0200851#if _LIBCPP_STD_VER > 20
852 basic_string(nullptr_t) = delete;
853#endif
854
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200855 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000856 basic_string(const _CharT* __s, size_type __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200857 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000858 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200859 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000860 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000861
Louis Dionne9ce598d2021-09-08 09:14:43 -0400862 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200863 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000864 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
865
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200866 _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +0000867 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000868 const _Allocator& __a = _Allocator());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200869 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +0000870 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000871 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000872
Louis Dionne9ce598d2021-09-08 09:14:43 -0400873 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200874 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000875 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500876 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000877
Louis Dionne9ce598d2021-09-08 09:14:43 -0400878 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500879 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200880 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000881 explicit basic_string(const _Tp& __t);
882
Louis Dionne9ce598d2021-09-08 09:14:43 -0400883 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200884 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000885 explicit basic_string(const _Tp& __t, const allocator_type& __a);
886
Louis Dionne9ce598d2021-09-08 09:14:43 -0400887 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200888 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400890 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200891 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000893#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200894 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000895 basic_string(initializer_list<_CharT> __il);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200896 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000897 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400898#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200900 inline _LIBCPP_CONSTEXPR_AFTER_CXX17 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200902 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000903 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
904
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200905 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000906
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200907 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
908 !__is_same_uncvref<_Tp, basic_string>::value> >
909 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(const _Tp& __t) {
910 __self_view __sv = __t;
911 return assign(__sv);
912 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000913
Eric Fiselierfc92be82017-04-19 00:28:44 +0000914#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200915 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +0000916 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000917 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200918 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierfc92be82017-04-19 00:28:44 +0000919 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200921 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200922 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200923#if _LIBCPP_STD_VER > 20
924 basic_string& operator=(nullptr_t) = delete;
925#endif
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200926 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200928 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000929 iterator begin() _NOEXCEPT
930 {return iterator(this, __get_pointer());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200931 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000932 const_iterator begin() const _NOEXCEPT
933 {return const_iterator(this, __get_pointer());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200934 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000935 iterator end() _NOEXCEPT
936 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200937 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000938 const_iterator end() const _NOEXCEPT
939 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -0400940
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200941 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000942 reverse_iterator rbegin() _NOEXCEPT
943 {return reverse_iterator(end());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200944 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000945 const_reverse_iterator rbegin() const _NOEXCEPT
946 {return const_reverse_iterator(end());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200947 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000948 reverse_iterator rend() _NOEXCEPT
949 {return reverse_iterator(begin());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200950 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000951 const_reverse_iterator rend() const _NOEXCEPT
952 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200954 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000955 const_iterator cbegin() const _NOEXCEPT
956 {return begin();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200957 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000958 const_iterator cend() const _NOEXCEPT
959 {return end();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200960 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000961 const_reverse_iterator crbegin() const _NOEXCEPT
962 {return rbegin();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200963 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000964 const_reverse_iterator crend() const _NOEXCEPT
965 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200967 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200969 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type length() const _NOEXCEPT {return size();}
970 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type max_size() const _NOEXCEPT;
971 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200972 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
973 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200975 _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200976 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200978 _LIBCPP_CONSTEXPR_AFTER_CXX17 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100979
980#if _LIBCPP_STD_VER > 20
981 template <class _Op>
982 _LIBCPP_HIDE_FROM_ABI constexpr
983 void resize_and_overwrite(size_type __n, _Op __op) {
984 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200985 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100986 }
987#endif
988
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200989 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +0000990
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200991 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
992 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void shrink_to_fit() _NOEXCEPT;
993 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200994
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200995 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowb7db4972017-11-15 20:02:27 +0000996 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200998 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200999 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001000 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001002 _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference at(size_type __n) const;
1003 _LIBCPP_CONSTEXPR_AFTER_CXX17 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001005 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001006 return append(__str);
1007 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001008
1009 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001010 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001011 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001012 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001013 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1014 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001015 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001016 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001017 operator+=(const _Tp& __t) {
1018 __self_view __sv = __t; return append(__sv);
1019 }
1020
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001021 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001022 return append(__s);
1023 }
1024
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001025 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001026 push_back(__c);
1027 return *this;
1028 }
1029
Eric Fiselierfc92be82017-04-19 00:28:44 +00001030#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001031 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001032 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001033#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001035 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001037
1038 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001039 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001040 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001041 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1042 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001043 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001044 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001045 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001046 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001047
Marshall Clow62953962016-10-03 23:40:48 +00001048 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001049 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001050 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001051 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001052 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1053 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001054 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001055 >
Marshall Clow82513342016-09-24 22:45:42 +00001056 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001057 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s, size_type __n);
1058 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s);
1059 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001060
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001061 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier451d5582018-11-26 20:15:38 +00001062 void __append_default_init(size_type __n);
1063
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001065 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001066 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001068 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001070 >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001071 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001072 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001073 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001074 append(__temp.data(), __temp.size());
1075 return *this;
1076 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001078 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001079 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001081 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001083 >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001084 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001085 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001086
Eric Fiselierfc92be82017-04-19 00:28:44 +00001087#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001088 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001090#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001092 _LIBCPP_CONSTEXPR_AFTER_CXX17 void push_back(value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001093 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void pop_back();
1094 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference front() _NOEXCEPT;
1095 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference front() const _NOEXCEPT;
1096 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference back() _NOEXCEPT;
1097 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098
Marshall Clowe46031a2018-07-02 18:41:15 +00001099 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001100 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001101 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001102 <
1103 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1104 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001105 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001106 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001107 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001108 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001109#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001110 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001111 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001112 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001113 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001114#endif
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001115 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001116 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001117 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001118 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001119 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001120 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1121 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001122 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001123 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001124 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001125 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s, size_type __n);
1126 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s);
1127 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001129 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001130 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001132 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001134 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135 assign(_InputIterator __first, _InputIterator __last);
1136 template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001137 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001138 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001140 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001142 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001144#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001145 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001147#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001149 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001151
1152 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001153 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001154 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001155 <
1156 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1157 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001158 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001159 insert(size_type __pos1, const _Tp& __t)
1160 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1161
Marshall Clow82513342016-09-24 22:45:42 +00001162 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001163 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001164 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001165 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001166 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001167 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001168 >
Marshall Clow82513342016-09-24 22:45:42 +00001169 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001170 _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow8db7fb02014-03-04 19:17:19 +00001171 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001172 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1173 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s);
1174 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1175 _LIBCPP_CONSTEXPR_AFTER_CXX17 iterator insert(const_iterator __pos, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001176 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1178 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001179 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001180 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001182 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001184 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1186 template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001187 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001188 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001190 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001192 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001194#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001195 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1197 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001198#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001200 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001201 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 iterator erase(const_iterator __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001203 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 iterator erase(const_iterator __first, const_iterator __last);
1205
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001206 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001208
1209 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001210 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001211 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001212 <
1213 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1214 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001215 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001216 replace(size_type __pos1, size_type __n1, const _Tp& __t) { __self_view __sv = __t; return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001217 _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow8db7fb02014-03-04 19:17:19 +00001218 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 +00001219 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001220 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001221 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001222 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001223 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001224 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001225 >
Marshall Clow82513342016-09-24 22:45:42 +00001226 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001227 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001228 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001229 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1230 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001231 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant990d6e82010-11-17 21:11:40 +00001232 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001233
1234 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001235 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001236 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001237 <
1238 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1239 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001240 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001241 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1242
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001243 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001244 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001245 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001246 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001247 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant990d6e82010-11-17 21:11:40 +00001248 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001250 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001251 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001253 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001255 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001256 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001257#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001258 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant990d6e82010-11-17 21:11:40 +00001259 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001261#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001263 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001264 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1266
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001267 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001268 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001269#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001270 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001271#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001272 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001273 __is_nothrow_swappable<allocator_type>::value);
1274#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001276 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001277 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001278 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1279 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001280#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001281 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1282 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001283#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001285 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001286 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001288 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001289 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001290
1291 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001292 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001293 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001294 <
1295 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1296 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001297 >
zoecarver1997e0a2021-02-05 11:54:47 -08001298 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001299 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001301 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001302 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001303 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001305 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001306 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001307
1308 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001309 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001310 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001311 <
1312 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1313 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001314 >
zoecarver1997e0a2021-02-05 11:54:47 -08001315 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001316 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001317 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001319 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001320 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001322 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001323 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001324
1325 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001326 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001327 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001328 <
1329 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1330 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001331 >
zoecarver1997e0a2021-02-05 11:54:47 -08001332 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001333 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001334 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001335 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001336 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001337 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001338 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001340 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001341 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001342
1343 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001344 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001345 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001346 <
1347 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1348 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001349 >
zoecarver1997e0a2021-02-05 11:54:47 -08001350 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001351 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001352 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001353 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001354 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001355 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001356 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001358 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001359 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001360
1361 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001362 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001363 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001364 <
1365 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1366 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001367 >
zoecarver1997e0a2021-02-05 11:54:47 -08001368 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001369 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001370 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001371 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001372 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001373 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001374 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1375
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001376 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001377 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001378
1379 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001380 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001381 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001382 <
1383 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1384 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001385 >
zoecarver1997e0a2021-02-05 11:54:47 -08001386 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001387 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001388 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001389 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001390 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001391 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001392 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1393
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001394 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001395 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001396
1397 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001398 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001399 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001400 <
1401 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1402 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001403 >
zoecarver1997e0a2021-02-05 11:54:47 -08001404 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001405
1406 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001407 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001408 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001409 <
1410 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1411 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001412 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001413 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1414
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001415 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001417 _LIBCPP_CONSTEXPR_AFTER_CXX17
1418 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1419 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001420
Marshall Clow82513342016-09-24 22:45:42 +00001421 template <class _Tp>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001422 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001423 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001424 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001425 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001426 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001427 >
Marshall Clow82513342016-09-24 22:45:42 +00001428 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001429 _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(const value_type* __s) const _NOEXCEPT;
1430 _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1431 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001432 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433
Marshall Clow18c293b2017-12-04 20:11:38 +00001434#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001435 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001436 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001437 { return __self_view(data(), size()).starts_with(__sv); }
1438
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001439 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001440 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001441 { return !empty() && _Traits::eq(front(), __c); }
1442
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001443 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001444 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001445 { return starts_with(__self_view(__s)); }
1446
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001447 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001448 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001449 { return __self_view(data(), size()).ends_with( __sv); }
1450
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001451 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001452 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001453 { return !empty() && _Traits::eq(back(), __c); }
1454
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001455 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001456 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001457 { return ends_with(__self_view(__s)); }
1458#endif
1459
Wim Leflere023c3542021-01-19 14:33:30 -05001460#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001461 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001462 bool contains(__self_view __sv) const noexcept
1463 { return __self_view(data(), size()).contains(__sv); }
1464
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001465 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001466 bool contains(value_type __c) const noexcept
1467 { return __self_view(data(), size()).contains(__c); }
1468
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001469 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001470 bool contains(const value_type* __s) const
1471 { return __self_view(data(), size()).contains(__s); }
1472#endif
1473
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001474 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001475
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001476 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001477
Louis Dionne510450b2022-04-01 16:38:30 -04001478#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001479
1480 bool __dereferenceable(const const_iterator* __i) const;
1481 bool __decrementable(const const_iterator* __i) const;
1482 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1483 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1484
Louis Dionne510450b2022-04-01 16:38:30 -04001485#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001486
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001488 template<class _Alloc>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001489 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001490 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1491 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1492
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001493 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001494
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001495 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001496 bool __is_long() const _NOEXCEPT {
1497 if (__libcpp_is_constant_evaluated())
1498 return true;
1499 return __r_.first().__s.__is_long_;
1500 }
1501
1502 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __begin_lifetime(pointer __begin, size_type __n) {
1503#if _LIBCPP_STD_VER > 17
1504 if (__libcpp_is_constant_evaluated()) {
1505 for (size_type __i = 0; __i != __n; ++__i)
1506 std::construct_at(std::addressof(__begin[__i]));
1507 }
1508#else
1509 (void)__begin;
1510 (void)__n;
1511#endif // _LIBCPP_STD_VER > 17
1512 }
1513
1514 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __default_init() {
1515 __zero();
1516 if (__libcpp_is_constant_evaluated()) {
1517 size_type __sz = __recommend(0) + 1;
1518 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1519 __begin_lifetime(__ptr, __sz);
1520 __set_long_pointer(__ptr);
1521 __set_long_cap(__sz);
1522 __set_long_size(0);
1523 }
1524 }
1525
1526 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __deallocate_constexpr() {
1527 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1528 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1529 }
1530
Nikolas Klauser93826d12022-01-04 17:24:03 +01001531 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1532 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1533 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1534 }
1535
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001536 template <class _ForwardIterator>
1537 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1538 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1539 size_type __sz = size();
1540 size_type __cap = capacity();
1541 value_type* __p;
1542 if (__cap - __sz >= __n)
1543 {
1544 __p = std::__to_address(__get_pointer());
1545 size_type __n_move = __sz - __ip;
1546 if (__n_move != 0)
1547 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1548 }
1549 else
1550 {
1551 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1552 __p = std::__to_address(__get_long_pointer());
1553 }
1554 __sz += __n;
1555 __set_size(__sz);
1556 traits_type::assign(__p[__sz], value_type());
1557 for (__p += __ip; __first != __last; ++__p, ++__first)
1558 traits_type::assign(*__p, *__first);
1559
1560 return begin() + __ip;
1561 }
1562
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001563 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1564 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001566 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001567 void __set_short_size(size_type __s) _NOEXCEPT {
1568 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1569 __r_.first().__s.__size_ = __s;
1570 __r_.first().__s.__is_long_ = false;
1571 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001572
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001573 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001574 size_type __get_short_size() const _NOEXCEPT {
1575 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1576 return __r_.first().__s.__size_;
1577 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001578
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001579 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001580 void __set_long_size(size_type __s) _NOEXCEPT
1581 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001582 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001583 size_type __get_long_size() const _NOEXCEPT
1584 {return __r_.first().__l.__size_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001585 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001586 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1588
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001589 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001590 void __set_long_cap(size_type __s) _NOEXCEPT {
1591 __r_.first().__l.__cap_ = __s / __endian_factor;
1592 __r_.first().__l.__is_long_ = true;
1593 }
1594
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001595 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001596 size_type __get_long_cap() const _NOEXCEPT {
1597 return __r_.first().__l.__cap_ * __endian_factor;
1598 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001600 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001601 void __set_long_pointer(pointer __p) _NOEXCEPT
1602 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001603 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001604 pointer __get_long_pointer() _NOEXCEPT
1605 {return __r_.first().__l.__data_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001606 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001607 const_pointer __get_long_pointer() const _NOEXCEPT
1608 {return __r_.first().__l.__data_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001609 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001610 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001611 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001612 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001613 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001614 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001615 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001616 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001618 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001619 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1621
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001622 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001623 void __zero() _NOEXCEPT {
1624 __r_.first() = __rep();
1625 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626
1627 template <size_type __a> static
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001628 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea382952013-08-14 18:00:20 +00001629 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001630 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631 enum {__alignment = 16};
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001632 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001633 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001634 {
1635 if (__s < __min_cap) {
1636 if (__libcpp_is_constant_evaluated())
1637 return static_cast<size_type>(__min_cap);
1638 else
1639 return static_cast<size_type>(__min_cap) - 1;
1640 }
Marshall Clow80584522018-02-07 21:30:17 +00001641 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1642 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1643 if (__guess == __min_cap) ++__guess;
1644 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001645 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001647 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001648 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001649 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001650 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001651 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001653
Martijn Vels5e7c9752020-03-04 17:52:46 -05001654 // Slow path for the (inlined) copy constructor for 'long' strings.
1655 // Always externally instantiated and not inlined.
1656 // Requires that __s is zero terminated.
1657 // The main reason for this function to exist is because for unstable, we
1658 // want to allow inlining of the copy constructor. However, we don't want
1659 // to call the __init() functions as those are marked as inline which may
1660 // result in over-aggressive inlining by the compiler, where our aim is
1661 // to only inline the fast path code directly in the ctor.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001662 _LIBCPP_CONSTEXPR_AFTER_CXX17 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001663
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664 template <class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001665 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001666 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001668 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1669 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670 __init(_InputIterator __first, _InputIterator __last);
1671
1672 template <class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001673 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001674 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001676 __is_cpp17_forward_iterator<_ForwardIterator>::value
1677 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678 __init(_ForwardIterator __first, _ForwardIterator __last);
1679
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001680 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001682 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001683 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1685 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001686 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687
Martijn Vels596e3de2020-02-26 15:55:49 -05001688 // __assign_no_alias is invoked for assignment operations where we
1689 // have proof that the input does not alias the current instance.
1690 // For example, operator=(basic_string) performs a 'self' check.
1691 template <bool __is_short>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001692 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001693
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001694 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695 void __erase_to_end(size_type __pos);
1696
Martijn Velsa81fc792020-02-26 13:25:43 -05001697 // __erase_external_with_move is invoked for erase() invocations where
1698 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001699 _LIBCPP_CONSTEXPR_AFTER_CXX17 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001700
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001701 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001702 void __copy_assign_alloc(const basic_string& __str)
1703 {__copy_assign_alloc(__str, integral_constant<bool,
1704 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1705
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001706 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001707 void __copy_assign_alloc(const basic_string& __str, true_type)
1708 {
Marshall Clowf258c202017-01-31 03:40:52 +00001709 if (__alloc() == __str.__alloc())
1710 __alloc() = __str.__alloc();
1711 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001712 {
Marshall Clowf258c202017-01-31 03:40:52 +00001713 if (!__str.__is_long())
1714 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001715 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001716 __alloc() = __str.__alloc();
1717 }
1718 else
1719 {
1720 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001721 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001722 __begin_lifetime(__allocation.ptr, __allocation.count);
Vedant Kumar55e007e2018-03-08 21:15:26 +00001723 __clear_and_shrink();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001724 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001725 __set_long_pointer(__allocation.ptr);
1726 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001727 __set_long_size(__str.size());
1728 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001729 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001730 }
1731
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001732 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant28b24882011-12-01 20:21:04 +00001733 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001734 {}
1735
Eric Fiselierfc92be82017-04-19 00:28:44 +00001736#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001737 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001738 void __move_assign(basic_string& __str, false_type)
1739 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001740 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001741 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001742#if _LIBCPP_STD_VER > 14
1743 _NOEXCEPT;
1744#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001745 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001746#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001747#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001748
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001749 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001750 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001751 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001752 _NOEXCEPT_(
1753 !__alloc_traits::propagate_on_container_move_assignment::value ||
1754 is_nothrow_move_assignable<allocator_type>::value)
1755 {__move_assign_alloc(__str, integral_constant<bool,
1756 __alloc_traits::propagate_on_container_move_assignment::value>());}
1757
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001758 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc2734962011-09-02 20:42:31 +00001759 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001760 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1761 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001762 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001763 }
1764
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001765 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant28b24882011-12-01 20:21:04 +00001766 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001767 _NOEXCEPT
1768 {}
1769
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001770 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s);
1771 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001772
1773 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1774 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1775 pointer __p = __is_long()
1776 ? (__set_long_size(__n), __get_long_pointer())
1777 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001778 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001779 traits_type::assign(__p[__n], value_type());
1780 return *this;
1781 }
1782
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001783 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1784 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001785 __set_size(__newsz);
1786 __invalidate_iterators_past(__newsz);
1787 traits_type::assign(__p[__newsz], value_type());
1788 return *this;
1789 }
1790
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001791 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001793 template<class _Tp>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001794 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001795 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001796 // assume that the ranges overlap, because we can't check during constant evaluation
1797 if (__libcpp_is_constant_evaluated())
1798 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001799 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001800 return data() <= __p && __p <= data() + size();
1801 }
1802
Louis Dionned24191c2021-08-19 12:39:16 -04001803 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1804 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001805 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001806 }
1807
1808 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1809 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001810 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001811 }
1812
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001813 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const basic_string&);
1814 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const value_type*, const basic_string&);
1815 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(value_type, const basic_string&);
1816 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const value_type*);
1817 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818};
1819
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001820// These declarations must appear before any functions are implicitly used
1821// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001822#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001823#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001824 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001825# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001826 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001827# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001828#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001829 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001830# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001831 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001832# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001833#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001834#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001835
1836
Louis Dionned59f8a52021-08-17 11:59:07 -04001837#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001838template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001839 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001840 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001841 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1842 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001843 >
1844basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1845 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001846
1847template<class _CharT,
1848 class _Traits,
1849 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001850 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001851 >
1852explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1853 -> basic_string<_CharT, _Traits, _Allocator>;
1854
1855template<class _CharT,
1856 class _Traits,
1857 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001858 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001859 class _Sz = typename allocator_traits<_Allocator>::size_type
1860 >
1861basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1862 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001863#endif
1864
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001866inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001868basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869{
Louis Dionne510450b2022-04-01 16:38:30 -04001870#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001871 if (!__libcpp_is_constant_evaluated()) {
1872 __c_node* __c = __get_db()->__find_c_and_lock(this);
1873 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001875 const_pointer __new_last = __get_pointer() + __pos;
1876 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001878 --__p;
1879 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1880 if (__i->base() > __new_last)
1881 {
1882 (*__p)->__c_ = nullptr;
1883 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001884 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001885 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001887 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888 }
1889 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001890#else
1891 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04001892#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893}
1894
1895template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001896inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001897basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001898 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001899 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001901 std::__debug_db_insert_c(this);
1902 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903}
1904
1905template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001906inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001908#if _LIBCPP_STD_VER <= 14
1909 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1910#else
1911 _NOEXCEPT
1912#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001913: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001915 std::__debug_db_insert_c(this);
1916 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917}
1918
1919template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001920_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier815ed732016-09-16 00:00:48 +00001921void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1922 size_type __sz,
1923 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001925 if (__libcpp_is_constant_evaluated())
1926 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001928 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001930 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931 {
1932 __set_short_size(__sz);
1933 __p = __get_short_pointer();
1934 }
1935 else
1936 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001937 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1938 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001939 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001941 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942 __set_long_size(__sz);
1943 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001944 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945 traits_type::assign(__p[__sz], value_type());
1946}
1947
1948template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001949_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001951basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001953 if (__libcpp_is_constant_evaluated())
1954 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001956 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001957 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001958 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959 {
1960 __set_short_size(__sz);
1961 __p = __get_short_pointer();
1962 }
1963 else
1964 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001965 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1966 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001967 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001969 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970 __set_long_size(__sz);
1971 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001972 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973 traits_type::assign(__p[__sz], value_type());
1974}
1975
1976template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001977template <class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001978_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001979basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001980 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001982 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001984 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985}
1986
1987template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001988inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001989basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001990 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001992 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1993 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001994 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995}
1996
1997template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001998inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001999basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002000 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002002 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002004 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005}
2006
2007template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002008_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002010 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011{
2012 if (!__str.__is_long())
2013 __r_.first().__r = __str.__r_.first().__r;
2014 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002015 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002016 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002017 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018}
2019
2020template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002021_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002022basic_string<_CharT, _Traits, _Allocator>::basic_string(
2023 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002024 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025{
2026 if (!__str.__is_long())
2027 __r_.first().__r = __str.__r_.first().__r;
2028 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002029 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002030 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002031 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032}
2033
Martijn Vels5e7c9752020-03-04 17:52:46 -05002034template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002035_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Vels5e7c9752020-03-04 17:52:46 -05002036void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2037 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002038 if (__libcpp_is_constant_evaluated())
2039 __zero();
Martijn Vels5e7c9752020-03-04 17:52:46 -05002040 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002041 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002042 __p = __get_short_pointer();
2043 __set_short_size(__sz);
2044 } else {
2045 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002046 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002047 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2048 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002049 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002050 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002051 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002052 __set_long_size(__sz);
2053 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002054 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002055}
2056
Eric Fiselierfc92be82017-04-19 00:28:44 +00002057#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058
2059template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002060inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00002061basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00002062#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00002063 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00002064#else
2065 _NOEXCEPT
2066#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002067 : __r_(std::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002069 __str.__default_init();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002070 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002071 if (__is_long())
2072 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073}
2074
2075template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002076inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002077basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002078 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079{
Marshall Clowd2d24692014-07-17 15:32:20 +00002080 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002081 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002082 else
2083 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002084 if (__libcpp_is_constant_evaluated()) {
2085 __zero();
2086 __r_.first().__l = __str.__r_.first().__l;
2087 } else {
2088 __r_.first().__r = __str.__r_.first().__r;
2089 }
2090 __str.__default_init();
Marshall Clowd2d24692014-07-17 15:32:20 +00002091 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002092 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002093 if (__is_long())
2094 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095}
2096
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002097#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098
2099template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002100_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002101void
2102basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2103{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002104 if (__libcpp_is_constant_evaluated())
2105 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002107 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002109 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110 {
2111 __set_short_size(__n);
2112 __p = __get_short_pointer();
2113 }
2114 else
2115 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002116 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2117 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002118 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002120 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 __set_long_size(__n);
2122 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002123 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124 traits_type::assign(__p[__n], value_type());
2125}
2126
2127template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002128inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002129basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002130 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131{
2132 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002133 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134}
2135
2136template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002137template <class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002138_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002139basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002140 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141{
2142 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002143 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144}
2145
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002147_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002148basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2149 size_type __pos, size_type __n,
2150 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002151 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152{
2153 size_type __str_sz = __str.size();
2154 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002155 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002156 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2157 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158}
2159
2160template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002161inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +00002162basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002163 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002164 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002165{
2166 size_type __str_sz = __str.size();
2167 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002168 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002169 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002170 std::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002171}
2172
2173template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002174template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002175_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow78dbe462016-11-14 18:22:19 +00002176basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002177 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002178 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002179{
Marshall Clowe46031a2018-07-02 18:41:15 +00002180 __self_view __sv0 = __t;
2181 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002182 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002183 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002184}
2185
2186template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002187template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002188_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +00002189basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002190 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002191{
Marshall Clowe46031a2018-07-02 18:41:15 +00002192 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002193 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002194 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002195}
2196
2197template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002198template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002199_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +00002200basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002201 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002202{
Marshall Clowe46031a2018-07-02 18:41:15 +00002203 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002204 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002205 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002206}
2207
2208template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209template <class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002210_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002211__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002213 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2214>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2216{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002217 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218#ifndef _LIBCPP_NO_EXCEPTIONS
2219 try
2220 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002221#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 for (; __first != __last; ++__first)
2223 push_back(*__first);
2224#ifndef _LIBCPP_NO_EXCEPTIONS
2225 }
2226 catch (...)
2227 {
2228 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002229 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 throw;
2231 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002232#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233}
2234
2235template <class _CharT, class _Traits, class _Allocator>
2236template <class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002237_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002238__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002240 __is_cpp17_forward_iterator<_ForwardIterator>::value
2241>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2243{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002244 if (__libcpp_is_constant_evaluated())
2245 __zero();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002246 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002248 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002250 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 {
2252 __set_short_size(__sz);
2253 __p = __get_short_pointer();
2254 }
2255 else
2256 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002257 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2258 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002259 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002261 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 __set_long_size(__sz);
2263 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002264
2265#ifndef _LIBCPP_NO_EXCEPTIONS
2266 try
2267 {
2268#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002269 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 traits_type::assign(*__p, *__first);
2271 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002272#ifndef _LIBCPP_NO_EXCEPTIONS
2273 }
2274 catch (...)
2275 {
2276 if (__is_long())
2277 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2278 throw;
2279 }
2280#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281}
2282
2283template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002284template<class _InputIterator, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002285inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002287 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288{
2289 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002290 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291}
2292
2293template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002294template<class _InputIterator, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002295inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2297 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002298 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299{
2300 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002301 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302}
2303
Eric Fiselierfc92be82017-04-19 00:28:44 +00002304#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002305
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002307inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002308basic_string<_CharT, _Traits, _Allocator>::basic_string(
2309 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002310 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311{
2312 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002313 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314}
2315
2316template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002317inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002318basic_string<_CharT, _Traits, _Allocator>::basic_string(
2319 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002320 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321{
2322 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002323 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324}
2325
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002326#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002327
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002329_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2331{
Nikolas Klauser98833542022-05-07 22:20:23 +02002332 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002334 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002335}
2336
2337template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002338_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339void
2340basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2341 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002342 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 +00002343{
2344 size_type __ms = max_size();
2345 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002346 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347 pointer __old_p = __get_pointer();
2348 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002349 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002351 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2352 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002353 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002354 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002356 traits_type::copy(std::__to_address(__p),
2357 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002359 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2361 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002362 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2363 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002364 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002365 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002367 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2369 __set_long_size(__old_sz);
2370 traits_type::assign(__p[__old_sz], value_type());
2371}
2372
2373template <class _CharT, class _Traits, class _Allocator>
2374void
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002375_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002376basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2377 size_type __n_copy, size_type __n_del, size_type __n_add)
2378{
2379 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002380 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002381 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382 pointer __old_p = __get_pointer();
2383 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002384 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002386 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2387 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002388 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002389 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002391 traits_type::copy(std::__to_address(__p),
2392 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2394 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002395 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2396 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002397 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002398 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2399 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002401 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402}
2403
2404// assign
2405
2406template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002407template <bool __is_short>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002408_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsb6a08b62020-04-10 18:36:31 -04002409basic_string<_CharT, _Traits, _Allocator>&
2410basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002411 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002412 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002413 if (__n < __cap) {
2414 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2415 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002416 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002417 traits_type::assign(__p[__n], value_type());
2418 __invalidate_iterators_past(__n);
2419 } else {
2420 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2421 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2422 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002423 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002424}
2425
2426template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002427_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002428basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002429basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2430 const value_type* __s, size_type __n) {
2431 size_type __cap = capacity();
2432 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002433 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002434 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002435 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002436 } else {
2437 size_type __sz = size();
2438 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002439 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002440 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002441}
2442
2443template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002444_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsda7d94f2020-06-19 14:24:03 -04002445basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002446basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002448 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002449 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002450 ? __assign_short(__s, __n)
2451 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452}
2453
2454template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002455_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456basic_string<_CharT, _Traits, _Allocator>&
2457basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2458{
2459 size_type __cap = capacity();
2460 if (__cap < __n)
2461 {
2462 size_type __sz = size();
2463 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2464 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002465 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002467 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468}
2469
2470template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002471_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472basic_string<_CharT, _Traits, _Allocator>&
2473basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2474{
2475 pointer __p;
2476 if (__is_long())
2477 {
2478 __p = __get_long_pointer();
2479 __set_long_size(1);
2480 }
2481 else
2482 {
2483 __p = __get_short_pointer();
2484 __set_short_size(1);
2485 }
2486 traits_type::assign(*__p, __c);
2487 traits_type::assign(*++__p, value_type());
2488 __invalidate_iterators_past(1);
2489 return *this;
2490}
2491
2492template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002493_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002494basic_string<_CharT, _Traits, _Allocator>&
2495basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2496{
Martijn Vels596e3de2020-02-26 15:55:49 -05002497 if (this != &__str) {
2498 __copy_assign_alloc(__str);
2499 if (!__is_long()) {
2500 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002501 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002502 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002503 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002504 }
2505 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002506 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002507 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002508 }
2509 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002510}
2511
Eric Fiselierfc92be82017-04-19 00:28:44 +00002512#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002513
2514template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002515inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002516void
2517basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002518 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002519{
2520 if (__alloc() != __str.__alloc())
2521 assign(__str);
2522 else
2523 __move_assign(__str, true_type());
2524}
2525
2526template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002527inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002528void
2529basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002530#if _LIBCPP_STD_VER > 14
2531 _NOEXCEPT
2532#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002533 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002534#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002535{
marshall2ed41622019-11-27 07:13:00 -08002536 if (__is_long()) {
2537 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2538 __get_long_cap());
2539#if _LIBCPP_STD_VER <= 14
2540 if (!is_nothrow_move_assignable<allocator_type>::value) {
2541 __set_short_size(0);
2542 traits_type::assign(__get_short_pointer()[0], value_type());
2543 }
2544#endif
2545 }
2546 __move_assign_alloc(__str);
2547 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002548 if (__libcpp_is_constant_evaluated()) {
2549 __str.__default_init();
2550 } else {
2551 __str.__set_short_size(0);
2552 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2553 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002554}
2555
2556template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002557inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002558basic_string<_CharT, _Traits, _Allocator>&
2559basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002560 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002561{
2562 __move_assign(__str, integral_constant<bool,
2563 __alloc_traits::propagate_on_container_move_assignment::value>());
2564 return *this;
2565}
2566
2567#endif
2568
2569template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002571_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002572__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002574 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002576>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2578{
Marshall Clow958362f2016-09-05 01:54:30 +00002579 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002580 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002581 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582}
2583
2584template <class _CharT, class _Traits, class _Allocator>
2585template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002586_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002587__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002589 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002591>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2593{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002595 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002596 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002597
2598 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2599 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002601 if (__cap < __n)
2602 {
2603 size_type __sz = size();
2604 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2605 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002606 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002607 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002608 traits_type::assign(*__p, *__first);
2609 traits_type::assign(*__p, value_type());
2610 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002611 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612 }
2613 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002614 {
2615 const basic_string __temp(__first, __last, __alloc());
2616 assign(__temp.data(), __temp.size());
2617 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618 return *this;
2619}
2620
2621template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002622_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623basic_string<_CharT, _Traits, _Allocator>&
2624basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2625{
2626 size_type __sz = __str.size();
2627 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002628 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002629 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630}
2631
2632template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002633template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002634_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002635__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002636<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002637 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2638 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002639 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002640>
Marshall Clow82513342016-09-24 22:45:42 +00002641basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002642{
Marshall Clow82513342016-09-24 22:45:42 +00002643 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002644 size_type __sz = __sv.size();
2645 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002646 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002647 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002648}
2649
2650
2651template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002652_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002654basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2655 return __assign_external(__s, traits_type::length(__s));
2656}
2657
2658template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002659_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsda7d94f2020-06-19 14:24:03 -04002660basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002661basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002662{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002663 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002664 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002665 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002666 ? __assign_short(__s, traits_type::length(__s))
2667 : __assign_external(__s, traits_type::length(__s)))
2668 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670// append
2671
2672template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002673_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002675basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002677 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 size_type __cap = capacity();
2679 size_type __sz = size();
2680 if (__cap - __sz >= __n)
2681 {
2682 if (__n)
2683 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002684 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 traits_type::copy(__p + __sz, __s, __n);
2686 __sz += __n;
2687 __set_size(__sz);
2688 traits_type::assign(__p[__sz], value_type());
2689 }
2690 }
2691 else
2692 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2693 return *this;
2694}
2695
2696template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002697_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698basic_string<_CharT, _Traits, _Allocator>&
2699basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2700{
2701 if (__n)
2702 {
2703 size_type __cap = capacity();
2704 size_type __sz = size();
2705 if (__cap - __sz < __n)
2706 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2707 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002708 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709 __sz += __n;
2710 __set_size(__sz);
2711 traits_type::assign(__p[__sz], value_type());
2712 }
2713 return *this;
2714}
2715
2716template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002717_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002718basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2719{
2720 if (__n)
2721 {
2722 size_type __cap = capacity();
2723 size_type __sz = size();
2724 if (__cap - __sz < __n)
2725 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2726 pointer __p = __get_pointer();
2727 __sz += __n;
2728 __set_size(__sz);
2729 traits_type::assign(__p[__sz], value_type());
2730 }
2731}
2732
2733template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002734_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735void
2736basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2737{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002738 bool __is_short = !__is_long();
2739 size_type __cap;
2740 size_type __sz;
2741 if (__is_short)
2742 {
2743 __cap = __min_cap - 1;
2744 __sz = __get_short_size();
2745 }
2746 else
2747 {
2748 __cap = __get_long_cap() - 1;
2749 __sz = __get_long_size();
2750 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002752 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002754 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002755 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002756 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002757 if (__is_short)
2758 {
2759 __p = __get_short_pointer() + __sz;
2760 __set_short_size(__sz+1);
2761 }
2762 else
2763 {
2764 __p = __get_long_pointer() + __sz;
2765 __set_long_size(__sz+1);
2766 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767 traits_type::assign(*__p, __c);
2768 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769}
2770
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771template <class _CharT, class _Traits, class _Allocator>
2772template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002773_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002774__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002775<
2776 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2777 basic_string<_CharT, _Traits, _Allocator>&
2778>
2779basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002780 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781{
2782 size_type __sz = size();
2783 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002784 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785 if (__n)
2786 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002787 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2788 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002789 {
2790 if (__cap - __sz < __n)
2791 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2792 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002793 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002794 traits_type::assign(*__p, *__first);
2795 traits_type::assign(*__p, value_type());
2796 __set_size(__sz + __n);
2797 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002798 else
2799 {
2800 const basic_string __temp(__first, __last, __alloc());
2801 append(__temp.data(), __temp.size());
2802 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803 }
2804 return *this;
2805}
2806
2807template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002808inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809basic_string<_CharT, _Traits, _Allocator>&
2810basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2811{
2812 return append(__str.data(), __str.size());
2813}
2814
2815template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002816_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817basic_string<_CharT, _Traits, _Allocator>&
2818basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2819{
2820 size_type __sz = __str.size();
2821 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002822 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002823 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824}
2825
2826template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002827template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002828_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002829 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002830 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002831 __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 +00002832 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002833 >
Marshall Clow82513342016-09-24 22:45:42 +00002834basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002835{
Marshall Clow82513342016-09-24 22:45:42 +00002836 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002837 size_type __sz = __sv.size();
2838 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002839 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002840 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002841}
2842
2843template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002844_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002846basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002848 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 return append(__s, traits_type::length(__s));
2850}
2851
2852// insert
2853
2854template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002855_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002857basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002859 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 size_type __sz = size();
2861 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002862 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002864 if (__libcpp_is_constant_evaluated()) {
2865 if (__cap - __sz >= __n)
2866 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2867 else
2868 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2869 return *this;
2870 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871 if (__cap - __sz >= __n)
2872 {
2873 if (__n)
2874 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002875 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876 size_type __n_move = __sz - __pos;
2877 if (__n_move != 0)
2878 {
2879 if (__p + __pos <= __s && __s < __p + __sz)
2880 __s += __n;
2881 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2882 }
2883 traits_type::move(__p + __pos, __s, __n);
2884 __sz += __n;
2885 __set_size(__sz);
2886 traits_type::assign(__p[__sz], value_type());
2887 }
2888 }
2889 else
2890 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2891 return *this;
2892}
2893
2894template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002895_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896basic_string<_CharT, _Traits, _Allocator>&
2897basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2898{
2899 size_type __sz = size();
2900 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002901 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002902 if (__n)
2903 {
2904 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002905 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 if (__cap - __sz >= __n)
2907 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002908 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 size_type __n_move = __sz - __pos;
2910 if (__n_move != 0)
2911 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2912 }
2913 else
2914 {
2915 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002916 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917 }
2918 traits_type::assign(__p + __pos, __n, __c);
2919 __sz += __n;
2920 __set_size(__sz);
2921 traits_type::assign(__p[__sz], value_type());
2922 }
2923 return *this;
2924}
2925
2926template <class _CharT, class _Traits, class _Allocator>
2927template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002928_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002929__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002931 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002932 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002933>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2935{
Nikolas Klausereed25832021-12-15 01:32:30 +01002936 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2937 "string::insert(iterator, range) called with an iterator not"
2938 " referring to this string");
2939 const basic_string __temp(__first, __last, __alloc());
2940 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941}
2942
2943template <class _CharT, class _Traits, class _Allocator>
2944template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002945_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002946__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002948 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002950>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2952{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002953 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2954 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002955
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002957 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2958 if (__n == 0)
2959 return begin() + __ip;
2960
2961 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002963 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002965 else
2966 {
2967 const basic_string __temp(__first, __last, __alloc());
2968 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2969 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970}
2971
2972template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002973inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974basic_string<_CharT, _Traits, _Allocator>&
2975basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2976{
2977 return insert(__pos1, __str.data(), __str.size());
2978}
2979
2980template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002981_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982basic_string<_CharT, _Traits, _Allocator>&
2983basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2984 size_type __pos2, size_type __n)
2985{
2986 size_type __str_sz = __str.size();
2987 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002988 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002989 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990}
2991
2992template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002993template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002994_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002995__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002996<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002997 __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 +00002998 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002999>
Marshall Clow82513342016-09-24 22:45:42 +00003000basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003001 size_type __pos2, size_type __n)
3002{
Marshall Clow82513342016-09-24 22:45:42 +00003003 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003004 size_type __str_sz = __sv.size();
3005 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003006 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003007 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003008}
3009
3010template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003011_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003012basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003013basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003015 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003016 return insert(__pos, __s, traits_type::length(__s));
3017}
3018
3019template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003020_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021typename basic_string<_CharT, _Traits, _Allocator>::iterator
3022basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
3023{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05003024 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3025 "string::insert(iterator, character) called with an iterator not"
3026 " referring to this string");
3027
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028 size_type __ip = static_cast<size_type>(__pos - begin());
3029 size_type __sz = size();
3030 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003031 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032 if (__cap == __sz)
3033 {
3034 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003035 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036 }
3037 else
3038 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003039 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 size_type __n_move = __sz - __ip;
3041 if (__n_move != 0)
3042 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3043 }
3044 traits_type::assign(__p[__ip], __c);
3045 traits_type::assign(__p[++__sz], value_type());
3046 __set_size(__sz);
3047 return begin() + static_cast<difference_type>(__ip);
3048}
3049
3050template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003051inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052typename basic_string<_CharT, _Traits, _Allocator>::iterator
3053basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3054{
Nikolas Klausereed25832021-12-15 01:32:30 +01003055 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3056 "string::insert(iterator, n, value) called with an iterator not"
3057 " referring to this string");
3058 difference_type __p = __pos - begin();
3059 insert(static_cast<size_type>(__p), __n, __c);
3060 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061}
3062
3063// replace
3064
3065template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003066_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003068basic_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 +00003069 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003071 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072 size_type __sz = size();
3073 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003074 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003075 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076 size_type __cap = capacity();
3077 if (__cap - __sz + __n1 >= __n2)
3078 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003079 if (__libcpp_is_constant_evaluated()) {
3080 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3081 return *this;
3082 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003083 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084 if (__n1 != __n2)
3085 {
3086 size_type __n_move = __sz - __pos - __n1;
3087 if (__n_move != 0)
3088 {
3089 if (__n1 > __n2)
3090 {
3091 traits_type::move(__p + __pos, __s, __n2);
3092 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003093 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094 }
3095 if (__p + __pos < __s && __s < __p + __sz)
3096 {
3097 if (__p + __pos + __n1 <= __s)
3098 __s += __n2 - __n1;
3099 else // __p + __pos < __s < __p + __pos + __n1
3100 {
3101 traits_type::move(__p + __pos, __s, __n1);
3102 __pos += __n1;
3103 __s += __n2;
3104 __n2 -= __n1;
3105 __n1 = 0;
3106 }
3107 }
3108 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3109 }
3110 }
3111 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003112 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 }
3114 else
3115 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3116 return *this;
3117}
3118
3119template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003120_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121basic_string<_CharT, _Traits, _Allocator>&
3122basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3123{
3124 size_type __sz = size();
3125 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003126 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003127 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003129 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 if (__cap - __sz + __n1 >= __n2)
3131 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003132 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 if (__n1 != __n2)
3134 {
3135 size_type __n_move = __sz - __pos - __n1;
3136 if (__n_move != 0)
3137 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3138 }
3139 }
3140 else
3141 {
3142 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003143 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144 }
3145 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003146 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147}
3148
3149template <class _CharT, class _Traits, class _Allocator>
3150template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003151_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003152__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003154 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003156>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003157basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158 _InputIterator __j1, _InputIterator __j2)
3159{
Marshall Clow958362f2016-09-05 01:54:30 +00003160 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003161 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162}
3163
3164template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003165inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166basic_string<_CharT, _Traits, _Allocator>&
3167basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3168{
3169 return replace(__pos1, __n1, __str.data(), __str.size());
3170}
3171
3172template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003173_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174basic_string<_CharT, _Traits, _Allocator>&
3175basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3176 size_type __pos2, size_type __n2)
3177{
3178 size_type __str_sz = __str.size();
3179 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003180 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003181 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182}
3183
3184template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003185template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003186_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003187__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003188<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003189 __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 +00003190 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003191>
Marshall Clow82513342016-09-24 22:45:42 +00003192basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003193 size_type __pos2, size_type __n2)
3194{
Marshall Clow82513342016-09-24 22:45:42 +00003195 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003196 size_type __str_sz = __sv.size();
3197 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003198 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003199 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003200}
3201
3202template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003203_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003204basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003205basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003207 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208 return replace(__pos, __n1, __s, traits_type::length(__s));
3209}
3210
3211template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003212inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003214basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215{
3216 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3217 __str.data(), __str.size());
3218}
3219
3220template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003221inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003223basic_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 +00003224{
3225 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3226}
3227
3228template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003229inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003231basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232{
3233 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3234}
3235
3236template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003237inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003239basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240{
3241 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3242}
3243
3244// erase
3245
Martijn Velsa81fc792020-02-26 13:25:43 -05003246// 'externally instantiated' erase() implementation, called when __n != npos.
3247// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003249_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsa81fc792020-02-26 13:25:43 -05003250void
3251basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3252 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254 if (__n)
3255 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003256 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003257 value_type* __p = std::__to_address(__get_pointer());
3258 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259 size_type __n_move = __sz - __pos - __n;
3260 if (__n_move != 0)
3261 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003262 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003264}
3265
3266template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003267_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsa81fc792020-02-26 13:25:43 -05003268basic_string<_CharT, _Traits, _Allocator>&
3269basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3270 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003271 if (__pos > size())
3272 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003273 if (__n == npos) {
3274 __erase_to_end(__pos);
3275 } else {
3276 __erase_external_with_move(__pos, __n);
3277 }
3278 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279}
3280
3281template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003282inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003283typename basic_string<_CharT, _Traits, _Allocator>::iterator
3284basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3285{
Nikolas Klausereed25832021-12-15 01:32:30 +01003286 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3287 "string::erase(iterator) called with an iterator not"
3288 " referring to this string");
3289
3290 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3291 iterator __b = begin();
3292 size_type __r = static_cast<size_type>(__pos - __b);
3293 erase(__r, 1);
3294 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295}
3296
3297template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003298inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299typename basic_string<_CharT, _Traits, _Allocator>::iterator
3300basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3301{
Nikolas Klausereed25832021-12-15 01:32:30 +01003302 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3303 "string::erase(iterator, iterator) called with an iterator not"
3304 " referring to this string");
3305
3306 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3307 iterator __b = begin();
3308 size_type __r = static_cast<size_type>(__first - __b);
3309 erase(__r, static_cast<size_type>(__last - __first));
3310 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311}
3312
3313template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003314inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003315void
3316basic_string<_CharT, _Traits, _Allocator>::pop_back()
3317{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003318 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003319 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320}
3321
3322template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003323inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003325basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003326{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003327 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328 if (__is_long())
3329 {
3330 traits_type::assign(*__get_long_pointer(), value_type());
3331 __set_long_size(0);
3332 }
3333 else
3334 {
3335 traits_type::assign(*__get_short_pointer(), value_type());
3336 __set_short_size(0);
3337 }
3338}
3339
3340template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003341inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342void
3343basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3344{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003345 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346}
3347
3348template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003349_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350void
3351basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3352{
3353 size_type __sz = size();
3354 if (__n > __sz)
3355 append(__n - __sz, __c);
3356 else
3357 __erase_to_end(__n);
3358}
3359
3360template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003361_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003362basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3363{
3364 size_type __sz = size();
3365 if (__n > __sz) {
3366 __append_default_init(__n - __sz);
3367 } else
3368 __erase_to_end(__n);
3369}
3370
3371template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003372inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003374basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003375{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003376 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003377 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3378 return __m - __alignment;
3379 } else {
3380 bool __uses_lsb = __endian_factor == 2;
3381 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3382 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383}
3384
3385template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003386_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387void
Marek Kurdejc9848142020-11-26 10:07:16 +01003388basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389{
Marek Kurdejc9848142020-11-26 10:07:16 +01003390 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003391 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003392
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003393 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3394 // and later (since P0966R1), however we provide consistent behavior in all Standard
3395 // modes because this function is instantiated in the shared library.
3396 if (__requested_capacity <= capacity())
3397 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003398
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003399 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003400 __target_capacity = __recommend(__target_capacity);
3401 if (__target_capacity == capacity()) return;
3402
3403 __shrink_or_extend(__target_capacity);
3404}
3405
3406template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003407inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marek Kurdejc9848142020-11-26 10:07:16 +01003408void
3409basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3410{
3411 size_type __target_capacity = __recommend(size());
3412 if (__target_capacity == capacity()) return;
3413
3414 __shrink_or_extend(__target_capacity);
3415}
3416
3417template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003418inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marek Kurdejc9848142020-11-26 10:07:16 +01003419void
3420basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3421{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003422 size_type __cap = capacity();
3423 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003424
3425 pointer __new_data, __p;
3426 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003427 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003429 __was_long = true;
3430 __now_long = false;
3431 __new_data = __get_short_pointer();
3432 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003433 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003434 else
3435 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003436 if (__target_capacity > __cap) {
3437 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3438 __new_data = __allocation.ptr;
3439 __target_capacity = __allocation.count - 1;
3440 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003441 else
3442 {
3443 #ifndef _LIBCPP_NO_EXCEPTIONS
3444 try
3445 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003446 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003447 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3448 __new_data = __allocation.ptr;
3449 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003450 #ifndef _LIBCPP_NO_EXCEPTIONS
3451 }
3452 catch (...)
3453 {
3454 return;
3455 }
3456 #else // _LIBCPP_NO_EXCEPTIONS
3457 if (__new_data == nullptr)
3458 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003459 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003460 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003461 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003462 __now_long = true;
3463 __was_long = __is_long();
3464 __p = __get_pointer();
3465 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003466 traits_type::copy(std::__to_address(__new_data),
3467 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003468 if (__was_long)
3469 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3470 if (__now_long)
3471 {
3472 __set_long_cap(__target_capacity+1);
3473 __set_long_size(__sz);
3474 __set_long_pointer(__new_data);
3475 }
3476 else
3477 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003478 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479}
3480
3481template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003482inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003484basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003486 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487 return *(data() + __pos);
3488}
3489
3490template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003491inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003493basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003495 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496 return *(__get_pointer() + __pos);
3497}
3498
3499template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003500_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3502basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3503{
3504 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003505 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506 return (*this)[__n];
3507}
3508
3509template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003510_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511typename basic_string<_CharT, _Traits, _Allocator>::reference
3512basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3513{
3514 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003515 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516 return (*this)[__n];
3517}
3518
3519template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003520inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003522basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003524 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525 return *__get_pointer();
3526}
3527
3528template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003529inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003531basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003533 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534 return *data();
3535}
3536
3537template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003538inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003540basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003542 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543 return *(__get_pointer() + size() - 1);
3544}
3545
3546template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003547inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003549basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003551 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552 return *(data() + size() - 1);
3553}
3554
3555template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003556_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003558basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
3560 size_type __sz = size();
3561 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003562 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003563 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564 traits_type::copy(__s, data() + __pos, __rlen);
3565 return __rlen;
3566}
3567
3568template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003569inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570basic_string<_CharT, _Traits, _Allocator>
3571basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3572{
3573 return basic_string(*this, __pos, __n, __alloc());
3574}
3575
3576template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003577inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578void
3579basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003580#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003581 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003582#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003583 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003584 __is_nothrow_swappable<allocator_type>::value)
3585#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586{
Nikolas Klauser98833542022-05-07 22:20:23 +02003587 if (!__is_long())
3588 std::__debug_db_invalidate_all(this);
3589 if (!__str.__is_long())
3590 std::__debug_db_invalidate_all(&__str);
3591 std::__debug_db_swap(this, &__str);
3592
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003593 _LIBCPP_ASSERT(
3594 __alloc_traits::propagate_on_container_swap::value ||
3595 __alloc_traits::is_always_equal::value ||
3596 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003597 std::swap(__r_.first(), __str.__r_.first());
3598 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599}
3600
3601// find
3602
3603template <class _Traits>
3604struct _LIBCPP_HIDDEN __traits_eq
3605{
3606 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003607 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003608 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3609 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610};
3611
3612template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003613_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003615basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003616 size_type __pos,
3617 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003619 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003620 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003621 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622}
3623
3624template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003625inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003627basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3628 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003630 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003631 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632}
3633
3634template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003635template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003636_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003637__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003638<
3639 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3640 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003641>
Marshall Clowe46031a2018-07-02 18:41:15 +00003642basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003643 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003644{
Marshall Clowe46031a2018-07-02 18:41:15 +00003645 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003646 return __str_find<value_type, size_type, traits_type, npos>
3647 (data(), size(), __sv.data(), __pos, __sv.size());
3648}
3649
3650template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003651inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003652typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003653basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003654 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003656 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003657 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003658 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659}
3660
3661template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003662_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003664basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3665 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003667 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003668 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669}
3670
3671// rfind
3672
3673template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003674_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003676basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003677 size_type __pos,
3678 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003680 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003681 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003682 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683}
3684
3685template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003686inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003688basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3689 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003691 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003692 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693}
3694
3695template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003696template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003697_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003698__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003699<
3700 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3701 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003702>
Marshall Clowe46031a2018-07-02 18:41:15 +00003703basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003704 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003705{
Marshall Clowe46031a2018-07-02 18:41:15 +00003706 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003707 return __str_rfind<value_type, size_type, traits_type, npos>
3708 (data(), size(), __sv.data(), __pos, __sv.size());
3709}
3710
3711template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003712inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003713typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003714basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003715 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003717 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003718 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003719 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720}
3721
3722template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003723_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003725basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3726 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003728 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003729 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730}
3731
3732// find_first_of
3733
3734template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003735_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003737basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003738 size_type __pos,
3739 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003741 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003742 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003743 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744}
3745
3746template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003747inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003749basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3750 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003752 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003753 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754}
3755
3756template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003757template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003758_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003759__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003760<
3761 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3762 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003763>
Marshall Clowe46031a2018-07-02 18:41:15 +00003764basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003765 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003766{
Marshall Clowe46031a2018-07-02 18:41:15 +00003767 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003768 return __str_find_first_of<value_type, size_type, traits_type, npos>
3769 (data(), size(), __sv.data(), __pos, __sv.size());
3770}
3771
3772template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003773inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003774typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003775basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003776 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003778 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003779 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003780 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781}
3782
3783template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003784inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003786basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3787 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788{
3789 return find(__c, __pos);
3790}
3791
3792// find_last_of
3793
3794template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003795inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003797basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003798 size_type __pos,
3799 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003800{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003801 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003802 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003803 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804}
3805
3806template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003807inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003808typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003809basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3810 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003812 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003813 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814}
3815
3816template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003817template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003818_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003819__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003820<
3821 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3822 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003823>
Marshall Clowe46031a2018-07-02 18:41:15 +00003824basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003825 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003826{
Marshall Clowe46031a2018-07-02 18:41:15 +00003827 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003828 return __str_find_last_of<value_type, size_type, traits_type, npos>
3829 (data(), size(), __sv.data(), __pos, __sv.size());
3830}
3831
3832template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003833inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003834typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003835basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003836 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003838 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003839 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003840 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003841}
3842
3843template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003844inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003846basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3847 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848{
3849 return rfind(__c, __pos);
3850}
3851
3852// find_first_not_of
3853
3854template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003855_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003857basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003858 size_type __pos,
3859 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003861 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003862 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003863 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864}
3865
3866template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003867inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003868typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003869basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3870 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003871{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003872 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003873 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874}
3875
3876template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003877template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003878_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003879__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003880<
3881 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3882 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003883>
Marshall Clowe46031a2018-07-02 18:41:15 +00003884basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003885 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003886{
Marshall Clowe46031a2018-07-02 18:41:15 +00003887 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003888 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3889 (data(), size(), __sv.data(), __pos, __sv.size());
3890}
3891
3892template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003893inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003894typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003895basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003896 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003897{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003898 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003899 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003900 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901}
3902
3903template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003904inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003906basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3907 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003909 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003910 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911}
3912
3913// find_last_not_of
3914
3915template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003916_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003918basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003919 size_type __pos,
3920 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003922 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003923 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003924 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925}
3926
3927template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003928inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003930basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3931 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003933 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003934 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935}
3936
3937template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003938template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003939_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003940__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003941<
3942 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3943 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003944>
Marshall Clowe46031a2018-07-02 18:41:15 +00003945basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003946 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003947{
Marshall Clowe46031a2018-07-02 18:41:15 +00003948 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003949 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3950 (data(), size(), __sv.data(), __pos, __sv.size());
3951}
3952
3953template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003954inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003955typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003956basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003957 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003959 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003960 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003961 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003962}
3963
3964template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003965inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003967basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3968 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003970 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003971 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972}
3973
3974// compare
3975
3976template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003977template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003978_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003979__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003980<
3981 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3982 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003983>
zoecarver1997e0a2021-02-05 11:54:47 -08003984basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985{
Marshall Clowe46031a2018-07-02 18:41:15 +00003986 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003987 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003988 size_t __rhs_sz = __sv.size();
3989 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003990 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003991 if (__result != 0)
3992 return __result;
3993 if (__lhs_sz < __rhs_sz)
3994 return -1;
3995 if (__lhs_sz > __rhs_sz)
3996 return 1;
3997 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998}
3999
4000template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004001inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004003basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004005 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006}
4007
4008template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004009inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004010int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004011basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4012 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00004013 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004014 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015{
Alp Tokerb8a95f52014-05-15 11:27:39 +00004016 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017 size_type __sz = size();
4018 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004019 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004020 size_type __rlen = std::min(__n1, __sz - __pos1);
4021 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022 if (__r == 0)
4023 {
4024 if (__rlen < __n2)
4025 __r = -1;
4026 else if (__rlen > __n2)
4027 __r = 1;
4028 }
4029 return __r;
4030}
4031
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004032template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00004033template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004034_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04004035__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00004036<
4037 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
4038 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004039>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004040basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4041 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00004042 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004043{
Marshall Clowe46031a2018-07-02 18:41:15 +00004044 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004045 return compare(__pos1, __n1, __sv.data(), __sv.size());
4046}
4047
4048template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004049inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004050int
4051basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4052 size_type __n1,
4053 const basic_string& __str) const
4054{
4055 return compare(__pos1, __n1, __str.data(), __str.size());
4056}
4057
4058template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00004059template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004060_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04004061__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00004062<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004063 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
4064 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00004065 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004066>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004067basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4068 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00004069 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004070 size_type __pos2,
4071 size_type __n2) const
4072{
Marshall Clow82513342016-09-24 22:45:42 +00004073 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004074 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
4075}
4076
4077template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004078_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004079int
4080basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4081 size_type __n1,
4082 const basic_string& __str,
4083 size_type __pos2,
4084 size_type __n2) const
4085{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004086 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004087}
4088
4089template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004090_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004091int
4092basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4093{
4094 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4095 return compare(0, npos, __s, traits_type::length(__s));
4096}
4097
4098template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004099_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004100int
4101basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4102 size_type __n1,
4103 const value_type* __s) const
4104{
4105 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4106 return compare(__pos1, __n1, __s, traits_type::length(__s));
4107}
4108
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109// __invariants
4110
4111template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004112inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113bool
4114basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4115{
4116 if (size() > capacity())
4117 return false;
4118 if (capacity() < __min_cap - 1)
4119 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004120 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004122 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123 return false;
4124 return true;
4125}
4126
Vedant Kumar55e007e2018-03-08 21:15:26 +00004127// __clear_and_shrink
4128
4129template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004130inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne173f29e2019-05-29 16:01:36 +00004131void
Marshall Clowe60a7182018-05-29 17:04:37 +00004132basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004133{
4134 clear();
4135 if(__is_long())
4136 {
4137 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4138 __set_long_cap(0);
4139 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004140 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004141 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004142}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004143
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144// operator==
4145
4146template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004147inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004148bool
4149operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004150 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004152 size_t __lhs_sz = __lhs.size();
4153 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4154 __rhs.data(),
4155 __lhs_sz) == 0;
4156}
4157
4158template<class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004159inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004160bool
4161operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4162 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4163{
4164 size_t __lhs_sz = __lhs.size();
4165 if (__lhs_sz != __rhs.size())
4166 return false;
4167 const char* __lp = __lhs.data();
4168 const char* __rp = __rhs.data();
4169 if (__lhs.__is_long())
4170 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4171 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4172 if (*__lp != *__rp)
4173 return false;
4174 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004175}
4176
4177template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004178inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004180operator==(const _CharT* __lhs,
4181 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004183 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4184 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4185 size_t __lhs_len = _Traits::length(__lhs);
4186 if (__lhs_len != __rhs.size()) return false;
4187 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188}
4189
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004191inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004193operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4194 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004196 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4197 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4198 size_t __rhs_len = _Traits::length(__rhs);
4199 if (__rhs_len != __lhs.size()) return false;
4200 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201}
4202
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004203template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004204inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205bool
4206operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004207 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208{
4209 return !(__lhs == __rhs);
4210}
4211
4212template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004213inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004215operator!=(const _CharT* __lhs,
4216 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217{
4218 return !(__lhs == __rhs);
4219}
4220
4221template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004222inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004224operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4225 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226{
4227 return !(__lhs == __rhs);
4228}
4229
4230// operator<
4231
4232template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004233inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234bool
4235operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004236 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004238 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239}
4240
4241template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004242inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004244operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4245 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004247 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248}
4249
4250template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004251inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004253operator< (const _CharT* __lhs,
4254 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255{
4256 return __rhs.compare(__lhs) > 0;
4257}
4258
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259// operator>
4260
4261template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004262inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263bool
4264operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004265 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004266{
4267 return __rhs < __lhs;
4268}
4269
4270template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004271inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004273operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4274 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275{
4276 return __rhs < __lhs;
4277}
4278
4279template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004280inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004282operator> (const _CharT* __lhs,
4283 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284{
4285 return __rhs < __lhs;
4286}
4287
4288// operator<=
4289
4290template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004291inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292bool
4293operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004294 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004295{
4296 return !(__rhs < __lhs);
4297}
4298
4299template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004300inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004301bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004302operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4303 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304{
4305 return !(__rhs < __lhs);
4306}
4307
4308template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004309inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004311operator<=(const _CharT* __lhs,
4312 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313{
4314 return !(__rhs < __lhs);
4315}
4316
4317// operator>=
4318
4319template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004320inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321bool
4322operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004323 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324{
4325 return !(__lhs < __rhs);
4326}
4327
4328template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004329inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004331operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4332 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333{
4334 return !(__lhs < __rhs);
4335}
4336
4337template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004338inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004340operator>=(const _CharT* __lhs,
4341 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342{
4343 return !(__lhs < __rhs);
4344}
4345
4346// operator +
4347
4348template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004349_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350basic_string<_CharT, _Traits, _Allocator>
4351operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4352 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4353{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004354 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004355 auto __lhs_sz = __lhs.size();
4356 auto __rhs_sz = __rhs.size();
4357 _String __r(__uninitialized_size_tag(),
4358 __lhs_sz + __rhs_sz,
4359 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4360 auto __ptr = std::__to_address(__r.__get_pointer());
4361 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4362 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4363 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364 return __r;
4365}
4366
4367template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004368_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369basic_string<_CharT, _Traits, _Allocator>
4370operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4371{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004372 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004373 auto __lhs_sz = _Traits::length(__lhs);
4374 auto __rhs_sz = __rhs.size();
4375 _String __r(__uninitialized_size_tag(),
4376 __lhs_sz + __rhs_sz,
4377 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4378 auto __ptr = std::__to_address(__r.__get_pointer());
4379 _Traits::copy(__ptr, __lhs, __lhs_sz);
4380 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4381 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382 return __r;
4383}
4384
4385template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004386_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004387basic_string<_CharT, _Traits, _Allocator>
4388operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4389{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004390 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004391 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004392 _String __r(__uninitialized_size_tag(),
4393 __rhs_sz + 1,
4394 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4395 auto __ptr = std::__to_address(__r.__get_pointer());
4396 _Traits::assign(__ptr, 1, __lhs);
4397 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4398 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004399 return __r;
4400}
4401
4402template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004403inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404basic_string<_CharT, _Traits, _Allocator>
4405operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4406{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004407 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004408 typename _String::size_type __lhs_sz = __lhs.size();
4409 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004410 _String __r(__uninitialized_size_tag(),
4411 __lhs_sz + __rhs_sz,
4412 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4413 auto __ptr = std::__to_address(__r.__get_pointer());
4414 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4415 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4416 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417 return __r;
4418}
4419
4420template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004421_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422basic_string<_CharT, _Traits, _Allocator>
4423operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4424{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004425 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004426 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004427 _String __r(__uninitialized_size_tag(),
4428 __lhs_sz + 1,
4429 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4430 auto __ptr = std::__to_address(__r.__get_pointer());
4431 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4432 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4433 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434 return __r;
4435}
4436
Eric Fiselierfc92be82017-04-19 00:28:44 +00004437#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438
4439template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004440inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441basic_string<_CharT, _Traits, _Allocator>
4442operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4443{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004444 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004445}
4446
4447template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004448inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004449basic_string<_CharT, _Traits, _Allocator>
4450operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4451{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004452 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004453}
4454
4455template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004456inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004457basic_string<_CharT, _Traits, _Allocator>
4458operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4459{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004460 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004461}
4462
4463template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004464inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004465basic_string<_CharT, _Traits, _Allocator>
4466operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4467{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004468 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004469}
4470
4471template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004472inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004473basic_string<_CharT, _Traits, _Allocator>
4474operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4475{
4476 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004477 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004478}
4479
4480template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004481inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004482basic_string<_CharT, _Traits, _Allocator>
4483operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4484{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004485 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486}
4487
4488template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004489inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490basic_string<_CharT, _Traits, _Allocator>
4491operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4492{
4493 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004494 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495}
4496
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004497#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004498
4499// swap
4500
4501template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004502inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004504swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004505 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4506 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507{
4508 __lhs.swap(__rhs);
4509}
4510
Bruce Mitchener170d8972020-11-24 12:53:53 -05004511_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4512_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4513_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4514_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4515_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004516
Bruce Mitchener170d8972020-11-24 12:53:53 -05004517_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4518_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4519_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004520
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004521_LIBCPP_FUNC_VIS string to_string(int __val);
4522_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4523_LIBCPP_FUNC_VIS string to_string(long __val);
4524_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4525_LIBCPP_FUNC_VIS string to_string(long long __val);
4526_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4527_LIBCPP_FUNC_VIS string to_string(float __val);
4528_LIBCPP_FUNC_VIS string to_string(double __val);
4529_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004530
Louis Dionne89258142021-08-23 15:32:36 -04004531#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004532_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4533_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4534_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4535_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4536_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004537
Bruce Mitchener170d8972020-11-24 12:53:53 -05004538_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4539_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4540_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004541
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004542_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4543_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4544_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4545_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4546_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4547_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4548_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4549_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4550_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004551#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004552
Howard Hinnantc51e1022010-05-11 19:42:16 +00004553template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004554_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004555const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4556 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004557
Marshall Clow851b9ec2019-05-20 21:56:51 +00004558template <class _CharT, class _Allocator>
4559struct _LIBCPP_TEMPLATE_VIS
4560 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
Nikolas Klauser672ee852022-06-22 10:11:14 +02004561 : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004562{
4563 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004564 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4565 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566};
4567
Howard Hinnantdc095972011-07-18 15:51:59 +00004568template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004569_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004570operator<<(basic_ostream<_CharT, _Traits>& __os,
4571 const basic_string<_CharT, _Traits, _Allocator>& __str);
4572
4573template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004574_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004575operator>>(basic_istream<_CharT, _Traits>& __is,
4576 basic_string<_CharT, _Traits, _Allocator>& __str);
4577
4578template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004579_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004580getline(basic_istream<_CharT, _Traits>& __is,
4581 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4582
4583template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004584inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004585basic_istream<_CharT, _Traits>&
4586getline(basic_istream<_CharT, _Traits>& __is,
4587 basic_string<_CharT, _Traits, _Allocator>& __str);
4588
Howard Hinnantdc095972011-07-18 15:51:59 +00004589template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004590inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004591basic_istream<_CharT, _Traits>&
4592getline(basic_istream<_CharT, _Traits>&& __is,
4593 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4594
4595template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004596inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004597basic_istream<_CharT, _Traits>&
4598getline(basic_istream<_CharT, _Traits>&& __is,
4599 basic_string<_CharT, _Traits, _Allocator>& __str);
4600
Marshall Clow29b53f22018-12-14 18:49:35 +00004601#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004602template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004603inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004604 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4605 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4606 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004607 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004608 return __old_size - __str.size();
4609}
Marshall Clow29b53f22018-12-14 18:49:35 +00004610
Marek Kurdeja98b1412020-05-02 13:58:03 +02004611template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004612inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004613 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4614 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4615 _Predicate __pred) {
4616 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004617 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004618 __str.end());
4619 return __old_size - __str.size();
4620}
Marshall Clow29b53f22018-12-14 18:49:35 +00004621#endif
4622
Louis Dionne510450b2022-04-01 16:38:30 -04004623#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004624
4625template<class _CharT, class _Traits, class _Allocator>
4626bool
4627basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4628{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004629 return data() <= std::__to_address(__i->base()) &&
4630 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004631}
4632
4633template<class _CharT, class _Traits, class _Allocator>
4634bool
4635basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4636{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004637 return data() < std::__to_address(__i->base()) &&
4638 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004639}
4640
4641template<class _CharT, class _Traits, class _Allocator>
4642bool
4643basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4644{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004645 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004646 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004647}
4648
4649template<class _CharT, class _Traits, class _Allocator>
4650bool
4651basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4652{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004653 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004654 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004655}
4656
Louis Dionne510450b2022-04-01 16:38:30 -04004657#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004658
Louis Dionne173f29e2019-05-29 16:01:36 +00004659#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004660// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004661inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004662{
4663 inline namespace string_literals
4664 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004665 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004666 basic_string<char> operator "" s( const char *__str, size_t __len )
4667 {
4668 return basic_string<char> (__str, __len);
4669 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004670
Louis Dionne89258142021-08-23 15:32:36 -04004671#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004672 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004673 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4674 {
4675 return basic_string<wchar_t> (__str, __len);
4676 }
Louis Dionne89258142021-08-23 15:32:36 -04004677#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004678
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004679#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004680 inline _LIBCPP_HIDE_FROM_ABI constexpr
Marshall Clow8732fed2018-12-11 04:35:44 +00004681 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4682 {
4683 return basic_string<char8_t> (__str, __len);
4684 }
4685#endif
4686
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004687 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004688 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4689 {
4690 return basic_string<char16_t> (__str, __len);
4691 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004692
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004693 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004694 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4695 {
4696 return basic_string<char32_t> (__str, __len);
4697 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004698 } // namespace string_literals
4699} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004700
4701#if _LIBCPP_STD_VER > 17
4702template <>
4703inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4704#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4705template <>
4706inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4707#endif
4708#endif
4709
Marshall Clowcba751f2013-07-23 17:05:24 +00004710#endif
4711
Howard Hinnantc51e1022010-05-11 19:42:16 +00004712_LIBCPP_END_NAMESPACE_STD
4713
Eric Fiselierf4433a32017-05-31 22:07:49 +00004714_LIBCPP_POP_MACROS
4715
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004716#endif // _LIBCPP_STRING