blob: 8eb1d30970421621d79bca8808a2c850b1492cca [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
Mark de Weverb4830e62022-07-19 07:56:23 +020016#include <compare>
17#include <initializer_list>
18
Howard Hinnantc51e1022010-05-11 19:42:16 +000019namespace std
20{
21
22template <class stateT>
23class fpos
24{
25private:
26 stateT st;
27public:
28 fpos(streamoff = streamoff());
29
30 operator streamoff() const;
31
32 stateT state() const;
33 void state(stateT);
34
35 fpos& operator+=(streamoff);
36 fpos operator+ (streamoff) const;
37 fpos& operator-=(streamoff);
38 fpos operator- (streamoff) const;
39};
40
41template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
44template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
45
46template <class charT>
47struct char_traits
48{
Mark de Weverb4830e62022-07-19 07:56:23 +020049 using char_type = charT;
50 using int_type = ...;
51 using off_type = streamoff;
52 using pos_type = streampos;
53 using state_type = mbstate_t;
54 using comparison_category = strong_ordering; // Since C++20 only for the specializations
55 // char, wchar_t, char8_t, char16_t, and char32_t.
Howard Hinnantc51e1022010-05-11 19:42:16 +000056
Howard Hinnantaeb17d82011-05-29 19:57:12 +000057 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000058 static constexpr bool eq(char_type c1, char_type c2) noexcept;
59 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000060
61 static int compare(const char_type* s1, const char_type* s2, size_t n);
62 static size_t length(const char_type* s);
63 static const char_type* find(const char_type* s, size_t n, const char_type& a);
64 static char_type* move(char_type* s1, const char_type* s2, size_t n);
65 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
66 static char_type* assign(char_type* s, size_t n, char_type a);
67
Howard Hinnant270c00e2012-07-20 19:09:12 +000068 static constexpr int_type not_eof(int_type c) noexcept;
69 static constexpr char_type to_char_type(int_type c) noexcept;
70 static constexpr int_type to_int_type(char_type c) noexcept;
71 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
72 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000073};
74
75template <> struct char_traits<char>;
76template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010077template <> struct char_traits<char8_t>; // C++20
78template <> struct char_traits<char16_t>;
79template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
Howard Hinnant3b6579a2010-08-22 00:02:43 +000081template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000082class basic_string
83{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000084public:
85// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000086 typedef traits traits_type;
87 typedef typename traits_type::char_type value_type;
88 typedef Allocator allocator_type;
89 typedef typename allocator_type::size_type size_type;
90 typedef typename allocator_type::difference_type difference_type;
91 typedef typename allocator_type::reference reference;
92 typedef typename allocator_type::const_reference const_reference;
93 typedef typename allocator_type::pointer pointer;
94 typedef typename allocator_type::const_pointer const_pointer;
95 typedef implementation-defined iterator;
96 typedef implementation-defined const_iterator;
97 typedef std::reverse_iterator<iterator> reverse_iterator;
98 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
99
100 static const size_type npos = -1;
101
Howard Hinnant3e276872011-06-03 18:40:47 +0000102 basic_string()
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200103 noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20
104 explicit basic_string(const allocator_type& a); // constexpr since C++20
105 basic_string(const basic_string& str); // constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000106 basic_string(basic_string&& str)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200107 noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000108 basic_string(const basic_string& str, size_type pos,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200109 const allocator_type& a = allocator_type()); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000110 basic_string(const basic_string& str, size_type pos, size_type n,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200111 const Allocator& a = Allocator()); // constexpr since C++20
Marshall Clow78dbe462016-11-14 18:22:19 +0000112 template<class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200113 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 +0000114 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200115 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17, constexpr since C++20
116 basic_string(const value_type* s, const allocator_type& a = allocator_type()); // constexpr since C++20
117 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 +0200118 basic_string(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200119 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 +0000120 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000121 basic_string(InputIterator begin, InputIterator end,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200122 const allocator_type& a = allocator_type()); // constexpr since C++20
123 basic_string(initializer_list<value_type>, const Allocator& = Allocator()); // constexpr since C++20
124 basic_string(const basic_string&, const Allocator&); // constexpr since C++20
125 basic_string(basic_string&&, const Allocator&); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200127 ~basic_string(); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200129 operator basic_string_view<charT, traits>() const noexcept; // constexpr since C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000130
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200131 basic_string& operator=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000132 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200133 basic_string& operator=(const T& t); // C++17, constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000134 basic_string& operator=(basic_string&& str)
135 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000136 allocator_type::propagate_on_container_move_assignment::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200137 allocator_type::is_always_equal::value ); // C++17, constexpr since C++20
138 basic_string& operator=(const value_type* s); // constexpr since C++20
Marek Kurdej90d79712021-07-27 16:16:21 +0200139 basic_string& operator=(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200140 basic_string& operator=(value_type c); // constexpr since C++20
141 basic_string& operator=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200143 iterator begin() noexcept; // constexpr since C++20
144 const_iterator begin() const noexcept; // constexpr since C++20
145 iterator end() noexcept; // constexpr since C++20
146 const_iterator end() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200148 reverse_iterator rbegin() noexcept; // constexpr since C++20
149 const_reverse_iterator rbegin() const noexcept; // constexpr since C++20
150 reverse_iterator rend() noexcept; // constexpr since C++20
151 const_reverse_iterator rend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200153 const_iterator cbegin() const noexcept; // constexpr since C++20
154 const_iterator cend() const noexcept; // constexpr since C++20
155 const_reverse_iterator crbegin() const noexcept; // constexpr since C++20
156 const_reverse_iterator crend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200158 size_type size() const noexcept; // constexpr since C++20
159 size_type length() const noexcept; // constexpr since C++20
160 size_type max_size() const noexcept; // constexpr since C++20
161 size_type capacity() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000162
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200163 void resize(size_type n, value_type c); // constexpr since C++20
164 void resize(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100166 template<class Operation>
167 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
168
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200169 void reserve(size_type res_arg); // constexpr since C++20
Marek Kurdejc9848142020-11-26 10:07:16 +0100170 void reserve(); // deprecated in C++20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200171 void shrink_to_fit(); // constexpr since C++20
172 void clear() noexcept; // constexpr since C++20
173 bool empty() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200175 const_reference operator[](size_type pos) const; // constexpr since C++20
176 reference operator[](size_type pos); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200178 const_reference at(size_type n) const; // constexpr since C++20
179 reference at(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200181 basic_string& operator+=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000182 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200183 basic_string& operator+=(const T& t); // C++17, constexpr since C++20
184 basic_string& operator+=(const value_type* s); // constexpr since C++20
185 basic_string& operator+=(value_type c); // constexpr since C++20
186 basic_string& operator+=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200188 basic_string& append(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000189 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200190 basic_string& append(const T& t); // C++17, constexpr since C++20
191 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 +0000192 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200193 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
194 basic_string& append(const value_type* s, size_type n); // constexpr since C++20
195 basic_string& append(const value_type* s); // constexpr since C++20
196 basic_string& append(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000197 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200198 basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
199 basic_string& append(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200201 void push_back(value_type c); // constexpr since C++20
202 void pop_back(); // constexpr since C++20
203 reference front(); // constexpr since C++20
204 const_reference front() const; // constexpr since C++20
205 reference back(); // constexpr since C++20
206 const_reference back() const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200208 basic_string& assign(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000209 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200210 basic_string& assign(const T& t); // C++17, constexpr since C++20
211 basic_string& assign(basic_string&& str); // constexpr since C++20
212 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 +0000213 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200214 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
215 basic_string& assign(const value_type* s, size_type n); // constexpr since C++20
216 basic_string& assign(const value_type* s); // constexpr since C++20
217 basic_string& assign(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000218 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200219 basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
220 basic_string& assign(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200222 basic_string& insert(size_type pos1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000223 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200224 basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000225 basic_string& insert(size_type pos1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200226 size_type pos2, size_type n); // constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000227 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200228 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
229 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
230 basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
231 basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
232 iterator insert(const_iterator p, value_type c); // constexpr since C++20
233 iterator insert(const_iterator p, size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000234 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200235 iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20
236 iterator insert(const_iterator p, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200238 basic_string& erase(size_type pos = 0, size_type n = npos); // constexpr since C++20
239 iterator erase(const_iterator position); // constexpr since C++20
240 iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200242 basic_string& replace(size_type pos1, size_type n1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000243 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200244 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 +0000245 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200246 size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000247 template <class T>
248 basic_string& replace(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200249 size_type pos2, size_type n); // C++17, constexpr since C++20
250 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
251 basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
252 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
253 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000254 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200255 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17, constexpr since C++20
256 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
257 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s); // constexpr since C++20
258 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 +0000259 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200260 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
261 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200263 size_type copy(value_type* s, size_type n, size_type pos = 0) const; // constexpr since C++20
264 basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265
Howard Hinnant3e276872011-06-03 18:40:47 +0000266 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000267 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200268 allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200270 const value_type* c_str() const noexcept; // constexpr since C++20
271 const value_type* data() const noexcept; // constexpr since C++20
272 value_type* data() noexcept; // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200274 allocator_type get_allocator() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200276 size_type find(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000277 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200278 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
279 size_type find(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
280 size_type find(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
281 size_type find(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200283 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000284 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200285 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
286 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
287 size_type rfind(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
288 size_type rfind(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200290 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 +0000291 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200292 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
293 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
294 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
295 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 +0000296
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200297 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 +0000298 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200299 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
300 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
301 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
302 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 +0000303
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200304 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 +0000305 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200306 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
307 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
308 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
309 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 +0000310
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200311 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 +0000312 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200313 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
314 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
315 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
316 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 +0000317
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200318 int compare(const basic_string& str) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000319 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200320 int compare(const T& t) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
321 int compare(size_type pos1, size_type n1, const basic_string& str) const; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000322 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200323 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 +0000324 int compare(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200325 size_type pos2, size_type n2=npos) const; // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000326 template <class T>
327 int compare(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200328 size_type pos2, size_type n2=npos) const; // C++17, constexpr since C++20
329 int compare(const value_type* s) const noexcept; // constexpr since C++20
330 int compare(size_type pos1, size_type n1, const value_type* s) const; // constexpr since C++20
331 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 +0000332
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200333 constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
334 constexpr bool starts_with(charT c) const noexcept; // C++20
335 constexpr bool starts_with(const charT* s) const; // C++20
336 constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
337 constexpr bool ends_with(charT c) const noexcept; // C++20
338 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000339
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200340 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
341 constexpr bool contains(charT c) const noexcept; // C++2b
342 constexpr bool contains(const charT* s) const; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000343};
344
Marshall Clowa0563332018-02-08 06:34:03 +0000345template<class InputIterator,
346 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
347basic_string(InputIterator, InputIterator, Allocator = Allocator())
348 -> basic_string<typename iterator_traits<InputIterator>::value_type,
349 char_traits<typename iterator_traits<InputIterator>::value_type>,
350 Allocator>; // C++17
351
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352template<class charT, class traits, class Allocator>
353basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000354operator+(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200355 const basic_string<charT, traits, Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356
357template<class charT, class traits, class Allocator>
358basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200359operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
361template<class charT, class traits, class Allocator>
362basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200363operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
366basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200367operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368
369template<class charT, class traits, class Allocator>
370basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200371operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372
373template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000374bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200375 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200378bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
380template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200381bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000383template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000384bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200385 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200388bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200391bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392
393template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000394bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200395 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200398bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200401bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402
403template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000404bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200405 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200408bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200411bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412
413template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000414bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200415 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200418bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200421bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422
423template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000424bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200425 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200428bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200431bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Mark de Weverb4830e62022-07-19 07:56:23 +0200432
433template<class charT, class traits, class Allocator> // since C++20
434constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
435 const basic_string<charT, traits, Allocator>& rhs) noexcept;
436
437template<class charT, class traits, class Allocator> // since C++20
438constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
439 const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440
441template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000442void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000443 basic_string<charT, traits, Allocator>& rhs)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200444 noexcept(noexcept(lhs.swap(rhs))); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445
446template<class charT, class traits, class Allocator>
447basic_istream<charT, traits>&
448operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
449
450template<class charT, class traits, class Allocator>
451basic_ostream<charT, traits>&
452operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
453
454template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000455basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000456getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
457 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000458
459template<class charT, class traits, class Allocator>
460basic_istream<charT, traits>&
461getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
462
Marshall Clow29b53f22018-12-14 18:49:35 +0000463template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200464typename basic_string<charT, traits, Allocator>::size_type
465erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000466template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200467typename basic_string<charT, traits, Allocator>::size_type
468erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000469
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470typedef basic_string<char> string;
471typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100472typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000473typedef basic_string<char16_t> u16string;
474typedef basic_string<char32_t> u32string;
475
Bruce Mitchener170d8972020-11-24 12:53:53 -0500476int stoi (const string& str, size_t* idx = nullptr, int base = 10);
477long stol (const string& str, size_t* idx = nullptr, int base = 10);
478unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
479long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
480unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000481
Bruce Mitchener170d8972020-11-24 12:53:53 -0500482float stof (const string& str, size_t* idx = nullptr);
483double stod (const string& str, size_t* idx = nullptr);
484long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000485
486string to_string(int val);
487string to_string(unsigned val);
488string to_string(long val);
489string to_string(unsigned long val);
490string to_string(long long val);
491string to_string(unsigned long long val);
492string to_string(float val);
493string to_string(double val);
494string to_string(long double val);
495
Bruce Mitchener170d8972020-11-24 12:53:53 -0500496int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
497long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
498unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
499long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
500unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000501
Bruce Mitchener170d8972020-11-24 12:53:53 -0500502float stof (const wstring& str, size_t* idx = nullptr);
503double stod (const wstring& str, size_t* idx = nullptr);
504long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000505
506wstring to_wstring(int val);
507wstring to_wstring(unsigned val);
508wstring to_wstring(long val);
509wstring to_wstring(unsigned long val);
510wstring to_wstring(long long val);
511wstring to_wstring(unsigned long long val);
512wstring to_wstring(float val);
513wstring to_wstring(double val);
514wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515
516template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100517template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518template <> struct hash<u16string>;
519template <> struct hash<u32string>;
520template <> struct hash<wstring>;
521
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200522basic_string<char> operator "" s( const char *str, size_t len ); // C++14, constexpr since C++20
523basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14, constexpr since C++20
524constexpr basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
525basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14, constexpr since C++20
526basic_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 +0000527
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528} // std
529
530*/
531
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100532#include <__algorithm/max.h>
533#include <__algorithm/min.h>
534#include <__algorithm/remove.h>
535#include <__algorithm/remove_if.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400536#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400538#include <__debug>
Mark de Weverdf3e0d42021-09-26 15:47:42 +0200539#include <__format/enable_insertable.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200540#include <__functional/hash.h>
Nikolas Klauser24540d32022-05-21 00:45:51 +0200541#include <__functional/unary_function.h>
Nikolas Klauser713bfda2022-10-13 01:03:58 +0200542#include <__fwd/string.h>
Nikolas Klauser80840272022-02-04 13:04:33 +0100543#include <__ios/fpos.h>
Nikolas Klauserfb1b0672022-06-10 19:53:10 +0200544#include <__iterator/distance.h>
545#include <__iterator/iterator_traits.h>
546#include <__iterator/reverse_iterator.h>
Louis Dionne77249522021-06-11 09:55:11 -0400547#include <__iterator/wrap_iter.h>
Nikolas Klauserc513eba2022-04-09 09:41:19 +0200548#include <__memory/allocate_at_least.h>
Nikolas Klauser0b5df932022-09-05 00:01:15 +0200549#include <__memory/allocator.h>
550#include <__memory/allocator_traits.h>
551#include <__memory/compressed_pair.h>
552#include <__memory/construct_at.h>
553#include <__memory/pointer_traits.h>
Nikolas Klauser35080b42022-07-26 16:13:56 +0200554#include <__memory/swap_allocator.h>
Arthur O'Dwyerebf2d342022-10-06 16:53:30 -0400555#include <__memory_resource/polymorphic_allocator.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200556#include <__string/char_traits.h>
557#include <__string/extern_template_lists.h>
Nikolas Klauser0b5df932022-09-05 00:01:15 +0200558#include <__type_traits/is_allocator.h>
559#include <__type_traits/noexcept_move_assign_container.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100560#include <__utility/auto_cast.h>
561#include <__utility/move.h>
562#include <__utility/swap.h>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200563#include <__utility/unreachable.h>
564#include <climits>
Louis Dionne44962c32022-06-13 11:21:16 -0400565#include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400566#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400567#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400568#include <cstring>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200569#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400571#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000573#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000574
Louis Dionne89258142021-08-23 15:32:36 -0400575#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100576# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400577#endif
578
Nikolas Klausera0e0edb2022-06-16 22:43:46 +0200579// standard-mandated includes
580
581// [iterator.range]
582#include <__iterator/access.h>
583#include <__iterator/data.h>
584#include <__iterator/empty.h>
585#include <__iterator/reverse_access.h>
586#include <__iterator/size.h>
587
588// [string.syn]
589#include <compare>
590#include <initializer_list>
591
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000592#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500593# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000594#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
Eric Fiselierf4433a32017-05-31 22:07:49 +0000596_LIBCPP_PUSH_MACROS
597#include <__undef_macros>
598
599
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600_LIBCPP_BEGIN_NAMESPACE_STD
601
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602// basic_string
603
604template<class _CharT, class _Traits, class _Allocator>
605basic_string<_CharT, _Traits, _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200606_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant944510a2011-06-14 19:58:17 +0000607operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
608 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609
610template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200611_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000613operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614
615template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200616_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000618operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619
620template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200621inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000623operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624
625template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200626_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000628operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629
Louis Dionnedc496ec2021-06-08 17:25:08 -0400630extern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Shoaib Meenaiea363712017-07-29 02:54:41 +0000631
Marshall Clow039b2f02016-01-13 21:54:34 +0000632template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400633struct __string_is_trivial_iterator : public false_type {};
634
635template <class _Tp>
636struct __string_is_trivial_iterator<_Tp*>
637 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000638
Louis Dionne173f29e2019-05-29 16:01:36 +0000639template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400640struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
641 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000642
Marshall Clow82513342016-09-24 22:45:42 +0000643template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500644struct __can_be_converted_to_string_view : public _BoolConstant<
645 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
646 !is_convertible<const _Tp&, const _CharT*>::value
647 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000648
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200649struct __uninitialized_size_tag {};
650
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000651template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser713bfda2022-10-13 01:03:58 +0200652class basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000653{
654public:
655 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000656 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000657 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000658 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000660 typedef allocator_traits<allocator_type> __alloc_traits;
661 typedef typename __alloc_traits::size_type size_type;
662 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000663 typedef value_type& reference;
664 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000665 typedef typename __alloc_traits::pointer pointer;
666 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667
Marshall Clow79f33542018-03-21 00:36:05 +0000668 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
669 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
670 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
671 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000672 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000673 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000674 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000675
Nikolas Klausereb116e22022-10-08 22:17:32 +0200676 static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
677 "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
678 "original allocator");
679
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680 typedef __wrap_iter<pointer> iterator;
681 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200682 typedef std::reverse_iterator<iterator> reverse_iterator;
683 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684
685private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200686 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000687
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000688#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000689
690 struct __long
691 {
692 pointer __data_;
693 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200694 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
695 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000696 };
697
Howard Hinnant68bf1812013-04-30 21:44:48 +0000698 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
699 (sizeof(__long) - 1)/sizeof(value_type) : 2};
700
701 struct __short
702 {
703 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200704 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200705 unsigned char __size_ : 7;
706 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000707 };
708
Nikolas Klauser62060be2022-04-20 10:52:04 +0200709// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200710// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200711//
712// If the LSB is used to store the short-flag in the short string representation,
713// we have to multiply the size by two when it is stored and divide it by two when
714// it is loaded to make sure that we always store an even number. In the long string
715// representation, we can ignore this because we can assume that we always allocate
716// an even amount of value_types.
717//
718// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
719// This does not impact the short string representation, since we never need the MSB
720// for representing the size of a short string anyway.
721
722#ifdef _LIBCPP_BIG_ENDIAN
723 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000724#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200725 static const size_type __endian_factor = 1;
726#endif
727
728#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
729
730#ifdef _LIBCPP_BIG_ENDIAN
731 static const size_type __endian_factor = 1;
732#else
733 static const size_type __endian_factor = 2;
734#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000735
Xing Xue38b9fc42022-06-24 17:25:15 -0400736 // Attribute 'packed' is used to keep the layout compatible with the
737 // previous definition that did not use bit fields. This is because on
738 // some platforms bit fields have a default size rather than the actual
739 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 struct __long
741 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400742 struct _LIBCPP_PACKED {
743 size_type __is_long_ : 1;
744 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
745 };
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 size_type __size_;
747 pointer __data_;
748 };
749
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
751 (sizeof(__long) - 1)/sizeof(value_type) : 2};
752
753 struct __short
754 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400755 struct _LIBCPP_PACKED {
756 unsigned char __is_long_ : 1;
757 unsigned char __size_ : 7;
758 };
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200759 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 value_type __data_[__min_cap];
761 };
762
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400763#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000764
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200765 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
766
Howard Hinnant8ea98242013-08-23 17:37:05 +0000767 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768
Howard Hinnant8ea98242013-08-23 17:37:05 +0000769 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770
771 struct __raw
772 {
773 size_type __words[__n_words];
774 };
775
776 struct __rep
777 {
778 union
779 {
780 __long __l;
781 __short __s;
782 __raw __r;
783 };
784 };
785
786 __compressed_pair<__rep, allocator_type> __r_;
787
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200788 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
789 // don't initialize the characters. The contents of the string, including the null terminator, must be
790 // initialized separately.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200791 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200792 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200793 : __r_(__default_init_tag(), __a) {
794 if (__size > max_size())
795 __throw_length_error();
796 if (__fits_in_sso(__size)) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +0200797 __r_.first() = __rep();
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200798 __set_short_size(__size);
799 } else {
800 auto __capacity = __recommend(__size) + 1;
801 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200802 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200803 __set_long_cap(__capacity);
804 __set_long_pointer(__allocation);
805 __set_long_size(__size);
806 }
807 std::__debug_db_insert_c(this);
808 }
809
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810public:
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200811 _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200813 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
814 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
815 : __r_(__default_init_tag(), __default_init_tag()) {
816 std::__debug_db_insert_c(this);
817 __default_init();
818 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000819
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200820 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000821#if _LIBCPP_STD_VER <= 14
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200822 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +0000823#else
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200824 _NOEXCEPT
Marshall Clowa80abc72015-06-03 19:56:43 +0000825#endif
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200826 : __r_(__default_init_tag(), __a) {
827 std::__debug_db_insert_c(this);
828 __default_init();
829 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000830
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200831 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
832 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000833
Eric Fiselierfc92be82017-04-19 00:28:44 +0000834#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200835 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)
836# if _LIBCPP_STD_VER <= 14
837 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
838# else
839 _NOEXCEPT
840# endif
841 : __r_(std::move(__str.__r_)) {
842 __str.__default_init();
843 std::__debug_db_insert_c(this);
844 if (__is_long())
845 std::__debug_db_swap(this, &__str);
846 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000847
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200848 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)
849 : __r_(__default_init_tag(), __a) {
850 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
851 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
852 else {
853 if (__libcpp_is_constant_evaluated())
854 __r_.first() = __rep();
855 __r_.first() = __str.__r_.first();
856 __str.__default_init();
857 }
858 std::__debug_db_insert_c(this);
859 if (__is_long())
860 std::__debug_db_swap(this, &__str);
861 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400862#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000863
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200864 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
865 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s)
866 : __r_(__default_init_tag(), __default_init_tag()) {
867 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
868 __init(__s, traits_type::length(__s));
869 std::__debug_db_insert_c(this);
870 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000871
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200872 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
873 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, const _Allocator& __a);
Marshall Clowe46031a2018-07-02 18:41:15 +0000874
Marek Kurdej90d79712021-07-27 16:16:21 +0200875#if _LIBCPP_STD_VER > 20
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200876 basic_string(nullptr_t) = delete;
Marek Kurdej90d79712021-07-27 16:16:21 +0200877#endif
878
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200879 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n)
880 : __r_(__default_init_tag(), __default_init_tag()) {
881 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
882 __init(__s, __n);
883 std::__debug_db_insert_c(this);
884 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000885
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200886 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
887 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
888 : __r_(__default_init_tag(), __a) {
889 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
890 __init(__s, __n);
891 std::__debug_db_insert_c(this);
892 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000893
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200894 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c)
895 : __r_(__default_init_tag(), __default_init_tag()) {
896 __init(__n, __c);
897 std::__debug_db_insert_c(this);
898 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000899
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200900 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
901 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
Marshall Clowe46031a2018-07-02 18:41:15 +0000902
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200903 _LIBCPP_CONSTEXPR_SINCE_CXX20
904 basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000905
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200906 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
907 basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())
908 : __r_(__default_init_tag(), __a) {
909 size_type __str_sz = __str.size();
910 if (__pos > __str_sz)
911 __throw_out_of_range();
912 __init(__str.data() + __pos, __str_sz - __pos);
913 std::__debug_db_insert_c(this);
914 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000915
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200916 template <class _Tp,
917 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
918 !__is_same_uncvref<_Tp, basic_string>::value> >
919 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
920 basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type());
921
922 template <class _Tp,
923 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
924 !__is_same_uncvref<_Tp, basic_string>::value> >
925 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
926 const _Tp& __t);
927
928 template <class _Tp,
929 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
930 !__is_same_uncvref<_Tp, basic_string>::value> >
931 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
932 const _Tp& __t, const allocator_type& __a);
933
934 template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
935 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last)
936 : __r_(__default_init_tag(), __default_init_tag()) {
937 __init(__first, __last);
938 std::__debug_db_insert_c(this);
939 }
940
941 template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
942 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
943 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
944 : __r_(__default_init_tag(), __a) {
945 __init(__first, __last);
946 std::__debug_db_insert_c(this);
947 }
948
Eric Fiselierfc92be82017-04-19 00:28:44 +0000949#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200950 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il)
951 : __r_(__default_init_tag(), __default_init_tag()) {
952 __init(__il.begin(), __il.end());
953 std::__debug_db_insert_c(this);
954 }
955
956 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)
957 : __r_(__default_init_tag(), __a) {
958 __init(__il.begin(), __il.end());
959 std::__debug_db_insert_c(this);
960 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400961#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200963 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200965 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000966 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
967
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200968 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000969
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200970 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
971 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200972 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200973 __self_view __sv = __t;
974 return assign(__sv);
975 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000976
Eric Fiselierfc92be82017-04-19 00:28:44 +0000977#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200978 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +0000979 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000980 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200981 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierfc92be82017-04-19 00:28:44 +0000982 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200984 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200985 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200986#if _LIBCPP_STD_VER > 20
987 basic_string& operator=(nullptr_t) = delete;
988#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200989 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200991 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000992 iterator begin() _NOEXCEPT
993 {return iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200994 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000995 const_iterator begin() const _NOEXCEPT
996 {return const_iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200997 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000998 iterator end() _NOEXCEPT
999 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001000 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001001 const_iterator end() const _NOEXCEPT
1002 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -04001003
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001004 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001005 reverse_iterator rbegin() _NOEXCEPT
1006 {return reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001007 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001008 const_reverse_iterator rbegin() const _NOEXCEPT
1009 {return const_reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001010 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001011 reverse_iterator rend() _NOEXCEPT
1012 {return reverse_iterator(begin());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001013 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001014 const_reverse_iterator rend() const _NOEXCEPT
1015 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001017 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001018 const_iterator cbegin() const _NOEXCEPT
1019 {return begin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001020 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001021 const_iterator cend() const _NOEXCEPT
1022 {return end();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001023 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001024 const_reverse_iterator crbegin() const _NOEXCEPT
1025 {return rbegin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001026 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001027 const_reverse_iterator crend() const _NOEXCEPT
1028 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001030 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001032 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {return size();}
1033 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT;
1034 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001035 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
1036 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001038 _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
1039 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001041 _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +01001042
1043#if _LIBCPP_STD_VER > 20
1044 template <class _Op>
1045 _LIBCPP_HIDE_FROM_ABI constexpr
1046 void resize_and_overwrite(size_type __n, _Op __op) {
1047 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001048 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +01001049 }
1050#endif
1051
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001052 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +00001053
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001054 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001055 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
1056 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001057
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001058 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowb7db4972017-11-15 20:02:27 +00001059 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001061 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001062 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001063 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001065 _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
1066 _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001068 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001069 return append(__str);
1070 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001071
1072 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001073 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001074 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001075 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001076 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1077 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001078 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001079 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001080 operator+=(const _Tp& __t) {
1081 __self_view __sv = __t; return append(__sv);
1082 }
1083
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001084 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001085 return append(__s);
1086 }
1087
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001088 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001089 push_back(__c);
1090 return *this;
1091 }
1092
Eric Fiselierfc92be82017-04-19 00:28:44 +00001093#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001094 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001095 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001096#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001098 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001100
1101 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001102 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001103 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001104 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1105 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001106 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001107 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001108 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001109 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001110
Marshall Clow62953962016-10-03 23:40:48 +00001111 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001112 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001113 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001114 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001115 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1116 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001117 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001118 >
Marshall Clow82513342016-09-24 22:45:42 +00001119 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001120 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n);
1121 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s);
1122 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001123
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001124 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier451d5582018-11-26 20:15:38 +00001125 void __append_default_init(size_type __n);
1126
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001128 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001129 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001131 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001134 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001135 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001136 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001137 append(__temp.data(), __temp.size());
1138 return *this;
1139 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001141 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001142 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001144 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001146 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001147 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001148 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001149
Eric Fiselierfc92be82017-04-19 00:28:44 +00001150#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001151 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001153#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001155 _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
1156 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
1157 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT;
1158 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT;
1159 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT;
1160 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161
Marshall Clowe46031a2018-07-02 18:41:15 +00001162 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001163 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001164 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001165 <
1166 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1167 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001168 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001169 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001170 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001171 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001172#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001173 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001174 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001175 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001176 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001177#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001178 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001179 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001180 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001181 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001182 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001183 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1184 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001185 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001186 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001187 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001188 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n);
1189 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s);
1190 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001192 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001193 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001195 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001197 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 assign(_InputIterator __first, _InputIterator __last);
1199 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001200 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001201 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001203 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001205 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001207#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001208 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001210#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001212 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001214
1215 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001216 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001217 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001218 <
1219 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1220 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001221 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001222 insert(size_type __pos1, const _Tp& __t)
1223 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1224
Marshall Clow82513342016-09-24 22:45:42 +00001225 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001226 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001227 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001228 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001229 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001230 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001231 >
Marshall Clow82513342016-09-24 22:45:42 +00001232 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001233 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001234 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001235 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1236 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s);
1237 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1238 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
1239 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1241 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001242 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001243 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001245 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001247 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1249 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001250 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001251 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001253 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001255 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001257#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001258 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1260 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001261#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001263 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1264 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265 iterator erase(const_iterator __pos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001266 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 iterator erase(const_iterator __first, const_iterator __last);
1268
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001269 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001271
1272 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001273 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001274 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001275 <
1276 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1277 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001278 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001279 replace(size_type __pos1, size_type __n1, const _Tp& __t) { __self_view __sv = __t; return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001280 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001281 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 +00001282 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001283 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001284 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001285 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001286 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001287 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001288 >
Marshall Clow82513342016-09-24 22:45:42 +00001289 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001290 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001291 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001292 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1293 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1294 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001295 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001296
1297 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001298 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001299 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001300 <
1301 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1302 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001303 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001304 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1305
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001306 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001307 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001308 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001309 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001310 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001311 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001312 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001313 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001314 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001316 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001318 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001319 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001320#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001321 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001322 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001324#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001326 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
1327 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1329
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001330 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001331 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001332#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001333 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001334#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001335 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001336 __is_nothrow_swappable<allocator_type>::value);
1337#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001339 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001340 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001341 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001342 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001343#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001344 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001345 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001346#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001348 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001349 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001351 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001352 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001353
1354 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001355 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001356 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001357 <
1358 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1359 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001360 >
zoecarver1997e0a2021-02-05 11:54:47 -08001361 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001362 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001363 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001364 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001365 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001366 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001368 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001369 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001370
1371 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001372 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001373 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001374 <
1375 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1376 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001377 >
zoecarver1997e0a2021-02-05 11:54:47 -08001378 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001379 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001380 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001381 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001382 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001383 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001385 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001386 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001387
1388 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001389 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001390 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001391 <
1392 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1393 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001394 >
zoecarver1997e0a2021-02-05 11:54:47 -08001395 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001396 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001397 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001398 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001399 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001400 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001401 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001403 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001404 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001405
1406 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001407 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001408 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001409 <
1410 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1411 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001412 >
zoecarver1997e0a2021-02-05 11:54:47 -08001413 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001414 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001415 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001416 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001417 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001418 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001419 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001421 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001422 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001423
1424 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001425 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001426 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001427 <
1428 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1429 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001430 >
zoecarver1997e0a2021-02-05 11:54:47 -08001431 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001432 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001433 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001434 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001435 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001436 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001437 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1438
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001439 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001440 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001441
1442 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001443 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001444 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001445 <
1446 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1447 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001448 >
zoecarver1997e0a2021-02-05 11:54:47 -08001449 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001450 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001451 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001452 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001453 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001454 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001455 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1456
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001457 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001458 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001459
1460 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001461 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001462 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001463 <
1464 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1465 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001466 >
zoecarver1997e0a2021-02-05 11:54:47 -08001467 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001468
1469 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001470 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001471 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001472 <
1473 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1474 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001475 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001476 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1477
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001478 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001480 _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001481 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1482 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001483
Marshall Clow82513342016-09-24 22:45:42 +00001484 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001485 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001486 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001487 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001488 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001489 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001490 >
Marshall Clow82513342016-09-24 22:45:42 +00001491 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001492 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const value_type* __s) const _NOEXCEPT;
1493 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1494 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001495 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496
Marshall Clow18c293b2017-12-04 20:11:38 +00001497#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001498 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001499 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001500 { return __self_view(data(), size()).starts_with(__sv); }
1501
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001502 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001503 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001504 { return !empty() && _Traits::eq(front(), __c); }
1505
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001506 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001507 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001508 { return starts_with(__self_view(__s)); }
1509
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001510 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001511 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001512 { return __self_view(data(), size()).ends_with( __sv); }
1513
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001514 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001515 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001516 { return !empty() && _Traits::eq(back(), __c); }
1517
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001518 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001519 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001520 { return ends_with(__self_view(__s)); }
1521#endif
1522
Wim Leflere023c3542021-01-19 14:33:30 -05001523#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001524 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001525 bool contains(__self_view __sv) const noexcept
1526 { return __self_view(data(), size()).contains(__sv); }
1527
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001528 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001529 bool contains(value_type __c) const noexcept
1530 { return __self_view(data(), size()).contains(__c); }
1531
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001532 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001533 bool contains(const value_type* __s) const
1534 { return __self_view(data(), size()).contains(__s); }
1535#endif
1536
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001537 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001538
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001539 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001540
Louis Dionne510450b2022-04-01 16:38:30 -04001541#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001542
1543 bool __dereferenceable(const const_iterator* __i) const;
1544 bool __decrementable(const const_iterator* __i) const;
1545 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1546 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1547
Louis Dionne510450b2022-04-01 16:38:30 -04001548#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001549
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001551 template<class _Alloc>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001552 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001553 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1554 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1555
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001556 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001557
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001558 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001559 bool __is_long() const _NOEXCEPT {
1560 if (__libcpp_is_constant_evaluated())
1561 return true;
1562 return __r_.first().__s.__is_long_;
1563 }
1564
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001565 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __begin_lifetime(pointer __begin, size_type __n) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001566#if _LIBCPP_STD_VER > 17
1567 if (__libcpp_is_constant_evaluated()) {
1568 for (size_type __i = 0; __i != __n; ++__i)
1569 std::construct_at(std::addressof(__begin[__i]));
1570 }
1571#else
1572 (void)__begin;
1573 (void)__n;
1574#endif // _LIBCPP_STD_VER > 17
1575 }
1576
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001577 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __default_init() {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001578 __r_.first() = __rep();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001579 if (__libcpp_is_constant_evaluated()) {
1580 size_type __sz = __recommend(0) + 1;
1581 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1582 __begin_lifetime(__ptr, __sz);
1583 __set_long_pointer(__ptr);
1584 __set_long_cap(__sz);
1585 __set_long_size(0);
1586 }
1587 }
1588
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001589 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __deallocate_constexpr() {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001590 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1591 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1592 }
1593
Nikolas Klauser93826d12022-01-04 17:24:03 +01001594 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1595 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1596 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1597 }
1598
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001599 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001600 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001601 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1602 size_type __sz = size();
1603 size_type __cap = capacity();
1604 value_type* __p;
1605 if (__cap - __sz >= __n)
1606 {
1607 __p = std::__to_address(__get_pointer());
1608 size_type __n_move = __sz - __ip;
1609 if (__n_move != 0)
1610 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1611 }
1612 else
1613 {
1614 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1615 __p = std::__to_address(__get_long_pointer());
1616 }
1617 __sz += __n;
1618 __set_size(__sz);
1619 traits_type::assign(__p[__sz], value_type());
1620 for (__p += __ip; __first != __last; ++__p, ++__first)
1621 traits_type::assign(*__p, *__first);
1622
1623 return begin() + __ip;
1624 }
1625
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001626 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001627 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001629 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001630 void __set_short_size(size_type __s) _NOEXCEPT {
1631 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1632 __r_.first().__s.__size_ = __s;
1633 __r_.first().__s.__is_long_ = false;
1634 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001635
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001636 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001637 size_type __get_short_size() const _NOEXCEPT {
1638 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1639 return __r_.first().__s.__size_;
1640 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001641
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001642 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001643 void __set_long_size(size_type __s) _NOEXCEPT
1644 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001645 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001646 size_type __get_long_size() const _NOEXCEPT
1647 {return __r_.first().__l.__size_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001648 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001649 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1651
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001652 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001653 void __set_long_cap(size_type __s) _NOEXCEPT {
1654 __r_.first().__l.__cap_ = __s / __endian_factor;
1655 __r_.first().__l.__is_long_ = true;
1656 }
1657
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001658 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001659 size_type __get_long_cap() const _NOEXCEPT {
1660 return __r_.first().__l.__cap_ * __endian_factor;
1661 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001663 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001664 void __set_long_pointer(pointer __p) _NOEXCEPT
1665 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001666 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001667 pointer __get_long_pointer() _NOEXCEPT
1668 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001669 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001670 const_pointer __get_long_pointer() const _NOEXCEPT
1671 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001672 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001673 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001674 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001675 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001676 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001677 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001678 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001679 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001681 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001682 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1684
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685 template <size_type __a> static
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001686 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea382952013-08-14 18:00:20 +00001687 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001688 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689 enum {__alignment = 16};
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001690 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001691 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001692 {
1693 if (__s < __min_cap) {
1694 if (__libcpp_is_constant_evaluated())
1695 return static_cast<size_type>(__min_cap);
1696 else
1697 return static_cast<size_type>(__min_cap) - 1;
1698 }
Marshall Clow80584522018-02-07 21:30:17 +00001699 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1700 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1701 if (__guess == __min_cap) ++__guess;
1702 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001703 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001705 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001706 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001707 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001708 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001709 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001711
Martijn Vels5e7c9752020-03-04 17:52:46 -05001712 // Slow path for the (inlined) copy constructor for 'long' strings.
1713 // Always externally instantiated and not inlined.
1714 // Requires that __s is zero terminated.
1715 // The main reason for this function to exist is because for unstable, we
1716 // want to allow inlining of the copy constructor. However, we don't want
1717 // to call the __init() functions as those are marked as inline which may
1718 // result in over-aggressive inlining by the compiler, where our aim is
1719 // to only inline the fast path code directly in the ctor.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001720 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001721
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001723 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001724 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001726 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1727 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728 __init(_InputIterator __first, _InputIterator __last);
1729
1730 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001731 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001732 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001734 __is_cpp17_forward_iterator<_ForwardIterator>::value
1735 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 __init(_ForwardIterator __first, _ForwardIterator __last);
1737
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001738 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001740 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001741 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1743 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001744 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745
Martijn Vels596e3de2020-02-26 15:55:49 -05001746 // __assign_no_alias is invoked for assignment operations where we
1747 // have proof that the input does not alias the current instance.
1748 // For example, operator=(basic_string) performs a 'self' check.
1749 template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001750 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001751
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001752 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753 void __erase_to_end(size_type __pos);
1754
Martijn Velsa81fc792020-02-26 13:25:43 -05001755 // __erase_external_with_move is invoked for erase() invocations where
1756 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001757 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001758
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001759 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001760 void __copy_assign_alloc(const basic_string& __str)
1761 {__copy_assign_alloc(__str, integral_constant<bool,
1762 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1763
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001764 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001765 void __copy_assign_alloc(const basic_string& __str, true_type)
1766 {
Marshall Clowf258c202017-01-31 03:40:52 +00001767 if (__alloc() == __str.__alloc())
1768 __alloc() = __str.__alloc();
1769 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001770 {
Marshall Clowf258c202017-01-31 03:40:52 +00001771 if (!__str.__is_long())
1772 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001773 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001774 __alloc() = __str.__alloc();
1775 }
1776 else
1777 {
1778 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001779 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001780 __begin_lifetime(__allocation.ptr, __allocation.count);
Nikolas Klauser1821ec32022-10-01 15:37:24 +02001781 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001782 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001783 __set_long_pointer(__allocation.ptr);
1784 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001785 __set_long_size(__str.size());
1786 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001787 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001788 }
1789
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001790 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001791 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001792 {}
1793
Eric Fiselierfc92be82017-04-19 00:28:44 +00001794#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001795 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001796 void __move_assign(basic_string& __str, false_type)
1797 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001798 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001799 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001800#if _LIBCPP_STD_VER > 14
1801 _NOEXCEPT;
1802#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001803 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001804#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001805#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001806
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001807 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001808 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001809 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001810 _NOEXCEPT_(
1811 !__alloc_traits::propagate_on_container_move_assignment::value ||
1812 is_nothrow_move_assignable<allocator_type>::value)
1813 {__move_assign_alloc(__str, integral_constant<bool,
1814 __alloc_traits::propagate_on_container_move_assignment::value>());}
1815
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001816 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc2734962011-09-02 20:42:31 +00001817 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001818 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1819 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001820 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001821 }
1822
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001823 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001824 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001825 _NOEXCEPT
1826 {}
1827
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001828 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s);
1829 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001830
1831 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1832 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1833 pointer __p = __is_long()
1834 ? (__set_long_size(__n), __get_long_pointer())
1835 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001836 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001837 traits_type::assign(__p[__n], value_type());
1838 return *this;
1839 }
1840
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001841 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001842 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001843 __set_size(__newsz);
1844 __invalidate_iterators_past(__newsz);
1845 traits_type::assign(__p[__newsz], value_type());
1846 return *this;
1847 }
1848
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001849 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001851 template<class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001852 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001853 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001854 // assume that the ranges overlap, because we can't check during constant evaluation
1855 if (__libcpp_is_constant_evaluated())
1856 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001857 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001858 return data() <= __p && __p <= data() + size();
1859 }
1860
Louis Dionned24191c2021-08-19 12:39:16 -04001861 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1862 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001863 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001864 }
1865
1866 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1867 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001868 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001869 }
1870
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001871 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const basic_string&);
1872 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const value_type*, const basic_string&);
1873 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(value_type, const basic_string&);
1874 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const value_type*);
1875 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876};
1877
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001878// These declarations must appear before any functions are implicitly used
1879// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001880#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001881#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001882 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001883# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001884 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001885# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001886#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001887 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001888# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001889 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001890# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001891#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001892#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001893
1894
Louis Dionned59f8a52021-08-17 11:59:07 -04001895#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001896template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001897 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001898 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001899 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1900 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001901 >
1902basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1903 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001904
1905template<class _CharT,
1906 class _Traits,
1907 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001908 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001909 >
1910explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1911 -> basic_string<_CharT, _Traits, _Allocator>;
1912
1913template<class _CharT,
1914 class _Traits,
1915 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001916 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001917 class _Sz = typename allocator_traits<_Allocator>::size_type
1918 >
1919basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1920 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001921#endif
1922
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001924inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001926basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927{
Louis Dionne510450b2022-04-01 16:38:30 -04001928#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001929 if (!__libcpp_is_constant_evaluated()) {
1930 __c_node* __c = __get_db()->__find_c_and_lock(this);
1931 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001933 const_pointer __new_last = __get_pointer() + __pos;
1934 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001936 --__p;
1937 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1938 if (__i->base() > __new_last)
1939 {
1940 (*__p)->__c_ = nullptr;
1941 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001942 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001943 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001945 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 }
1947 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001948#else
1949 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04001950#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951}
1952
1953template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001954_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier815ed732016-09-16 00:00:48 +00001955void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1956 size_type __sz,
1957 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001959 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001960 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001962 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001964 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 {
1966 __set_short_size(__sz);
1967 __p = __get_short_pointer();
1968 }
1969 else
1970 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001971 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1972 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001973 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001975 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976 __set_long_size(__sz);
1977 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001978 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 traits_type::assign(__p[__sz], value_type());
1980}
1981
1982template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001983_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001985basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001987 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001988 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001990 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001992 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993 {
1994 __set_short_size(__sz);
1995 __p = __get_short_pointer();
1996 }
1997 else
1998 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001999 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2000 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002001 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002003 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004 __set_long_size(__sz);
2005 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002006 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007 traits_type::assign(__p[__sz], value_type());
2008}
2009
2010template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002011template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002012_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002013basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002014 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002016 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002018 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002019}
2020
2021template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002022_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002024 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025{
2026 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002027 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002029 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002030 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002031 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032}
2033
2034template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002035_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002036basic_string<_CharT, _Traits, _Allocator>::basic_string(
2037 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002038 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039{
2040 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002041 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002043 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002044 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002045 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046}
2047
Martijn Vels5e7c9752020-03-04 17:52:46 -05002048template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002049_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Vels5e7c9752020-03-04 17:52:46 -05002050void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2051 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002052 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002053 __r_.first() = __rep();
2054
Martijn Vels5e7c9752020-03-04 17:52:46 -05002055 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002056 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002057 __p = __get_short_pointer();
2058 __set_short_size(__sz);
2059 } else {
2060 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002061 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002062 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2063 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002064 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002065 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002066 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002067 __set_long_size(__sz);
2068 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002069 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002070}
2071
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002073_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074void
2075basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2076{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002077 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002078 __r_.first() = __rep();
2079
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002081 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002083 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084 {
2085 __set_short_size(__n);
2086 __p = __get_short_pointer();
2087 }
2088 else
2089 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002090 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2091 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002092 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002094 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095 __set_long_size(__n);
2096 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002097 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098 traits_type::assign(__p[__n], value_type());
2099}
2100
2101template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002102template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002103_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002104basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002105 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106{
2107 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002108 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109}
2110
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002112_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002113basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2114 size_type __pos, size_type __n,
2115 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002116 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117{
2118 size_type __str_sz = __str.size();
2119 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002120 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002121 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2122 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123}
2124
2125template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002126template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002127_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow78dbe462016-11-14 18:22:19 +00002128basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002129 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002130 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002131{
Marshall Clowe46031a2018-07-02 18:41:15 +00002132 __self_view __sv0 = __t;
2133 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002134 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002135 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002136}
2137
2138template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002139template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002140_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002141basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002142 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002143{
Marshall Clowe46031a2018-07-02 18:41:15 +00002144 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002145 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002146 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002147}
2148
2149template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002150template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002151_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002152basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002153 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002154{
Marshall Clowe46031a2018-07-02 18:41:15 +00002155 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002156 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002157 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002158}
2159
2160template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002162_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002163__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002165 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2166>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2168{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002169 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170#ifndef _LIBCPP_NO_EXCEPTIONS
2171 try
2172 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002173#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 for (; __first != __last; ++__first)
2175 push_back(*__first);
2176#ifndef _LIBCPP_NO_EXCEPTIONS
2177 }
2178 catch (...)
2179 {
2180 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002181 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182 throw;
2183 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002184#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185}
2186
2187template <class _CharT, class _Traits, class _Allocator>
2188template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002189_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002190__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002192 __is_cpp17_forward_iterator<_ForwardIterator>::value
2193>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2195{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002196 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002197 __r_.first() = __rep();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002198 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002200 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002202 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203 {
2204 __set_short_size(__sz);
2205 __p = __get_short_pointer();
2206 }
2207 else
2208 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002209 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2210 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002211 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002213 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 __set_long_size(__sz);
2215 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002216
2217#ifndef _LIBCPP_NO_EXCEPTIONS
2218 try
2219 {
2220#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002221 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 traits_type::assign(*__p, *__first);
2223 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002224#ifndef _LIBCPP_NO_EXCEPTIONS
2225 }
2226 catch (...)
2227 {
2228 if (__is_long())
2229 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2230 throw;
2231 }
2232#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233}
2234
2235template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002236_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2238{
Nikolas Klauser98833542022-05-07 22:20:23 +02002239 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002241 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242}
2243
2244template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002245_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246void
2247basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2248 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002249 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 +00002250{
2251 size_type __ms = max_size();
2252 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002253 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 pointer __old_p = __get_pointer();
2255 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002256 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002258 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2259 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002260 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002261 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002263 traits_type::copy(std::__to_address(__p),
2264 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002266 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2268 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002269 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2270 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002271 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002272 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002274 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2276 __set_long_size(__old_sz);
2277 traits_type::assign(__p[__old_sz], value_type());
2278}
2279
2280template <class _CharT, class _Traits, class _Allocator>
2281void
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002282_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2284 size_type __n_copy, size_type __n_del, size_type __n_add)
2285{
2286 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002287 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002288 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289 pointer __old_p = __get_pointer();
2290 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002291 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002293 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2294 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002295 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002296 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002298 traits_type::copy(std::__to_address(__p),
2299 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2301 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002302 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2303 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002304 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002305 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2306 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002308 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309}
2310
2311// assign
2312
2313template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002314template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002315_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsb6a08b62020-04-10 18:36:31 -04002316basic_string<_CharT, _Traits, _Allocator>&
2317basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002318 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002319 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002320 if (__n < __cap) {
2321 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2322 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002323 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002324 traits_type::assign(__p[__n], value_type());
2325 __invalidate_iterators_past(__n);
2326 } else {
2327 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2328 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2329 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002330 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002331}
2332
2333template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002334_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002335basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002336basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2337 const value_type* __s, size_type __n) {
2338 size_type __cap = capacity();
2339 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002340 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002341 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002342 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002343 } else {
2344 size_type __sz = size();
2345 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002346 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002347 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002348}
2349
2350template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002351_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002352basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002353basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002355 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002356 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002357 ? __assign_short(__s, __n)
2358 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359}
2360
2361template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002362_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363basic_string<_CharT, _Traits, _Allocator>&
2364basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2365{
2366 size_type __cap = capacity();
2367 if (__cap < __n)
2368 {
2369 size_type __sz = size();
2370 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2371 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002372 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002374 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375}
2376
2377template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002378_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379basic_string<_CharT, _Traits, _Allocator>&
2380basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2381{
2382 pointer __p;
2383 if (__is_long())
2384 {
2385 __p = __get_long_pointer();
2386 __set_long_size(1);
2387 }
2388 else
2389 {
2390 __p = __get_short_pointer();
2391 __set_short_size(1);
2392 }
2393 traits_type::assign(*__p, __c);
2394 traits_type::assign(*++__p, value_type());
2395 __invalidate_iterators_past(1);
2396 return *this;
2397}
2398
2399template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002400_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002401basic_string<_CharT, _Traits, _Allocator>&
2402basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2403{
Martijn Vels596e3de2020-02-26 15:55:49 -05002404 if (this != &__str) {
2405 __copy_assign_alloc(__str);
2406 if (!__is_long()) {
2407 if (!__str.__is_long()) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002408 __r_.first() = __str.__r_.first();
Martijn Vels596e3de2020-02-26 15:55:49 -05002409 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002410 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002411 }
2412 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002413 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002414 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002415 }
2416 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002417}
2418
Eric Fiselierfc92be82017-04-19 00:28:44 +00002419#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002420
2421template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002422inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002423void
2424basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002425 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002426{
2427 if (__alloc() != __str.__alloc())
2428 assign(__str);
2429 else
2430 __move_assign(__str, true_type());
2431}
2432
2433template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002434inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002435void
2436basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002437#if _LIBCPP_STD_VER > 14
2438 _NOEXCEPT
2439#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002440 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002441#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002442{
marshall2ed41622019-11-27 07:13:00 -08002443 if (__is_long()) {
2444 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2445 __get_long_cap());
2446#if _LIBCPP_STD_VER <= 14
2447 if (!is_nothrow_move_assignable<allocator_type>::value) {
2448 __set_short_size(0);
2449 traits_type::assign(__get_short_pointer()[0], value_type());
2450 }
2451#endif
2452 }
2453 __move_assign_alloc(__str);
2454 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002455 if (__libcpp_is_constant_evaluated()) {
2456 __str.__default_init();
2457 } else {
2458 __str.__set_short_size(0);
2459 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2460 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002461}
2462
2463template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002464inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002465basic_string<_CharT, _Traits, _Allocator>&
2466basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002467 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002468{
2469 __move_assign(__str, integral_constant<bool,
2470 __alloc_traits::propagate_on_container_move_assignment::value>());
2471 return *this;
2472}
2473
2474#endif
2475
2476template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002478_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002479__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002481 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002483>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2485{
Marshall Clow958362f2016-09-05 01:54:30 +00002486 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002487 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002488 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489}
2490
2491template <class _CharT, class _Traits, class _Allocator>
2492template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002493_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002494__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002496 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002498>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2500{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002501 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002502 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002503 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002504
2505 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2506 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002508 if (__cap < __n)
2509 {
2510 size_type __sz = size();
2511 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2512 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002513 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002514 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002515 traits_type::assign(*__p, *__first);
2516 traits_type::assign(*__p, value_type());
2517 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002518 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519 }
2520 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002521 {
2522 const basic_string __temp(__first, __last, __alloc());
2523 assign(__temp.data(), __temp.size());
2524 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525 return *this;
2526}
2527
2528template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002529_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530basic_string<_CharT, _Traits, _Allocator>&
2531basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2532{
2533 size_type __sz = __str.size();
2534 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002535 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002536 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537}
2538
2539template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002540template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002541_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002542__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002543<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002544 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2545 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002546 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002547>
Marshall Clow82513342016-09-24 22:45:42 +00002548basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002549{
Marshall Clow82513342016-09-24 22:45:42 +00002550 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002551 size_type __sz = __sv.size();
2552 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002553 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002554 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002555}
2556
2557
2558template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002559_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002561basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2562 return __assign_external(__s, traits_type::length(__s));
2563}
2564
2565template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002566_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002567basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002568basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002570 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002571 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002572 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002573 ? __assign_short(__s, traits_type::length(__s))
2574 : __assign_external(__s, traits_type::length(__s)))
2575 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577// append
2578
2579template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002580_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002582basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002584 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585 size_type __cap = capacity();
2586 size_type __sz = size();
2587 if (__cap - __sz >= __n)
2588 {
2589 if (__n)
2590 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002591 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592 traits_type::copy(__p + __sz, __s, __n);
2593 __sz += __n;
2594 __set_size(__sz);
2595 traits_type::assign(__p[__sz], value_type());
2596 }
2597 }
2598 else
2599 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2600 return *this;
2601}
2602
2603template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002604_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605basic_string<_CharT, _Traits, _Allocator>&
2606basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2607{
2608 if (__n)
2609 {
2610 size_type __cap = capacity();
2611 size_type __sz = size();
2612 if (__cap - __sz < __n)
2613 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2614 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002615 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616 __sz += __n;
2617 __set_size(__sz);
2618 traits_type::assign(__p[__sz], value_type());
2619 }
2620 return *this;
2621}
2622
2623template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002624_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002625basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2626{
2627 if (__n)
2628 {
2629 size_type __cap = capacity();
2630 size_type __sz = size();
2631 if (__cap - __sz < __n)
2632 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2633 pointer __p = __get_pointer();
2634 __sz += __n;
2635 __set_size(__sz);
2636 traits_type::assign(__p[__sz], value_type());
2637 }
2638}
2639
2640template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002641_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642void
2643basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2644{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002645 bool __is_short = !__is_long();
2646 size_type __cap;
2647 size_type __sz;
2648 if (__is_short)
2649 {
2650 __cap = __min_cap - 1;
2651 __sz = __get_short_size();
2652 }
2653 else
2654 {
2655 __cap = __get_long_cap() - 1;
2656 __sz = __get_long_size();
2657 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002659 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002661 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002662 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002663 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002664 if (__is_short)
2665 {
2666 __p = __get_short_pointer() + __sz;
2667 __set_short_size(__sz+1);
2668 }
2669 else
2670 {
2671 __p = __get_long_pointer() + __sz;
2672 __set_long_size(__sz+1);
2673 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674 traits_type::assign(*__p, __c);
2675 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676}
2677
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678template <class _CharT, class _Traits, class _Allocator>
2679template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002680_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002681__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002682<
2683 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2684 basic_string<_CharT, _Traits, _Allocator>&
2685>
2686basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002687 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688{
2689 size_type __sz = size();
2690 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002691 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 if (__n)
2693 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002694 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2695 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002696 {
2697 if (__cap - __sz < __n)
2698 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2699 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002700 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002701 traits_type::assign(*__p, *__first);
2702 traits_type::assign(*__p, value_type());
2703 __set_size(__sz + __n);
2704 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002705 else
2706 {
2707 const basic_string __temp(__first, __last, __alloc());
2708 append(__temp.data(), __temp.size());
2709 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 }
2711 return *this;
2712}
2713
2714template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002715inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716basic_string<_CharT, _Traits, _Allocator>&
2717basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2718{
2719 return append(__str.data(), __str.size());
2720}
2721
2722template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002723_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724basic_string<_CharT, _Traits, _Allocator>&
2725basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2726{
2727 size_type __sz = __str.size();
2728 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002729 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002730 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731}
2732
2733template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002734template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002735_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002736 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002737 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002738 __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 +00002739 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002740 >
Marshall Clow82513342016-09-24 22:45:42 +00002741basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002742{
Marshall Clow82513342016-09-24 22:45:42 +00002743 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002744 size_type __sz = __sv.size();
2745 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002746 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002747 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002748}
2749
2750template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002751_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002753basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002755 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 return append(__s, traits_type::length(__s));
2757}
2758
2759// insert
2760
2761template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002762_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002764basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002766 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767 size_type __sz = size();
2768 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002769 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002771 if (__libcpp_is_constant_evaluated()) {
2772 if (__cap - __sz >= __n)
2773 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2774 else
2775 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2776 return *this;
2777 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 if (__cap - __sz >= __n)
2779 {
2780 if (__n)
2781 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002782 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 size_type __n_move = __sz - __pos;
2784 if (__n_move != 0)
2785 {
2786 if (__p + __pos <= __s && __s < __p + __sz)
2787 __s += __n;
2788 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2789 }
2790 traits_type::move(__p + __pos, __s, __n);
2791 __sz += __n;
2792 __set_size(__sz);
2793 traits_type::assign(__p[__sz], value_type());
2794 }
2795 }
2796 else
2797 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2798 return *this;
2799}
2800
2801template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002802_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803basic_string<_CharT, _Traits, _Allocator>&
2804basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2805{
2806 size_type __sz = size();
2807 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002808 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809 if (__n)
2810 {
2811 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002812 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 if (__cap - __sz >= __n)
2814 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002815 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 size_type __n_move = __sz - __pos;
2817 if (__n_move != 0)
2818 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2819 }
2820 else
2821 {
2822 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002823 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 }
2825 traits_type::assign(__p + __pos, __n, __c);
2826 __sz += __n;
2827 __set_size(__sz);
2828 traits_type::assign(__p[__sz], value_type());
2829 }
2830 return *this;
2831}
2832
2833template <class _CharT, class _Traits, class _Allocator>
2834template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002835_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002836__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002838 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002839 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002840>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2842{
Nikolas Klausereed25832021-12-15 01:32:30 +01002843 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2844 "string::insert(iterator, range) called with an iterator not"
2845 " referring to this string");
2846 const basic_string __temp(__first, __last, __alloc());
2847 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848}
2849
2850template <class _CharT, class _Traits, class _Allocator>
2851template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002852_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002853__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002855 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002857>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2859{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002860 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2861 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002862
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002864 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2865 if (__n == 0)
2866 return begin() + __ip;
2867
2868 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002870 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002872 else
2873 {
2874 const basic_string __temp(__first, __last, __alloc());
2875 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2876 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877}
2878
2879template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002880inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881basic_string<_CharT, _Traits, _Allocator>&
2882basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2883{
2884 return insert(__pos1, __str.data(), __str.size());
2885}
2886
2887template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002888_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889basic_string<_CharT, _Traits, _Allocator>&
2890basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2891 size_type __pos2, size_type __n)
2892{
2893 size_type __str_sz = __str.size();
2894 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002895 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002896 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897}
2898
2899template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002900template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002901_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002902__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002903<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002904 __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 +00002905 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002906>
Marshall Clow82513342016-09-24 22:45:42 +00002907basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002908 size_type __pos2, size_type __n)
2909{
Marshall Clow82513342016-09-24 22:45:42 +00002910 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002911 size_type __str_sz = __sv.size();
2912 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002913 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002914 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002915}
2916
2917template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002918_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002920basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002922 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 return insert(__pos, __s, traits_type::length(__s));
2924}
2925
2926template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002927_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928typename basic_string<_CharT, _Traits, _Allocator>::iterator
2929basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2930{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002931 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2932 "string::insert(iterator, character) called with an iterator not"
2933 " referring to this string");
2934
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935 size_type __ip = static_cast<size_type>(__pos - begin());
2936 size_type __sz = size();
2937 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002938 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 if (__cap == __sz)
2940 {
2941 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002942 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 }
2944 else
2945 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002946 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 size_type __n_move = __sz - __ip;
2948 if (__n_move != 0)
2949 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2950 }
2951 traits_type::assign(__p[__ip], __c);
2952 traits_type::assign(__p[++__sz], value_type());
2953 __set_size(__sz);
2954 return begin() + static_cast<difference_type>(__ip);
2955}
2956
2957template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002958inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959typename basic_string<_CharT, _Traits, _Allocator>::iterator
2960basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2961{
Nikolas Klausereed25832021-12-15 01:32:30 +01002962 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2963 "string::insert(iterator, n, value) called with an iterator not"
2964 " referring to this string");
2965 difference_type __p = __pos - begin();
2966 insert(static_cast<size_type>(__p), __n, __c);
2967 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968}
2969
2970// replace
2971
2972template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002973_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002975basic_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 +00002976 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002978 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979 size_type __sz = size();
2980 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002981 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002982 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983 size_type __cap = capacity();
2984 if (__cap - __sz + __n1 >= __n2)
2985 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002986 if (__libcpp_is_constant_evaluated()) {
2987 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
2988 return *this;
2989 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002990 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991 if (__n1 != __n2)
2992 {
2993 size_type __n_move = __sz - __pos - __n1;
2994 if (__n_move != 0)
2995 {
2996 if (__n1 > __n2)
2997 {
2998 traits_type::move(__p + __pos, __s, __n2);
2999 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003000 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001 }
3002 if (__p + __pos < __s && __s < __p + __sz)
3003 {
3004 if (__p + __pos + __n1 <= __s)
3005 __s += __n2 - __n1;
3006 else // __p + __pos < __s < __p + __pos + __n1
3007 {
3008 traits_type::move(__p + __pos, __s, __n1);
3009 __pos += __n1;
3010 __s += __n2;
3011 __n2 -= __n1;
3012 __n1 = 0;
3013 }
3014 }
3015 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3016 }
3017 }
3018 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003019 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003020 }
3021 else
3022 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3023 return *this;
3024}
3025
3026template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003027_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028basic_string<_CharT, _Traits, _Allocator>&
3029basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3030{
3031 size_type __sz = size();
3032 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003033 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003034 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003036 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037 if (__cap - __sz + __n1 >= __n2)
3038 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003039 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 if (__n1 != __n2)
3041 {
3042 size_type __n_move = __sz - __pos - __n1;
3043 if (__n_move != 0)
3044 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3045 }
3046 }
3047 else
3048 {
3049 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003050 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051 }
3052 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003053 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054}
3055
3056template <class _CharT, class _Traits, class _Allocator>
3057template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003058_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003059__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003061 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003063>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003064basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065 _InputIterator __j1, _InputIterator __j2)
3066{
Marshall Clow958362f2016-09-05 01:54:30 +00003067 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003068 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069}
3070
3071template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003072inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073basic_string<_CharT, _Traits, _Allocator>&
3074basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3075{
3076 return replace(__pos1, __n1, __str.data(), __str.size());
3077}
3078
3079template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003080_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081basic_string<_CharT, _Traits, _Allocator>&
3082basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3083 size_type __pos2, size_type __n2)
3084{
3085 size_type __str_sz = __str.size();
3086 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003087 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003088 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089}
3090
3091template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003092template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003093_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003094__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003095<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003096 __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 +00003097 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003098>
Marshall Clow82513342016-09-24 22:45:42 +00003099basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003100 size_type __pos2, size_type __n2)
3101{
Marshall Clow82513342016-09-24 22:45:42 +00003102 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003103 size_type __str_sz = __sv.size();
3104 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003105 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003106 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003107}
3108
3109template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003110_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003111basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003112basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003114 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115 return replace(__pos, __n1, __s, traits_type::length(__s));
3116}
3117
3118template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003119inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003121basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122{
3123 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3124 __str.data(), __str.size());
3125}
3126
3127template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003128inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003130basic_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 +00003131{
3132 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3133}
3134
3135template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003136inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003138basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139{
3140 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3141}
3142
3143template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003144inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003146basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147{
3148 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3149}
3150
3151// erase
3152
Martijn Velsa81fc792020-02-26 13:25:43 -05003153// 'externally instantiated' erase() implementation, called when __n != npos.
3154// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003156_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003157void
3158basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3159 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161 if (__n)
3162 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003163 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003164 value_type* __p = std::__to_address(__get_pointer());
3165 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166 size_type __n_move = __sz - __pos - __n;
3167 if (__n_move != 0)
3168 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003169 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003171}
3172
3173template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003174_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003175basic_string<_CharT, _Traits, _Allocator>&
3176basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3177 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003178 if (__pos > size())
3179 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003180 if (__n == npos) {
3181 __erase_to_end(__pos);
3182 } else {
3183 __erase_external_with_move(__pos, __n);
3184 }
3185 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186}
3187
3188template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003189inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190typename basic_string<_CharT, _Traits, _Allocator>::iterator
3191basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3192{
Nikolas Klausereed25832021-12-15 01:32:30 +01003193 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3194 "string::erase(iterator) called with an iterator not"
3195 " referring to this string");
3196
3197 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3198 iterator __b = begin();
3199 size_type __r = static_cast<size_type>(__pos - __b);
3200 erase(__r, 1);
3201 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202}
3203
3204template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003205inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206typename basic_string<_CharT, _Traits, _Allocator>::iterator
3207basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3208{
Nikolas Klausereed25832021-12-15 01:32:30 +01003209 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3210 "string::erase(iterator, iterator) called with an iterator not"
3211 " referring to this string");
3212
3213 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3214 iterator __b = begin();
3215 size_type __r = static_cast<size_type>(__first - __b);
3216 erase(__r, static_cast<size_type>(__last - __first));
3217 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218}
3219
3220template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003221inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222void
3223basic_string<_CharT, _Traits, _Allocator>::pop_back()
3224{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003225 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003226 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227}
3228
3229template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003230inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003232basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003234 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235 if (__is_long())
3236 {
3237 traits_type::assign(*__get_long_pointer(), value_type());
3238 __set_long_size(0);
3239 }
3240 else
3241 {
3242 traits_type::assign(*__get_short_pointer(), value_type());
3243 __set_short_size(0);
3244 }
3245}
3246
3247template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003248inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249void
3250basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3251{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003252 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253}
3254
3255template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003256_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257void
3258basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3259{
3260 size_type __sz = size();
3261 if (__n > __sz)
3262 append(__n - __sz, __c);
3263 else
3264 __erase_to_end(__n);
3265}
3266
3267template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003268_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003269basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3270{
3271 size_type __sz = size();
3272 if (__n > __sz) {
3273 __append_default_init(__n - __sz);
3274 } else
3275 __erase_to_end(__n);
3276}
3277
3278template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003279inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003281basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003283 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003284 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3285 return __m - __alignment;
3286 } else {
3287 bool __uses_lsb = __endian_factor == 2;
3288 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3289 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290}
3291
3292template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003293_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294void
Marek Kurdejc9848142020-11-26 10:07:16 +01003295basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296{
Marek Kurdejc9848142020-11-26 10:07:16 +01003297 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003298 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003299
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003300 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3301 // and later (since P0966R1), however we provide consistent behavior in all Standard
3302 // modes because this function is instantiated in the shared library.
3303 if (__requested_capacity <= capacity())
3304 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003305
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003306 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003307 __target_capacity = __recommend(__target_capacity);
3308 if (__target_capacity == capacity()) return;
3309
3310 __shrink_or_extend(__target_capacity);
3311}
3312
3313template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003314inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003315void
3316basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3317{
3318 size_type __target_capacity = __recommend(size());
3319 if (__target_capacity == capacity()) return;
3320
3321 __shrink_or_extend(__target_capacity);
3322}
3323
3324template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003325inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003326void
3327basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3328{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329 size_type __cap = capacity();
3330 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003331
3332 pointer __new_data, __p;
3333 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003334 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003335 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003336 __was_long = true;
3337 __now_long = false;
3338 __new_data = __get_short_pointer();
3339 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003341 else
3342 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003343 if (__target_capacity > __cap) {
3344 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3345 __new_data = __allocation.ptr;
3346 __target_capacity = __allocation.count - 1;
3347 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003348 else
3349 {
3350 #ifndef _LIBCPP_NO_EXCEPTIONS
3351 try
3352 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003353 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003354 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3355 __new_data = __allocation.ptr;
3356 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003357 #ifndef _LIBCPP_NO_EXCEPTIONS
3358 }
3359 catch (...)
3360 {
3361 return;
3362 }
3363 #else // _LIBCPP_NO_EXCEPTIONS
3364 if (__new_data == nullptr)
3365 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003366 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003367 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003368 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003369 __now_long = true;
3370 __was_long = __is_long();
3371 __p = __get_pointer();
3372 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003373 traits_type::copy(std::__to_address(__new_data),
3374 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003375 if (__was_long)
3376 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3377 if (__now_long)
3378 {
3379 __set_long_cap(__target_capacity+1);
3380 __set_long_size(__sz);
3381 __set_long_pointer(__new_data);
3382 }
3383 else
3384 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003385 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386}
3387
3388template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003389inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003391basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003393 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394 return *(data() + __pos);
3395}
3396
3397template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003398inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003400basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003402 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003403 return *(__get_pointer() + __pos);
3404}
3405
3406template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003407_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3409basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3410{
3411 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003412 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413 return (*this)[__n];
3414}
3415
3416template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003417_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418typename basic_string<_CharT, _Traits, _Allocator>::reference
3419basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3420{
3421 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003422 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423 return (*this)[__n];
3424}
3425
3426template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003427inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003429basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003431 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432 return *__get_pointer();
3433}
3434
3435template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003436inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003438basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003439{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003440 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441 return *data();
3442}
3443
3444template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003445inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003447basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003449 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450 return *(__get_pointer() + size() - 1);
3451}
3452
3453template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003454inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003456basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003458 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459 return *(data() + size() - 1);
3460}
3461
3462template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003463_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003465basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466{
3467 size_type __sz = size();
3468 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003469 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003470 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003471 traits_type::copy(__s, data() + __pos, __rlen);
3472 return __rlen;
3473}
3474
3475template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003476inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477basic_string<_CharT, _Traits, _Allocator>
3478basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3479{
3480 return basic_string(*this, __pos, __n, __alloc());
3481}
3482
3483template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003484inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485void
3486basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003487#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003488 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003489#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003490 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003491 __is_nothrow_swappable<allocator_type>::value)
3492#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493{
Nikolas Klauser98833542022-05-07 22:20:23 +02003494 if (!__is_long())
3495 std::__debug_db_invalidate_all(this);
3496 if (!__str.__is_long())
3497 std::__debug_db_invalidate_all(&__str);
3498 std::__debug_db_swap(this, &__str);
3499
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003500 _LIBCPP_ASSERT(
3501 __alloc_traits::propagate_on_container_swap::value ||
3502 __alloc_traits::is_always_equal::value ||
3503 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003504 std::swap(__r_.first(), __str.__r_.first());
3505 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506}
3507
3508// find
3509
3510template <class _Traits>
3511struct _LIBCPP_HIDDEN __traits_eq
3512{
3513 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003514 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003515 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3516 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517};
3518
3519template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003520_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003522basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003523 size_type __pos,
3524 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003526 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003527 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003528 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529}
3530
3531template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003532inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003534basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3535 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003537 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003538 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539}
3540
3541template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003542template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003543_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003544__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003545<
3546 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3547 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003548>
Marshall Clowe46031a2018-07-02 18:41:15 +00003549basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003550 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003551{
Marshall Clowe46031a2018-07-02 18:41:15 +00003552 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003553 return __str_find<value_type, size_type, traits_type, npos>
3554 (data(), size(), __sv.data(), __pos, __sv.size());
3555}
3556
3557template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003558inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003559typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003560basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003561 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003563 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003564 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003565 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566}
3567
3568template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003569_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003571basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3572 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003574 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003575 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576}
3577
3578// rfind
3579
3580template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003581_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003583basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003584 size_type __pos,
3585 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003587 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003588 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003589 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590}
3591
3592template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003593inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003595basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3596 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003598 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003599 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600}
3601
3602template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003603template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003604_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003605__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003606<
3607 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3608 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003609>
Marshall Clowe46031a2018-07-02 18:41:15 +00003610basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003611 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003612{
Marshall Clowe46031a2018-07-02 18:41:15 +00003613 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003614 return __str_rfind<value_type, size_type, traits_type, npos>
3615 (data(), size(), __sv.data(), __pos, __sv.size());
3616}
3617
3618template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003619inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003620typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003621basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003622 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003624 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003625 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003626 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627}
3628
3629template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003630_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003632basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3633 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003635 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003636 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637}
3638
3639// find_first_of
3640
3641template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003642_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003644basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003645 size_type __pos,
3646 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003648 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003649 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003650 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651}
3652
3653template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003654inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003656basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3657 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003659 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003660 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661}
3662
3663template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003664template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003665_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003666__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003667<
3668 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3669 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003670>
Marshall Clowe46031a2018-07-02 18:41:15 +00003671basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003672 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003673{
Marshall Clowe46031a2018-07-02 18:41:15 +00003674 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003675 return __str_find_first_of<value_type, size_type, traits_type, npos>
3676 (data(), size(), __sv.data(), __pos, __sv.size());
3677}
3678
3679template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003680inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003681typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003682basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003683 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003685 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003686 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003687 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688}
3689
3690template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003691inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003693basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3694 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003695{
3696 return find(__c, __pos);
3697}
3698
3699// find_last_of
3700
3701template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003702inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003704basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003705 size_type __pos,
3706 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003708 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003709 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003710 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711}
3712
3713template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003714inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003716basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3717 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003719 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003720 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721}
3722
3723template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003724template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003725_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003726__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003727<
3728 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3729 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003730>
Marshall Clowe46031a2018-07-02 18:41:15 +00003731basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003732 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003733{
Marshall Clowe46031a2018-07-02 18:41:15 +00003734 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003735 return __str_find_last_of<value_type, size_type, traits_type, npos>
3736 (data(), size(), __sv.data(), __pos, __sv.size());
3737}
3738
3739template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003740inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003741typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003742basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003743 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003745 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003746 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003747 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748}
3749
3750template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003751inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003752typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003753basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3754 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755{
3756 return rfind(__c, __pos);
3757}
3758
3759// find_first_not_of
3760
3761template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003762_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003764basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003765 size_type __pos,
3766 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003768 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003769 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003770 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771}
3772
3773template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003774inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003776basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3777 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003779 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003780 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781}
3782
3783template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003784template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003785_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003786__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003787<
3788 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3789 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003790>
Marshall Clowe46031a2018-07-02 18:41:15 +00003791basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003792 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003793{
Marshall Clowe46031a2018-07-02 18:41:15 +00003794 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003795 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3796 (data(), size(), __sv.data(), __pos, __sv.size());
3797}
3798
3799template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003800inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003801typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003802basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003803 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003805 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003806 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003807 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003808}
3809
3810template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003811inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003813basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3814 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003816 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003817 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818}
3819
3820// find_last_not_of
3821
3822template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003823_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003825basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003826 size_type __pos,
3827 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003829 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003830 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003831 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832}
3833
3834template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003835inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003837basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3838 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003840 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003841 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842}
3843
3844template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003845template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003846_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003847__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003848<
3849 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3850 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003851>
Marshall Clowe46031a2018-07-02 18:41:15 +00003852basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003853 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003854{
Marshall Clowe46031a2018-07-02 18:41:15 +00003855 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003856 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3857 (data(), size(), __sv.data(), __pos, __sv.size());
3858}
3859
3860template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003861inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003862typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003863basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003864 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003866 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003867 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003868 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869}
3870
3871template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003872inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003874basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3875 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003877 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003878 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879}
3880
3881// compare
3882
3883template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003884template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003885_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003886__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003887<
3888 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3889 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003890>
zoecarver1997e0a2021-02-05 11:54:47 -08003891basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892{
Marshall Clowe46031a2018-07-02 18:41:15 +00003893 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003894 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003895 size_t __rhs_sz = __sv.size();
3896 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003897 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003898 if (__result != 0)
3899 return __result;
3900 if (__lhs_sz < __rhs_sz)
3901 return -1;
3902 if (__lhs_sz > __rhs_sz)
3903 return 1;
3904 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905}
3906
3907template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003908inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003910basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003912 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913}
3914
3915template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003916inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003918basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3919 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003920 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003921 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003922{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003923 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924 size_type __sz = size();
3925 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003926 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003927 size_type __rlen = std::min(__n1, __sz - __pos1);
3928 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929 if (__r == 0)
3930 {
3931 if (__rlen < __n2)
3932 __r = -1;
3933 else if (__rlen > __n2)
3934 __r = 1;
3935 }
3936 return __r;
3937}
3938
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003939template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003940template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003941_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003942__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003943<
3944 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3945 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003946>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003947basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3948 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003949 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003950{
Marshall Clowe46031a2018-07-02 18:41:15 +00003951 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003952 return compare(__pos1, __n1, __sv.data(), __sv.size());
3953}
3954
3955template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003956inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003957int
3958basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3959 size_type __n1,
3960 const basic_string& __str) const
3961{
3962 return compare(__pos1, __n1, __str.data(), __str.size());
3963}
3964
3965template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003966template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003967_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003968__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003969<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003970 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3971 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003972 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003973>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003974basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3975 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003976 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003977 size_type __pos2,
3978 size_type __n2) const
3979{
Marshall Clow82513342016-09-24 22:45:42 +00003980 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003981 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3982}
3983
3984template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003985_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003986int
3987basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3988 size_type __n1,
3989 const basic_string& __str,
3990 size_type __pos2,
3991 size_type __n2) const
3992{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003993 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003994}
3995
3996template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003997_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003998int
3999basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4000{
4001 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4002 return compare(0, npos, __s, traits_type::length(__s));
4003}
4004
4005template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004006_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004007int
4008basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4009 size_type __n1,
4010 const value_type* __s) const
4011{
4012 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4013 return compare(__pos1, __n1, __s, traits_type::length(__s));
4014}
4015
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016// __invariants
4017
4018template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004019inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020bool
4021basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4022{
4023 if (size() > capacity())
4024 return false;
4025 if (capacity() < __min_cap - 1)
4026 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004027 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004029 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030 return false;
4031 return true;
4032}
4033
Vedant Kumar55e007e2018-03-08 21:15:26 +00004034// __clear_and_shrink
4035
4036template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004037inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne173f29e2019-05-29 16:01:36 +00004038void
Marshall Clowe60a7182018-05-29 17:04:37 +00004039basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004040{
4041 clear();
4042 if(__is_long())
4043 {
4044 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
Nikolas Klauser1821ec32022-10-01 15:37:24 +02004045 __default_init();
Vedant Kumar55e007e2018-03-08 21:15:26 +00004046 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004047}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004048
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049// operator==
4050
4051template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004052inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053bool
4054operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004055 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056{
Mark de Weverb4830e62022-07-19 07:56:23 +02004057#if _LIBCPP_STD_VER > 17
4058 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4059#else
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004060 size_t __lhs_sz = __lhs.size();
4061 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4062 __rhs.data(),
4063 __lhs_sz) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004064#endif
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004065}
4066
4067template<class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004068inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004069bool
4070operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4071 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4072{
4073 size_t __lhs_sz = __lhs.size();
4074 if (__lhs_sz != __rhs.size())
4075 return false;
4076 const char* __lp = __lhs.data();
4077 const char* __rp = __rhs.data();
4078 if (__lhs.__is_long())
4079 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4080 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4081 if (*__lp != *__rp)
4082 return false;
4083 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084}
4085
Mark de Weverb4830e62022-07-19 07:56:23 +02004086#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004088inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004090operator==(const _CharT* __lhs,
4091 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004093 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4094 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4095 size_t __lhs_len = _Traits::length(__lhs);
4096 if (__lhs_len != __rhs.size()) return false;
4097 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098}
Mark de Weverb4830e62022-07-19 07:56:23 +02004099#endif // _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004102inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004104operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4105 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106{
Mark de Weverb4830e62022-07-19 07:56:23 +02004107#if _LIBCPP_STD_VER > 17
4108 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4109#else
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004110 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4111 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4112 size_t __rhs_len = _Traits::length(__rhs);
4113 if (__rhs_len != __lhs.size()) return false;
4114 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004115#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116}
4117
Mark de Weverb4830e62022-07-19 07:56:23 +02004118#if _LIBCPP_STD_VER > 17
4119
4120template <class _CharT, class _Traits, class _Allocator>
4121_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
4122 const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4123 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
4124 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4125}
4126
4127template <class _CharT, class _Traits, class _Allocator>
4128_LIBCPP_HIDE_FROM_ABI constexpr auto
4129operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
4130 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4131}
4132
4133#else // _LIBCPP_STD_VER > 17
4134
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004135template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004136inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137bool
4138operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004139 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140{
4141 return !(__lhs == __rhs);
4142}
4143
4144template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004145inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004146bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004147operator!=(const _CharT* __lhs,
4148 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149{
4150 return !(__lhs == __rhs);
4151}
4152
4153template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004154inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004156operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4157 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158{
4159 return !(__lhs == __rhs);
4160}
4161
4162// operator<
4163
4164template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004165inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166bool
4167operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004168 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004170 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171}
4172
4173template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004174inline _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{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004179 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180}
4181
4182template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004183inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004185operator< (const _CharT* __lhs,
4186 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187{
4188 return __rhs.compare(__lhs) > 0;
4189}
4190
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191// operator>
4192
4193template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004194inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195bool
4196operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004197 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198{
4199 return __rhs < __lhs;
4200}
4201
4202template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004203inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004205operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4206 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207{
4208 return __rhs < __lhs;
4209}
4210
4211template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004212inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004214operator> (const _CharT* __lhs,
4215 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004216{
4217 return __rhs < __lhs;
4218}
4219
4220// operator<=
4221
4222template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004223inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224bool
4225operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004226 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227{
4228 return !(__rhs < __lhs);
4229}
4230
4231template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004232inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004234operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4235 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004236{
4237 return !(__rhs < __lhs);
4238}
4239
4240template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004241inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004243operator<=(const _CharT* __lhs,
4244 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245{
4246 return !(__rhs < __lhs);
4247}
4248
4249// operator>=
4250
4251template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004252inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253bool
4254operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004255 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256{
4257 return !(__lhs < __rhs);
4258}
4259
4260template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004261inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004262bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004263operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4264 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265{
4266 return !(__lhs < __rhs);
4267}
4268
4269template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004270inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004272operator>=(const _CharT* __lhs,
4273 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274{
4275 return !(__lhs < __rhs);
4276}
Mark de Weverb4830e62022-07-19 07:56:23 +02004277#endif // _LIBCPP_STD_VER > 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278
4279// operator +
4280
4281template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004282_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283basic_string<_CharT, _Traits, _Allocator>
4284operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4285 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4286{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004287 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004288 auto __lhs_sz = __lhs.size();
4289 auto __rhs_sz = __rhs.size();
4290 _String __r(__uninitialized_size_tag(),
4291 __lhs_sz + __rhs_sz,
4292 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4293 auto __ptr = std::__to_address(__r.__get_pointer());
4294 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4295 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4296 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297 return __r;
4298}
4299
4300template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004301_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302basic_string<_CharT, _Traits, _Allocator>
4303operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4304{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004305 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004306 auto __lhs_sz = _Traits::length(__lhs);
4307 auto __rhs_sz = __rhs.size();
4308 _String __r(__uninitialized_size_tag(),
4309 __lhs_sz + __rhs_sz,
4310 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4311 auto __ptr = std::__to_address(__r.__get_pointer());
4312 _Traits::copy(__ptr, __lhs, __lhs_sz);
4313 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4314 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315 return __r;
4316}
4317
4318template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004319_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004320basic_string<_CharT, _Traits, _Allocator>
4321operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4322{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004323 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004324 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004325 _String __r(__uninitialized_size_tag(),
4326 __rhs_sz + 1,
4327 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4328 auto __ptr = std::__to_address(__r.__get_pointer());
4329 _Traits::assign(__ptr, 1, __lhs);
4330 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4331 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004332 return __r;
4333}
4334
4335template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004336inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004337basic_string<_CharT, _Traits, _Allocator>
4338operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4339{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004340 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004341 typename _String::size_type __lhs_sz = __lhs.size();
4342 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004343 _String __r(__uninitialized_size_tag(),
4344 __lhs_sz + __rhs_sz,
4345 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4346 auto __ptr = std::__to_address(__r.__get_pointer());
4347 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4348 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4349 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350 return __r;
4351}
4352
4353template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004354_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355basic_string<_CharT, _Traits, _Allocator>
4356operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4357{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004358 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004359 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004360 _String __r(__uninitialized_size_tag(),
4361 __lhs_sz + 1,
4362 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4363 auto __ptr = std::__to_address(__r.__get_pointer());
4364 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4365 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4366 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367 return __r;
4368}
4369
Eric Fiselierfc92be82017-04-19 00:28:44 +00004370#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371
4372template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004373inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004374basic_string<_CharT, _Traits, _Allocator>
4375operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4376{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004377 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378}
4379
4380template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004381inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382basic_string<_CharT, _Traits, _Allocator>
4383operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4384{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004385 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386}
4387
4388template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004389inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004390basic_string<_CharT, _Traits, _Allocator>
4391operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4392{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004393 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394}
4395
4396template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004397inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004398basic_string<_CharT, _Traits, _Allocator>
4399operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4400{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004401 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004402}
4403
4404template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004405inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004406basic_string<_CharT, _Traits, _Allocator>
4407operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4408{
4409 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004410 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004411}
4412
4413template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004414inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415basic_string<_CharT, _Traits, _Allocator>
4416operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4417{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004418 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004419}
4420
4421template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004422inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423basic_string<_CharT, _Traits, _Allocator>
4424operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4425{
4426 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004427 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428}
4429
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004430#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004431
4432// swap
4433
4434template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004435inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004437swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004438 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4439 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440{
4441 __lhs.swap(__rhs);
4442}
4443
Bruce Mitchener170d8972020-11-24 12:53:53 -05004444_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4445_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4446_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4447_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4448_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004449
Bruce Mitchener170d8972020-11-24 12:53:53 -05004450_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4451_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4452_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004453
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004454_LIBCPP_FUNC_VIS string to_string(int __val);
4455_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4456_LIBCPP_FUNC_VIS string to_string(long __val);
4457_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4458_LIBCPP_FUNC_VIS string to_string(long long __val);
4459_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4460_LIBCPP_FUNC_VIS string to_string(float __val);
4461_LIBCPP_FUNC_VIS string to_string(double __val);
4462_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004463
Louis Dionne89258142021-08-23 15:32:36 -04004464#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004465_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4466_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4467_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4468_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4469_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004470
Bruce Mitchener170d8972020-11-24 12:53:53 -05004471_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4472_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4473_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004474
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004475_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4476_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4477_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4478_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4479_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4480_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4481_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4482_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4483_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004484#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004485
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004487_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004488const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4489 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490
Marshall Clow851b9ec2019-05-20 21:56:51 +00004491template <class _CharT, class _Allocator>
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004492struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004493{
4494 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004495 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4496 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497};
4498
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004499template <class _Allocator>
4500struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
4501
4502#ifndef _LIBCPP_HAS_NO_CHAR8_T
4503template <class _Allocator>
4504struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
4505#endif
4506
4507template <class _Allocator>
4508struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
4509
4510template <class _Allocator>
4511struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
4512
4513#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4514template <class _Allocator>
4515struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
4516#endif
4517
Howard Hinnantdc095972011-07-18 15:51:59 +00004518template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004519_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004520operator<<(basic_ostream<_CharT, _Traits>& __os,
4521 const basic_string<_CharT, _Traits, _Allocator>& __str);
4522
4523template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004524_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004525operator>>(basic_istream<_CharT, _Traits>& __is,
4526 basic_string<_CharT, _Traits, _Allocator>& __str);
4527
4528template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004529_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004530getline(basic_istream<_CharT, _Traits>& __is,
4531 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4532
4533template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004534inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004535basic_istream<_CharT, _Traits>&
4536getline(basic_istream<_CharT, _Traits>& __is,
4537 basic_string<_CharT, _Traits, _Allocator>& __str);
4538
Howard Hinnantdc095972011-07-18 15:51:59 +00004539template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004540inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004541basic_istream<_CharT, _Traits>&
4542getline(basic_istream<_CharT, _Traits>&& __is,
4543 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4544
4545template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004546inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004547basic_istream<_CharT, _Traits>&
4548getline(basic_istream<_CharT, _Traits>&& __is,
4549 basic_string<_CharT, _Traits, _Allocator>& __str);
4550
Marshall Clow29b53f22018-12-14 18:49:35 +00004551#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004552template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004553inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004554 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4555 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4556 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004557 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004558 return __old_size - __str.size();
4559}
Marshall Clow29b53f22018-12-14 18:49:35 +00004560
Marek Kurdeja98b1412020-05-02 13:58:03 +02004561template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004562inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004563 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4564 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4565 _Predicate __pred) {
4566 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004567 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004568 __str.end());
4569 return __old_size - __str.size();
4570}
Marshall Clow29b53f22018-12-14 18:49:35 +00004571#endif
4572
Louis Dionne510450b2022-04-01 16:38:30 -04004573#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004574
4575template<class _CharT, class _Traits, class _Allocator>
4576bool
4577basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4578{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004579 return data() <= std::__to_address(__i->base()) &&
4580 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004581}
4582
4583template<class _CharT, class _Traits, class _Allocator>
4584bool
4585basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4586{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004587 return data() < std::__to_address(__i->base()) &&
4588 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004589}
4590
4591template<class _CharT, class _Traits, class _Allocator>
4592bool
4593basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4594{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004595 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004596 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004597}
4598
4599template<class _CharT, class _Traits, class _Allocator>
4600bool
4601basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4602{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004603 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004604 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004605}
4606
Louis Dionne510450b2022-04-01 16:38:30 -04004607#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004608
Louis Dionne173f29e2019-05-29 16:01:36 +00004609#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004610// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004611inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004612{
4613 inline namespace string_literals
4614 {
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004615 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004616 basic_string<char> operator "" s( const char *__str, size_t __len )
4617 {
4618 return basic_string<char> (__str, __len);
4619 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004620
Louis Dionne89258142021-08-23 15:32:36 -04004621#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004622 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004623 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4624 {
4625 return basic_string<wchar_t> (__str, __len);
4626 }
Louis Dionne89258142021-08-23 15:32:36 -04004627#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004628
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004629#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004630 inline _LIBCPP_HIDE_FROM_ABI constexpr
Nikolas Klauser5da956d2022-09-02 16:20:28 +02004631 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len)
Marshall Clow8732fed2018-12-11 04:35:44 +00004632 {
4633 return basic_string<char8_t> (__str, __len);
4634 }
4635#endif
4636
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004637 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004638 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4639 {
4640 return basic_string<char16_t> (__str, __len);
4641 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004642
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004643 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004644 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4645 {
4646 return basic_string<char32_t> (__str, __len);
4647 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004648 } // namespace string_literals
4649} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004650
4651#if _LIBCPP_STD_VER > 17
4652template <>
4653inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4654#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4655template <>
4656inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4657#endif
4658#endif
4659
Marshall Clowcba751f2013-07-23 17:05:24 +00004660#endif
4661
Howard Hinnantc51e1022010-05-11 19:42:16 +00004662_LIBCPP_END_NAMESPACE_STD
4663
Eric Fiselierf4433a32017-05-31 22:07:49 +00004664_LIBCPP_POP_MACROS
4665
Mark de Weveree5fe272022-09-02 17:53:28 +02004666#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
4667# include <algorithm>
4668# include <functional>
4669# include <iterator>
4670# include <new>
4671# include <typeinfo>
4672# include <utility>
4673# include <vector>
4674#endif
4675
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004676#endif // _LIBCPP_STRING