blob: 44c2045e37e5f13d16bf763f5db55e7c30923fa7 [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 Klauser80840272022-02-04 13:04:33 +0100542#include <__ios/fpos.h>
Nikolas Klauserfb1b0672022-06-10 19:53:10 +0200543#include <__iterator/distance.h>
544#include <__iterator/iterator_traits.h>
545#include <__iterator/reverse_iterator.h>
Louis Dionne77249522021-06-11 09:55:11 -0400546#include <__iterator/wrap_iter.h>
Nikolas Klauserc513eba2022-04-09 09:41:19 +0200547#include <__memory/allocate_at_least.h>
Nikolas Klauser0b5df932022-09-05 00:01:15 +0200548#include <__memory/allocator.h>
549#include <__memory/allocator_traits.h>
550#include <__memory/compressed_pair.h>
551#include <__memory/construct_at.h>
552#include <__memory/pointer_traits.h>
Nikolas Klauser35080b42022-07-26 16:13:56 +0200553#include <__memory/swap_allocator.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200554#include <__string/char_traits.h>
555#include <__string/extern_template_lists.h>
Nikolas Klauser0b5df932022-09-05 00:01:15 +0200556#include <__type_traits/is_allocator.h>
557#include <__type_traits/noexcept_move_assign_container.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100558#include <__utility/auto_cast.h>
559#include <__utility/move.h>
560#include <__utility/swap.h>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200561#include <__utility/unreachable.h>
562#include <climits>
Louis Dionne44962c32022-06-13 11:21:16 -0400563#include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400564#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400565#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400566#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400567#include <iosfwd>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200568#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400570#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000572#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000573
Louis Dionne89258142021-08-23 15:32:36 -0400574#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100575# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400576#endif
577
Nikolas Klausera0e0edb2022-06-16 22:43:46 +0200578// standard-mandated includes
579
580// [iterator.range]
581#include <__iterator/access.h>
582#include <__iterator/data.h>
583#include <__iterator/empty.h>
584#include <__iterator/reverse_access.h>
585#include <__iterator/size.h>
586
587// [string.syn]
588#include <compare>
589#include <initializer_list>
590
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000591#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500592# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000593#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594
Eric Fiselierf4433a32017-05-31 22:07:49 +0000595_LIBCPP_PUSH_MACROS
596#include <__undef_macros>
597
598
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599_LIBCPP_BEGIN_NAMESPACE_STD
600
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601// basic_string
602
603template<class _CharT, class _Traits, class _Allocator>
604basic_string<_CharT, _Traits, _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200605_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant944510a2011-06-14 19:58:17 +0000606operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
607 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608
609template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200610_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000612operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613
614template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200615_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000617operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618
619template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200620inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000622operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623
624template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200625_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000626basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000627operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000628
Louis Dionnedc496ec2021-06-08 17:25:08 -0400629extern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Shoaib Meenaiea363712017-07-29 02:54:41 +0000630
Marshall Clow039b2f02016-01-13 21:54:34 +0000631template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400632struct __string_is_trivial_iterator : public false_type {};
633
634template <class _Tp>
635struct __string_is_trivial_iterator<_Tp*>
636 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000637
Louis Dionne173f29e2019-05-29 16:01:36 +0000638template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400639struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
640 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000641
Marshall Clow82513342016-09-24 22:45:42 +0000642template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500643struct __can_be_converted_to_string_view : public _BoolConstant<
644 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
645 !is_convertible<const _Tp&, const _CharT*>::value
646 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000647
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400648#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800649typedef basic_string<char8_t> u8string;
650#endif
Richard Smith256954d2020-11-11 17:12:18 -0800651typedef basic_string<char16_t> u16string;
652typedef basic_string<char32_t> u32string;
Richard Smith256954d2020-11-11 17:12:18 -0800653
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200654struct __uninitialized_size_tag {};
655
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000656template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800657class
658 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400659#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800660 _LIBCPP_PREFERRED_NAME(u8string)
661#endif
Richard Smith256954d2020-11-11 17:12:18 -0800662 _LIBCPP_PREFERRED_NAME(u16string)
663 _LIBCPP_PREFERRED_NAME(u32string)
Richard Smith256954d2020-11-11 17:12:18 -0800664 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665{
666public:
667 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000668 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000670 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000672 typedef allocator_traits<allocator_type> __alloc_traits;
673 typedef typename __alloc_traits::size_type size_type;
674 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000675 typedef value_type& reference;
676 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000677 typedef typename __alloc_traits::pointer pointer;
678 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679
Marshall Clow79f33542018-03-21 00:36:05 +0000680 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
681 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
682 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
683 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000684 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000685 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000686 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000687
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 typedef __wrap_iter<pointer> iterator;
689 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200690 typedef std::reverse_iterator<iterator> reverse_iterator;
691 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692
693private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200694 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000695
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000696#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000697
698 struct __long
699 {
700 pointer __data_;
701 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200702 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
703 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000704 };
705
Howard Hinnant68bf1812013-04-30 21:44:48 +0000706 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
707 (sizeof(__long) - 1)/sizeof(value_type) : 2};
708
709 struct __short
710 {
711 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200712 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200713 unsigned char __size_ : 7;
714 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000715 };
716
Nikolas Klauser62060be2022-04-20 10:52:04 +0200717// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200718// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200719//
720// If the LSB is used to store the short-flag in the short string representation,
721// we have to multiply the size by two when it is stored and divide it by two when
722// it is loaded to make sure that we always store an even number. In the long string
723// representation, we can ignore this because we can assume that we always allocate
724// an even amount of value_types.
725//
726// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
727// This does not impact the short string representation, since we never need the MSB
728// for representing the size of a short string anyway.
729
730#ifdef _LIBCPP_BIG_ENDIAN
731 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000732#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200733 static const size_type __endian_factor = 1;
734#endif
735
736#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
737
738#ifdef _LIBCPP_BIG_ENDIAN
739 static const size_type __endian_factor = 1;
740#else
741 static const size_type __endian_factor = 2;
742#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000743
Xing Xue38b9fc42022-06-24 17:25:15 -0400744 // Attribute 'packed' is used to keep the layout compatible with the
745 // previous definition that did not use bit fields. This is because on
746 // some platforms bit fields have a default size rather than the actual
747 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 struct __long
749 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400750 struct _LIBCPP_PACKED {
751 size_type __is_long_ : 1;
752 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
753 };
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754 size_type __size_;
755 pointer __data_;
756 };
757
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
759 (sizeof(__long) - 1)/sizeof(value_type) : 2};
760
761 struct __short
762 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400763 struct _LIBCPP_PACKED {
764 unsigned char __is_long_ : 1;
765 unsigned char __size_ : 7;
766 };
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200767 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768 value_type __data_[__min_cap];
769 };
770
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400771#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000772
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200773 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
774
Howard Hinnant8ea98242013-08-23 17:37:05 +0000775 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776
Howard Hinnant8ea98242013-08-23 17:37:05 +0000777 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778
779 struct __raw
780 {
781 size_type __words[__n_words];
782 };
783
784 struct __rep
785 {
786 union
787 {
788 __long __l;
789 __short __s;
790 __raw __r;
791 };
792 };
793
794 __compressed_pair<__rep, allocator_type> __r_;
795
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200796 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
797 // don't initialize the characters. The contents of the string, including the null terminator, must be
798 // initialized separately.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200799 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200800 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200801 : __r_(__default_init_tag(), __a) {
802 if (__size > max_size())
803 __throw_length_error();
804 if (__fits_in_sso(__size)) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +0200805 __r_.first() = __rep();
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200806 __set_short_size(__size);
807 } else {
808 auto __capacity = __recommend(__size) + 1;
809 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200810 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200811 __set_long_cap(__capacity);
812 __set_long_pointer(__allocation);
813 __set_long_size(__size);
814 }
815 std::__debug_db_insert_c(this);
816 }
817
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200819 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820 static const size_type npos = -1;
821
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200822 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
Howard Hinnant3e276872011-06-03 18:40:47 +0000823 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000824
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200825 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000826#if _LIBCPP_STD_VER <= 14
827 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
828#else
829 _NOEXCEPT;
830#endif
831
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200832 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
833 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000834
Eric Fiselierfc92be82017-04-19 00:28:44 +0000835#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200836 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +0000837 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000838#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000839 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000840#else
841 _NOEXCEPT;
842#endif
843
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200844 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400846#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000847
Louis Dionne9ce598d2021-09-08 09:14:43 -0400848 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200849 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500850 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000851 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
852 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200853 std::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000854 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000855
Louis Dionne9ce598d2021-09-08 09:14:43 -0400856 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200857 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000858 basic_string(const _CharT* __s, const _Allocator& __a);
859
Marek Kurdej90d79712021-07-27 16:16:21 +0200860#if _LIBCPP_STD_VER > 20
861 basic_string(nullptr_t) = delete;
862#endif
863
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200864 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000865 basic_string(const _CharT* __s, size_type __n);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200866 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000867 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200868 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000869 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000870
Louis Dionne9ce598d2021-09-08 09:14:43 -0400871 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200872 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000873 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
874
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200875 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow83445802016-04-07 18:13:41 +0000876 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000877 const _Allocator& __a = _Allocator());
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200878 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow83445802016-04-07 18:13:41 +0000879 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000880 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000881
Louis Dionne9ce598d2021-09-08 09:14:43 -0400882 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200883 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000884 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500885 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000886
Louis Dionne9ce598d2021-09-08 09:14:43 -0400887 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500888 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200889 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000890 explicit basic_string(const _Tp& __t);
891
Louis Dionne9ce598d2021-09-08 09:14:43 -0400892 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200893 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000894 explicit basic_string(const _Tp& __t, const allocator_type& __a);
895
Louis Dionne9ce598d2021-09-08 09:14:43 -0400896 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200897 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400899 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200900 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000902#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200903 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000904 basic_string(initializer_list<_CharT> __il);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200905 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000906 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400907#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200909 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200911 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000912 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
913
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200914 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000915
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200916 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
917 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200918 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200919 __self_view __sv = __t;
920 return assign(__sv);
921 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000922
Eric Fiselierfc92be82017-04-19 00:28:44 +0000923#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200924 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +0000925 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000926 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200927 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierfc92be82017-04-19 00:28:44 +0000928 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200930 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200931 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200932#if _LIBCPP_STD_VER > 20
933 basic_string& operator=(nullptr_t) = delete;
934#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200935 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200937 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000938 iterator begin() _NOEXCEPT
939 {return iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200940 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000941 const_iterator begin() const _NOEXCEPT
942 {return const_iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200943 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000944 iterator end() _NOEXCEPT
945 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200946 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000947 const_iterator end() const _NOEXCEPT
948 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -0400949
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200950 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000951 reverse_iterator rbegin() _NOEXCEPT
952 {return reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200953 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000954 const_reverse_iterator rbegin() const _NOEXCEPT
955 {return const_reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200956 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000957 reverse_iterator rend() _NOEXCEPT
958 {return reverse_iterator(begin());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200959 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000960 const_reverse_iterator rend() const _NOEXCEPT
961 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200963 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000964 const_iterator cbegin() const _NOEXCEPT
965 {return begin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200966 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000967 const_iterator cend() const _NOEXCEPT
968 {return end();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200969 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000970 const_reverse_iterator crbegin() const _NOEXCEPT
971 {return rbegin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200972 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000973 const_reverse_iterator crend() const _NOEXCEPT
974 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200976 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200978 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {return size();}
979 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT;
980 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200981 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
982 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200984 _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
985 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200987 _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100988
989#if _LIBCPP_STD_VER > 20
990 template <class _Op>
991 _LIBCPP_HIDE_FROM_ABI constexpr
992 void resize_and_overwrite(size_type __n, _Op __op) {
993 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200994 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100995 }
996#endif
997
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200998 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +0000999
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001000 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001001 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
1002 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001003
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001004 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowb7db4972017-11-15 20:02:27 +00001005 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001007 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001008 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001009 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001011 _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
1012 _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001014 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001015 return append(__str);
1016 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001017
1018 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001019 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001020 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001021 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001022 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1023 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001024 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001025 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001026 operator+=(const _Tp& __t) {
1027 __self_view __sv = __t; return append(__sv);
1028 }
1029
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001030 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001031 return append(__s);
1032 }
1033
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001034 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001035 push_back(__c);
1036 return *this;
1037 }
1038
Eric Fiselierfc92be82017-04-19 00:28:44 +00001039#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001040 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001041 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001042#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001044 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001046
1047 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001048 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001049 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001050 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1051 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001052 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001053 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001054 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001055 _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 +00001056
Marshall Clow62953962016-10-03 23:40:48 +00001057 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001058 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001059 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001060 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001061 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1062 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001063 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001064 >
Marshall Clow82513342016-09-24 22:45:42 +00001065 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001066 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n);
1067 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s);
1068 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001069
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001070 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier451d5582018-11-26 20:15:38 +00001071 void __append_default_init(size_type __n);
1072
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001074 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001075 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001077 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001079 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001080 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001081 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001082 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001083 append(__temp.data(), __temp.size());
1084 return *this;
1085 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001087 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001088 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001090 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001092 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001093 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001094 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001095
Eric Fiselierfc92be82017-04-19 00:28:44 +00001096#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001097 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001099#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001101 _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
1102 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
1103 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT;
1104 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT;
1105 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT;
1106 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107
Marshall Clowe46031a2018-07-02 18:41:15 +00001108 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001109 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001110 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001111 <
1112 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1113 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001115 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001116 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001117 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001118#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001119 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001120 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001121 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001122 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001123#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001124 _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 +00001125 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001126 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001127 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001128 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001129 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1130 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001131 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001132 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001133 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001134 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n);
1135 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s);
1136 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001138 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001139 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001141 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001143 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 assign(_InputIterator __first, _InputIterator __last);
1145 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001146 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001147 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001149 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001151 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001153#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001154 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001156#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001158 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001160
1161 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001162 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001163 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001164 <
1165 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1166 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001167 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001168 insert(size_type __pos1, const _Tp& __t)
1169 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1170
Marshall Clow82513342016-09-24 22:45:42 +00001171 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001172 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001173 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001174 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001175 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001176 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001177 >
Marshall Clow82513342016-09-24 22:45:42 +00001178 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001179 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001180 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001181 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1182 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s);
1183 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1184 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
1185 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1187 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001188 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001189 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001191 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001193 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1195 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001196 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001197 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001199 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001201 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001203#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001204 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1206 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001207#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001209 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1210 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211 iterator erase(const_iterator __pos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001212 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 iterator erase(const_iterator __first, const_iterator __last);
1214
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001215 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001217
1218 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001219 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001220 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001221 <
1222 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1223 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001224 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001225 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 +02001226 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001227 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 +00001228 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001229 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001230 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001231 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001232 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001233 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001234 >
Marshall Clow82513342016-09-24 22:45:42 +00001235 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001236 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001237 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001238 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1239 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1240 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001241 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001242
1243 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001244 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001245 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001246 <
1247 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1248 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001249 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001250 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1251
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001252 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001253 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001254 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001255 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001256 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001257 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001259 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001260 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001262 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001264 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001265 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001266#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001267 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001268 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001270#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001272 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
1273 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1275
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001276 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001277 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001278#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001279 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001280#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001281 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001282 __is_nothrow_swappable<allocator_type>::value);
1283#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001285 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001286 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001287 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001288 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001289#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001290 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001291 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001292#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001294 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001295 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001297 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001298 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001299
1300 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001301 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001302 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001303 <
1304 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1305 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001306 >
zoecarver1997e0a2021-02-05 11:54:47 -08001307 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001308 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001309 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001310 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001311 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001312 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001314 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001315 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001316
1317 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001318 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001319 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001320 <
1321 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1322 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001323 >
zoecarver1997e0a2021-02-05 11:54:47 -08001324 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001325 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001326 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001327 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001328 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001329 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001331 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001332 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001333
1334 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001335 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001336 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001337 <
1338 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1339 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001340 >
zoecarver1997e0a2021-02-05 11:54:47 -08001341 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001342 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001343 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001344 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001345 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001346 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001347 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001349 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001350 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001351
1352 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001353 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001354 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001355 <
1356 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1357 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001358 >
zoecarver1997e0a2021-02-05 11:54:47 -08001359 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001360 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001361 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001362 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001363 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001364 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001365 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001367 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001368 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001369
1370 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001371 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001372 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001373 <
1374 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1375 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001376 >
zoecarver1997e0a2021-02-05 11:54:47 -08001377 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001378 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001379 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 +02001380 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001381 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001382 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001383 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1384
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_last_not_of(const basic_string& __str, size_type __pos = npos) 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_last_not_of(const _Tp& __t, size_type __pos = npos) 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_last_not_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_last_not_of(const value_type* __s, size_type __pos = npos) 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_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1402
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001403 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001404 int compare(const basic_string& __str) 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 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001412 >
zoecarver1997e0a2021-02-05 11:54:47 -08001413 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001414
1415 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001416 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001417 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001418 <
1419 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1420 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001421 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001422 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1423
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001424 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001426 _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001427 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1428 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001429
Marshall Clow82513342016-09-24 22:45:42 +00001430 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001431 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001432 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001433 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001434 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001435 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001436 >
Marshall Clow82513342016-09-24 22:45:42 +00001437 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 +02001438 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const value_type* __s) const _NOEXCEPT;
1439 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1440 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001441 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442
Marshall Clow18c293b2017-12-04 20:11:38 +00001443#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001444 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001445 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001446 { return __self_view(data(), size()).starts_with(__sv); }
1447
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001448 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001449 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001450 { return !empty() && _Traits::eq(front(), __c); }
1451
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001452 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001453 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001454 { return starts_with(__self_view(__s)); }
1455
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001456 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001457 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001458 { return __self_view(data(), size()).ends_with( __sv); }
1459
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001460 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001461 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001462 { return !empty() && _Traits::eq(back(), __c); }
1463
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001464 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001465 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001466 { return ends_with(__self_view(__s)); }
1467#endif
1468
Wim Leflere023c3542021-01-19 14:33:30 -05001469#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001470 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001471 bool contains(__self_view __sv) const noexcept
1472 { return __self_view(data(), size()).contains(__sv); }
1473
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001474 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001475 bool contains(value_type __c) const noexcept
1476 { return __self_view(data(), size()).contains(__c); }
1477
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001478 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001479 bool contains(const value_type* __s) const
1480 { return __self_view(data(), size()).contains(__s); }
1481#endif
1482
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001483 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001484
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001485 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001486
Louis Dionne510450b2022-04-01 16:38:30 -04001487#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001488
1489 bool __dereferenceable(const const_iterator* __i) const;
1490 bool __decrementable(const const_iterator* __i) const;
1491 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1492 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1493
Louis Dionne510450b2022-04-01 16:38:30 -04001494#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001495
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001497 template<class _Alloc>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001498 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001499 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1500 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1501
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001502 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001503
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001504 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001505 bool __is_long() const _NOEXCEPT {
1506 if (__libcpp_is_constant_evaluated())
1507 return true;
1508 return __r_.first().__s.__is_long_;
1509 }
1510
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001511 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __begin_lifetime(pointer __begin, size_type __n) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001512#if _LIBCPP_STD_VER > 17
1513 if (__libcpp_is_constant_evaluated()) {
1514 for (size_type __i = 0; __i != __n; ++__i)
1515 std::construct_at(std::addressof(__begin[__i]));
1516 }
1517#else
1518 (void)__begin;
1519 (void)__n;
1520#endif // _LIBCPP_STD_VER > 17
1521 }
1522
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001523 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __default_init() {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001524 __r_.first() = __rep();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001525 if (__libcpp_is_constant_evaluated()) {
1526 size_type __sz = __recommend(0) + 1;
1527 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1528 __begin_lifetime(__ptr, __sz);
1529 __set_long_pointer(__ptr);
1530 __set_long_cap(__sz);
1531 __set_long_size(0);
1532 }
1533 }
1534
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001535 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __deallocate_constexpr() {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001536 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1537 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1538 }
1539
Nikolas Klauser93826d12022-01-04 17:24:03 +01001540 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1541 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1542 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1543 }
1544
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001545 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001546 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001547 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1548 size_type __sz = size();
1549 size_type __cap = capacity();
1550 value_type* __p;
1551 if (__cap - __sz >= __n)
1552 {
1553 __p = std::__to_address(__get_pointer());
1554 size_type __n_move = __sz - __ip;
1555 if (__n_move != 0)
1556 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1557 }
1558 else
1559 {
1560 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1561 __p = std::__to_address(__get_long_pointer());
1562 }
1563 __sz += __n;
1564 __set_size(__sz);
1565 traits_type::assign(__p[__sz], value_type());
1566 for (__p += __ip; __first != __last; ++__p, ++__first)
1567 traits_type::assign(*__p, *__first);
1568
1569 return begin() + __ip;
1570 }
1571
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001572 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001573 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001575 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001576 void __set_short_size(size_type __s) _NOEXCEPT {
1577 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1578 __r_.first().__s.__size_ = __s;
1579 __r_.first().__s.__is_long_ = false;
1580 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001581
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001582 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001583 size_type __get_short_size() const _NOEXCEPT {
1584 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1585 return __r_.first().__s.__size_;
1586 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001587
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001588 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001589 void __set_long_size(size_type __s) _NOEXCEPT
1590 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001591 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001592 size_type __get_long_size() const _NOEXCEPT
1593 {return __r_.first().__l.__size_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001594 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001595 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1597
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001598 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001599 void __set_long_cap(size_type __s) _NOEXCEPT {
1600 __r_.first().__l.__cap_ = __s / __endian_factor;
1601 __r_.first().__l.__is_long_ = true;
1602 }
1603
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001604 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001605 size_type __get_long_cap() const _NOEXCEPT {
1606 return __r_.first().__l.__cap_ * __endian_factor;
1607 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001609 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001610 void __set_long_pointer(pointer __p) _NOEXCEPT
1611 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001612 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001613 pointer __get_long_pointer() _NOEXCEPT
1614 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001615 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001616 const_pointer __get_long_pointer() const _NOEXCEPT
1617 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001618 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001619 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001620 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001621 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001622 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001623 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001624 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001625 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001627 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001628 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1630
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631 template <size_type __a> static
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001632 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea382952013-08-14 18:00:20 +00001633 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001634 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635 enum {__alignment = 16};
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001636 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001637 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001638 {
1639 if (__s < __min_cap) {
1640 if (__libcpp_is_constant_evaluated())
1641 return static_cast<size_type>(__min_cap);
1642 else
1643 return static_cast<size_type>(__min_cap) - 1;
1644 }
Marshall Clow80584522018-02-07 21:30:17 +00001645 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1646 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1647 if (__guess == __min_cap) ++__guess;
1648 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001649 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001651 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001652 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001653 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001654 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001655 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001657
Martijn Vels5e7c9752020-03-04 17:52:46 -05001658 // Slow path for the (inlined) copy constructor for 'long' strings.
1659 // Always externally instantiated and not inlined.
1660 // Requires that __s is zero terminated.
1661 // The main reason for this function to exist is because for unstable, we
1662 // want to allow inlining of the copy constructor. However, we don't want
1663 // to call the __init() functions as those are marked as inline which may
1664 // result in over-aggressive inlining by the compiler, where our aim is
1665 // to only inline the fast path code directly in the ctor.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001666 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001667
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668 template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001669 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001670 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001671 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001672 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1673 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001674 __init(_InputIterator __first, _InputIterator __last);
1675
1676 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001677 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001678 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001680 __is_cpp17_forward_iterator<_ForwardIterator>::value
1681 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 __init(_ForwardIterator __first, _ForwardIterator __last);
1683
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001684 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001686 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001687 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1689 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001690 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691
Martijn Vels596e3de2020-02-26 15:55:49 -05001692 // __assign_no_alias is invoked for assignment operations where we
1693 // have proof that the input does not alias the current instance.
1694 // For example, operator=(basic_string) performs a 'self' check.
1695 template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001696 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001697
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001698 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699 void __erase_to_end(size_type __pos);
1700
Martijn Velsa81fc792020-02-26 13:25:43 -05001701 // __erase_external_with_move is invoked for erase() invocations where
1702 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001703 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001704
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001705 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001706 void __copy_assign_alloc(const basic_string& __str)
1707 {__copy_assign_alloc(__str, integral_constant<bool,
1708 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1709
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001710 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001711 void __copy_assign_alloc(const basic_string& __str, true_type)
1712 {
Marshall Clowf258c202017-01-31 03:40:52 +00001713 if (__alloc() == __str.__alloc())
1714 __alloc() = __str.__alloc();
1715 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001716 {
Marshall Clowf258c202017-01-31 03:40:52 +00001717 if (!__str.__is_long())
1718 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001719 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001720 __alloc() = __str.__alloc();
1721 }
1722 else
1723 {
1724 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001725 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001726 __begin_lifetime(__allocation.ptr, __allocation.count);
Vedant Kumar55e007e2018-03-08 21:15:26 +00001727 __clear_and_shrink();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001728 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001729 __set_long_pointer(__allocation.ptr);
1730 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001731 __set_long_size(__str.size());
1732 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001733 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001734 }
1735
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001736 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001737 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001738 {}
1739
Eric Fiselierfc92be82017-04-19 00:28:44 +00001740#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001741 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001742 void __move_assign(basic_string& __str, false_type)
1743 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001744 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001745 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001746#if _LIBCPP_STD_VER > 14
1747 _NOEXCEPT;
1748#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001749 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001750#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001751#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001752
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001753 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001754 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001755 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001756 _NOEXCEPT_(
1757 !__alloc_traits::propagate_on_container_move_assignment::value ||
1758 is_nothrow_move_assignable<allocator_type>::value)
1759 {__move_assign_alloc(__str, integral_constant<bool,
1760 __alloc_traits::propagate_on_container_move_assignment::value>());}
1761
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001762 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc2734962011-09-02 20:42:31 +00001763 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001764 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1765 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001766 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001767 }
1768
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001769 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001770 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001771 _NOEXCEPT
1772 {}
1773
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001774 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s);
1775 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001776
1777 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1778 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1779 pointer __p = __is_long()
1780 ? (__set_long_size(__n), __get_long_pointer())
1781 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001782 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001783 traits_type::assign(__p[__n], value_type());
1784 return *this;
1785 }
1786
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001787 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001788 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001789 __set_size(__newsz);
1790 __invalidate_iterators_past(__newsz);
1791 traits_type::assign(__p[__newsz], value_type());
1792 return *this;
1793 }
1794
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001795 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001797 template<class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001798 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001799 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001800 // assume that the ranges overlap, because we can't check during constant evaluation
1801 if (__libcpp_is_constant_evaluated())
1802 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001803 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001804 return data() <= __p && __p <= data() + size();
1805 }
1806
Louis Dionned24191c2021-08-19 12:39:16 -04001807 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1808 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001809 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001810 }
1811
1812 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1813 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001814 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001815 }
1816
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001817 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const basic_string&);
1818 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const value_type*, const basic_string&);
1819 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(value_type, const basic_string&);
1820 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const value_type*);
1821 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822};
1823
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001824// These declarations must appear before any functions are implicitly used
1825// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001826#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001827#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001828 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001829# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001830 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001831# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001832#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001833 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001834# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001835 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001836# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001837#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001838#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001839
1840
Louis Dionned59f8a52021-08-17 11:59:07 -04001841#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001842template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001843 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001844 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001845 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1846 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001847 >
1848basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1849 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001850
1851template<class _CharT,
1852 class _Traits,
1853 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001854 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001855 >
1856explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1857 -> basic_string<_CharT, _Traits, _Allocator>;
1858
1859template<class _CharT,
1860 class _Traits,
1861 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001862 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001863 class _Sz = typename allocator_traits<_Allocator>::size_type
1864 >
1865basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1866 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001867#endif
1868
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001870inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001872basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873{
Louis Dionne510450b2022-04-01 16:38:30 -04001874#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001875 if (!__libcpp_is_constant_evaluated()) {
1876 __c_node* __c = __get_db()->__find_c_and_lock(this);
1877 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001879 const_pointer __new_last = __get_pointer() + __pos;
1880 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001881 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001882 --__p;
1883 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1884 if (__i->base() > __new_last)
1885 {
1886 (*__p)->__c_ = nullptr;
1887 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001888 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001889 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001891 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 }
1893 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001894#else
1895 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04001896#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897}
1898
1899template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001900inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001901basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001902 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001903 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001905 std::__debug_db_insert_c(this);
1906 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907}
1908
1909template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001910inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001912#if _LIBCPP_STD_VER <= 14
1913 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1914#else
1915 _NOEXCEPT
1916#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001917: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001919 std::__debug_db_insert_c(this);
1920 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921}
1922
1923template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001924_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier815ed732016-09-16 00:00:48 +00001925void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1926 size_type __sz,
1927 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001929 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001930 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001932 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001934 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935 {
1936 __set_short_size(__sz);
1937 __p = __get_short_pointer();
1938 }
1939 else
1940 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001941 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1942 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001943 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001945 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 __set_long_size(__sz);
1947 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001948 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949 traits_type::assign(__p[__sz], value_type());
1950}
1951
1952template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001953_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001955basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001957 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001958 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001960 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001962 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 {
1964 __set_short_size(__sz);
1965 __p = __get_short_pointer();
1966 }
1967 else
1968 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001969 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1970 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001971 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001973 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 __set_long_size(__sz);
1975 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001976 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977 traits_type::assign(__p[__sz], value_type());
1978}
1979
1980template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001981template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001982_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00001983basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001984 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001986 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001988 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989}
1990
1991template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001992inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00001993basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001994 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001996 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1997 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001998 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999}
2000
2001template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002002inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002003basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002004 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002006 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002008 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009}
2010
2011template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002012_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002014 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015{
2016 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002017 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002019 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002020 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002021 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022}
2023
2024template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002025_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002026basic_string<_CharT, _Traits, _Allocator>::basic_string(
2027 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002028 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029{
2030 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002031 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002033 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002034 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002035 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036}
2037
Martijn Vels5e7c9752020-03-04 17:52:46 -05002038template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002039_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Vels5e7c9752020-03-04 17:52:46 -05002040void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2041 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002042 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002043 __r_.first() = __rep();
2044
Martijn Vels5e7c9752020-03-04 17:52:46 -05002045 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002046 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002047 __p = __get_short_pointer();
2048 __set_short_size(__sz);
2049 } else {
2050 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002051 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002052 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2053 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002054 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002055 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002056 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002057 __set_long_size(__sz);
2058 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002059 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002060}
2061
Eric Fiselierfc92be82017-04-19 00:28:44 +00002062#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063
2064template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002065inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00002066basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00002067#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00002068 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00002069#else
2070 _NOEXCEPT
2071#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002072 : __r_(std::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002074 __str.__default_init();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002075 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002076 if (__is_long())
2077 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078}
2079
2080template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002081inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002083 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084{
Marshall Clowd2d24692014-07-17 15:32:20 +00002085 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002086 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002087 else
2088 {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002089 if (__libcpp_is_constant_evaluated())
2090 __r_.first() = __rep();
2091 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002092 __str.__default_init();
Marshall Clowd2d24692014-07-17 15:32:20 +00002093 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002094 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002095 if (__is_long())
2096 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097}
2098
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002099#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100
2101template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002102_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103void
2104basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2105{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002106 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002107 __r_.first() = __rep();
2108
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002110 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002112 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113 {
2114 __set_short_size(__n);
2115 __p = __get_short_pointer();
2116 }
2117 else
2118 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002119 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2120 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002121 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002123 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124 __set_long_size(__n);
2125 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002126 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127 traits_type::assign(__p[__n], value_type());
2128}
2129
2130template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002131inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002132basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002133 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134{
2135 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002136 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137}
2138
2139template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002140template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002141_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002142basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002143 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144{
2145 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002146 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147}
2148
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002150_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002151basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2152 size_type __pos, size_type __n,
2153 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002154 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155{
2156 size_type __str_sz = __str.size();
2157 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002158 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002159 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2160 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161}
2162
2163template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002164inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow83445802016-04-07 18:13:41 +00002165basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002166 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002167 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002168{
2169 size_type __str_sz = __str.size();
2170 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002171 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002172 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002173 std::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002174}
2175
2176template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002177template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002178_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow78dbe462016-11-14 18:22:19 +00002179basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002180 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002181 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002182{
Marshall Clowe46031a2018-07-02 18:41:15 +00002183 __self_view __sv0 = __t;
2184 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002185 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002186 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002187}
2188
2189template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002190template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002191_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002192basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002193 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002194{
Marshall Clowe46031a2018-07-02 18:41:15 +00002195 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002196 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002197 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002198}
2199
2200template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002201template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002202_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002203basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002204 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002205{
Marshall Clowe46031a2018-07-02 18:41:15 +00002206 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002207 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002208 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002209}
2210
2211template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002213_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002214__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002216 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2217>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2219{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002220 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221#ifndef _LIBCPP_NO_EXCEPTIONS
2222 try
2223 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002224#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225 for (; __first != __last; ++__first)
2226 push_back(*__first);
2227#ifndef _LIBCPP_NO_EXCEPTIONS
2228 }
2229 catch (...)
2230 {
2231 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002232 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233 throw;
2234 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002235#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236}
2237
2238template <class _CharT, class _Traits, class _Allocator>
2239template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002240_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002241__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002243 __is_cpp17_forward_iterator<_ForwardIterator>::value
2244>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2246{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002247 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002248 __r_.first() = __rep();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002249 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002251 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002253 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 {
2255 __set_short_size(__sz);
2256 __p = __get_short_pointer();
2257 }
2258 else
2259 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002260 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2261 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002262 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002264 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265 __set_long_size(__sz);
2266 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002267
2268#ifndef _LIBCPP_NO_EXCEPTIONS
2269 try
2270 {
2271#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002272 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 traits_type::assign(*__p, *__first);
2274 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002275#ifndef _LIBCPP_NO_EXCEPTIONS
2276 }
2277 catch (...)
2278 {
2279 if (__is_long())
2280 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2281 throw;
2282 }
2283#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284}
2285
2286template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002287template<class _InputIterator, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002288inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002290 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291{
2292 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002293 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294}
2295
2296template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002297template<class _InputIterator, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002298inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2300 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002301 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302{
2303 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002304 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305}
2306
Eric Fiselierfc92be82017-04-19 00:28:44 +00002307#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002308
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002310inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002311basic_string<_CharT, _Traits, _Allocator>::basic_string(
2312 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002313 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314{
2315 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002316 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317}
2318
2319template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002320inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002321basic_string<_CharT, _Traits, _Allocator>::basic_string(
2322 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002323 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324{
2325 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002326 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327}
2328
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002329#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002330
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002332_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2334{
Nikolas Klauser98833542022-05-07 22:20:23 +02002335 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002337 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338}
2339
2340template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002341_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342void
2343basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2344 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002345 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 +00002346{
2347 size_type __ms = max_size();
2348 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002349 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350 pointer __old_p = __get_pointer();
2351 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002352 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002354 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2355 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002356 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002357 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002359 traits_type::copy(std::__to_address(__p),
2360 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002361 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002362 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2364 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002365 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2366 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002367 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002368 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002369 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002370 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2372 __set_long_size(__old_sz);
2373 traits_type::assign(__p[__old_sz], value_type());
2374}
2375
2376template <class _CharT, class _Traits, class _Allocator>
2377void
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002378_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2380 size_type __n_copy, size_type __n_del, size_type __n_add)
2381{
2382 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002383 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002384 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385 pointer __old_p = __get_pointer();
2386 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002387 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002388 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002389 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2390 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002391 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002392 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002394 traits_type::copy(std::__to_address(__p),
2395 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2397 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002398 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2399 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002400 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002401 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2402 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002404 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405}
2406
2407// assign
2408
2409template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002410template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002411_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsb6a08b62020-04-10 18:36:31 -04002412basic_string<_CharT, _Traits, _Allocator>&
2413basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002414 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002415 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002416 if (__n < __cap) {
2417 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2418 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002419 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002420 traits_type::assign(__p[__n], value_type());
2421 __invalidate_iterators_past(__n);
2422 } else {
2423 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2424 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2425 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002426 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002427}
2428
2429template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002430_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002432basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2433 const value_type* __s, size_type __n) {
2434 size_type __cap = capacity();
2435 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002436 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002437 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002438 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002439 } else {
2440 size_type __sz = size();
2441 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002442 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002443 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002444}
2445
2446template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002447_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002448basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002449basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002451 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002452 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002453 ? __assign_short(__s, __n)
2454 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455}
2456
2457template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002458_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459basic_string<_CharT, _Traits, _Allocator>&
2460basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2461{
2462 size_type __cap = capacity();
2463 if (__cap < __n)
2464 {
2465 size_type __sz = size();
2466 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2467 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002468 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002470 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471}
2472
2473template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002474_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475basic_string<_CharT, _Traits, _Allocator>&
2476basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2477{
2478 pointer __p;
2479 if (__is_long())
2480 {
2481 __p = __get_long_pointer();
2482 __set_long_size(1);
2483 }
2484 else
2485 {
2486 __p = __get_short_pointer();
2487 __set_short_size(1);
2488 }
2489 traits_type::assign(*__p, __c);
2490 traits_type::assign(*++__p, value_type());
2491 __invalidate_iterators_past(1);
2492 return *this;
2493}
2494
2495template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002496_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002497basic_string<_CharT, _Traits, _Allocator>&
2498basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2499{
Martijn Vels596e3de2020-02-26 15:55:49 -05002500 if (this != &__str) {
2501 __copy_assign_alloc(__str);
2502 if (!__is_long()) {
2503 if (!__str.__is_long()) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002504 __r_.first() = __str.__r_.first();
Martijn Vels596e3de2020-02-26 15:55:49 -05002505 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002506 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002507 }
2508 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002509 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002510 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002511 }
2512 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002513}
2514
Eric Fiselierfc92be82017-04-19 00:28:44 +00002515#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002516
2517template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002518inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002519void
2520basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002521 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002522{
2523 if (__alloc() != __str.__alloc())
2524 assign(__str);
2525 else
2526 __move_assign(__str, true_type());
2527}
2528
2529template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002530inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002531void
2532basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002533#if _LIBCPP_STD_VER > 14
2534 _NOEXCEPT
2535#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002536 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002537#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002538{
marshall2ed41622019-11-27 07:13:00 -08002539 if (__is_long()) {
2540 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2541 __get_long_cap());
2542#if _LIBCPP_STD_VER <= 14
2543 if (!is_nothrow_move_assignable<allocator_type>::value) {
2544 __set_short_size(0);
2545 traits_type::assign(__get_short_pointer()[0], value_type());
2546 }
2547#endif
2548 }
2549 __move_assign_alloc(__str);
2550 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002551 if (__libcpp_is_constant_evaluated()) {
2552 __str.__default_init();
2553 } else {
2554 __str.__set_short_size(0);
2555 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2556 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002557}
2558
2559template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002560inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002561basic_string<_CharT, _Traits, _Allocator>&
2562basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002563 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002564{
2565 __move_assign(__str, integral_constant<bool,
2566 __alloc_traits::propagate_on_container_move_assignment::value>());
2567 return *this;
2568}
2569
2570#endif
2571
2572template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002574_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002575__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002577 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002579>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2581{
Marshall Clow958362f2016-09-05 01:54:30 +00002582 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002583 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002584 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585}
2586
2587template <class _CharT, class _Traits, class _Allocator>
2588template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002589_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002590__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002592 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002594>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2596{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002598 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002599 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002600
2601 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2602 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002604 if (__cap < __n)
2605 {
2606 size_type __sz = size();
2607 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2608 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002609 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002610 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002611 traits_type::assign(*__p, *__first);
2612 traits_type::assign(*__p, value_type());
2613 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002614 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615 }
2616 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002617 {
2618 const basic_string __temp(__first, __last, __alloc());
2619 assign(__temp.data(), __temp.size());
2620 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621 return *this;
2622}
2623
2624template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002625_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626basic_string<_CharT, _Traits, _Allocator>&
2627basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2628{
2629 size_type __sz = __str.size();
2630 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002631 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002632 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633}
2634
2635template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002636template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002637_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002638__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002639<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002640 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2641 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002642 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002643>
Marshall Clow82513342016-09-24 22:45:42 +00002644basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002645{
Marshall Clow82513342016-09-24 22:45:42 +00002646 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002647 size_type __sz = __sv.size();
2648 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002649 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002650 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002651}
2652
2653
2654template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002655_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002657basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2658 return __assign_external(__s, traits_type::length(__s));
2659}
2660
2661template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002662_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002663basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002664basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002666 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002667 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002668 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002669 ? __assign_short(__s, traits_type::length(__s))
2670 : __assign_external(__s, traits_type::length(__s)))
2671 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673// append
2674
2675template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002676_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002678basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002680 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681 size_type __cap = capacity();
2682 size_type __sz = size();
2683 if (__cap - __sz >= __n)
2684 {
2685 if (__n)
2686 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002687 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688 traits_type::copy(__p + __sz, __s, __n);
2689 __sz += __n;
2690 __set_size(__sz);
2691 traits_type::assign(__p[__sz], value_type());
2692 }
2693 }
2694 else
2695 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2696 return *this;
2697}
2698
2699template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002700_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701basic_string<_CharT, _Traits, _Allocator>&
2702basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2703{
2704 if (__n)
2705 {
2706 size_type __cap = capacity();
2707 size_type __sz = size();
2708 if (__cap - __sz < __n)
2709 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2710 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002711 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 __sz += __n;
2713 __set_size(__sz);
2714 traits_type::assign(__p[__sz], value_type());
2715 }
2716 return *this;
2717}
2718
2719template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002720_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002721basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2722{
2723 if (__n)
2724 {
2725 size_type __cap = capacity();
2726 size_type __sz = size();
2727 if (__cap - __sz < __n)
2728 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2729 pointer __p = __get_pointer();
2730 __sz += __n;
2731 __set_size(__sz);
2732 traits_type::assign(__p[__sz], value_type());
2733 }
2734}
2735
2736template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002737_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738void
2739basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2740{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002741 bool __is_short = !__is_long();
2742 size_type __cap;
2743 size_type __sz;
2744 if (__is_short)
2745 {
2746 __cap = __min_cap - 1;
2747 __sz = __get_short_size();
2748 }
2749 else
2750 {
2751 __cap = __get_long_cap() - 1;
2752 __sz = __get_long_size();
2753 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002755 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002757 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002758 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002759 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002760 if (__is_short)
2761 {
2762 __p = __get_short_pointer() + __sz;
2763 __set_short_size(__sz+1);
2764 }
2765 else
2766 {
2767 __p = __get_long_pointer() + __sz;
2768 __set_long_size(__sz+1);
2769 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770 traits_type::assign(*__p, __c);
2771 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772}
2773
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774template <class _CharT, class _Traits, class _Allocator>
2775template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002776_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002777__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002778<
2779 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2780 basic_string<_CharT, _Traits, _Allocator>&
2781>
2782basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002783 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784{
2785 size_type __sz = size();
2786 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002787 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 if (__n)
2789 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002790 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2791 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002792 {
2793 if (__cap - __sz < __n)
2794 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2795 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002796 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002797 traits_type::assign(*__p, *__first);
2798 traits_type::assign(*__p, value_type());
2799 __set_size(__sz + __n);
2800 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002801 else
2802 {
2803 const basic_string __temp(__first, __last, __alloc());
2804 append(__temp.data(), __temp.size());
2805 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806 }
2807 return *this;
2808}
2809
2810template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002811inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812basic_string<_CharT, _Traits, _Allocator>&
2813basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2814{
2815 return append(__str.data(), __str.size());
2816}
2817
2818template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002819_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820basic_string<_CharT, _Traits, _Allocator>&
2821basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2822{
2823 size_type __sz = __str.size();
2824 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002825 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002826 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827}
2828
2829template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002830template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002831_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002832 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002833 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002834 __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 +00002835 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002836 >
Marshall Clow82513342016-09-24 22:45:42 +00002837basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002838{
Marshall Clow82513342016-09-24 22:45:42 +00002839 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002840 size_type __sz = __sv.size();
2841 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002842 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002843 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002844}
2845
2846template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002847_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002849basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002851 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852 return append(__s, traits_type::length(__s));
2853}
2854
2855// insert
2856
2857template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002858_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002860basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002862 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 size_type __sz = size();
2864 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002865 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002867 if (__libcpp_is_constant_evaluated()) {
2868 if (__cap - __sz >= __n)
2869 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2870 else
2871 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2872 return *this;
2873 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874 if (__cap - __sz >= __n)
2875 {
2876 if (__n)
2877 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002878 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879 size_type __n_move = __sz - __pos;
2880 if (__n_move != 0)
2881 {
2882 if (__p + __pos <= __s && __s < __p + __sz)
2883 __s += __n;
2884 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2885 }
2886 traits_type::move(__p + __pos, __s, __n);
2887 __sz += __n;
2888 __set_size(__sz);
2889 traits_type::assign(__p[__sz], value_type());
2890 }
2891 }
2892 else
2893 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2894 return *this;
2895}
2896
2897template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002898_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899basic_string<_CharT, _Traits, _Allocator>&
2900basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2901{
2902 size_type __sz = size();
2903 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002904 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905 if (__n)
2906 {
2907 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002908 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 if (__cap - __sz >= __n)
2910 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002911 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 size_type __n_move = __sz - __pos;
2913 if (__n_move != 0)
2914 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2915 }
2916 else
2917 {
2918 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002919 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920 }
2921 traits_type::assign(__p + __pos, __n, __c);
2922 __sz += __n;
2923 __set_size(__sz);
2924 traits_type::assign(__p[__sz], value_type());
2925 }
2926 return *this;
2927}
2928
2929template <class _CharT, class _Traits, class _Allocator>
2930template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002931_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002932__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002934 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002935 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002936>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2938{
Nikolas Klausereed25832021-12-15 01:32:30 +01002939 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2940 "string::insert(iterator, range) called with an iterator not"
2941 " referring to this string");
2942 const basic_string __temp(__first, __last, __alloc());
2943 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944}
2945
2946template <class _CharT, class _Traits, class _Allocator>
2947template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002948_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002949__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002951 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002953>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2955{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002956 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2957 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002958
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002960 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2961 if (__n == 0)
2962 return begin() + __ip;
2963
2964 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002966 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002968 else
2969 {
2970 const basic_string __temp(__first, __last, __alloc());
2971 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2972 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973}
2974
2975template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002976inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977basic_string<_CharT, _Traits, _Allocator>&
2978basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2979{
2980 return insert(__pos1, __str.data(), __str.size());
2981}
2982
2983template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002984_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985basic_string<_CharT, _Traits, _Allocator>&
2986basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2987 size_type __pos2, size_type __n)
2988{
2989 size_type __str_sz = __str.size();
2990 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002991 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002992 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993}
2994
2995template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002996template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002997_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002998__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002999<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003000 __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 +00003001 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003002>
Marshall Clow82513342016-09-24 22:45:42 +00003003basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003004 size_type __pos2, size_type __n)
3005{
Marshall Clow82513342016-09-24 22:45:42 +00003006 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003007 size_type __str_sz = __sv.size();
3008 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003009 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003010 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003011}
3012
3013template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003014_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003016basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003017{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003018 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 return insert(__pos, __s, traits_type::length(__s));
3020}
3021
3022template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003023_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024typename basic_string<_CharT, _Traits, _Allocator>::iterator
3025basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
3026{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05003027 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3028 "string::insert(iterator, character) called with an iterator not"
3029 " referring to this string");
3030
Howard Hinnantc51e1022010-05-11 19:42:16 +00003031 size_type __ip = static_cast<size_type>(__pos - begin());
3032 size_type __sz = size();
3033 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003034 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 if (__cap == __sz)
3036 {
3037 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003038 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039 }
3040 else
3041 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003042 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043 size_type __n_move = __sz - __ip;
3044 if (__n_move != 0)
3045 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3046 }
3047 traits_type::assign(__p[__ip], __c);
3048 traits_type::assign(__p[++__sz], value_type());
3049 __set_size(__sz);
3050 return begin() + static_cast<difference_type>(__ip);
3051}
3052
3053template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003054inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055typename basic_string<_CharT, _Traits, _Allocator>::iterator
3056basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3057{
Nikolas Klausereed25832021-12-15 01:32:30 +01003058 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3059 "string::insert(iterator, n, value) called with an iterator not"
3060 " referring to this string");
3061 difference_type __p = __pos - begin();
3062 insert(static_cast<size_type>(__p), __n, __c);
3063 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064}
3065
3066// replace
3067
3068template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003069_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003071basic_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 +00003072 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003074 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075 size_type __sz = size();
3076 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003077 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003078 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079 size_type __cap = capacity();
3080 if (__cap - __sz + __n1 >= __n2)
3081 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003082 if (__libcpp_is_constant_evaluated()) {
3083 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3084 return *this;
3085 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003086 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087 if (__n1 != __n2)
3088 {
3089 size_type __n_move = __sz - __pos - __n1;
3090 if (__n_move != 0)
3091 {
3092 if (__n1 > __n2)
3093 {
3094 traits_type::move(__p + __pos, __s, __n2);
3095 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003096 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 }
3098 if (__p + __pos < __s && __s < __p + __sz)
3099 {
3100 if (__p + __pos + __n1 <= __s)
3101 __s += __n2 - __n1;
3102 else // __p + __pos < __s < __p + __pos + __n1
3103 {
3104 traits_type::move(__p + __pos, __s, __n1);
3105 __pos += __n1;
3106 __s += __n2;
3107 __n2 -= __n1;
3108 __n1 = 0;
3109 }
3110 }
3111 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3112 }
3113 }
3114 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003115 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 }
3117 else
3118 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3119 return *this;
3120}
3121
3122template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003123_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124basic_string<_CharT, _Traits, _Allocator>&
3125basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3126{
3127 size_type __sz = size();
3128 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003129 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003130 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003132 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 if (__cap - __sz + __n1 >= __n2)
3134 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003135 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136 if (__n1 != __n2)
3137 {
3138 size_type __n_move = __sz - __pos - __n1;
3139 if (__n_move != 0)
3140 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3141 }
3142 }
3143 else
3144 {
3145 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003146 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 }
3148 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003149 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150}
3151
3152template <class _CharT, class _Traits, class _Allocator>
3153template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003154_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003155__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003156<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003157 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003159>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003160basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161 _InputIterator __j1, _InputIterator __j2)
3162{
Marshall Clow958362f2016-09-05 01:54:30 +00003163 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003164 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165}
3166
3167template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003168inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169basic_string<_CharT, _Traits, _Allocator>&
3170basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3171{
3172 return replace(__pos1, __n1, __str.data(), __str.size());
3173}
3174
3175template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003176_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177basic_string<_CharT, _Traits, _Allocator>&
3178basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3179 size_type __pos2, size_type __n2)
3180{
3181 size_type __str_sz = __str.size();
3182 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003183 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003184 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185}
3186
3187template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003188template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003189_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003190__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003191<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003192 __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 +00003193 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003194>
Marshall Clow82513342016-09-24 22:45:42 +00003195basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003196 size_type __pos2, size_type __n2)
3197{
Marshall Clow82513342016-09-24 22:45:42 +00003198 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003199 size_type __str_sz = __sv.size();
3200 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003201 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003202 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003203}
3204
3205template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003206_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003207basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003208basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003210 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211 return replace(__pos, __n1, __s, traits_type::length(__s));
3212}
3213
3214template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003215inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003217basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218{
3219 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3220 __str.data(), __str.size());
3221}
3222
3223template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003224inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003226basic_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 +00003227{
3228 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3229}
3230
3231template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003232inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003234basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235{
3236 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3237}
3238
3239template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003240inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003242basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243{
3244 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3245}
3246
3247// erase
3248
Martijn Velsa81fc792020-02-26 13:25:43 -05003249// 'externally instantiated' erase() implementation, called when __n != npos.
3250// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003252_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003253void
3254basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3255 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003256{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257 if (__n)
3258 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003259 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003260 value_type* __p = std::__to_address(__get_pointer());
3261 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262 size_type __n_move = __sz - __pos - __n;
3263 if (__n_move != 0)
3264 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003265 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003267}
3268
3269template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003270_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003271basic_string<_CharT, _Traits, _Allocator>&
3272basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3273 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003274 if (__pos > size())
3275 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003276 if (__n == npos) {
3277 __erase_to_end(__pos);
3278 } else {
3279 __erase_external_with_move(__pos, __n);
3280 }
3281 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282}
3283
3284template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003285inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286typename basic_string<_CharT, _Traits, _Allocator>::iterator
3287basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3288{
Nikolas Klausereed25832021-12-15 01:32:30 +01003289 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3290 "string::erase(iterator) called with an iterator not"
3291 " referring to this string");
3292
3293 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3294 iterator __b = begin();
3295 size_type __r = static_cast<size_type>(__pos - __b);
3296 erase(__r, 1);
3297 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298}
3299
3300template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003301inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302typename basic_string<_CharT, _Traits, _Allocator>::iterator
3303basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3304{
Nikolas Klausereed25832021-12-15 01:32:30 +01003305 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3306 "string::erase(iterator, iterator) called with an iterator not"
3307 " referring to this string");
3308
3309 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3310 iterator __b = begin();
3311 size_type __r = static_cast<size_type>(__first - __b);
3312 erase(__r, static_cast<size_type>(__last - __first));
3313 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314}
3315
3316template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003317inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318void
3319basic_string<_CharT, _Traits, _Allocator>::pop_back()
3320{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003321 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003322 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323}
3324
3325template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003326inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003328basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003330 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331 if (__is_long())
3332 {
3333 traits_type::assign(*__get_long_pointer(), value_type());
3334 __set_long_size(0);
3335 }
3336 else
3337 {
3338 traits_type::assign(*__get_short_pointer(), value_type());
3339 __set_short_size(0);
3340 }
3341}
3342
3343template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003344inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345void
3346basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3347{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003348 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349}
3350
3351template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003352_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353void
3354basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3355{
3356 size_type __sz = size();
3357 if (__n > __sz)
3358 append(__n - __sz, __c);
3359 else
3360 __erase_to_end(__n);
3361}
3362
3363template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003364_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003365basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3366{
3367 size_type __sz = size();
3368 if (__n > __sz) {
3369 __append_default_init(__n - __sz);
3370 } else
3371 __erase_to_end(__n);
3372}
3373
3374template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003375inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003377basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003379 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003380 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3381 return __m - __alignment;
3382 } else {
3383 bool __uses_lsb = __endian_factor == 2;
3384 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3385 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386}
3387
3388template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003389_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390void
Marek Kurdejc9848142020-11-26 10:07:16 +01003391basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392{
Marek Kurdejc9848142020-11-26 10:07:16 +01003393 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003394 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003395
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003396 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3397 // and later (since P0966R1), however we provide consistent behavior in all Standard
3398 // modes because this function is instantiated in the shared library.
3399 if (__requested_capacity <= capacity())
3400 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003401
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003402 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003403 __target_capacity = __recommend(__target_capacity);
3404 if (__target_capacity == capacity()) return;
3405
3406 __shrink_or_extend(__target_capacity);
3407}
3408
3409template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003410inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003411void
3412basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3413{
3414 size_type __target_capacity = __recommend(size());
3415 if (__target_capacity == capacity()) return;
3416
3417 __shrink_or_extend(__target_capacity);
3418}
3419
3420template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003421inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003422void
3423basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3424{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425 size_type __cap = capacity();
3426 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003427
3428 pointer __new_data, __p;
3429 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003430 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003432 __was_long = true;
3433 __now_long = false;
3434 __new_data = __get_short_pointer();
3435 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003437 else
3438 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003439 if (__target_capacity > __cap) {
3440 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3441 __new_data = __allocation.ptr;
3442 __target_capacity = __allocation.count - 1;
3443 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003444 else
3445 {
3446 #ifndef _LIBCPP_NO_EXCEPTIONS
3447 try
3448 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003449 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003450 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3451 __new_data = __allocation.ptr;
3452 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003453 #ifndef _LIBCPP_NO_EXCEPTIONS
3454 }
3455 catch (...)
3456 {
3457 return;
3458 }
3459 #else // _LIBCPP_NO_EXCEPTIONS
3460 if (__new_data == nullptr)
3461 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003462 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003463 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003464 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003465 __now_long = true;
3466 __was_long = __is_long();
3467 __p = __get_pointer();
3468 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003469 traits_type::copy(std::__to_address(__new_data),
3470 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003471 if (__was_long)
3472 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3473 if (__now_long)
3474 {
3475 __set_long_cap(__target_capacity+1);
3476 __set_long_size(__sz);
3477 __set_long_pointer(__new_data);
3478 }
3479 else
3480 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003481 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482}
3483
3484template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003485inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003487basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003489 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490 return *(data() + __pos);
3491}
3492
3493template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003494inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003496basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003498 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499 return *(__get_pointer() + __pos);
3500}
3501
3502template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003503_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3505basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3506{
3507 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003508 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509 return (*this)[__n];
3510}
3511
3512template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003513_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514typename basic_string<_CharT, _Traits, _Allocator>::reference
3515basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3516{
3517 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003518 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519 return (*this)[__n];
3520}
3521
3522template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003523inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003525basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003527 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528 return *__get_pointer();
3529}
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>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003534basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003536 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537 return *data();
3538}
3539
3540template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003541inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003543basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003545 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546 return *(__get_pointer() + size() - 1);
3547}
3548
3549template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003550inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003552basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003554 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555 return *(data() + size() - 1);
3556}
3557
3558template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003559_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003560typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003561basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562{
3563 size_type __sz = size();
3564 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003565 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003566 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567 traits_type::copy(__s, data() + __pos, __rlen);
3568 return __rlen;
3569}
3570
3571template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003572inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573basic_string<_CharT, _Traits, _Allocator>
3574basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3575{
3576 return basic_string(*this, __pos, __n, __alloc());
3577}
3578
3579template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003580inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581void
3582basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003583#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003584 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003585#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003586 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003587 __is_nothrow_swappable<allocator_type>::value)
3588#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589{
Nikolas Klauser98833542022-05-07 22:20:23 +02003590 if (!__is_long())
3591 std::__debug_db_invalidate_all(this);
3592 if (!__str.__is_long())
3593 std::__debug_db_invalidate_all(&__str);
3594 std::__debug_db_swap(this, &__str);
3595
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003596 _LIBCPP_ASSERT(
3597 __alloc_traits::propagate_on_container_swap::value ||
3598 __alloc_traits::is_always_equal::value ||
3599 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003600 std::swap(__r_.first(), __str.__r_.first());
3601 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602}
3603
3604// find
3605
3606template <class _Traits>
3607struct _LIBCPP_HIDDEN __traits_eq
3608{
3609 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003610 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003611 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3612 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613};
3614
3615template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003616_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003618basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003619 size_type __pos,
3620 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003622 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003623 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003624 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625}
3626
3627template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003628inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003630basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3631 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003633 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003634 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635}
3636
3637template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003638template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003639_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003640__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003641<
3642 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3643 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003644>
Marshall Clowe46031a2018-07-02 18:41:15 +00003645basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003646 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003647{
Marshall Clowe46031a2018-07-02 18:41:15 +00003648 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003649 return __str_find<value_type, size_type, traits_type, npos>
3650 (data(), size(), __sv.data(), __pos, __sv.size());
3651}
3652
3653template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003654inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003655typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003656basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003657 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003659 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003660 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003661 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662}
3663
3664template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003665_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003667basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3668 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003670 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003671 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672}
3673
3674// rfind
3675
3676template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003677_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003679basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003680 size_type __pos,
3681 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003683 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003684 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003685 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686}
3687
3688template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003689inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003691basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3692 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003695 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696}
3697
3698template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003699template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003700_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003701__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003702<
3703 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3704 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003705>
Marshall Clowe46031a2018-07-02 18:41:15 +00003706basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003707 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003708{
Marshall Clowe46031a2018-07-02 18:41:15 +00003709 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003710 return __str_rfind<value_type, size_type, traits_type, npos>
3711 (data(), size(), __sv.data(), __pos, __sv.size());
3712}
3713
3714template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003715inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003716typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003717basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003718 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003720 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003721 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003722 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723}
3724
3725template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003726_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003728basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3729 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003731 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003732 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733}
3734
3735// find_first_of
3736
3737template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003738_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003739typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003740basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003741 size_type __pos,
3742 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003744 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003745 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003746 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747}
3748
3749template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003750inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003752basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3753 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003755 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003756 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003757}
3758
3759template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003760template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003761_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003762__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003763<
3764 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3765 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003766>
Marshall Clowe46031a2018-07-02 18:41:15 +00003767basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003768 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003769{
Marshall Clowe46031a2018-07-02 18:41:15 +00003770 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003771 return __str_find_first_of<value_type, size_type, traits_type, npos>
3772 (data(), size(), __sv.data(), __pos, __sv.size());
3773}
3774
3775template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003776inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003777typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003778basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003779 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003781 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003782 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003783 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784}
3785
3786template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003787inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003789basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3790 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791{
3792 return find(__c, __pos);
3793}
3794
3795// find_last_of
3796
3797template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003798inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003800basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003801 size_type __pos,
3802 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003804 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003805 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003806 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807}
3808
3809template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003810inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003812basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3813 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003815 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003816 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817}
3818
3819template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003820template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003821_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003822__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003823<
3824 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3825 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003826>
Marshall Clowe46031a2018-07-02 18:41:15 +00003827basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003828 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003829{
Marshall Clowe46031a2018-07-02 18:41:15 +00003830 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003831 return __str_find_last_of<value_type, size_type, traits_type, npos>
3832 (data(), size(), __sv.data(), __pos, __sv.size());
3833}
3834
3835template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003836inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003837typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003838basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003839 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003841 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003842 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003843 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844}
3845
3846template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003847inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003849basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3850 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851{
3852 return rfind(__c, __pos);
3853}
3854
3855// find_first_not_of
3856
3857template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003858_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003860basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003861 size_type __pos,
3862 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003864 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003865 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003866 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867}
3868
3869template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003870inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003871typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003872basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3873 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003875 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003876 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877}
3878
3879template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003880template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003881_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003882__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003883<
3884 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3885 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003886>
Marshall Clowe46031a2018-07-02 18:41:15 +00003887basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003888 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003889{
Marshall Clowe46031a2018-07-02 18:41:15 +00003890 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003891 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3892 (data(), size(), __sv.data(), __pos, __sv.size());
3893}
3894
3895template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003896inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003897typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003898basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003899 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003901 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003902 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003903 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904}
3905
3906template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003907inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003909basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3910 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003912 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003913 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003914}
3915
3916// find_last_not_of
3917
3918template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003919_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003921basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003922 size_type __pos,
3923 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003925 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003926 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003927 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928}
3929
3930template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003931inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003933basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3934 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003936 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003937 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003938}
3939
3940template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003941template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003942_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003943__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003944<
3945 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3946 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003947>
Marshall Clowe46031a2018-07-02 18:41:15 +00003948basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003949 size_type __pos) const _NOEXCEPT
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 __str_find_last_not_of<value_type, size_type, traits_type, npos>
3953 (data(), size(), __sv.data(), __pos, __sv.size());
3954}
3955
3956template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003957inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003958typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003959basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003960 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003962 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003963 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003964 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965}
3966
3967template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003968inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003970basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3971 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003973 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003974 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975}
3976
3977// compare
3978
3979template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003980template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003981_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003982__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003983<
3984 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3985 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003986>
zoecarver1997e0a2021-02-05 11:54:47 -08003987basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003988{
Marshall Clowe46031a2018-07-02 18:41:15 +00003989 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003990 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003991 size_t __rhs_sz = __sv.size();
3992 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003993 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003994 if (__result != 0)
3995 return __result;
3996 if (__lhs_sz < __rhs_sz)
3997 return -1;
3998 if (__lhs_sz > __rhs_sz)
3999 return 1;
4000 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004001}
4002
4003template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004004inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004006basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004008 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009}
4010
4011template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004012inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004014basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4015 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00004016 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004017 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018{
Alp Tokerb8a95f52014-05-15 11:27:39 +00004019 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020 size_type __sz = size();
4021 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004022 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004023 size_type __rlen = std::min(__n1, __sz - __pos1);
4024 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025 if (__r == 0)
4026 {
4027 if (__rlen < __n2)
4028 __r = -1;
4029 else if (__rlen > __n2)
4030 __r = 1;
4031 }
4032 return __r;
4033}
4034
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004035template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00004036template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004037_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04004038__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00004039<
4040 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
4041 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004042>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004043basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4044 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00004045 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004046{
Marshall Clowe46031a2018-07-02 18:41:15 +00004047 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004048 return compare(__pos1, __n1, __sv.data(), __sv.size());
4049}
4050
4051template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004052inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004053int
4054basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4055 size_type __n1,
4056 const basic_string& __str) const
4057{
4058 return compare(__pos1, __n1, __str.data(), __str.size());
4059}
4060
4061template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00004062template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004063_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04004064__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00004065<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004066 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
4067 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00004068 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004069>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004070basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4071 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00004072 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004073 size_type __pos2,
4074 size_type __n2) const
4075{
Marshall Clow82513342016-09-24 22:45:42 +00004076 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004077 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
4078}
4079
4080template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004081_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004082int
4083basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4084 size_type __n1,
4085 const basic_string& __str,
4086 size_type __pos2,
4087 size_type __n2) const
4088{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004089 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004090}
4091
4092template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004093_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004094int
4095basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4096{
4097 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4098 return compare(0, npos, __s, traits_type::length(__s));
4099}
4100
4101template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004102_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004103int
4104basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4105 size_type __n1,
4106 const value_type* __s) const
4107{
4108 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4109 return compare(__pos1, __n1, __s, traits_type::length(__s));
4110}
4111
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112// __invariants
4113
4114template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004115inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116bool
4117basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4118{
4119 if (size() > capacity())
4120 return false;
4121 if (capacity() < __min_cap - 1)
4122 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004123 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004125 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126 return false;
4127 return true;
4128}
4129
Vedant Kumar55e007e2018-03-08 21:15:26 +00004130// __clear_and_shrink
4131
4132template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004133inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne173f29e2019-05-29 16:01:36 +00004134void
Marshall Clowe60a7182018-05-29 17:04:37 +00004135basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004136{
4137 clear();
4138 if(__is_long())
4139 {
4140 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4141 __set_long_cap(0);
4142 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004143 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004144 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004145}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004146
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147// operator==
4148
4149template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004150inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151bool
4152operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004153 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154{
Mark de Weverb4830e62022-07-19 07:56:23 +02004155#if _LIBCPP_STD_VER > 17
4156 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4157#else
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004158 size_t __lhs_sz = __lhs.size();
4159 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4160 __rhs.data(),
4161 __lhs_sz) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004162#endif
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004163}
4164
4165template<class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004166inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004167bool
4168operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4169 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4170{
4171 size_t __lhs_sz = __lhs.size();
4172 if (__lhs_sz != __rhs.size())
4173 return false;
4174 const char* __lp = __lhs.data();
4175 const char* __rp = __rhs.data();
4176 if (__lhs.__is_long())
4177 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4178 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4179 if (*__lp != *__rp)
4180 return false;
4181 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182}
4183
Mark de Weverb4830e62022-07-19 07:56:23 +02004184#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004186inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004188operator==(const _CharT* __lhs,
4189 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004191 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4192 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4193 size_t __lhs_len = _Traits::length(__lhs);
4194 if (__lhs_len != __rhs.size()) return false;
4195 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196}
Mark de Weverb4830e62022-07-19 07:56:23 +02004197#endif // _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198
Howard Hinnantc51e1022010-05-11 19:42:16 +00004199template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004200inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004202operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4203 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204{
Mark de Weverb4830e62022-07-19 07:56:23 +02004205#if _LIBCPP_STD_VER > 17
4206 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4207#else
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004208 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4209 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4210 size_t __rhs_len = _Traits::length(__rhs);
4211 if (__rhs_len != __lhs.size()) return false;
4212 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004213#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214}
4215
Mark de Weverb4830e62022-07-19 07:56:23 +02004216#if _LIBCPP_STD_VER > 17
4217
4218template <class _CharT, class _Traits, class _Allocator>
4219_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
4220 const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4221 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
4222 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4223}
4224
4225template <class _CharT, class _Traits, class _Allocator>
4226_LIBCPP_HIDE_FROM_ABI constexpr auto
4227operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
4228 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4229}
4230
4231#else // _LIBCPP_STD_VER > 17
4232
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004233template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004234inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235bool
4236operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004237 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238{
4239 return !(__lhs == __rhs);
4240}
4241
4242template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004243inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004245operator!=(const _CharT* __lhs,
4246 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247{
4248 return !(__lhs == __rhs);
4249}
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004254operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4255 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256{
4257 return !(__lhs == __rhs);
4258}
4259
4260// operator<
4261
4262template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004263inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264bool
4265operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004266 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004268 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269}
4270
4271template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004272inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004274operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4275 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004277 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278}
4279
4280template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004281inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004283operator< (const _CharT* __lhs,
4284 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285{
4286 return __rhs.compare(__lhs) > 0;
4287}
4288
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289// operator>
4290
4291template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004292inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293bool
4294operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004295 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296{
4297 return __rhs < __lhs;
4298}
4299
4300template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004301inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004303operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4304 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305{
4306 return __rhs < __lhs;
4307}
4308
4309template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004310inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004312operator> (const _CharT* __lhs,
4313 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314{
4315 return __rhs < __lhs;
4316}
4317
4318// operator<=
4319
4320template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004321inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322bool
4323operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004324 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325{
4326 return !(__rhs < __lhs);
4327}
4328
4329template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004330inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004332operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4333 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334{
4335 return !(__rhs < __lhs);
4336}
4337
4338template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004339inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004341operator<=(const _CharT* __lhs,
4342 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343{
4344 return !(__rhs < __lhs);
4345}
4346
4347// operator>=
4348
4349template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004350inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351bool
4352operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004353 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354{
4355 return !(__lhs < __rhs);
4356}
4357
4358template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004359inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004361operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4362 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363{
4364 return !(__lhs < __rhs);
4365}
4366
4367template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004368inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004370operator>=(const _CharT* __lhs,
4371 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372{
4373 return !(__lhs < __rhs);
4374}
Mark de Weverb4830e62022-07-19 07:56:23 +02004375#endif // _LIBCPP_STD_VER > 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376
4377// operator +
4378
4379template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004380_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004381basic_string<_CharT, _Traits, _Allocator>
4382operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4383 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4384{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004385 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004386 auto __lhs_sz = __lhs.size();
4387 auto __rhs_sz = __rhs.size();
4388 _String __r(__uninitialized_size_tag(),
4389 __lhs_sz + __rhs_sz,
4390 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4391 auto __ptr = std::__to_address(__r.__get_pointer());
4392 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4393 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4394 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004395 return __r;
4396}
4397
4398template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004399_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004400basic_string<_CharT, _Traits, _Allocator>
4401operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4402{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004403 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004404 auto __lhs_sz = _Traits::length(__lhs);
4405 auto __rhs_sz = __rhs.size();
4406 _String __r(__uninitialized_size_tag(),
4407 __lhs_sz + __rhs_sz,
4408 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4409 auto __ptr = std::__to_address(__r.__get_pointer());
4410 _Traits::copy(__ptr, __lhs, __lhs_sz);
4411 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4412 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413 return __r;
4414}
4415
4416template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004417_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004418basic_string<_CharT, _Traits, _Allocator>
4419operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4420{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004421 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004422 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004423 _String __r(__uninitialized_size_tag(),
4424 __rhs_sz + 1,
4425 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4426 auto __ptr = std::__to_address(__r.__get_pointer());
4427 _Traits::assign(__ptr, 1, __lhs);
4428 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4429 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004430 return __r;
4431}
4432
4433template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004434inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004435basic_string<_CharT, _Traits, _Allocator>
4436operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4437{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004438 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004439 typename _String::size_type __lhs_sz = __lhs.size();
4440 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004441 _String __r(__uninitialized_size_tag(),
4442 __lhs_sz + __rhs_sz,
4443 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4444 auto __ptr = std::__to_address(__r.__get_pointer());
4445 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4446 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4447 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004448 return __r;
4449}
4450
4451template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004452_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004453basic_string<_CharT, _Traits, _Allocator>
4454operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4455{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004456 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004457 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004458 _String __r(__uninitialized_size_tag(),
4459 __lhs_sz + 1,
4460 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4461 auto __ptr = std::__to_address(__r.__get_pointer());
4462 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4463 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4464 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004465 return __r;
4466}
4467
Eric Fiselierfc92be82017-04-19 00:28:44 +00004468#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004469
4470template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004471inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004472basic_string<_CharT, _Traits, _Allocator>
4473operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4474{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004475 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004476}
4477
4478template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004479inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004480basic_string<_CharT, _Traits, _Allocator>
4481operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4482{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004483 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484}
4485
4486template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004487inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004488basic_string<_CharT, _Traits, _Allocator>
4489operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4490{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004491 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004492}
4493
4494template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004495inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004496basic_string<_CharT, _Traits, _Allocator>
4497operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4498{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004499 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004500}
4501
4502template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004503inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004504basic_string<_CharT, _Traits, _Allocator>
4505operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4506{
4507 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004508 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004509}
4510
4511template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004512inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004513basic_string<_CharT, _Traits, _Allocator>
4514operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4515{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004516 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004517}
4518
4519template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004520inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521basic_string<_CharT, _Traits, _Allocator>
4522operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4523{
4524 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004525 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004526}
4527
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004528#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004529
4530// swap
4531
4532template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004533inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004534void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004535swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004536 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4537 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538{
4539 __lhs.swap(__rhs);
4540}
4541
Bruce Mitchener170d8972020-11-24 12:53:53 -05004542_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4543_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4544_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4545_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4546_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004547
Bruce Mitchener170d8972020-11-24 12:53:53 -05004548_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4549_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4550_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004551
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004552_LIBCPP_FUNC_VIS string to_string(int __val);
4553_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4554_LIBCPP_FUNC_VIS string to_string(long __val);
4555_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4556_LIBCPP_FUNC_VIS string to_string(long long __val);
4557_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4558_LIBCPP_FUNC_VIS string to_string(float __val);
4559_LIBCPP_FUNC_VIS string to_string(double __val);
4560_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004561
Louis Dionne89258142021-08-23 15:32:36 -04004562#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004563_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4564_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4565_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4566_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4567_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004568
Bruce Mitchener170d8972020-11-24 12:53:53 -05004569_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4570_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4571_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004572
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004573_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4574_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4575_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4576_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4577_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4578_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4579_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4580_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4581_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004582#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004583
Howard Hinnantc51e1022010-05-11 19:42:16 +00004584template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004585_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004586const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4587 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004588
Marshall Clow851b9ec2019-05-20 21:56:51 +00004589template <class _CharT, class _Allocator>
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004590struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004591{
4592 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004593 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4594 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004595};
4596
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004597template <class _Allocator>
4598struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
4599
4600#ifndef _LIBCPP_HAS_NO_CHAR8_T
4601template <class _Allocator>
4602struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
4603#endif
4604
4605template <class _Allocator>
4606struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
4607
4608template <class _Allocator>
4609struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
4610
4611#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4612template <class _Allocator>
4613struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
4614#endif
4615
Howard Hinnantdc095972011-07-18 15:51:59 +00004616template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004617_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004618operator<<(basic_ostream<_CharT, _Traits>& __os,
4619 const basic_string<_CharT, _Traits, _Allocator>& __str);
4620
4621template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004622_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004623operator>>(basic_istream<_CharT, _Traits>& __is,
4624 basic_string<_CharT, _Traits, _Allocator>& __str);
4625
4626template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004627_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004628getline(basic_istream<_CharT, _Traits>& __is,
4629 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4630
4631template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004632inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004633basic_istream<_CharT, _Traits>&
4634getline(basic_istream<_CharT, _Traits>& __is,
4635 basic_string<_CharT, _Traits, _Allocator>& __str);
4636
Howard Hinnantdc095972011-07-18 15:51:59 +00004637template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004638inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004639basic_istream<_CharT, _Traits>&
4640getline(basic_istream<_CharT, _Traits>&& __is,
4641 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4642
4643template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004644inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004645basic_istream<_CharT, _Traits>&
4646getline(basic_istream<_CharT, _Traits>&& __is,
4647 basic_string<_CharT, _Traits, _Allocator>& __str);
4648
Marshall Clow29b53f22018-12-14 18:49:35 +00004649#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004650template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004651inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004652 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4653 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4654 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004655 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004656 return __old_size - __str.size();
4657}
Marshall Clow29b53f22018-12-14 18:49:35 +00004658
Marek Kurdeja98b1412020-05-02 13:58:03 +02004659template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004660inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004661 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4662 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4663 _Predicate __pred) {
4664 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004665 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004666 __str.end());
4667 return __old_size - __str.size();
4668}
Marshall Clow29b53f22018-12-14 18:49:35 +00004669#endif
4670
Louis Dionne510450b2022-04-01 16:38:30 -04004671#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004672
4673template<class _CharT, class _Traits, class _Allocator>
4674bool
4675basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4676{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004677 return data() <= std::__to_address(__i->base()) &&
4678 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004679}
4680
4681template<class _CharT, class _Traits, class _Allocator>
4682bool
4683basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4684{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004685 return data() < std::__to_address(__i->base()) &&
4686 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004687}
4688
4689template<class _CharT, class _Traits, class _Allocator>
4690bool
4691basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4692{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004693 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004694 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004695}
4696
4697template<class _CharT, class _Traits, class _Allocator>
4698bool
4699basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4700{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004701 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004702 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004703}
4704
Louis Dionne510450b2022-04-01 16:38:30 -04004705#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004706
Louis Dionne173f29e2019-05-29 16:01:36 +00004707#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004708// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004709inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004710{
4711 inline namespace string_literals
4712 {
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004713 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004714 basic_string<char> operator "" s( const char *__str, size_t __len )
4715 {
4716 return basic_string<char> (__str, __len);
4717 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004718
Louis Dionne89258142021-08-23 15:32:36 -04004719#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004720 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004721 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4722 {
4723 return basic_string<wchar_t> (__str, __len);
4724 }
Louis Dionne89258142021-08-23 15:32:36 -04004725#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004726
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004727#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004728 inline _LIBCPP_HIDE_FROM_ABI constexpr
Nikolas Klauser5da956d2022-09-02 16:20:28 +02004729 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len)
Marshall Clow8732fed2018-12-11 04:35:44 +00004730 {
4731 return basic_string<char8_t> (__str, __len);
4732 }
4733#endif
4734
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004735 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004736 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4737 {
4738 return basic_string<char16_t> (__str, __len);
4739 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004740
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004741 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004742 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4743 {
4744 return basic_string<char32_t> (__str, __len);
4745 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004746 } // namespace string_literals
4747} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004748
4749#if _LIBCPP_STD_VER > 17
4750template <>
4751inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4752#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4753template <>
4754inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4755#endif
4756#endif
4757
Marshall Clowcba751f2013-07-23 17:05:24 +00004758#endif
4759
Howard Hinnantc51e1022010-05-11 19:42:16 +00004760_LIBCPP_END_NAMESPACE_STD
4761
Eric Fiselierf4433a32017-05-31 22:07:49 +00004762_LIBCPP_POP_MACROS
4763
Mark de Weveree5fe272022-09-02 17:53:28 +02004764#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
4765# include <algorithm>
4766# include <functional>
4767# include <iterator>
4768# include <new>
4769# include <typeinfo>
4770# include <utility>
4771# include <vector>
4772#endif
4773
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004774#endif // _LIBCPP_STRING