blob: 7229a568a2a10d55ac6a6bb2dcc732499aacfbc7 [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 Klauser11a0ff32022-06-06 23:35:24 +0200535#include <__string/char_traits.h>
536#include <__string/extern_template_lists.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100537#include <__utility/auto_cast.h>
538#include <__utility/move.h>
539#include <__utility/swap.h>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200540#include <__utility/unreachable.h>
541#include <climits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400542#include <compare>
543#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400544#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400545#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400546#include <initializer_list>
547#include <iosfwd>
Nikolas Klauserfb1b0672022-06-10 19:53:10 +0200548#include <iterator> // TODO: Remove this include
Nikolas Klauser62060be2022-04-20 10:52:04 +0200549#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550#include <memory>
551#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400552#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000554#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000555
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100556// TODO: remove these headers
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100557#include <new>
558#include <typeinfo>
559
Louis Dionne89258142021-08-23 15:32:36 -0400560#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100561# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400562#endif
563
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400564#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100565# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400566#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000567
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000568#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500569# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000570#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571
Eric Fiselierf4433a32017-05-31 22:07:49 +0000572_LIBCPP_PUSH_MACROS
573#include <__undef_macros>
574
575
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576_LIBCPP_BEGIN_NAMESPACE_STD
577
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578// basic_string
579
580template<class _CharT, class _Traits, class _Allocator>
581basic_string<_CharT, _Traits, _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200582_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant944510a2011-06-14 19:58:17 +0000583operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
584 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585
586template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200587_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000589operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590
591template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200592_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000594operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
596template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200597inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000599operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600
601template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200602_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000604operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605
Louis Dionnedc496ec2021-06-08 17:25:08 -0400606extern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Shoaib Meenaiea363712017-07-29 02:54:41 +0000607
Marshall Clow039b2f02016-01-13 21:54:34 +0000608template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400609struct __string_is_trivial_iterator : public false_type {};
610
611template <class _Tp>
612struct __string_is_trivial_iterator<_Tp*>
613 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000614
Louis Dionne173f29e2019-05-29 16:01:36 +0000615template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400616struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
617 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000618
Marshall Clow82513342016-09-24 22:45:42 +0000619template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500620struct __can_be_converted_to_string_view : public _BoolConstant<
621 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
622 !is_convertible<const _Tp&, const _CharT*>::value
623 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000624
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400625#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800626typedef basic_string<char8_t> u8string;
627#endif
628
629#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
630typedef basic_string<char16_t> u16string;
631typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400632#endif
Richard Smith256954d2020-11-11 17:12:18 -0800633
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200634struct __uninitialized_size_tag {};
635
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000636template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800637class
638 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400639#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800640 _LIBCPP_PREFERRED_NAME(u8string)
641#endif
642#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
643 _LIBCPP_PREFERRED_NAME(u16string)
644 _LIBCPP_PREFERRED_NAME(u32string)
645#endif
646 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000647{
648public:
649 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000650 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000652 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000653 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000654 typedef allocator_traits<allocator_type> __alloc_traits;
655 typedef typename __alloc_traits::size_type size_type;
656 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000657 typedef value_type& reference;
658 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000659 typedef typename __alloc_traits::pointer pointer;
660 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661
Marshall Clow79f33542018-03-21 00:36:05 +0000662 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
663 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
664 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
665 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000666 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000667 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000668 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000669
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670 typedef __wrap_iter<pointer> iterator;
671 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200672 typedef std::reverse_iterator<iterator> reverse_iterator;
673 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674
675private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200676 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000677
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000678#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000679
680 struct __long
681 {
682 pointer __data_;
683 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200684 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
685 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000686 };
687
Howard Hinnant68bf1812013-04-30 21:44:48 +0000688 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
689 (sizeof(__long) - 1)/sizeof(value_type) : 2};
690
691 struct __short
692 {
693 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200694 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200695 unsigned char __size_ : 7;
696 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000697 };
698
Nikolas Klauser62060be2022-04-20 10:52:04 +0200699// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200700// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200701//
702// If the LSB is used to store the short-flag in the short string representation,
703// we have to multiply the size by two when it is stored and divide it by two when
704// it is loaded to make sure that we always store an even number. In the long string
705// representation, we can ignore this because we can assume that we always allocate
706// an even amount of value_types.
707//
708// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
709// This does not impact the short string representation, since we never need the MSB
710// for representing the size of a short string anyway.
711
712#ifdef _LIBCPP_BIG_ENDIAN
713 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000714#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200715 static const size_type __endian_factor = 1;
716#endif
717
718#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
719
720#ifdef _LIBCPP_BIG_ENDIAN
721 static const size_type __endian_factor = 1;
722#else
723 static const size_type __endian_factor = 2;
724#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000725
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 struct __long
727 {
Nikolas Klauser62060be2022-04-20 10:52:04 +0200728 size_type __is_long_ : 1;
729 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730 size_type __size_;
731 pointer __data_;
732 };
733
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
735 (sizeof(__long) - 1)/sizeof(value_type) : 2};
736
737 struct __short
738 {
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200739 unsigned char __is_long_ : 1;
740 unsigned char __size_ : 7;
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200741 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742 value_type __data_[__min_cap];
743 };
744
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400745#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000746
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200747 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
748
Howard Hinnant8ea98242013-08-23 17:37:05 +0000749 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750
Howard Hinnant8ea98242013-08-23 17:37:05 +0000751 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752
753 struct __raw
754 {
755 size_type __words[__n_words];
756 };
757
758 struct __rep
759 {
760 union
761 {
762 __long __l;
763 __short __s;
764 __raw __r;
765 };
766 };
767
768 __compressed_pair<__rep, allocator_type> __r_;
769
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200770 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
771 // don't initialize the characters. The contents of the string, including the null terminator, must be
772 // initialized separately.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200773 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
774 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200775 : __r_(__default_init_tag(), __a) {
776 if (__size > max_size())
777 __throw_length_error();
778 if (__fits_in_sso(__size)) {
779 __zero();
780 __set_short_size(__size);
781 } else {
782 auto __capacity = __recommend(__size) + 1;
783 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200784 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200785 __set_long_cap(__capacity);
786 __set_long_pointer(__allocation);
787 __set_long_size(__size);
788 }
789 std::__debug_db_insert_c(this);
790 }
791
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200793 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794 static const size_type npos = -1;
795
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200796 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string()
Howard Hinnant3e276872011-06-03 18:40:47 +0000797 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000798
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200799 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000800#if _LIBCPP_STD_VER <= 14
801 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
802#else
803 _NOEXCEPT;
804#endif
805
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200806 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string(const basic_string& __str);
807 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000808
Eric Fiselierfc92be82017-04-19 00:28:44 +0000809#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200810 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +0000811 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000812#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000813 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000814#else
815 _NOEXCEPT;
816#endif
817
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200818 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400820#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000821
Louis Dionne9ce598d2021-09-08 09:14:43 -0400822 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200823 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500824 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000825 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
826 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200827 std::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000828 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000829
Louis Dionne9ce598d2021-09-08 09:14:43 -0400830 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200831 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000832 basic_string(const _CharT* __s, const _Allocator& __a);
833
Marek Kurdej90d79712021-07-27 16:16:21 +0200834#if _LIBCPP_STD_VER > 20
835 basic_string(nullptr_t) = delete;
836#endif
837
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200838 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000839 basic_string(const _CharT* __s, size_type __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200840 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000841 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200842 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000843 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000844
Louis Dionne9ce598d2021-09-08 09:14:43 -0400845 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200846 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000847 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
848
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200849 _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +0000850 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000851 const _Allocator& __a = _Allocator());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200852 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +0000853 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000854 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000855
Louis Dionne9ce598d2021-09-08 09:14:43 -0400856 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200857 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000858 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500859 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000860
Louis Dionne9ce598d2021-09-08 09:14:43 -0400861 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500862 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200863 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000864 explicit basic_string(const _Tp& __t);
865
Louis Dionne9ce598d2021-09-08 09:14:43 -0400866 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200867 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000868 explicit basic_string(const _Tp& __t, const allocator_type& __a);
869
Louis Dionne9ce598d2021-09-08 09:14:43 -0400870 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200871 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400873 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200874 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000876#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200877 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000878 basic_string(initializer_list<_CharT> __il);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200879 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000880 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400881#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200883 inline _LIBCPP_CONSTEXPR_AFTER_CXX17 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200885 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000886 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
887
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200888 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000889
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200890 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
891 !__is_same_uncvref<_Tp, basic_string>::value> >
892 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(const _Tp& __t) {
893 __self_view __sv = __t;
894 return assign(__sv);
895 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000896
Eric Fiselierfc92be82017-04-19 00:28:44 +0000897#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200898 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +0000899 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000900 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200901 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierfc92be82017-04-19 00:28:44 +0000902 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200904 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200905 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200906#if _LIBCPP_STD_VER > 20
907 basic_string& operator=(nullptr_t) = delete;
908#endif
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200909 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200911 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000912 iterator begin() _NOEXCEPT
913 {return iterator(this, __get_pointer());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200914 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000915 const_iterator begin() const _NOEXCEPT
916 {return const_iterator(this, __get_pointer());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200917 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000918 iterator end() _NOEXCEPT
919 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200920 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000921 const_iterator end() const _NOEXCEPT
922 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -0400923
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200924 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000925 reverse_iterator rbegin() _NOEXCEPT
926 {return reverse_iterator(end());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200927 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000928 const_reverse_iterator rbegin() const _NOEXCEPT
929 {return const_reverse_iterator(end());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200930 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000931 reverse_iterator rend() _NOEXCEPT
932 {return reverse_iterator(begin());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200933 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000934 const_reverse_iterator rend() const _NOEXCEPT
935 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200937 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000938 const_iterator cbegin() const _NOEXCEPT
939 {return begin();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200940 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000941 const_iterator cend() const _NOEXCEPT
942 {return end();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200943 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000944 const_reverse_iterator crbegin() const _NOEXCEPT
945 {return rbegin();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200946 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000947 const_reverse_iterator crend() const _NOEXCEPT
948 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200950 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200952 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type length() const _NOEXCEPT {return size();}
953 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type max_size() const _NOEXCEPT;
954 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200955 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
956 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200958 _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200959 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200961 _LIBCPP_CONSTEXPR_AFTER_CXX17 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100962
963#if _LIBCPP_STD_VER > 20
964 template <class _Op>
965 _LIBCPP_HIDE_FROM_ABI constexpr
966 void resize_and_overwrite(size_type __n, _Op __op) {
967 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200968 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100969 }
970#endif
971
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200972 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +0000973
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200974 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
975 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void shrink_to_fit() _NOEXCEPT;
976 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200977
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200978 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowb7db4972017-11-15 20:02:27 +0000979 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200981 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200982 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200983 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200985 _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference at(size_type __n) const;
986 _LIBCPP_CONSTEXPR_AFTER_CXX17 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200988 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200989 return append(__str);
990 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000991
992 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200993 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -0400994 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000995 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500996 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
997 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000998 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500999 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001000 operator+=(const _Tp& __t) {
1001 __self_view __sv = __t; return append(__sv);
1002 }
1003
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001004 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001005 return append(__s);
1006 }
1007
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001008 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001009 push_back(__c);
1010 return *this;
1011 }
1012
Eric Fiselierfc92be82017-04-19 00:28:44 +00001013#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001014 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001015 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001016#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001018 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001020
1021 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001022 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001023 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001024 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1025 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001026 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001027 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001028 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001029 _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 +00001030
Marshall Clow62953962016-10-03 23:40:48 +00001031 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001032 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001033 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001034 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001035 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1036 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001037 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001038 >
Marshall Clow82513342016-09-24 22:45:42 +00001039 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001040 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s, size_type __n);
1041 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s);
1042 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001043
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001044 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier451d5582018-11-26 20:15:38 +00001045 void __append_default_init(size_type __n);
1046
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001048 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001049 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001051 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001053 >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001054 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001055 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001056 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001057 append(__temp.data(), __temp.size());
1058 return *this;
1059 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001061 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001062 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001064 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001066 >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001067 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001068 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001069
Eric Fiselierfc92be82017-04-19 00:28:44 +00001070#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001071 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001073#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001075 _LIBCPP_CONSTEXPR_AFTER_CXX17 void push_back(value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001076 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void pop_back();
1077 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference front() _NOEXCEPT;
1078 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference front() const _NOEXCEPT;
1079 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference back() _NOEXCEPT;
1080 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081
Marshall Clowe46031a2018-07-02 18:41:15 +00001082 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001083 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001084 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001085 <
1086 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1087 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001088 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001089 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001090 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001091 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001092#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001093 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001094 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001095 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001096 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001097#endif
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001098 _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 +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 Clow82513342016-09-24 22:45:42 +00001102 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001103 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1104 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001105 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001107 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001108 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s, size_type __n);
1109 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s);
1110 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001112 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001113 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001115 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001117 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 assign(_InputIterator __first, _InputIterator __last);
1119 template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001120 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001121 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001123 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001125 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001127#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001128 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001130#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001132 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001134
1135 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001136 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001137 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001138 <
1139 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1140 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001141 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001142 insert(size_type __pos1, const _Tp& __t)
1143 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1144
Marshall Clow82513342016-09-24 22:45:42 +00001145 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001146 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001147 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001148 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001149 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001150 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001151 >
Marshall Clow82513342016-09-24 22:45:42 +00001152 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001153 _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow8db7fb02014-03-04 19:17:19 +00001154 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001155 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1156 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s);
1157 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1158 _LIBCPP_CONSTEXPR_AFTER_CXX17 iterator insert(const_iterator __pos, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001159 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1161 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001162 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001163 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001165 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001167 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1169 template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001170 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001171 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001173 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001175 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001177#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001178 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1180 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001181#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001183 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001184 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 iterator erase(const_iterator __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001186 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 iterator erase(const_iterator __first, const_iterator __last);
1188
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001189 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001191
1192 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001193 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001194 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001195 <
1196 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1197 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001198 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001199 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 +02001200 _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow8db7fb02014-03-04 19:17:19 +00001201 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 +00001202 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001203 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001204 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001205 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001206 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001207 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001208 >
Marshall Clow82513342016-09-24 22:45:42 +00001209 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001210 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001211 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001212 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1213 _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 +02001214 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant990d6e82010-11-17 21:11:40 +00001215 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001216
1217 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001218 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001219 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001220 <
1221 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1222 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001223 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001224 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1225
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001226 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001227 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001228 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001229 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001230 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant990d6e82010-11-17 21:11:40 +00001231 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001233 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001234 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001236 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001238 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001239 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001240#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001241 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant990d6e82010-11-17 21:11:40 +00001242 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001244#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001246 _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 +02001247 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1249
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001250 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001251 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001252#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001253 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001254#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001255 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001256 __is_nothrow_swappable<allocator_type>::value);
1257#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001259 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001260 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001261 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1262 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001263#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001264 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1265 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001266#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001268 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001269 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001271 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001272 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001273
1274 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001275 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001276 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001277 <
1278 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1279 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001280 >
zoecarver1997e0a2021-02-05 11:54:47 -08001281 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001282 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001283 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001284 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001285 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001286 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
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 rfind(const basic_string& __str, size_type __pos = npos) 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 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001299 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 size_type rfind(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 rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001303 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type rfind(value_type __c, size_type __pos = npos) 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 find_first_of(const basic_string& __str, size_type __pos = 0) 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 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001316 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001317 size_type find_first_of(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 find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001320 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001321 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001323 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001324 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001325
1326 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001327 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001328 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001329 <
1330 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1331 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001332 >
zoecarver1997e0a2021-02-05 11:54:47 -08001333 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001334 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001335 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001336 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001337 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001338 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001339 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001341 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001342 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001343
1344 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001345 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001346 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001347 <
1348 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1349 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001350 >
zoecarver1997e0a2021-02-05 11:54:47 -08001351 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001352 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001353 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 +02001354 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001355 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001356 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001357 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1358
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001359 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001360 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001361
1362 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001363 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001364 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001365 <
1366 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1367 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001368 >
zoecarver1997e0a2021-02-05 11:54:47 -08001369 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001370 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001371 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 +02001372 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001373 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001374 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001375 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1376
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001377 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001378 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001379
1380 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001381 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001382 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001383 <
1384 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1385 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001386 >
zoecarver1997e0a2021-02-05 11:54:47 -08001387 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001388
1389 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001390 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001391 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001392 <
1393 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1394 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001395 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001396 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1397
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001398 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001400 _LIBCPP_CONSTEXPR_AFTER_CXX17
1401 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1402 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001403
Marshall Clow82513342016-09-24 22:45:42 +00001404 template <class _Tp>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001405 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001406 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001407 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001408 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001409 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001410 >
Marshall Clow82513342016-09-24 22:45:42 +00001411 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 +02001412 _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(const value_type* __s) const _NOEXCEPT;
1413 _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1414 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001415 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416
Marshall Clow18c293b2017-12-04 20:11:38 +00001417#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001418 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001419 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001420 { return __self_view(data(), size()).starts_with(__sv); }
1421
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001422 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001423 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001424 { return !empty() && _Traits::eq(front(), __c); }
1425
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001426 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001427 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001428 { return starts_with(__self_view(__s)); }
1429
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001430 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001431 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001432 { return __self_view(data(), size()).ends_with( __sv); }
1433
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001434 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001435 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001436 { return !empty() && _Traits::eq(back(), __c); }
1437
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001438 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001439 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001440 { return ends_with(__self_view(__s)); }
1441#endif
1442
Wim Leflere023c3542021-01-19 14:33:30 -05001443#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001444 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001445 bool contains(__self_view __sv) const noexcept
1446 { return __self_view(data(), size()).contains(__sv); }
1447
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001448 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001449 bool contains(value_type __c) const noexcept
1450 { return __self_view(data(), size()).contains(__c); }
1451
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001452 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001453 bool contains(const value_type* __s) const
1454 { return __self_view(data(), size()).contains(__s); }
1455#endif
1456
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001457 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001458
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001459 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001460
Louis Dionne510450b2022-04-01 16:38:30 -04001461#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001462
1463 bool __dereferenceable(const const_iterator* __i) const;
1464 bool __decrementable(const const_iterator* __i) const;
1465 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1466 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1467
Louis Dionne510450b2022-04-01 16:38:30 -04001468#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001469
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001471 template<class _Alloc>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001472 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001473 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1474 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1475
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001476 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001477
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001478 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001479 bool __is_long() const _NOEXCEPT {
1480 if (__libcpp_is_constant_evaluated())
1481 return true;
1482 return __r_.first().__s.__is_long_;
1483 }
1484
1485 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __begin_lifetime(pointer __begin, size_type __n) {
1486#if _LIBCPP_STD_VER > 17
1487 if (__libcpp_is_constant_evaluated()) {
1488 for (size_type __i = 0; __i != __n; ++__i)
1489 std::construct_at(std::addressof(__begin[__i]));
1490 }
1491#else
1492 (void)__begin;
1493 (void)__n;
1494#endif // _LIBCPP_STD_VER > 17
1495 }
1496
1497 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __default_init() {
1498 __zero();
1499 if (__libcpp_is_constant_evaluated()) {
1500 size_type __sz = __recommend(0) + 1;
1501 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1502 __begin_lifetime(__ptr, __sz);
1503 __set_long_pointer(__ptr);
1504 __set_long_cap(__sz);
1505 __set_long_size(0);
1506 }
1507 }
1508
1509 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __deallocate_constexpr() {
1510 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1511 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1512 }
1513
Nikolas Klauser93826d12022-01-04 17:24:03 +01001514 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1515 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1516 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1517 }
1518
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001519 template <class _ForwardIterator>
1520 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1521 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1522 size_type __sz = size();
1523 size_type __cap = capacity();
1524 value_type* __p;
1525 if (__cap - __sz >= __n)
1526 {
1527 __p = std::__to_address(__get_pointer());
1528 size_type __n_move = __sz - __ip;
1529 if (__n_move != 0)
1530 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1531 }
1532 else
1533 {
1534 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1535 __p = std::__to_address(__get_long_pointer());
1536 }
1537 __sz += __n;
1538 __set_size(__sz);
1539 traits_type::assign(__p[__sz], value_type());
1540 for (__p += __ip; __first != __last; ++__p, ++__first)
1541 traits_type::assign(*__p, *__first);
1542
1543 return begin() + __ip;
1544 }
1545
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001546 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1547 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001549 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001550 void __set_short_size(size_type __s) _NOEXCEPT {
1551 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1552 __r_.first().__s.__size_ = __s;
1553 __r_.first().__s.__is_long_ = false;
1554 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001555
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001556 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001557 size_type __get_short_size() const _NOEXCEPT {
1558 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1559 return __r_.first().__s.__size_;
1560 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001561
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001562 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001563 void __set_long_size(size_type __s) _NOEXCEPT
1564 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001565 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001566 size_type __get_long_size() const _NOEXCEPT
1567 {return __r_.first().__l.__size_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001568 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001569 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1571
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001572 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001573 void __set_long_cap(size_type __s) _NOEXCEPT {
1574 __r_.first().__l.__cap_ = __s / __endian_factor;
1575 __r_.first().__l.__is_long_ = true;
1576 }
1577
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001578 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001579 size_type __get_long_cap() const _NOEXCEPT {
1580 return __r_.first().__l.__cap_ * __endian_factor;
1581 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001583 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001584 void __set_long_pointer(pointer __p) _NOEXCEPT
1585 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001586 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001587 pointer __get_long_pointer() _NOEXCEPT
1588 {return __r_.first().__l.__data_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001589 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001590 const_pointer __get_long_pointer() const _NOEXCEPT
1591 {return __r_.first().__l.__data_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001592 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001593 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001594 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001595 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001596 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001597 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001598 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001599 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001601 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001602 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1604
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001605 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001606 void __zero() _NOEXCEPT {
1607 __r_.first() = __rep();
1608 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609
1610 template <size_type __a> static
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001611 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea382952013-08-14 18:00:20 +00001612 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001613 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 enum {__alignment = 16};
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001615 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001616 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001617 {
1618 if (__s < __min_cap) {
1619 if (__libcpp_is_constant_evaluated())
1620 return static_cast<size_type>(__min_cap);
1621 else
1622 return static_cast<size_type>(__min_cap) - 1;
1623 }
Marshall Clow80584522018-02-07 21:30:17 +00001624 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1625 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1626 if (__guess == __min_cap) ++__guess;
1627 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001628 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001630 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001631 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001632 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001633 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001634 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001636
Martijn Vels5e7c9752020-03-04 17:52:46 -05001637 // Slow path for the (inlined) copy constructor for 'long' strings.
1638 // Always externally instantiated and not inlined.
1639 // Requires that __s is zero terminated.
1640 // The main reason for this function to exist is because for unstable, we
1641 // want to allow inlining of the copy constructor. However, we don't want
1642 // to call the __init() functions as those are marked as inline which may
1643 // result in over-aggressive inlining by the compiler, where our aim is
1644 // to only inline the fast path code directly in the ctor.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001645 _LIBCPP_CONSTEXPR_AFTER_CXX17 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001646
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647 template <class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001648 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001649 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001651 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1652 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001653 __init(_InputIterator __first, _InputIterator __last);
1654
1655 template <class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001656 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001657 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001659 __is_cpp17_forward_iterator<_ForwardIterator>::value
1660 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661 __init(_ForwardIterator __first, _ForwardIterator __last);
1662
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001663 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001665 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001666 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1668 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001669 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670
Martijn Vels596e3de2020-02-26 15:55:49 -05001671 // __assign_no_alias is invoked for assignment operations where we
1672 // have proof that the input does not alias the current instance.
1673 // For example, operator=(basic_string) performs a 'self' check.
1674 template <bool __is_short>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001675 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001676
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001677 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678 void __erase_to_end(size_type __pos);
1679
Martijn Velsa81fc792020-02-26 13:25:43 -05001680 // __erase_external_with_move is invoked for erase() invocations where
1681 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001682 _LIBCPP_CONSTEXPR_AFTER_CXX17 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001683
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001684 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001685 void __copy_assign_alloc(const basic_string& __str)
1686 {__copy_assign_alloc(__str, integral_constant<bool,
1687 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1688
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001689 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001690 void __copy_assign_alloc(const basic_string& __str, true_type)
1691 {
Marshall Clowf258c202017-01-31 03:40:52 +00001692 if (__alloc() == __str.__alloc())
1693 __alloc() = __str.__alloc();
1694 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001695 {
Marshall Clowf258c202017-01-31 03:40:52 +00001696 if (!__str.__is_long())
1697 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001698 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001699 __alloc() = __str.__alloc();
1700 }
1701 else
1702 {
1703 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001704 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001705 __begin_lifetime(__allocation.ptr, __allocation.count);
Vedant Kumar55e007e2018-03-08 21:15:26 +00001706 __clear_and_shrink();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001707 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001708 __set_long_pointer(__allocation.ptr);
1709 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001710 __set_long_size(__str.size());
1711 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001712 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001713 }
1714
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001715 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant28b24882011-12-01 20:21:04 +00001716 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001717 {}
1718
Eric Fiselierfc92be82017-04-19 00:28:44 +00001719#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001720 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001721 void __move_assign(basic_string& __str, false_type)
1722 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001723 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001724 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001725#if _LIBCPP_STD_VER > 14
1726 _NOEXCEPT;
1727#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001728 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001729#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001730#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001731
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001732 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001733 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001734 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001735 _NOEXCEPT_(
1736 !__alloc_traits::propagate_on_container_move_assignment::value ||
1737 is_nothrow_move_assignable<allocator_type>::value)
1738 {__move_assign_alloc(__str, integral_constant<bool,
1739 __alloc_traits::propagate_on_container_move_assignment::value>());}
1740
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001741 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc2734962011-09-02 20:42:31 +00001742 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001743 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1744 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001745 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001746 }
1747
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001748 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant28b24882011-12-01 20:21:04 +00001749 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001750 _NOEXCEPT
1751 {}
1752
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001753 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s);
1754 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001755
1756 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1757 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1758 pointer __p = __is_long()
1759 ? (__set_long_size(__n), __get_long_pointer())
1760 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001761 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001762 traits_type::assign(__p[__n], value_type());
1763 return *this;
1764 }
1765
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001766 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1767 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001768 __set_size(__newsz);
1769 __invalidate_iterators_past(__newsz);
1770 traits_type::assign(__p[__newsz], value_type());
1771 return *this;
1772 }
1773
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001774 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001776 template<class _Tp>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001777 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001778 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001779 // assume that the ranges overlap, because we can't check during constant evaluation
1780 if (__libcpp_is_constant_evaluated())
1781 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001782 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001783 return data() <= __p && __p <= data() + size();
1784 }
1785
Louis Dionned24191c2021-08-19 12:39:16 -04001786 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1787 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001788 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001789 }
1790
1791 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1792 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001793 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001794 }
1795
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001796 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const basic_string&);
1797 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const value_type*, const basic_string&);
1798 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(value_type, const basic_string&);
1799 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const value_type*);
1800 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801};
1802
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001803// These declarations must appear before any functions are implicitly used
1804// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001805#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001806#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001807 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001808# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001809 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001810# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001811#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001812 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001813# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001814 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001815# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001816#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001817#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001818
1819
Louis Dionned59f8a52021-08-17 11:59:07 -04001820#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001821template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001822 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001823 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001824 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1825 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001826 >
1827basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1828 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001829
1830template<class _CharT,
1831 class _Traits,
1832 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001833 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001834 >
1835explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1836 -> basic_string<_CharT, _Traits, _Allocator>;
1837
1838template<class _CharT,
1839 class _Traits,
1840 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001841 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001842 class _Sz = typename allocator_traits<_Allocator>::size_type
1843 >
1844basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1845 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001846#endif
1847
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001849inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001851basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852{
Louis Dionne510450b2022-04-01 16:38:30 -04001853#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001854 if (!__libcpp_is_constant_evaluated()) {
1855 __c_node* __c = __get_db()->__find_c_and_lock(this);
1856 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001858 const_pointer __new_last = __get_pointer() + __pos;
1859 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001861 --__p;
1862 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1863 if (__i->base() > __new_last)
1864 {
1865 (*__p)->__c_ = nullptr;
1866 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001867 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001868 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001870 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 }
1872 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001873#else
1874 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04001875#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876}
1877
1878template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001879inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001880basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001881 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001882 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001884 std::__debug_db_insert_c(this);
1885 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886}
1887
1888template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001889inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001891#if _LIBCPP_STD_VER <= 14
1892 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1893#else
1894 _NOEXCEPT
1895#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001896: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001898 std::__debug_db_insert_c(this);
1899 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900}
1901
1902template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001903_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier815ed732016-09-16 00:00:48 +00001904void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1905 size_type __sz,
1906 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001908 if (__libcpp_is_constant_evaluated())
1909 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001911 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001913 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 {
1915 __set_short_size(__sz);
1916 __p = __get_short_pointer();
1917 }
1918 else
1919 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001920 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1921 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001922 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001924 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925 __set_long_size(__sz);
1926 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001927 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928 traits_type::assign(__p[__sz], value_type());
1929}
1930
1931template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001932_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001934basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001936 if (__libcpp_is_constant_evaluated())
1937 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001939 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001941 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942 {
1943 __set_short_size(__sz);
1944 __p = __get_short_pointer();
1945 }
1946 else
1947 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001948 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1949 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001950 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001952 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953 __set_long_size(__sz);
1954 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001955 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956 traits_type::assign(__p[__sz], value_type());
1957}
1958
1959template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001960template <class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001961_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001962basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001963 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001965 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001967 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968}
1969
1970template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001971inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001972basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001973 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001975 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1976 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001977 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978}
1979
1980template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001981inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001982basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001983 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001985 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001987 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988}
1989
1990template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001991_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001993 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994{
1995 if (!__str.__is_long())
1996 __r_.first().__r = __str.__r_.first().__r;
1997 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001998 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05001999 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002000 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001}
2002
2003template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002004_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002005basic_string<_CharT, _Traits, _Allocator>::basic_string(
2006 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002007 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008{
2009 if (!__str.__is_long())
2010 __r_.first().__r = __str.__r_.first().__r;
2011 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002012 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002013 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002014 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015}
2016
Martijn Vels5e7c9752020-03-04 17:52:46 -05002017template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002018_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Vels5e7c9752020-03-04 17:52:46 -05002019void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2020 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002021 if (__libcpp_is_constant_evaluated())
2022 __zero();
Martijn Vels5e7c9752020-03-04 17:52:46 -05002023 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002024 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002025 __p = __get_short_pointer();
2026 __set_short_size(__sz);
2027 } else {
2028 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002029 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002030 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2031 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002032 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002033 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002034 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002035 __set_long_size(__sz);
2036 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002037 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002038}
2039
Eric Fiselierfc92be82017-04-19 00:28:44 +00002040#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041
2042template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002043inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00002044basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00002045#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00002046 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00002047#else
2048 _NOEXCEPT
2049#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002050 : __r_(std::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002052 __str.__default_init();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002053 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002054 if (__is_long())
2055 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056}
2057
2058template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002059inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002061 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062{
Marshall Clowd2d24692014-07-17 15:32:20 +00002063 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002064 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002065 else
2066 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002067 if (__libcpp_is_constant_evaluated()) {
2068 __zero();
2069 __r_.first().__l = __str.__r_.first().__l;
2070 } else {
2071 __r_.first().__r = __str.__r_.first().__r;
2072 }
2073 __str.__default_init();
Marshall Clowd2d24692014-07-17 15:32:20 +00002074 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002075 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002076 if (__is_long())
2077 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078}
2079
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002080#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081
2082template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002083_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084void
2085basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2086{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002087 if (__libcpp_is_constant_evaluated())
2088 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002090 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002092 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093 {
2094 __set_short_size(__n);
2095 __p = __get_short_pointer();
2096 }
2097 else
2098 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002099 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2100 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002101 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002103 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104 __set_long_size(__n);
2105 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002106 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107 traits_type::assign(__p[__n], value_type());
2108}
2109
2110template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002111inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002112basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002113 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114{
2115 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002116 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117}
2118
2119template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002120template <class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002121_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002122basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002123 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124{
2125 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002126 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127}
2128
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002130_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002131basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2132 size_type __pos, size_type __n,
2133 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002134 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135{
2136 size_type __str_sz = __str.size();
2137 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002138 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002139 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2140 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141}
2142
2143template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002144inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +00002145basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002146 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002147 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002148{
2149 size_type __str_sz = __str.size();
2150 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002151 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002152 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002153 std::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002154}
2155
2156template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002157template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002158_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow78dbe462016-11-14 18:22:19 +00002159basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002160 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002161 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002162{
Marshall Clowe46031a2018-07-02 18:41:15 +00002163 __self_view __sv0 = __t;
2164 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002165 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002166 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002167}
2168
2169template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002170template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002171_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +00002172basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002173 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002174{
Marshall Clowe46031a2018-07-02 18:41:15 +00002175 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002176 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002177 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002178}
2179
2180template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002181template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002182_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +00002183basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002184 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002185{
Marshall Clowe46031a2018-07-02 18:41:15 +00002186 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002187 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002188 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002189}
2190
2191template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192template <class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002193_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002194__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002196 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2197>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2199{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002200 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201#ifndef _LIBCPP_NO_EXCEPTIONS
2202 try
2203 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002204#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205 for (; __first != __last; ++__first)
2206 push_back(*__first);
2207#ifndef _LIBCPP_NO_EXCEPTIONS
2208 }
2209 catch (...)
2210 {
2211 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002212 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 throw;
2214 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002215#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216}
2217
2218template <class _CharT, class _Traits, class _Allocator>
2219template <class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002220_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002221__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002223 __is_cpp17_forward_iterator<_ForwardIterator>::value
2224>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2226{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002227 if (__libcpp_is_constant_evaluated())
2228 __zero();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002229 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002231 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002233 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 {
2235 __set_short_size(__sz);
2236 __p = __get_short_pointer();
2237 }
2238 else
2239 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002240 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2241 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002242 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002244 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 __set_long_size(__sz);
2246 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002247
2248#ifndef _LIBCPP_NO_EXCEPTIONS
2249 try
2250 {
2251#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002252 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253 traits_type::assign(*__p, *__first);
2254 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002255#ifndef _LIBCPP_NO_EXCEPTIONS
2256 }
2257 catch (...)
2258 {
2259 if (__is_long())
2260 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2261 throw;
2262 }
2263#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264}
2265
2266template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002267template<class _InputIterator, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002268inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002270 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271{
2272 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002273 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274}
2275
2276template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002277template<class _InputIterator, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002278inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2280 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002281 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282{
2283 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002284 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285}
2286
Eric Fiselierfc92be82017-04-19 00:28:44 +00002287#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002288
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002290inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002291basic_string<_CharT, _Traits, _Allocator>::basic_string(
2292 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002293 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294{
2295 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002296 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297}
2298
2299template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002300inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002301basic_string<_CharT, _Traits, _Allocator>::basic_string(
2302 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002303 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304{
2305 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002306 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307}
2308
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002309#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002310
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002312_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2314{
Nikolas Klauser98833542022-05-07 22:20:23 +02002315 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002317 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318}
2319
2320template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002321_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322void
2323basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2324 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002325 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 +00002326{
2327 size_type __ms = max_size();
2328 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002329 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330 pointer __old_p = __get_pointer();
2331 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002332 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002334 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2335 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002336 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002337 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002339 traits_type::copy(std::__to_address(__p),
2340 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002342 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2344 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002345 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2346 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002347 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002348 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002350 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2352 __set_long_size(__old_sz);
2353 traits_type::assign(__p[__old_sz], value_type());
2354}
2355
2356template <class _CharT, class _Traits, class _Allocator>
2357void
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002358_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2360 size_type __n_copy, size_type __n_del, size_type __n_add)
2361{
2362 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002363 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002364 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365 pointer __old_p = __get_pointer();
2366 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002367 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002369 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2370 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002371 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002372 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002374 traits_type::copy(std::__to_address(__p),
2375 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002376 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2377 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002378 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2379 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002380 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002381 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2382 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002384 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385}
2386
2387// assign
2388
2389template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002390template <bool __is_short>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002391_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsb6a08b62020-04-10 18:36:31 -04002392basic_string<_CharT, _Traits, _Allocator>&
2393basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002394 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002395 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002396 if (__n < __cap) {
2397 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2398 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002399 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002400 traits_type::assign(__p[__n], value_type());
2401 __invalidate_iterators_past(__n);
2402 } else {
2403 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2404 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2405 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002406 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002407}
2408
2409template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002410_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002412basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2413 const value_type* __s, size_type __n) {
2414 size_type __cap = capacity();
2415 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002416 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002417 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002418 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002419 } else {
2420 size_type __sz = size();
2421 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002422 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002423 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002424}
2425
2426template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002427_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsda7d94f2020-06-19 14:24:03 -04002428basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002429basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002431 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002432 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002433 ? __assign_short(__s, __n)
2434 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435}
2436
2437template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002438_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439basic_string<_CharT, _Traits, _Allocator>&
2440basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2441{
2442 size_type __cap = capacity();
2443 if (__cap < __n)
2444 {
2445 size_type __sz = size();
2446 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2447 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002448 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002450 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451}
2452
2453template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002454_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455basic_string<_CharT, _Traits, _Allocator>&
2456basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2457{
2458 pointer __p;
2459 if (__is_long())
2460 {
2461 __p = __get_long_pointer();
2462 __set_long_size(1);
2463 }
2464 else
2465 {
2466 __p = __get_short_pointer();
2467 __set_short_size(1);
2468 }
2469 traits_type::assign(*__p, __c);
2470 traits_type::assign(*++__p, value_type());
2471 __invalidate_iterators_past(1);
2472 return *this;
2473}
2474
2475template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002476_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002477basic_string<_CharT, _Traits, _Allocator>&
2478basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2479{
Martijn Vels596e3de2020-02-26 15:55:49 -05002480 if (this != &__str) {
2481 __copy_assign_alloc(__str);
2482 if (!__is_long()) {
2483 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002484 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002485 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002486 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002487 }
2488 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002489 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002490 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002491 }
2492 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002493}
2494
Eric Fiselierfc92be82017-04-19 00:28:44 +00002495#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002496
2497template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002498inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002499void
2500basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002501 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002502{
2503 if (__alloc() != __str.__alloc())
2504 assign(__str);
2505 else
2506 __move_assign(__str, true_type());
2507}
2508
2509template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002510inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002511void
2512basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002513#if _LIBCPP_STD_VER > 14
2514 _NOEXCEPT
2515#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002516 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002517#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002518{
marshall2ed41622019-11-27 07:13:00 -08002519 if (__is_long()) {
2520 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2521 __get_long_cap());
2522#if _LIBCPP_STD_VER <= 14
2523 if (!is_nothrow_move_assignable<allocator_type>::value) {
2524 __set_short_size(0);
2525 traits_type::assign(__get_short_pointer()[0], value_type());
2526 }
2527#endif
2528 }
2529 __move_assign_alloc(__str);
2530 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002531 if (__libcpp_is_constant_evaluated()) {
2532 __str.__default_init();
2533 } else {
2534 __str.__set_short_size(0);
2535 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2536 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002537}
2538
2539template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002540inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002541basic_string<_CharT, _Traits, _Allocator>&
2542basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002543 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002544{
2545 __move_assign(__str, integral_constant<bool,
2546 __alloc_traits::propagate_on_container_move_assignment::value>());
2547 return *this;
2548}
2549
2550#endif
2551
2552template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002554_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002555__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002557 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002559>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2561{
Marshall Clow958362f2016-09-05 01:54:30 +00002562 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002563 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002564 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565}
2566
2567template <class _CharT, class _Traits, class _Allocator>
2568template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002569_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002570__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002572 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002574>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2576{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002578 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002579 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002580
2581 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2582 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002584 if (__cap < __n)
2585 {
2586 size_type __sz = size();
2587 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2588 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002589 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002590 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002591 traits_type::assign(*__p, *__first);
2592 traits_type::assign(*__p, value_type());
2593 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002594 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595 }
2596 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002597 {
2598 const basic_string __temp(__first, __last, __alloc());
2599 assign(__temp.data(), __temp.size());
2600 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601 return *this;
2602}
2603
2604template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002605_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606basic_string<_CharT, _Traits, _Allocator>&
2607basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2608{
2609 size_type __sz = __str.size();
2610 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002611 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002612 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613}
2614
2615template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002616template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002617_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002618__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002619<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002620 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2621 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002622 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002623>
Marshall Clow82513342016-09-24 22:45:42 +00002624basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002625{
Marshall Clow82513342016-09-24 22:45:42 +00002626 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002627 size_type __sz = __sv.size();
2628 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002629 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002630 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002631}
2632
2633
2634template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002635_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002637basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2638 return __assign_external(__s, traits_type::length(__s));
2639}
2640
2641template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002642_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsda7d94f2020-06-19 14:24:03 -04002643basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002644basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002646 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002647 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002648 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002649 ? __assign_short(__s, traits_type::length(__s))
2650 : __assign_external(__s, traits_type::length(__s)))
2651 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653// append
2654
2655template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002656_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002658basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002660 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661 size_type __cap = capacity();
2662 size_type __sz = size();
2663 if (__cap - __sz >= __n)
2664 {
2665 if (__n)
2666 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002667 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668 traits_type::copy(__p + __sz, __s, __n);
2669 __sz += __n;
2670 __set_size(__sz);
2671 traits_type::assign(__p[__sz], value_type());
2672 }
2673 }
2674 else
2675 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2676 return *this;
2677}
2678
2679template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002680_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681basic_string<_CharT, _Traits, _Allocator>&
2682basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2683{
2684 if (__n)
2685 {
2686 size_type __cap = capacity();
2687 size_type __sz = size();
2688 if (__cap - __sz < __n)
2689 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2690 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002691 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 __sz += __n;
2693 __set_size(__sz);
2694 traits_type::assign(__p[__sz], value_type());
2695 }
2696 return *this;
2697}
2698
2699template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002700_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002701basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2702{
2703 if (__n)
2704 {
2705 size_type __cap = capacity();
2706 size_type __sz = size();
2707 if (__cap - __sz < __n)
2708 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2709 pointer __p = __get_pointer();
2710 __sz += __n;
2711 __set_size(__sz);
2712 traits_type::assign(__p[__sz], value_type());
2713 }
2714}
2715
2716template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002717_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718void
2719basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2720{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002721 bool __is_short = !__is_long();
2722 size_type __cap;
2723 size_type __sz;
2724 if (__is_short)
2725 {
2726 __cap = __min_cap - 1;
2727 __sz = __get_short_size();
2728 }
2729 else
2730 {
2731 __cap = __get_long_cap() - 1;
2732 __sz = __get_long_size();
2733 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002735 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002737 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002738 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002739 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002740 if (__is_short)
2741 {
2742 __p = __get_short_pointer() + __sz;
2743 __set_short_size(__sz+1);
2744 }
2745 else
2746 {
2747 __p = __get_long_pointer() + __sz;
2748 __set_long_size(__sz+1);
2749 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 traits_type::assign(*__p, __c);
2751 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752}
2753
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754template <class _CharT, class _Traits, class _Allocator>
2755template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002756_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002757__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002758<
2759 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2760 basic_string<_CharT, _Traits, _Allocator>&
2761>
2762basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002763 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764{
2765 size_type __sz = size();
2766 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002767 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768 if (__n)
2769 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002770 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2771 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002772 {
2773 if (__cap - __sz < __n)
2774 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2775 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002776 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002777 traits_type::assign(*__p, *__first);
2778 traits_type::assign(*__p, value_type());
2779 __set_size(__sz + __n);
2780 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002781 else
2782 {
2783 const basic_string __temp(__first, __last, __alloc());
2784 append(__temp.data(), __temp.size());
2785 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 }
2787 return *this;
2788}
2789
2790template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002791inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792basic_string<_CharT, _Traits, _Allocator>&
2793basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2794{
2795 return append(__str.data(), __str.size());
2796}
2797
2798template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002799_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800basic_string<_CharT, _Traits, _Allocator>&
2801basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2802{
2803 size_type __sz = __str.size();
2804 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002805 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002806 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807}
2808
2809template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002810template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002811_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002812 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002813 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002814 __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 +00002815 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002816 >
Marshall Clow82513342016-09-24 22:45:42 +00002817basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002818{
Marshall Clow82513342016-09-24 22:45:42 +00002819 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002820 size_type __sz = __sv.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(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002824}
2825
2826template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002827_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002829basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002831 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 return append(__s, traits_type::length(__s));
2833}
2834
2835// insert
2836
2837template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002838_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002840basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002842 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 size_type __sz = size();
2844 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002845 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002847 if (__libcpp_is_constant_evaluated()) {
2848 if (__cap - __sz >= __n)
2849 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2850 else
2851 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2852 return *this;
2853 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854 if (__cap - __sz >= __n)
2855 {
2856 if (__n)
2857 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002858 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859 size_type __n_move = __sz - __pos;
2860 if (__n_move != 0)
2861 {
2862 if (__p + __pos <= __s && __s < __p + __sz)
2863 __s += __n;
2864 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2865 }
2866 traits_type::move(__p + __pos, __s, __n);
2867 __sz += __n;
2868 __set_size(__sz);
2869 traits_type::assign(__p[__sz], value_type());
2870 }
2871 }
2872 else
2873 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2874 return *this;
2875}
2876
2877template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002878_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879basic_string<_CharT, _Traits, _Allocator>&
2880basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2881{
2882 size_type __sz = size();
2883 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002884 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885 if (__n)
2886 {
2887 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002888 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889 if (__cap - __sz >= __n)
2890 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002891 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 size_type __n_move = __sz - __pos;
2893 if (__n_move != 0)
2894 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2895 }
2896 else
2897 {
2898 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002899 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900 }
2901 traits_type::assign(__p + __pos, __n, __c);
2902 __sz += __n;
2903 __set_size(__sz);
2904 traits_type::assign(__p[__sz], value_type());
2905 }
2906 return *this;
2907}
2908
2909template <class _CharT, class _Traits, class _Allocator>
2910template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002911_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002912__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002914 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002915 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002916>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2918{
Nikolas Klausereed25832021-12-15 01:32:30 +01002919 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2920 "string::insert(iterator, range) called with an iterator not"
2921 " referring to this string");
2922 const basic_string __temp(__first, __last, __alloc());
2923 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924}
2925
2926template <class _CharT, class _Traits, class _Allocator>
2927template<class _ForwardIterator>
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_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +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, _ForwardIterator __first, _ForwardIterator __last)
2935{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002936 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2937 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002938
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002940 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2941 if (__n == 0)
2942 return begin() + __ip;
2943
2944 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002946 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002948 else
2949 {
2950 const basic_string __temp(__first, __last, __alloc());
2951 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2952 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953}
2954
2955template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002956inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957basic_string<_CharT, _Traits, _Allocator>&
2958basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2959{
2960 return insert(__pos1, __str.data(), __str.size());
2961}
2962
2963template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002964_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965basic_string<_CharT, _Traits, _Allocator>&
2966basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2967 size_type __pos2, size_type __n)
2968{
2969 size_type __str_sz = __str.size();
2970 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002971 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002972 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973}
2974
2975template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002976template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002977_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002978__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002979<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002980 __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 +00002981 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002982>
Marshall Clow82513342016-09-24 22:45:42 +00002983basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002984 size_type __pos2, size_type __n)
2985{
Marshall Clow82513342016-09-24 22:45:42 +00002986 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002987 size_type __str_sz = __sv.size();
2988 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002989 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002990 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002991}
2992
2993template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002994_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002996basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002998 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999 return insert(__pos, __s, traits_type::length(__s));
3000}
3001
3002template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003003_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004typename basic_string<_CharT, _Traits, _Allocator>::iterator
3005basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
3006{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05003007 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3008 "string::insert(iterator, character) called with an iterator not"
3009 " referring to this string");
3010
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011 size_type __ip = static_cast<size_type>(__pos - begin());
3012 size_type __sz = size();
3013 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003014 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015 if (__cap == __sz)
3016 {
3017 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003018 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 }
3020 else
3021 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003022 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023 size_type __n_move = __sz - __ip;
3024 if (__n_move != 0)
3025 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3026 }
3027 traits_type::assign(__p[__ip], __c);
3028 traits_type::assign(__p[++__sz], value_type());
3029 __set_size(__sz);
3030 return begin() + static_cast<difference_type>(__ip);
3031}
3032
3033template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003034inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035typename basic_string<_CharT, _Traits, _Allocator>::iterator
3036basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3037{
Nikolas Klausereed25832021-12-15 01:32:30 +01003038 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3039 "string::insert(iterator, n, value) called with an iterator not"
3040 " referring to this string");
3041 difference_type __p = __pos - begin();
3042 insert(static_cast<size_type>(__p), __n, __c);
3043 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003044}
3045
3046// replace
3047
3048template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003049_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003051basic_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 +00003052 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003054 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055 size_type __sz = size();
3056 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003057 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003058 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059 size_type __cap = capacity();
3060 if (__cap - __sz + __n1 >= __n2)
3061 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003062 if (__libcpp_is_constant_evaluated()) {
3063 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3064 return *this;
3065 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003066 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067 if (__n1 != __n2)
3068 {
3069 size_type __n_move = __sz - __pos - __n1;
3070 if (__n_move != 0)
3071 {
3072 if (__n1 > __n2)
3073 {
3074 traits_type::move(__p + __pos, __s, __n2);
3075 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003076 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077 }
3078 if (__p + __pos < __s && __s < __p + __sz)
3079 {
3080 if (__p + __pos + __n1 <= __s)
3081 __s += __n2 - __n1;
3082 else // __p + __pos < __s < __p + __pos + __n1
3083 {
3084 traits_type::move(__p + __pos, __s, __n1);
3085 __pos += __n1;
3086 __s += __n2;
3087 __n2 -= __n1;
3088 __n1 = 0;
3089 }
3090 }
3091 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3092 }
3093 }
3094 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003095 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096 }
3097 else
3098 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3099 return *this;
3100}
3101
3102template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003103_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104basic_string<_CharT, _Traits, _Allocator>&
3105basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3106{
3107 size_type __sz = size();
3108 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003109 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003110 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003112 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 if (__cap - __sz + __n1 >= __n2)
3114 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003115 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 if (__n1 != __n2)
3117 {
3118 size_type __n_move = __sz - __pos - __n1;
3119 if (__n_move != 0)
3120 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3121 }
3122 }
3123 else
3124 {
3125 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003126 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127 }
3128 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003129 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130}
3131
3132template <class _CharT, class _Traits, class _Allocator>
3133template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003134_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003135__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003137 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003139>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003140basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141 _InputIterator __j1, _InputIterator __j2)
3142{
Marshall Clow958362f2016-09-05 01:54:30 +00003143 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003144 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145}
3146
3147template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003148inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149basic_string<_CharT, _Traits, _Allocator>&
3150basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3151{
3152 return replace(__pos1, __n1, __str.data(), __str.size());
3153}
3154
3155template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003156_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157basic_string<_CharT, _Traits, _Allocator>&
3158basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3159 size_type __pos2, size_type __n2)
3160{
3161 size_type __str_sz = __str.size();
3162 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003163 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003164 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165}
3166
3167template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003168template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003169_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003170__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003171<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003172 __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 +00003173 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003174>
Marshall Clow82513342016-09-24 22:45:42 +00003175basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003176 size_type __pos2, size_type __n2)
3177{
Marshall Clow82513342016-09-24 22:45:42 +00003178 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003179 size_type __str_sz = __sv.size();
3180 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003181 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003182 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003183}
3184
3185template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003186_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003187basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003188basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003190 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191 return replace(__pos, __n1, __s, traits_type::length(__s));
3192}
3193
3194template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003195inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003197basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198{
3199 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3200 __str.data(), __str.size());
3201}
3202
3203template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003204inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003206basic_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 +00003207{
3208 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
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 Hinnantd17880b2013-06-28 16:59:19 +00003214basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215{
3216 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3217}
3218
3219template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003220inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003222basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223{
3224 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3225}
3226
3227// erase
3228
Martijn Velsa81fc792020-02-26 13:25:43 -05003229// 'externally instantiated' erase() implementation, called when __n != npos.
3230// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003232_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsa81fc792020-02-26 13:25:43 -05003233void
3234basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3235 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003236{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237 if (__n)
3238 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003239 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003240 value_type* __p = std::__to_address(__get_pointer());
3241 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003242 size_type __n_move = __sz - __pos - __n;
3243 if (__n_move != 0)
3244 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003245 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003247}
3248
3249template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003250_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsa81fc792020-02-26 13:25:43 -05003251basic_string<_CharT, _Traits, _Allocator>&
3252basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3253 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003254 if (__pos > size())
3255 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003256 if (__n == npos) {
3257 __erase_to_end(__pos);
3258 } else {
3259 __erase_external_with_move(__pos, __n);
3260 }
3261 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262}
3263
3264template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003265inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266typename basic_string<_CharT, _Traits, _Allocator>::iterator
3267basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3268{
Nikolas Klausereed25832021-12-15 01:32:30 +01003269 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3270 "string::erase(iterator) called with an iterator not"
3271 " referring to this string");
3272
3273 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3274 iterator __b = begin();
3275 size_type __r = static_cast<size_type>(__pos - __b);
3276 erase(__r, 1);
3277 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278}
3279
3280template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003281inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282typename basic_string<_CharT, _Traits, _Allocator>::iterator
3283basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3284{
Nikolas Klausereed25832021-12-15 01:32:30 +01003285 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3286 "string::erase(iterator, iterator) called with an iterator not"
3287 " referring to this string");
3288
3289 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3290 iterator __b = begin();
3291 size_type __r = static_cast<size_type>(__first - __b);
3292 erase(__r, static_cast<size_type>(__last - __first));
3293 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294}
3295
3296template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003297inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298void
3299basic_string<_CharT, _Traits, _Allocator>::pop_back()
3300{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003301 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003302 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303}
3304
3305template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003306inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003308basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003310 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311 if (__is_long())
3312 {
3313 traits_type::assign(*__get_long_pointer(), value_type());
3314 __set_long_size(0);
3315 }
3316 else
3317 {
3318 traits_type::assign(*__get_short_pointer(), value_type());
3319 __set_short_size(0);
3320 }
3321}
3322
3323template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003324inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325void
3326basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3327{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003328 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329}
3330
3331template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003332_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333void
3334basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3335{
3336 size_type __sz = size();
3337 if (__n > __sz)
3338 append(__n - __sz, __c);
3339 else
3340 __erase_to_end(__n);
3341}
3342
3343template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003344_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003345basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3346{
3347 size_type __sz = size();
3348 if (__n > __sz) {
3349 __append_default_init(__n - __sz);
3350 } else
3351 __erase_to_end(__n);
3352}
3353
3354template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003355inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003357basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003359 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003360 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3361 return __m - __alignment;
3362 } else {
3363 bool __uses_lsb = __endian_factor == 2;
3364 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3365 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003366}
3367
3368template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003369_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370void
Marek Kurdejc9848142020-11-26 10:07:16 +01003371basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372{
Marek Kurdejc9848142020-11-26 10:07:16 +01003373 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003374 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003375
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003376 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3377 // and later (since P0966R1), however we provide consistent behavior in all Standard
3378 // modes because this function is instantiated in the shared library.
3379 if (__requested_capacity <= capacity())
3380 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003381
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003382 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003383 __target_capacity = __recommend(__target_capacity);
3384 if (__target_capacity == capacity()) return;
3385
3386 __shrink_or_extend(__target_capacity);
3387}
3388
3389template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003390inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marek Kurdejc9848142020-11-26 10:07:16 +01003391void
3392basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3393{
3394 size_type __target_capacity = __recommend(size());
3395 if (__target_capacity == capacity()) return;
3396
3397 __shrink_or_extend(__target_capacity);
3398}
3399
3400template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003401inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marek Kurdejc9848142020-11-26 10:07:16 +01003402void
3403basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3404{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405 size_type __cap = capacity();
3406 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003407
3408 pointer __new_data, __p;
3409 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003410 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003412 __was_long = true;
3413 __now_long = false;
3414 __new_data = __get_short_pointer();
3415 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003417 else
3418 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003419 if (__target_capacity > __cap) {
3420 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3421 __new_data = __allocation.ptr;
3422 __target_capacity = __allocation.count - 1;
3423 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003424 else
3425 {
3426 #ifndef _LIBCPP_NO_EXCEPTIONS
3427 try
3428 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003429 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003430 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3431 __new_data = __allocation.ptr;
3432 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003433 #ifndef _LIBCPP_NO_EXCEPTIONS
3434 }
3435 catch (...)
3436 {
3437 return;
3438 }
3439 #else // _LIBCPP_NO_EXCEPTIONS
3440 if (__new_data == nullptr)
3441 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003442 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003443 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003444 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003445 __now_long = true;
3446 __was_long = __is_long();
3447 __p = __get_pointer();
3448 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003449 traits_type::copy(std::__to_address(__new_data),
3450 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003451 if (__was_long)
3452 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3453 if (__now_long)
3454 {
3455 __set_long_cap(__target_capacity+1);
3456 __set_long_size(__sz);
3457 __set_long_pointer(__new_data);
3458 }
3459 else
3460 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003461 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462}
3463
3464template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003465inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003467basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003469 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003470 return *(data() + __pos);
3471}
3472
3473template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003474inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003476basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003478 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479 return *(__get_pointer() + __pos);
3480}
3481
3482template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003483_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3485basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3486{
3487 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003488 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489 return (*this)[__n];
3490}
3491
3492template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003493_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494typename basic_string<_CharT, _Traits, _Allocator>::reference
3495basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3496{
3497 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003498 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499 return (*this)[__n];
3500}
3501
3502template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003503inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003505basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003507 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508 return *__get_pointer();
3509}
3510
3511template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003512inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003514basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003516 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517 return *data();
3518}
3519
3520template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003521inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003523basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003525 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526 return *(__get_pointer() + size() - 1);
3527}
3528
3529template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003530inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003532basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003534 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535 return *(data() + size() - 1);
3536}
3537
3538template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003539_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003541basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542{
3543 size_type __sz = size();
3544 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003545 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003546 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547 traits_type::copy(__s, data() + __pos, __rlen);
3548 return __rlen;
3549}
3550
3551template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003552inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553basic_string<_CharT, _Traits, _Allocator>
3554basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3555{
3556 return basic_string(*this, __pos, __n, __alloc());
3557}
3558
3559template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003560inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561void
3562basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003563#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003564 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003565#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003566 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003567 __is_nothrow_swappable<allocator_type>::value)
3568#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569{
Nikolas Klauser98833542022-05-07 22:20:23 +02003570 if (!__is_long())
3571 std::__debug_db_invalidate_all(this);
3572 if (!__str.__is_long())
3573 std::__debug_db_invalidate_all(&__str);
3574 std::__debug_db_swap(this, &__str);
3575
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003576 _LIBCPP_ASSERT(
3577 __alloc_traits::propagate_on_container_swap::value ||
3578 __alloc_traits::is_always_equal::value ||
3579 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003580 std::swap(__r_.first(), __str.__r_.first());
3581 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582}
3583
3584// find
3585
3586template <class _Traits>
3587struct _LIBCPP_HIDDEN __traits_eq
3588{
3589 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003590 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003591 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3592 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593};
3594
3595template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003596_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003598basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003599 size_type __pos,
3600 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003602 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003603 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003604 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605}
3606
3607template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003608inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003610basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3611 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003613 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003614 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615}
3616
3617template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003618template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003619_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003620__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003621<
3622 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3623 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003624>
Marshall Clowe46031a2018-07-02 18:41:15 +00003625basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003626 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003627{
Marshall Clowe46031a2018-07-02 18:41:15 +00003628 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003629 return __str_find<value_type, size_type, traits_type, npos>
3630 (data(), size(), __sv.data(), __pos, __sv.size());
3631}
3632
3633template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003634inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003635typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003636basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003637 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003639 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003640 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003641 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642}
3643
3644template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003645_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003647basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3648 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003650 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003651 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652}
3653
3654// rfind
3655
3656template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003657_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003659basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003660 size_type __pos,
3661 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003663 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003664 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003665 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666}
3667
3668template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003669inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003671basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3672 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003674 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003675 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003676}
3677
3678template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003679template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003680_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003681__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003682<
3683 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3684 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003685>
Marshall Clowe46031a2018-07-02 18:41:15 +00003686basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003687 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003688{
Marshall Clowe46031a2018-07-02 18:41:15 +00003689 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003690 return __str_rfind<value_type, size_type, traits_type, npos>
3691 (data(), size(), __sv.data(), __pos, __sv.size());
3692}
3693
3694template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003695inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003696typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003697basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003698 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003699{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003700 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003701 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003702 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703}
3704
3705template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003706_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003708basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3709 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003710{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003711 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003712 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713}
3714
3715// find_first_of
3716
3717template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003718_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003720basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003721 size_type __pos,
3722 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003724 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003725 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003726 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727}
3728
3729template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003730inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003731typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003732basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3733 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003735 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003736 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737}
3738
3739template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003740template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003741_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003742__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003743<
3744 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3745 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003746>
Marshall Clowe46031a2018-07-02 18:41:15 +00003747basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003748 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003749{
Marshall Clowe46031a2018-07-02 18:41:15 +00003750 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003751 return __str_find_first_of<value_type, size_type, traits_type, npos>
3752 (data(), size(), __sv.data(), __pos, __sv.size());
3753}
3754
3755template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003756inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003757typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003758basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003759 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003760{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003761 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003762 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003763 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764}
3765
3766template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003767inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003769basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3770 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771{
3772 return find(__c, __pos);
3773}
3774
3775// find_last_of
3776
3777template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003778inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003780basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003781 size_type __pos,
3782 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003784 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003785 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003786 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787}
3788
3789template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003790inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003792basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3793 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003795 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003796 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797}
3798
3799template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003800template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003801_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003802__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003803<
3804 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3805 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003806>
Marshall Clowe46031a2018-07-02 18:41:15 +00003807basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003808 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003809{
Marshall Clowe46031a2018-07-02 18:41:15 +00003810 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003811 return __str_find_last_of<value_type, size_type, traits_type, npos>
3812 (data(), size(), __sv.data(), __pos, __sv.size());
3813}
3814
3815template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003816inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003817typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003818basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003819 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003821 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003822 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003823 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824}
3825
3826template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003827inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003829basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3830 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003831{
3832 return rfind(__c, __pos);
3833}
3834
3835// find_first_not_of
3836
3837template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003838_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003840basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003841 size_type __pos,
3842 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003844 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003845 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003846 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847}
3848
3849template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003850inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003852basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3853 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003855 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003856 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857}
3858
3859template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003860template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003861_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003862__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003863<
3864 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3865 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003866>
Marshall Clowe46031a2018-07-02 18:41:15 +00003867basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003868 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003869{
Marshall Clowe46031a2018-07-02 18:41:15 +00003870 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003871 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3872 (data(), size(), __sv.data(), __pos, __sv.size());
3873}
3874
3875template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003876inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003877typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003878basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003879 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003880{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003881 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003882 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003883 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884}
3885
3886template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003887inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003888typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003889basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3890 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003891{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003892 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003893 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894}
3895
3896// find_last_not_of
3897
3898template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003899_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003901basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003902 size_type __pos,
3903 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003905 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003906 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003907 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908}
3909
3910template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003911inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003913basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3914 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003916 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003917 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918}
3919
3920template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003921template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003922_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003923__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003924<
3925 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3926 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003927>
Marshall Clowe46031a2018-07-02 18:41:15 +00003928basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003929 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003930{
Marshall Clowe46031a2018-07-02 18:41:15 +00003931 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003932 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3933 (data(), size(), __sv.data(), __pos, __sv.size());
3934}
3935
3936template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003937inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003938typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003939basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003940 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003941{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003942 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003943 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003944 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945}
3946
3947template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003948inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003949typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003950basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3951 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003953 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003954 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955}
3956
3957// compare
3958
3959template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003960template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003961_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003962__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003963<
3964 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3965 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003966>
zoecarver1997e0a2021-02-05 11:54:47 -08003967basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968{
Marshall Clowe46031a2018-07-02 18:41:15 +00003969 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003970 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003971 size_t __rhs_sz = __sv.size();
3972 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003973 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003974 if (__result != 0)
3975 return __result;
3976 if (__lhs_sz < __rhs_sz)
3977 return -1;
3978 if (__lhs_sz > __rhs_sz)
3979 return 1;
3980 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981}
3982
3983template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003984inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003986basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003988 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989}
3990
3991template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003992inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003994basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3995 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003996 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003997 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003999 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000 size_type __sz = size();
4001 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004002 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004003 size_type __rlen = std::min(__n1, __sz - __pos1);
4004 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005 if (__r == 0)
4006 {
4007 if (__rlen < __n2)
4008 __r = -1;
4009 else if (__rlen > __n2)
4010 __r = 1;
4011 }
4012 return __r;
4013}
4014
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004015template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00004016template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004017_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04004018__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00004019<
4020 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
4021 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004022>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004023basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4024 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00004025 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004026{
Marshall Clowe46031a2018-07-02 18:41:15 +00004027 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004028 return compare(__pos1, __n1, __sv.data(), __sv.size());
4029}
4030
4031template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004032inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004033int
4034basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4035 size_type __n1,
4036 const basic_string& __str) const
4037{
4038 return compare(__pos1, __n1, __str.data(), __str.size());
4039}
4040
4041template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00004042template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004043_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04004044__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00004045<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004046 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
4047 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00004048 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004049>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004050basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4051 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00004052 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004053 size_type __pos2,
4054 size_type __n2) const
4055{
Marshall Clow82513342016-09-24 22:45:42 +00004056 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004057 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
4058}
4059
4060template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004061_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004062int
4063basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4064 size_type __n1,
4065 const basic_string& __str,
4066 size_type __pos2,
4067 size_type __n2) const
4068{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004069 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004070}
4071
4072template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004073_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004074int
4075basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4076{
4077 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4078 return compare(0, npos, __s, traits_type::length(__s));
4079}
4080
4081template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004082_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004083int
4084basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4085 size_type __n1,
4086 const value_type* __s) const
4087{
4088 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4089 return compare(__pos1, __n1, __s, traits_type::length(__s));
4090}
4091
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092// __invariants
4093
4094template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004095inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096bool
4097basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4098{
4099 if (size() > capacity())
4100 return false;
4101 if (capacity() < __min_cap - 1)
4102 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004103 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004105 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106 return false;
4107 return true;
4108}
4109
Vedant Kumar55e007e2018-03-08 21:15:26 +00004110// __clear_and_shrink
4111
4112template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004113inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne173f29e2019-05-29 16:01:36 +00004114void
Marshall Clowe60a7182018-05-29 17:04:37 +00004115basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004116{
4117 clear();
4118 if(__is_long())
4119 {
4120 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4121 __set_long_cap(0);
4122 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004123 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004124 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004125}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004126
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127// operator==
4128
4129template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004130inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131bool
4132operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004133 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004135 size_t __lhs_sz = __lhs.size();
4136 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4137 __rhs.data(),
4138 __lhs_sz) == 0;
4139}
4140
4141template<class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004142inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004143bool
4144operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4145 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4146{
4147 size_t __lhs_sz = __lhs.size();
4148 if (__lhs_sz != __rhs.size())
4149 return false;
4150 const char* __lp = __lhs.data();
4151 const char* __rp = __rhs.data();
4152 if (__lhs.__is_long())
4153 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4154 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4155 if (*__lp != *__rp)
4156 return false;
4157 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158}
4159
4160template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004161inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004163operator==(const _CharT* __lhs,
4164 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004166 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4167 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4168 size_t __lhs_len = _Traits::length(__lhs);
4169 if (__lhs_len != __rhs.size()) return false;
4170 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171}
4172
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004174inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004175bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004176operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4177 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004179 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4180 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4181 size_t __rhs_len = _Traits::length(__rhs);
4182 if (__rhs_len != __lhs.size()) return false;
4183 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184}
4185
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004186template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004187inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188bool
4189operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004190 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191{
4192 return !(__lhs == __rhs);
4193}
4194
4195template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004196inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004198operator!=(const _CharT* __lhs,
4199 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200{
4201 return !(__lhs == __rhs);
4202}
4203
4204template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004205inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004207operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4208 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209{
4210 return !(__lhs == __rhs);
4211}
4212
4213// operator<
4214
4215template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004216inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217bool
4218operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004219 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004221 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222}
4223
4224template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004225inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004227operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4228 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004230 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231}
4232
4233template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004234inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004236operator< (const _CharT* __lhs,
4237 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238{
4239 return __rhs.compare(__lhs) > 0;
4240}
4241
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242// operator>
4243
4244template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004245inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246bool
4247operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004248 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249{
4250 return __rhs < __lhs;
4251}
4252
4253template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004254inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004256operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4257 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258{
4259 return __rhs < __lhs;
4260}
4261
4262template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004263inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004265operator> (const _CharT* __lhs,
4266 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267{
4268 return __rhs < __lhs;
4269}
4270
4271// operator<=
4272
4273template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004274inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275bool
4276operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004277 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278{
4279 return !(__rhs < __lhs);
4280}
4281
4282template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004283inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004285operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4286 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287{
4288 return !(__rhs < __lhs);
4289}
4290
4291template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004292inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004294operator<=(const _CharT* __lhs,
4295 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296{
4297 return !(__rhs < __lhs);
4298}
4299
4300// operator>=
4301
4302template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004303inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304bool
4305operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004306 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307{
4308 return !(__lhs < __rhs);
4309}
4310
4311template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004312inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004314operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4315 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004316{
4317 return !(__lhs < __rhs);
4318}
4319
4320template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004321inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004323operator>=(const _CharT* __lhs,
4324 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325{
4326 return !(__lhs < __rhs);
4327}
4328
4329// operator +
4330
4331template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004332_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333basic_string<_CharT, _Traits, _Allocator>
4334operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4335 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4336{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004337 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004338 auto __lhs_sz = __lhs.size();
4339 auto __rhs_sz = __rhs.size();
4340 _String __r(__uninitialized_size_tag(),
4341 __lhs_sz + __rhs_sz,
4342 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4343 auto __ptr = std::__to_address(__r.__get_pointer());
4344 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4345 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4346 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347 return __r;
4348}
4349
4350template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004351_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352basic_string<_CharT, _Traits, _Allocator>
4353operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4354{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004355 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004356 auto __lhs_sz = _Traits::length(__lhs);
4357 auto __rhs_sz = __rhs.size();
4358 _String __r(__uninitialized_size_tag(),
4359 __lhs_sz + __rhs_sz,
4360 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4361 auto __ptr = std::__to_address(__r.__get_pointer());
4362 _Traits::copy(__ptr, __lhs, __lhs_sz);
4363 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4364 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365 return __r;
4366}
4367
4368template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004369_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004370basic_string<_CharT, _Traits, _Allocator>
4371operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4372{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004373 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004374 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004375 _String __r(__uninitialized_size_tag(),
4376 __rhs_sz + 1,
4377 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4378 auto __ptr = std::__to_address(__r.__get_pointer());
4379 _Traits::assign(__ptr, 1, __lhs);
4380 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4381 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382 return __r;
4383}
4384
4385template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004386inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004387basic_string<_CharT, _Traits, _Allocator>
4388operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __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 __lhs_sz = __lhs.size();
4392 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004393 _String __r(__uninitialized_size_tag(),
4394 __lhs_sz + __rhs_sz,
4395 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4396 auto __ptr = std::__to_address(__r.__get_pointer());
4397 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4398 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4399 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004400 return __r;
4401}
4402
4403template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004404_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004405basic_string<_CharT, _Traits, _Allocator>
4406operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4407{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004408 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004409 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004410 _String __r(__uninitialized_size_tag(),
4411 __lhs_sz + 1,
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::assign(__ptr + __lhs_sz, 1, __rhs);
4416 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417 return __r;
4418}
4419
Eric Fiselierfc92be82017-04-19 00:28:44 +00004420#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004421
4422template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004423inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424basic_string<_CharT, _Traits, _Allocator>
4425operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4426{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004427 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428}
4429
4430template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004431inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432basic_string<_CharT, _Traits, _Allocator>
4433operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4434{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004435 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436}
4437
4438template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004439inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440basic_string<_CharT, _Traits, _Allocator>
4441operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4442{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004443 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004444}
4445
4446template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004447inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004448basic_string<_CharT, _Traits, _Allocator>
4449operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4450{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004451 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452}
4453
4454template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004455inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004456basic_string<_CharT, _Traits, _Allocator>
4457operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4458{
4459 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004460 return std::move(__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+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4467{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004468 return std::move(__lhs.append(__rhs));
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+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4475{
4476 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004477 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004478}
4479
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004480#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481
4482// swap
4483
4484template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004485inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004487swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004488 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4489 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490{
4491 __lhs.swap(__rhs);
4492}
4493
Bruce Mitchener170d8972020-11-24 12:53:53 -05004494_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4495_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4496_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4497_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4498_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004499
Bruce Mitchener170d8972020-11-24 12:53:53 -05004500_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4501_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4502_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004503
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004504_LIBCPP_FUNC_VIS string to_string(int __val);
4505_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4506_LIBCPP_FUNC_VIS string to_string(long __val);
4507_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4508_LIBCPP_FUNC_VIS string to_string(long long __val);
4509_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4510_LIBCPP_FUNC_VIS string to_string(float __val);
4511_LIBCPP_FUNC_VIS string to_string(double __val);
4512_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004513
Louis Dionne89258142021-08-23 15:32:36 -04004514#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004515_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4516_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4517_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4518_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4519_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004520
Bruce Mitchener170d8972020-11-24 12:53:53 -05004521_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4522_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4523_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004524
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004525_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4526_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4527_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4528_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4529_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4530_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4531_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4532_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4533_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004534#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004535
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004537_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004538const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4539 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004540
Marshall Clow851b9ec2019-05-20 21:56:51 +00004541template <class _CharT, class _Allocator>
4542struct _LIBCPP_TEMPLATE_VIS
4543 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4544 : public unary_function<
4545 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004546{
4547 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004548 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4549 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004550};
4551
Howard Hinnantc51e1022010-05-11 19:42:16 +00004552
Howard Hinnantdc095972011-07-18 15:51:59 +00004553template<class _CharT, class _Traits, class _Allocator>
4554basic_ostream<_CharT, _Traits>&
4555operator<<(basic_ostream<_CharT, _Traits>& __os,
4556 const basic_string<_CharT, _Traits, _Allocator>& __str);
4557
4558template<class _CharT, class _Traits, class _Allocator>
4559basic_istream<_CharT, _Traits>&
4560operator>>(basic_istream<_CharT, _Traits>& __is,
4561 basic_string<_CharT, _Traits, _Allocator>& __str);
4562
4563template<class _CharT, class _Traits, class _Allocator>
4564basic_istream<_CharT, _Traits>&
4565getline(basic_istream<_CharT, _Traits>& __is,
4566 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4567
4568template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004569inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004570basic_istream<_CharT, _Traits>&
4571getline(basic_istream<_CharT, _Traits>& __is,
4572 basic_string<_CharT, _Traits, _Allocator>& __str);
4573
Howard Hinnantdc095972011-07-18 15:51:59 +00004574template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004575inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004576basic_istream<_CharT, _Traits>&
4577getline(basic_istream<_CharT, _Traits>&& __is,
4578 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4579
4580template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004581inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004582basic_istream<_CharT, _Traits>&
4583getline(basic_istream<_CharT, _Traits>&& __is,
4584 basic_string<_CharT, _Traits, _Allocator>& __str);
4585
Marshall Clow29b53f22018-12-14 18:49:35 +00004586#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004587template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004588inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004589 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4590 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4591 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004592 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004593 return __old_size - __str.size();
4594}
Marshall Clow29b53f22018-12-14 18:49:35 +00004595
Marek Kurdeja98b1412020-05-02 13:58:03 +02004596template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004597inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004598 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4599 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4600 _Predicate __pred) {
4601 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004602 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004603 __str.end());
4604 return __old_size - __str.size();
4605}
Marshall Clow29b53f22018-12-14 18:49:35 +00004606#endif
4607
Louis Dionne510450b2022-04-01 16:38:30 -04004608#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004609
4610template<class _CharT, class _Traits, class _Allocator>
4611bool
4612basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4613{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004614 return data() <= std::__to_address(__i->base()) &&
4615 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004616}
4617
4618template<class _CharT, class _Traits, class _Allocator>
4619bool
4620basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4621{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004622 return data() < std::__to_address(__i->base()) &&
4623 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004624}
4625
4626template<class _CharT, class _Traits, class _Allocator>
4627bool
4628basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4629{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004630 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004631 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004632}
4633
4634template<class _CharT, class _Traits, class _Allocator>
4635bool
4636basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4637{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004638 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004639 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004640}
4641
Louis Dionne510450b2022-04-01 16:38:30 -04004642#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004643
Louis Dionne173f29e2019-05-29 16:01:36 +00004644#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004645// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004646inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004647{
4648 inline namespace string_literals
4649 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004650 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004651 basic_string<char> operator "" s( const char *__str, size_t __len )
4652 {
4653 return basic_string<char> (__str, __len);
4654 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004655
Louis Dionne89258142021-08-23 15:32:36 -04004656#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004657 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004658 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4659 {
4660 return basic_string<wchar_t> (__str, __len);
4661 }
Louis Dionne89258142021-08-23 15:32:36 -04004662#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004663
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004664#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004665 inline _LIBCPP_HIDE_FROM_ABI constexpr
Marshall Clow8732fed2018-12-11 04:35:44 +00004666 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4667 {
4668 return basic_string<char8_t> (__str, __len);
4669 }
4670#endif
4671
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<char16_t> operator "" s( const char16_t *__str, size_t __len )
4674 {
4675 return basic_string<char16_t> (__str, __len);
4676 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004677
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004678 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004679 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4680 {
4681 return basic_string<char32_t> (__str, __len);
4682 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004683 } // namespace string_literals
4684} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004685
4686#if _LIBCPP_STD_VER > 17
4687template <>
4688inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4689#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4690template <>
4691inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4692#endif
4693#endif
4694
Marshall Clowcba751f2013-07-23 17:05:24 +00004695#endif
4696
Howard Hinnantc51e1022010-05-11 19:42:16 +00004697_LIBCPP_END_NAMESPACE_STD
4698
Eric Fiselierf4433a32017-05-31 22:07:49 +00004699_LIBCPP_POP_MACROS
4700
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004701#endif // _LIBCPP_STRING