blob: 26e55ce444057475f4de412946196812c7e8db01 [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 Klauser35080b42022-07-26 16:13:56 +0200548#include <__memory/swap_allocator.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200549#include <__string/char_traits.h>
550#include <__string/extern_template_lists.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100551#include <__utility/auto_cast.h>
552#include <__utility/move.h>
553#include <__utility/swap.h>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200554#include <__utility/unreachable.h>
555#include <climits>
Louis Dionne44962c32022-06-13 11:21:16 -0400556#include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400557#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400558#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400559#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400560#include <iosfwd>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200561#include <limits>
Vitaly Buka5a807d92022-09-02 19:35:10 -0700562#include <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400564#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000566#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000567
Louis Dionne89258142021-08-23 15:32:36 -0400568#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100569# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400570#endif
571
Nikolas Klausera0e0edb2022-06-16 22:43:46 +0200572// standard-mandated includes
573
574// [iterator.range]
575#include <__iterator/access.h>
576#include <__iterator/data.h>
577#include <__iterator/empty.h>
578#include <__iterator/reverse_access.h>
579#include <__iterator/size.h>
580
581// [string.syn]
582#include <compare>
583#include <initializer_list>
584
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000585#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500586# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000587#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588
Eric Fiselierf4433a32017-05-31 22:07:49 +0000589_LIBCPP_PUSH_MACROS
590#include <__undef_macros>
591
592
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593_LIBCPP_BEGIN_NAMESPACE_STD
594
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595// basic_string
596
597template<class _CharT, class _Traits, class _Allocator>
598basic_string<_CharT, _Traits, _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200599_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant944510a2011-06-14 19:58:17 +0000600operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
601 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602
603template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200604_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000606operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607
608template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200609_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000611operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612
613template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200614inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000616operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617
618template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200619_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000621operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622
Louis Dionnedc496ec2021-06-08 17:25:08 -0400623extern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Shoaib Meenaiea363712017-07-29 02:54:41 +0000624
Marshall Clow039b2f02016-01-13 21:54:34 +0000625template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400626struct __string_is_trivial_iterator : public false_type {};
627
628template <class _Tp>
629struct __string_is_trivial_iterator<_Tp*>
630 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000631
Louis Dionne173f29e2019-05-29 16:01:36 +0000632template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400633struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
634 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000635
Marshall Clow82513342016-09-24 22:45:42 +0000636template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500637struct __can_be_converted_to_string_view : public _BoolConstant<
638 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
639 !is_convertible<const _Tp&, const _CharT*>::value
640 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000641
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400642#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800643typedef basic_string<char8_t> u8string;
644#endif
Richard Smith256954d2020-11-11 17:12:18 -0800645typedef basic_string<char16_t> u16string;
646typedef basic_string<char32_t> u32string;
Richard Smith256954d2020-11-11 17:12:18 -0800647
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200648struct __uninitialized_size_tag {};
649
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000650template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800651class
652 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400653#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800654 _LIBCPP_PREFERRED_NAME(u8string)
655#endif
Richard Smith256954d2020-11-11 17:12:18 -0800656 _LIBCPP_PREFERRED_NAME(u16string)
657 _LIBCPP_PREFERRED_NAME(u32string)
Richard Smith256954d2020-11-11 17:12:18 -0800658 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659{
660public:
661 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000662 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000663 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000664 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000666 typedef allocator_traits<allocator_type> __alloc_traits;
667 typedef typename __alloc_traits::size_type size_type;
668 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000669 typedef value_type& reference;
670 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000671 typedef typename __alloc_traits::pointer pointer;
672 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673
Marshall Clow79f33542018-03-21 00:36:05 +0000674 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
675 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
676 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
677 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000678 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000679 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000680 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000681
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682 typedef __wrap_iter<pointer> iterator;
683 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200684 typedef std::reverse_iterator<iterator> reverse_iterator;
685 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686
687private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200688 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000689
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000690#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000691
692 struct __long
693 {
694 pointer __data_;
695 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200696 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
697 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000698 };
699
Howard Hinnant68bf1812013-04-30 21:44:48 +0000700 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
701 (sizeof(__long) - 1)/sizeof(value_type) : 2};
702
703 struct __short
704 {
705 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200706 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200707 unsigned char __size_ : 7;
708 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000709 };
710
Nikolas Klauser62060be2022-04-20 10:52:04 +0200711// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200712// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200713//
714// If the LSB is used to store the short-flag in the short string representation,
715// we have to multiply the size by two when it is stored and divide it by two when
716// it is loaded to make sure that we always store an even number. In the long string
717// representation, we can ignore this because we can assume that we always allocate
718// an even amount of value_types.
719//
720// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
721// This does not impact the short string representation, since we never need the MSB
722// for representing the size of a short string anyway.
723
724#ifdef _LIBCPP_BIG_ENDIAN
725 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000726#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200727 static const size_type __endian_factor = 1;
728#endif
729
730#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
731
732#ifdef _LIBCPP_BIG_ENDIAN
733 static const size_type __endian_factor = 1;
734#else
735 static const size_type __endian_factor = 2;
736#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000737
Xing Xue38b9fc42022-06-24 17:25:15 -0400738 // Attribute 'packed' is used to keep the layout compatible with the
739 // previous definition that did not use bit fields. This is because on
740 // some platforms bit fields have a default size rather than the actual
741 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742 struct __long
743 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400744 struct _LIBCPP_PACKED {
745 size_type __is_long_ : 1;
746 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
747 };
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 size_type __size_;
749 pointer __data_;
750 };
751
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
753 (sizeof(__long) - 1)/sizeof(value_type) : 2};
754
755 struct __short
756 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400757 struct _LIBCPP_PACKED {
758 unsigned char __is_long_ : 1;
759 unsigned char __size_ : 7;
760 };
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200761 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762 value_type __data_[__min_cap];
763 };
764
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400765#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000766
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200767 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
768
Howard Hinnant8ea98242013-08-23 17:37:05 +0000769 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770
Howard Hinnant8ea98242013-08-23 17:37:05 +0000771 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772
773 struct __raw
774 {
775 size_type __words[__n_words];
776 };
777
778 struct __rep
779 {
780 union
781 {
782 __long __l;
783 __short __s;
784 __raw __r;
785 };
786 };
787
788 __compressed_pair<__rep, allocator_type> __r_;
789
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200790 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
791 // don't initialize the characters. The contents of the string, including the null terminator, must be
792 // initialized separately.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200793 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200794 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200795 : __r_(__default_init_tag(), __a) {
796 if (__size > max_size())
797 __throw_length_error();
798 if (__fits_in_sso(__size)) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +0200799 __r_.first() = __rep();
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200800 __set_short_size(__size);
801 } else {
802 auto __capacity = __recommend(__size) + 1;
803 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200804 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200805 __set_long_cap(__capacity);
806 __set_long_pointer(__allocation);
807 __set_long_size(__size);
808 }
809 std::__debug_db_insert_c(this);
810 }
811
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200813 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 static const size_type npos = -1;
815
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200816 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
Howard Hinnant3e276872011-06-03 18:40:47 +0000817 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000818
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200819 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000820#if _LIBCPP_STD_VER <= 14
821 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
822#else
823 _NOEXCEPT;
824#endif
825
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200826 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
827 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000828
Eric Fiselierfc92be82017-04-19 00:28:44 +0000829#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200830 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +0000831 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000832#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000833 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000834#else
835 _NOEXCEPT;
836#endif
837
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200838 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400840#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000841
Louis Dionne9ce598d2021-09-08 09:14:43 -0400842 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200843 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500844 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000845 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
846 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200847 std::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000848 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000849
Louis Dionne9ce598d2021-09-08 09:14:43 -0400850 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200851 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000852 basic_string(const _CharT* __s, const _Allocator& __a);
853
Marek Kurdej90d79712021-07-27 16:16:21 +0200854#if _LIBCPP_STD_VER > 20
855 basic_string(nullptr_t) = delete;
856#endif
857
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200858 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000859 basic_string(const _CharT* __s, size_type __n);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200860 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000861 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200862 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000863 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000864
Louis Dionne9ce598d2021-09-08 09:14:43 -0400865 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200866 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000867 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
868
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200869 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow83445802016-04-07 18:13:41 +0000870 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000871 const _Allocator& __a = _Allocator());
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200872 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow83445802016-04-07 18:13:41 +0000873 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000874 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000875
Louis Dionne9ce598d2021-09-08 09:14:43 -0400876 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 +0200877 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000878 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500879 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000880
Louis Dionne9ce598d2021-09-08 09:14:43 -0400881 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500882 !__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
Marshall Clowe46031a2018-07-02 18:41:15 +0000884 explicit basic_string(const _Tp& __t);
885
Louis Dionne9ce598d2021-09-08 09:14:43 -0400886 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 +0200887 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000888 explicit basic_string(const _Tp& __t, const allocator_type& __a);
889
Louis Dionne9ce598d2021-09-08 09:14:43 -0400890 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200891 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400893 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200894 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000895 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000896#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200897 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000898 basic_string(initializer_list<_CharT> __il);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200899 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000900 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400901#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200903 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200905 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000906 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
907
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200908 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000909
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200910 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
911 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200912 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200913 __self_view __sv = __t;
914 return assign(__sv);
915 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000916
Eric Fiselierfc92be82017-04-19 00:28:44 +0000917#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200918 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +0000919 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000920 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200921 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierfc92be82017-04-19 00:28:44 +0000922 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200924 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200925 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200926#if _LIBCPP_STD_VER > 20
927 basic_string& operator=(nullptr_t) = delete;
928#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200929 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200931 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000932 iterator begin() _NOEXCEPT
933 {return iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200934 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000935 const_iterator begin() const _NOEXCEPT
936 {return const_iterator(this, __get_pointer());}
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 end() _NOEXCEPT
939 {return iterator(this, __get_pointer() + size());}
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 end() const _NOEXCEPT
942 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -0400943
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200944 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000945 reverse_iterator rbegin() _NOEXCEPT
946 {return reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200947 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000948 const_reverse_iterator rbegin() const _NOEXCEPT
949 {return const_reverse_iterator(end());}
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 rend() _NOEXCEPT
952 {return reverse_iterator(begin());}
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 rend() const _NOEXCEPT
955 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200957 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000958 const_iterator cbegin() const _NOEXCEPT
959 {return begin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200960 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000961 const_iterator cend() const _NOEXCEPT
962 {return end();}
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_reverse_iterator crbegin() const _NOEXCEPT
965 {return rbegin();}
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_reverse_iterator crend() const _NOEXCEPT
968 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200970 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200972 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {return size();}
973 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT;
974 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200975 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
976 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200978 _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
979 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200981 _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100982
983#if _LIBCPP_STD_VER > 20
984 template <class _Op>
985 _LIBCPP_HIDE_FROM_ABI constexpr
986 void resize_and_overwrite(size_type __n, _Op __op) {
987 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200988 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100989 }
990#endif
991
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200992 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +0000993
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200994 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200995 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
996 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200997
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200998 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowb7db4972017-11-15 20:02:27 +0000999 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001001 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001002 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001003 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001005 _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
1006 _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001008 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001009 return append(__str);
1010 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001011
1012 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001013 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001014 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001015 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001016 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1017 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001018 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001019 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001020 operator+=(const _Tp& __t) {
1021 __self_view __sv = __t; return append(__sv);
1022 }
1023
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001024 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001025 return append(__s);
1026 }
1027
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001028 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001029 push_back(__c);
1030 return *this;
1031 }
1032
Eric Fiselierfc92be82017-04-19 00:28:44 +00001033#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001034 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001035 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001036#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001038 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001040
1041 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001042 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001043 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001044 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1045 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001046 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001047 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001048 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001049 _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 +00001050
Marshall Clow62953962016-10-03 23:40:48 +00001051 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001052 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001053 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001054 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001055 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1056 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001057 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001058 >
Marshall Clow82513342016-09-24 22:45:42 +00001059 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001060 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n);
1061 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s);
1062 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001063
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001064 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier451d5582018-11-26 20:15:38 +00001065 void __append_default_init(size_type __n);
1066
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001068 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001069 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001071 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001073 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001074 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001075 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001076 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001077 append(__temp.data(), __temp.size());
1078 return *this;
1079 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001081 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001082 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001084 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001086 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001087 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001088 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001089
Eric Fiselierfc92be82017-04-19 00:28:44 +00001090#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001091 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001093#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001095 _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
1096 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
1097 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT;
1098 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT;
1099 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT;
1100 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101
Marshall Clowe46031a2018-07-02 18:41:15 +00001102 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001103 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001104 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001105 <
1106 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1107 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001108 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001109 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001110 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001111 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001112#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001113 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001114 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001115 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001116 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001117#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001118 _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 +00001119 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001120 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001121 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001122 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001123 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1124 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001125 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001126 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001127 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001128 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n);
1129 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s);
1130 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001132 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001133 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001135 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001137 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138 assign(_InputIterator __first, _InputIterator __last);
1139 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001140 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001141 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001143 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001145 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001147#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001148 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001150#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001152 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001154
1155 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001156 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001157 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001158 <
1159 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1160 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001161 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001162 insert(size_type __pos1, const _Tp& __t)
1163 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1164
Marshall Clow82513342016-09-24 22:45:42 +00001165 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001166 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001167 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001168 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001169 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001170 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001171 >
Marshall Clow82513342016-09-24 22:45:42 +00001172 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001173 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001174 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001175 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1176 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s);
1177 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1178 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
1179 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1181 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001182 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001183 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001185 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001187 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1189 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001190 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001191 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001193 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001195 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001197#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001198 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1200 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001201#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001203 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1204 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 iterator erase(const_iterator __pos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001206 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207 iterator erase(const_iterator __first, const_iterator __last);
1208
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001209 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001211
1212 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001213 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001214 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001215 <
1216 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1217 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001218 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001219 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 +02001220 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001221 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 +00001222 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001223 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001224 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001225 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001226 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001227 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001228 >
Marshall Clow82513342016-09-24 22:45:42 +00001229 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001230 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001231 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001232 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1233 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1234 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001235 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001236
1237 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001238 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001239 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001240 <
1241 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1242 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001243 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001244 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1245
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001246 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001247 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001248 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001249 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001250 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001251 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001253 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001254 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001256 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001258 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001259 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001260#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001261 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001262 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001264#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001266 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
1267 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1269
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001270 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001271 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001272#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001273 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001274#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001275 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001276 __is_nothrow_swappable<allocator_type>::value);
1277#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001279 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001280 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001281 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001282 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001283#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001284 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001285 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001286#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001288 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001289 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001291 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001292 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001293
1294 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001295 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001296 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001297 <
1298 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1299 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001300 >
zoecarver1997e0a2021-02-05 11:54:47 -08001301 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001302 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001303 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001304 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001305 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001306 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001308 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001309 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001310
1311 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001312 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001313 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001314 <
1315 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1316 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001317 >
zoecarver1997e0a2021-02-05 11:54:47 -08001318 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001319 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001320 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001321 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001322 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001323 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001325 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001326 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001327
1328 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001329 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001330 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001331 <
1332 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1333 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001334 >
zoecarver1997e0a2021-02-05 11:54:47 -08001335 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001336 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001337 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001338 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001339 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001340 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001341 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001343 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001344 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001345
1346 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001347 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001348 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001349 <
1350 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1351 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001352 >
zoecarver1997e0a2021-02-05 11:54:47 -08001353 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001354 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001355 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001356 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001357 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001358 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001359 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001361 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001362 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001363
1364 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001365 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001366 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001367 <
1368 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1369 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001370 >
zoecarver1997e0a2021-02-05 11:54:47 -08001371 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001372 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001373 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 +02001374 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001375 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001376 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001377 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1378
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001379 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001380 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001381
1382 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001383 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001384 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001385 <
1386 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1387 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001388 >
zoecarver1997e0a2021-02-05 11:54:47 -08001389 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001390 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001391 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 +02001392 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001393 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001394 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001395 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1396
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001397 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001398 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001399
1400 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001401 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001402 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001403 <
1404 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1405 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001406 >
zoecarver1997e0a2021-02-05 11:54:47 -08001407 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001408
1409 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001410 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001411 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001412 <
1413 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1414 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001415 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001416 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1417
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001418 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001420 _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001421 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1422 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001423
Marshall Clow82513342016-09-24 22:45:42 +00001424 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001425 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001426 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001427 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001428 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001429 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001430 >
Marshall Clow82513342016-09-24 22:45:42 +00001431 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 +02001432 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const value_type* __s) const _NOEXCEPT;
1433 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1434 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001435 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436
Marshall Clow18c293b2017-12-04 20:11:38 +00001437#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001438 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001439 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001440 { return __self_view(data(), size()).starts_with(__sv); }
1441
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001442 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001443 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001444 { return !empty() && _Traits::eq(front(), __c); }
1445
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001446 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001447 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001448 { return starts_with(__self_view(__s)); }
1449
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001450 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001451 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001452 { return __self_view(data(), size()).ends_with( __sv); }
1453
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001454 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001455 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001456 { return !empty() && _Traits::eq(back(), __c); }
1457
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001458 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001459 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001460 { return ends_with(__self_view(__s)); }
1461#endif
1462
Wim Leflere023c3542021-01-19 14:33:30 -05001463#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001464 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001465 bool contains(__self_view __sv) const noexcept
1466 { return __self_view(data(), size()).contains(__sv); }
1467
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001468 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001469 bool contains(value_type __c) const noexcept
1470 { return __self_view(data(), size()).contains(__c); }
1471
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001472 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001473 bool contains(const value_type* __s) const
1474 { return __self_view(data(), size()).contains(__s); }
1475#endif
1476
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001477 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001478
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001479 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001480
Louis Dionne510450b2022-04-01 16:38:30 -04001481#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001482
1483 bool __dereferenceable(const const_iterator* __i) const;
1484 bool __decrementable(const const_iterator* __i) const;
1485 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1486 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1487
Louis Dionne510450b2022-04-01 16:38:30 -04001488#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001489
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001491 template<class _Alloc>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001492 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001493 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1494 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1495
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001496 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001497
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001498 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001499 bool __is_long() const _NOEXCEPT {
1500 if (__libcpp_is_constant_evaluated())
1501 return true;
1502 return __r_.first().__s.__is_long_;
1503 }
1504
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001505 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __begin_lifetime(pointer __begin, size_type __n) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001506#if _LIBCPP_STD_VER > 17
1507 if (__libcpp_is_constant_evaluated()) {
1508 for (size_type __i = 0; __i != __n; ++__i)
1509 std::construct_at(std::addressof(__begin[__i]));
1510 }
1511#else
1512 (void)__begin;
1513 (void)__n;
1514#endif // _LIBCPP_STD_VER > 17
1515 }
1516
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001517 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __default_init() {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001518 __r_.first() = __rep();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001519 if (__libcpp_is_constant_evaluated()) {
1520 size_type __sz = __recommend(0) + 1;
1521 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1522 __begin_lifetime(__ptr, __sz);
1523 __set_long_pointer(__ptr);
1524 __set_long_cap(__sz);
1525 __set_long_size(0);
1526 }
1527 }
1528
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001529 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __deallocate_constexpr() {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001530 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1531 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1532 }
1533
Nikolas Klauser93826d12022-01-04 17:24:03 +01001534 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1535 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1536 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1537 }
1538
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001539 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001540 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001541 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1542 size_type __sz = size();
1543 size_type __cap = capacity();
1544 value_type* __p;
1545 if (__cap - __sz >= __n)
1546 {
1547 __p = std::__to_address(__get_pointer());
1548 size_type __n_move = __sz - __ip;
1549 if (__n_move != 0)
1550 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1551 }
1552 else
1553 {
1554 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1555 __p = std::__to_address(__get_long_pointer());
1556 }
1557 __sz += __n;
1558 __set_size(__sz);
1559 traits_type::assign(__p[__sz], value_type());
1560 for (__p += __ip; __first != __last; ++__p, ++__first)
1561 traits_type::assign(*__p, *__first);
1562
1563 return begin() + __ip;
1564 }
1565
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001566 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001567 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001569 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001570 void __set_short_size(size_type __s) _NOEXCEPT {
1571 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1572 __r_.first().__s.__size_ = __s;
1573 __r_.first().__s.__is_long_ = false;
1574 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001575
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001576 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001577 size_type __get_short_size() const _NOEXCEPT {
1578 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1579 return __r_.first().__s.__size_;
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001583 void __set_long_size(size_type __s) _NOEXCEPT
1584 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001585 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001586 size_type __get_long_size() const _NOEXCEPT
1587 {return __r_.first().__l.__size_;}
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_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1591
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001592 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001593 void __set_long_cap(size_type __s) _NOEXCEPT {
1594 __r_.first().__l.__cap_ = __s / __endian_factor;
1595 __r_.first().__l.__is_long_ = true;
1596 }
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 size_type __get_long_cap() const _NOEXCEPT {
1600 return __r_.first().__l.__cap_ * __endian_factor;
1601 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001603 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001604 void __set_long_pointer(pointer __p) _NOEXCEPT
1605 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001606 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001607 pointer __get_long_pointer() _NOEXCEPT
1608 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001609 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001610 const_pointer __get_long_pointer() const _NOEXCEPT
1611 {return __r_.first().__l.__data_;}
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_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001614 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
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_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001617 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
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_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
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_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1624
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625 template <size_type __a> static
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001626 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea382952013-08-14 18:00:20 +00001627 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001628 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629 enum {__alignment = 16};
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001630 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001631 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001632 {
1633 if (__s < __min_cap) {
1634 if (__libcpp_is_constant_evaluated())
1635 return static_cast<size_type>(__min_cap);
1636 else
1637 return static_cast<size_type>(__min_cap) - 1;
1638 }
Marshall Clow80584522018-02-07 21:30:17 +00001639 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1640 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1641 if (__guess == __min_cap) ++__guess;
1642 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001643 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001645 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001646 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001647 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001648 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001649 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001651
Martijn Vels5e7c9752020-03-04 17:52:46 -05001652 // Slow path for the (inlined) copy constructor for 'long' strings.
1653 // Always externally instantiated and not inlined.
1654 // Requires that __s is zero terminated.
1655 // The main reason for this function to exist is because for unstable, we
1656 // want to allow inlining of the copy constructor. However, we don't want
1657 // to call the __init() functions as those are marked as inline which may
1658 // result in over-aggressive inlining by the compiler, where our aim is
1659 // to only inline the fast path code directly in the ctor.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001660 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001661
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662 template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001663 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001664 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001666 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1667 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668 __init(_InputIterator __first, _InputIterator __last);
1669
1670 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001671 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001672 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001674 __is_cpp17_forward_iterator<_ForwardIterator>::value
1675 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001676 __init(_ForwardIterator __first, _ForwardIterator __last);
1677
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001678 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001680 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001681 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1683 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001684 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685
Martijn Vels596e3de2020-02-26 15:55:49 -05001686 // __assign_no_alias is invoked for assignment operations where we
1687 // have proof that the input does not alias the current instance.
1688 // For example, operator=(basic_string) performs a 'self' check.
1689 template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001690 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001691
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001692 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693 void __erase_to_end(size_type __pos);
1694
Martijn Velsa81fc792020-02-26 13:25:43 -05001695 // __erase_external_with_move is invoked for erase() invocations where
1696 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001697 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001698
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001699 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001700 void __copy_assign_alloc(const basic_string& __str)
1701 {__copy_assign_alloc(__str, integral_constant<bool,
1702 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1703
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001704 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001705 void __copy_assign_alloc(const basic_string& __str, true_type)
1706 {
Marshall Clowf258c202017-01-31 03:40:52 +00001707 if (__alloc() == __str.__alloc())
1708 __alloc() = __str.__alloc();
1709 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001710 {
Marshall Clowf258c202017-01-31 03:40:52 +00001711 if (!__str.__is_long())
1712 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001713 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001714 __alloc() = __str.__alloc();
1715 }
1716 else
1717 {
1718 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001719 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001720 __begin_lifetime(__allocation.ptr, __allocation.count);
Vedant Kumar55e007e2018-03-08 21:15:26 +00001721 __clear_and_shrink();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001722 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001723 __set_long_pointer(__allocation.ptr);
1724 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001725 __set_long_size(__str.size());
1726 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001727 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001728 }
1729
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001730 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001731 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001732 {}
1733
Eric Fiselierfc92be82017-04-19 00:28:44 +00001734#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001735 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001736 void __move_assign(basic_string& __str, false_type)
1737 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001738 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001739 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001740#if _LIBCPP_STD_VER > 14
1741 _NOEXCEPT;
1742#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001743 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001744#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001745#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001746
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001747 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001748 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001749 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001750 _NOEXCEPT_(
1751 !__alloc_traits::propagate_on_container_move_assignment::value ||
1752 is_nothrow_move_assignable<allocator_type>::value)
1753 {__move_assign_alloc(__str, integral_constant<bool,
1754 __alloc_traits::propagate_on_container_move_assignment::value>());}
1755
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001756 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc2734962011-09-02 20:42:31 +00001757 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001758 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1759 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001760 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001761 }
1762
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001763 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001764 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001765 _NOEXCEPT
1766 {}
1767
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001768 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s);
1769 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001770
1771 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1772 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1773 pointer __p = __is_long()
1774 ? (__set_long_size(__n), __get_long_pointer())
1775 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001776 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001777 traits_type::assign(__p[__n], value_type());
1778 return *this;
1779 }
1780
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001781 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001782 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001783 __set_size(__newsz);
1784 __invalidate_iterators_past(__newsz);
1785 traits_type::assign(__p[__newsz], value_type());
1786 return *this;
1787 }
1788
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001789 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001791 template<class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001792 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001793 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001794 // assume that the ranges overlap, because we can't check during constant evaluation
1795 if (__libcpp_is_constant_evaluated())
1796 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001797 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001798 return data() <= __p && __p <= data() + size();
1799 }
1800
Louis Dionned24191c2021-08-19 12:39:16 -04001801 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1802 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001803 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001804 }
1805
1806 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1807 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001808 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001809 }
1810
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001811 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const basic_string&);
1812 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const value_type*, const basic_string&);
1813 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(value_type, const basic_string&);
1814 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const value_type*);
1815 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816};
1817
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001818// These declarations must appear before any functions are implicitly used
1819// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001820#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001821#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001822 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001823# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001824 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001825# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001826#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001827 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001828# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001829 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001830# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001831#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001832#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001833
1834
Louis Dionned59f8a52021-08-17 11:59:07 -04001835#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001836template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001837 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001838 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001839 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1840 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001841 >
1842basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1843 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001844
1845template<class _CharT,
1846 class _Traits,
1847 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001848 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001849 >
1850explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1851 -> basic_string<_CharT, _Traits, _Allocator>;
1852
1853template<class _CharT,
1854 class _Traits,
1855 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001856 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001857 class _Sz = typename allocator_traits<_Allocator>::size_type
1858 >
1859basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1860 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001861#endif
1862
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001864inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001866basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867{
Louis Dionne510450b2022-04-01 16:38:30 -04001868#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001869 if (!__libcpp_is_constant_evaluated()) {
1870 __c_node* __c = __get_db()->__find_c_and_lock(this);
1871 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001873 const_pointer __new_last = __get_pointer() + __pos;
1874 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001876 --__p;
1877 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1878 if (__i->base() > __new_last)
1879 {
1880 (*__p)->__c_ = nullptr;
1881 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001882 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001883 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001885 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 }
1887 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001888#else
1889 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04001890#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891}
1892
1893template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001894inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001895basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001896 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001897 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001899 std::__debug_db_insert_c(this);
1900 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901}
1902
1903template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001904inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001906#if _LIBCPP_STD_VER <= 14
1907 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1908#else
1909 _NOEXCEPT
1910#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001911: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001913 std::__debug_db_insert_c(this);
1914 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915}
1916
1917template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001918_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier815ed732016-09-16 00:00:48 +00001919void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1920 size_type __sz,
1921 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001923 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001924 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001926 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001928 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 {
1930 __set_short_size(__sz);
1931 __p = __get_short_pointer();
1932 }
1933 else
1934 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001935 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1936 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001937 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001939 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 __set_long_size(__sz);
1941 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001942 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943 traits_type::assign(__p[__sz], value_type());
1944}
1945
1946template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001947_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001949basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001951 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001952 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001954 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001956 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001957 {
1958 __set_short_size(__sz);
1959 __p = __get_short_pointer();
1960 }
1961 else
1962 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001963 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1964 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001965 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001967 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968 __set_long_size(__sz);
1969 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001970 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971 traits_type::assign(__p[__sz], value_type());
1972}
1973
1974template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001975template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001976_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00001977basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001978 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001980 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001982 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983}
1984
1985template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001986inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00001987basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001988 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001990 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1991 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001992 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993}
1994
1995template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001996inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00001997basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001998 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002000 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002002 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003}
2004
2005template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002006_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002008 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009{
2010 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002011 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002013 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002014 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002015 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016}
2017
2018template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002019_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002020basic_string<_CharT, _Traits, _Allocator>::basic_string(
2021 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002022 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002025 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002027 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002028 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002029 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030}
2031
Martijn Vels5e7c9752020-03-04 17:52:46 -05002032template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002033_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Vels5e7c9752020-03-04 17:52:46 -05002034void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2035 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002036 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002037 __r_.first() = __rep();
2038
Martijn Vels5e7c9752020-03-04 17:52:46 -05002039 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002040 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002041 __p = __get_short_pointer();
2042 __set_short_size(__sz);
2043 } else {
2044 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002045 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002046 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2047 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002048 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002049 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002050 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002051 __set_long_size(__sz);
2052 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002053 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002054}
2055
Eric Fiselierfc92be82017-04-19 00:28:44 +00002056#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057
2058template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002059inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00002060basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00002061#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00002062 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00002063#else
2064 _NOEXCEPT
2065#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002066 : __r_(std::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002068 __str.__default_init();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002069 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002070 if (__is_long())
2071 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072}
2073
2074template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002075inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002077 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078{
Marshall Clowd2d24692014-07-17 15:32:20 +00002079 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002080 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002081 else
2082 {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002083 if (__libcpp_is_constant_evaluated())
2084 __r_.first() = __rep();
2085 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002086 __str.__default_init();
Marshall Clowd2d24692014-07-17 15:32:20 +00002087 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002088 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002089 if (__is_long())
2090 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091}
2092
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002093#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094
2095template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002096_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097void
2098basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2099{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002100 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002101 __r_.first() = __rep();
2102
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002104 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002106 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107 {
2108 __set_short_size(__n);
2109 __p = __get_short_pointer();
2110 }
2111 else
2112 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002113 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2114 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002115 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002117 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118 __set_long_size(__n);
2119 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002120 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 traits_type::assign(__p[__n], value_type());
2122}
2123
2124template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002125inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002126basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002127 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128{
2129 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002130 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131}
2132
2133template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002134template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002135_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002136basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002137 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138{
2139 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002140 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141}
2142
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002144_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002145basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2146 size_type __pos, size_type __n,
2147 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002148 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149{
2150 size_type __str_sz = __str.size();
2151 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002152 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002153 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2154 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155}
2156
2157template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002158inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow83445802016-04-07 18:13:41 +00002159basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002160 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002161 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002162{
2163 size_type __str_sz = __str.size();
2164 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002165 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002166 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002167 std::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002168}
2169
2170template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002171template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002172_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow78dbe462016-11-14 18:22:19 +00002173basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002174 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002175 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002176{
Marshall Clowe46031a2018-07-02 18:41:15 +00002177 __self_view __sv0 = __t;
2178 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002179 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002180 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002181}
2182
2183template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002184template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002185_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002186basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002187 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002188{
Marshall Clowe46031a2018-07-02 18:41:15 +00002189 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002190 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002191 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002192}
2193
2194template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002195template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002196_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002197basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002198 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002199{
Marshall Clowe46031a2018-07-02 18:41:15 +00002200 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002201 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002202 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002203}
2204
2205template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002207_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002208__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002210 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2211>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2213{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002214 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215#ifndef _LIBCPP_NO_EXCEPTIONS
2216 try
2217 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002218#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219 for (; __first != __last; ++__first)
2220 push_back(*__first);
2221#ifndef _LIBCPP_NO_EXCEPTIONS
2222 }
2223 catch (...)
2224 {
2225 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002226 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227 throw;
2228 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002229#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230}
2231
2232template <class _CharT, class _Traits, class _Allocator>
2233template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002234_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002235__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002237 __is_cpp17_forward_iterator<_ForwardIterator>::value
2238>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2240{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002241 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002242 __r_.first() = __rep();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002243 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002245 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002247 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 {
2249 __set_short_size(__sz);
2250 __p = __get_short_pointer();
2251 }
2252 else
2253 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002254 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2255 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002256 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002258 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259 __set_long_size(__sz);
2260 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002261
2262#ifndef _LIBCPP_NO_EXCEPTIONS
2263 try
2264 {
2265#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002266 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 traits_type::assign(*__p, *__first);
2268 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002269#ifndef _LIBCPP_NO_EXCEPTIONS
2270 }
2271 catch (...)
2272 {
2273 if (__is_long())
2274 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2275 throw;
2276 }
2277#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278}
2279
2280template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002281template<class _InputIterator, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002282inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002284 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285{
2286 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002287 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288}
2289
2290template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002291template<class _InputIterator, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002292inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002293basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2294 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002295 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296{
2297 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002298 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299}
2300
Eric Fiselierfc92be82017-04-19 00:28:44 +00002301#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002302
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002304inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002305basic_string<_CharT, _Traits, _Allocator>::basic_string(
2306 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002307 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308{
2309 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002310 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311}
2312
2313template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002314inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002315basic_string<_CharT, _Traits, _Allocator>::basic_string(
2316 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002317 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318{
2319 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002320 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321}
2322
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002323#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002324
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002326_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2328{
Nikolas Klauser98833542022-05-07 22:20:23 +02002329 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002331 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002332}
2333
2334template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002335_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336void
2337basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2338 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002339 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 +00002340{
2341 size_type __ms = max_size();
2342 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002343 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002344 pointer __old_p = __get_pointer();
2345 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002346 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002348 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2349 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002350 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002351 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002353 traits_type::copy(std::__to_address(__p),
2354 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002356 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002357 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2358 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002359 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2360 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002361 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002362 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002364 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2366 __set_long_size(__old_sz);
2367 traits_type::assign(__p[__old_sz], value_type());
2368}
2369
2370template <class _CharT, class _Traits, class _Allocator>
2371void
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002372_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2374 size_type __n_copy, size_type __n_del, size_type __n_add)
2375{
2376 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002377 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002378 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379 pointer __old_p = __get_pointer();
2380 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002381 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002383 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2384 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002385 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002386 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002388 traits_type::copy(std::__to_address(__p),
2389 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2391 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002392 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2393 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002394 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002395 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2396 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002398 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399}
2400
2401// assign
2402
2403template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002404template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002405_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsb6a08b62020-04-10 18:36:31 -04002406basic_string<_CharT, _Traits, _Allocator>&
2407basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002408 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002409 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002410 if (__n < __cap) {
2411 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2412 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002413 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002414 traits_type::assign(__p[__n], value_type());
2415 __invalidate_iterators_past(__n);
2416 } else {
2417 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2418 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2419 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002420 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002421}
2422
2423template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002424_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002426basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2427 const value_type* __s, size_type __n) {
2428 size_type __cap = capacity();
2429 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002430 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002431 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002432 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002433 } else {
2434 size_type __sz = size();
2435 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002436 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002437 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002438}
2439
2440template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002441_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002442basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002443basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002445 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002446 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002447 ? __assign_short(__s, __n)
2448 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449}
2450
2451template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002452_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453basic_string<_CharT, _Traits, _Allocator>&
2454basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2455{
2456 size_type __cap = capacity();
2457 if (__cap < __n)
2458 {
2459 size_type __sz = size();
2460 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2461 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002462 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002464 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465}
2466
2467template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002468_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469basic_string<_CharT, _Traits, _Allocator>&
2470basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2471{
2472 pointer __p;
2473 if (__is_long())
2474 {
2475 __p = __get_long_pointer();
2476 __set_long_size(1);
2477 }
2478 else
2479 {
2480 __p = __get_short_pointer();
2481 __set_short_size(1);
2482 }
2483 traits_type::assign(*__p, __c);
2484 traits_type::assign(*++__p, value_type());
2485 __invalidate_iterators_past(1);
2486 return *this;
2487}
2488
2489template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002490_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002491basic_string<_CharT, _Traits, _Allocator>&
2492basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2493{
Martijn Vels596e3de2020-02-26 15:55:49 -05002494 if (this != &__str) {
2495 __copy_assign_alloc(__str);
2496 if (!__is_long()) {
2497 if (!__str.__is_long()) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002498 __r_.first() = __str.__r_.first();
Martijn Vels596e3de2020-02-26 15:55:49 -05002499 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002500 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002501 }
2502 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002503 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002504 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002505 }
2506 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002507}
2508
Eric Fiselierfc92be82017-04-19 00:28:44 +00002509#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002510
2511template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002512inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002513void
2514basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002515 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002516{
2517 if (__alloc() != __str.__alloc())
2518 assign(__str);
2519 else
2520 __move_assign(__str, true_type());
2521}
2522
2523template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002524inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002525void
2526basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002527#if _LIBCPP_STD_VER > 14
2528 _NOEXCEPT
2529#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002530 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002531#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002532{
marshall2ed41622019-11-27 07:13:00 -08002533 if (__is_long()) {
2534 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2535 __get_long_cap());
2536#if _LIBCPP_STD_VER <= 14
2537 if (!is_nothrow_move_assignable<allocator_type>::value) {
2538 __set_short_size(0);
2539 traits_type::assign(__get_short_pointer()[0], value_type());
2540 }
2541#endif
2542 }
2543 __move_assign_alloc(__str);
2544 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002545 if (__libcpp_is_constant_evaluated()) {
2546 __str.__default_init();
2547 } else {
2548 __str.__set_short_size(0);
2549 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2550 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002551}
2552
2553template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002554inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002555basic_string<_CharT, _Traits, _Allocator>&
2556basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002557 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002558{
2559 __move_assign(__str, integral_constant<bool,
2560 __alloc_traits::propagate_on_container_move_assignment::value>());
2561 return *this;
2562}
2563
2564#endif
2565
2566template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002568_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002569__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002571 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002573>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2575{
Marshall Clow958362f2016-09-05 01:54:30 +00002576 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002577 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002578 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579}
2580
2581template <class _CharT, class _Traits, class _Allocator>
2582template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002583_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002584__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002586 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002588>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2590{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002592 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002593 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002594
2595 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2596 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002598 if (__cap < __n)
2599 {
2600 size_type __sz = size();
2601 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2602 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002603 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002604 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002605 traits_type::assign(*__p, *__first);
2606 traits_type::assign(*__p, value_type());
2607 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002608 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609 }
2610 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002611 {
2612 const basic_string __temp(__first, __last, __alloc());
2613 assign(__temp.data(), __temp.size());
2614 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615 return *this;
2616}
2617
2618template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002619_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620basic_string<_CharT, _Traits, _Allocator>&
2621basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2622{
2623 size_type __sz = __str.size();
2624 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002625 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002626 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627}
2628
2629template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002630template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002631_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002632__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002633<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002634 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2635 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002636 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002637>
Marshall Clow82513342016-09-24 22:45:42 +00002638basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002639{
Marshall Clow82513342016-09-24 22:45:42 +00002640 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002641 size_type __sz = __sv.size();
2642 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002643 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002644 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002645}
2646
2647
2648template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002649_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002651basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2652 return __assign_external(__s, traits_type::length(__s));
2653}
2654
2655template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002656_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002657basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002658basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002660 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002661 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002662 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002663 ? __assign_short(__s, traits_type::length(__s))
2664 : __assign_external(__s, traits_type::length(__s)))
2665 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667// append
2668
2669template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002670_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002672basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002674 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675 size_type __cap = capacity();
2676 size_type __sz = size();
2677 if (__cap - __sz >= __n)
2678 {
2679 if (__n)
2680 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002681 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 traits_type::copy(__p + __sz, __s, __n);
2683 __sz += __n;
2684 __set_size(__sz);
2685 traits_type::assign(__p[__sz], value_type());
2686 }
2687 }
2688 else
2689 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2690 return *this;
2691}
2692
2693template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002694_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695basic_string<_CharT, _Traits, _Allocator>&
2696basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2697{
2698 if (__n)
2699 {
2700 size_type __cap = capacity();
2701 size_type __sz = size();
2702 if (__cap - __sz < __n)
2703 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2704 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002705 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 __sz += __n;
2707 __set_size(__sz);
2708 traits_type::assign(__p[__sz], value_type());
2709 }
2710 return *this;
2711}
2712
2713template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002714_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002715basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2716{
2717 if (__n)
2718 {
2719 size_type __cap = capacity();
2720 size_type __sz = size();
2721 if (__cap - __sz < __n)
2722 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2723 pointer __p = __get_pointer();
2724 __sz += __n;
2725 __set_size(__sz);
2726 traits_type::assign(__p[__sz], value_type());
2727 }
2728}
2729
2730template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002731_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732void
2733basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2734{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002735 bool __is_short = !__is_long();
2736 size_type __cap;
2737 size_type __sz;
2738 if (__is_short)
2739 {
2740 __cap = __min_cap - 1;
2741 __sz = __get_short_size();
2742 }
2743 else
2744 {
2745 __cap = __get_long_cap() - 1;
2746 __sz = __get_long_size();
2747 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002749 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002751 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002752 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002753 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002754 if (__is_short)
2755 {
2756 __p = __get_short_pointer() + __sz;
2757 __set_short_size(__sz+1);
2758 }
2759 else
2760 {
2761 __p = __get_long_pointer() + __sz;
2762 __set_long_size(__sz+1);
2763 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 traits_type::assign(*__p, __c);
2765 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766}
2767
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768template <class _CharT, class _Traits, class _Allocator>
2769template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002770_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002771__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002772<
2773 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2774 basic_string<_CharT, _Traits, _Allocator>&
2775>
2776basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002777 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778{
2779 size_type __sz = size();
2780 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002781 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 if (__n)
2783 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002784 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2785 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002786 {
2787 if (__cap - __sz < __n)
2788 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2789 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002790 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002791 traits_type::assign(*__p, *__first);
2792 traits_type::assign(*__p, value_type());
2793 __set_size(__sz + __n);
2794 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002795 else
2796 {
2797 const basic_string __temp(__first, __last, __alloc());
2798 append(__temp.data(), __temp.size());
2799 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800 }
2801 return *this;
2802}
2803
2804template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002805inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806basic_string<_CharT, _Traits, _Allocator>&
2807basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2808{
2809 return append(__str.data(), __str.size());
2810}
2811
2812template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002813_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814basic_string<_CharT, _Traits, _Allocator>&
2815basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2816{
2817 size_type __sz = __str.size();
2818 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002819 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002820 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821}
2822
2823template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002824template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002825_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002826 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002827 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002828 __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 +00002829 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002830 >
Marshall Clow82513342016-09-24 22:45:42 +00002831basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002832{
Marshall Clow82513342016-09-24 22:45:42 +00002833 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002834 size_type __sz = __sv.size();
2835 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002836 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002837 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002838}
2839
2840template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002841_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002843basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002845 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846 return append(__s, traits_type::length(__s));
2847}
2848
2849// insert
2850
2851template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002852_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002854basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002856 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 size_type __sz = size();
2858 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002859 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002861 if (__libcpp_is_constant_evaluated()) {
2862 if (__cap - __sz >= __n)
2863 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2864 else
2865 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2866 return *this;
2867 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868 if (__cap - __sz >= __n)
2869 {
2870 if (__n)
2871 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002872 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 size_type __n_move = __sz - __pos;
2874 if (__n_move != 0)
2875 {
2876 if (__p + __pos <= __s && __s < __p + __sz)
2877 __s += __n;
2878 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2879 }
2880 traits_type::move(__p + __pos, __s, __n);
2881 __sz += __n;
2882 __set_size(__sz);
2883 traits_type::assign(__p[__sz], value_type());
2884 }
2885 }
2886 else
2887 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2888 return *this;
2889}
2890
2891template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002892_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893basic_string<_CharT, _Traits, _Allocator>&
2894basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2895{
2896 size_type __sz = size();
2897 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002898 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899 if (__n)
2900 {
2901 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002902 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903 if (__cap - __sz >= __n)
2904 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002905 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 size_type __n_move = __sz - __pos;
2907 if (__n_move != 0)
2908 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2909 }
2910 else
2911 {
2912 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002913 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914 }
2915 traits_type::assign(__p + __pos, __n, __c);
2916 __sz += __n;
2917 __set_size(__sz);
2918 traits_type::assign(__p[__sz], value_type());
2919 }
2920 return *this;
2921}
2922
2923template <class _CharT, class _Traits, class _Allocator>
2924template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002925_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002926__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002928 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002929 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002930>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2932{
Nikolas Klausereed25832021-12-15 01:32:30 +01002933 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2934 "string::insert(iterator, range) called with an iterator not"
2935 " referring to this string");
2936 const basic_string __temp(__first, __last, __alloc());
2937 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938}
2939
2940template <class _CharT, class _Traits, class _Allocator>
2941template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002942_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002943__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002945 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002946 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002947>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2949{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002950 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2951 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002952
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002954 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2955 if (__n == 0)
2956 return begin() + __ip;
2957
2958 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002960 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002962 else
2963 {
2964 const basic_string __temp(__first, __last, __alloc());
2965 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2966 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967}
2968
2969template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002970inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971basic_string<_CharT, _Traits, _Allocator>&
2972basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2973{
2974 return insert(__pos1, __str.data(), __str.size());
2975}
2976
2977template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002978_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979basic_string<_CharT, _Traits, _Allocator>&
2980basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2981 size_type __pos2, size_type __n)
2982{
2983 size_type __str_sz = __str.size();
2984 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002985 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002986 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987}
2988
2989template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002990template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002991_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002992__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002993<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002994 __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 +00002995 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002996>
Marshall Clow82513342016-09-24 22:45:42 +00002997basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002998 size_type __pos2, size_type __n)
2999{
Marshall Clow82513342016-09-24 22:45:42 +00003000 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003001 size_type __str_sz = __sv.size();
3002 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003003 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003004 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003005}
3006
3007template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003008_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003010basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003012 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013 return insert(__pos, __s, traits_type::length(__s));
3014}
3015
3016template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003017_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018typename basic_string<_CharT, _Traits, _Allocator>::iterator
3019basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
3020{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05003021 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3022 "string::insert(iterator, character) called with an iterator not"
3023 " referring to this string");
3024
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025 size_type __ip = static_cast<size_type>(__pos - begin());
3026 size_type __sz = size();
3027 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003028 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029 if (__cap == __sz)
3030 {
3031 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003032 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033 }
3034 else
3035 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003036 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037 size_type __n_move = __sz - __ip;
3038 if (__n_move != 0)
3039 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3040 }
3041 traits_type::assign(__p[__ip], __c);
3042 traits_type::assign(__p[++__sz], value_type());
3043 __set_size(__sz);
3044 return begin() + static_cast<difference_type>(__ip);
3045}
3046
3047template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003048inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049typename basic_string<_CharT, _Traits, _Allocator>::iterator
3050basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3051{
Nikolas Klausereed25832021-12-15 01:32:30 +01003052 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3053 "string::insert(iterator, n, value) called with an iterator not"
3054 " referring to this string");
3055 difference_type __p = __pos - begin();
3056 insert(static_cast<size_type>(__p), __n, __c);
3057 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058}
3059
3060// replace
3061
3062template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003063_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003065basic_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 +00003066 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003068 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069 size_type __sz = size();
3070 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003071 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003072 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 size_type __cap = capacity();
3074 if (__cap - __sz + __n1 >= __n2)
3075 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003076 if (__libcpp_is_constant_evaluated()) {
3077 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3078 return *this;
3079 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003080 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081 if (__n1 != __n2)
3082 {
3083 size_type __n_move = __sz - __pos - __n1;
3084 if (__n_move != 0)
3085 {
3086 if (__n1 > __n2)
3087 {
3088 traits_type::move(__p + __pos, __s, __n2);
3089 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003090 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 }
3092 if (__p + __pos < __s && __s < __p + __sz)
3093 {
3094 if (__p + __pos + __n1 <= __s)
3095 __s += __n2 - __n1;
3096 else // __p + __pos < __s < __p + __pos + __n1
3097 {
3098 traits_type::move(__p + __pos, __s, __n1);
3099 __pos += __n1;
3100 __s += __n2;
3101 __n2 -= __n1;
3102 __n1 = 0;
3103 }
3104 }
3105 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3106 }
3107 }
3108 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003109 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 }
3111 else
3112 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3113 return *this;
3114}
3115
3116template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003117_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118basic_string<_CharT, _Traits, _Allocator>&
3119basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3120{
3121 size_type __sz = size();
3122 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003123 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003124 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003126 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127 if (__cap - __sz + __n1 >= __n2)
3128 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003129 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 if (__n1 != __n2)
3131 {
3132 size_type __n_move = __sz - __pos - __n1;
3133 if (__n_move != 0)
3134 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3135 }
3136 }
3137 else
3138 {
3139 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003140 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141 }
3142 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003143 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144}
3145
3146template <class _CharT, class _Traits, class _Allocator>
3147template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003148_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003149__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003151 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003153>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003154basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155 _InputIterator __j1, _InputIterator __j2)
3156{
Marshall Clow958362f2016-09-05 01:54:30 +00003157 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003158 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159}
3160
3161template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003162inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163basic_string<_CharT, _Traits, _Allocator>&
3164basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3165{
3166 return replace(__pos1, __n1, __str.data(), __str.size());
3167}
3168
3169template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003170_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171basic_string<_CharT, _Traits, _Allocator>&
3172basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3173 size_type __pos2, size_type __n2)
3174{
3175 size_type __str_sz = __str.size();
3176 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003177 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003178 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179}
3180
3181template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003182template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003183_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003184__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003185<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003186 __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 +00003187 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003188>
Marshall Clow82513342016-09-24 22:45:42 +00003189basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003190 size_type __pos2, size_type __n2)
3191{
Marshall Clow82513342016-09-24 22:45:42 +00003192 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003193 size_type __str_sz = __sv.size();
3194 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003195 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003196 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003197}
3198
3199template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003200_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003201basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003202basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003204 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205 return replace(__pos, __n1, __s, traits_type::length(__s));
3206}
3207
3208template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003209inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003211basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212{
3213 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3214 __str.data(), __str.size());
3215}
3216
3217template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003218inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003220basic_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 +00003221{
3222 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3223}
3224
3225template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003226inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003228basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229{
3230 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3231}
3232
3233template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003234inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003236basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237{
3238 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3239}
3240
3241// erase
3242
Martijn Velsa81fc792020-02-26 13:25:43 -05003243// 'externally instantiated' erase() implementation, called when __n != npos.
3244// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003246_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003247void
3248basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3249 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251 if (__n)
3252 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003253 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003254 value_type* __p = std::__to_address(__get_pointer());
3255 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003256 size_type __n_move = __sz - __pos - __n;
3257 if (__n_move != 0)
3258 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003259 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003261}
3262
3263template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003264_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003265basic_string<_CharT, _Traits, _Allocator>&
3266basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3267 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003268 if (__pos > size())
3269 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003270 if (__n == npos) {
3271 __erase_to_end(__pos);
3272 } else {
3273 __erase_external_with_move(__pos, __n);
3274 }
3275 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276}
3277
3278template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003279inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280typename basic_string<_CharT, _Traits, _Allocator>::iterator
3281basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3282{
Nikolas Klausereed25832021-12-15 01:32:30 +01003283 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3284 "string::erase(iterator) called with an iterator not"
3285 " referring to this string");
3286
3287 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3288 iterator __b = begin();
3289 size_type __r = static_cast<size_type>(__pos - __b);
3290 erase(__r, 1);
3291 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003292}
3293
3294template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003295inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296typename basic_string<_CharT, _Traits, _Allocator>::iterator
3297basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3298{
Nikolas Klausereed25832021-12-15 01:32:30 +01003299 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3300 "string::erase(iterator, iterator) called with an iterator not"
3301 " referring to this string");
3302
3303 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3304 iterator __b = begin();
3305 size_type __r = static_cast<size_type>(__first - __b);
3306 erase(__r, static_cast<size_type>(__last - __first));
3307 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308}
3309
3310template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003311inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312void
3313basic_string<_CharT, _Traits, _Allocator>::pop_back()
3314{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003315 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003316 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003317}
3318
3319template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003320inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003321void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003322basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003324 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325 if (__is_long())
3326 {
3327 traits_type::assign(*__get_long_pointer(), value_type());
3328 __set_long_size(0);
3329 }
3330 else
3331 {
3332 traits_type::assign(*__get_short_pointer(), value_type());
3333 __set_short_size(0);
3334 }
3335}
3336
3337template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003338inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339void
3340basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3341{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003342 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343}
3344
3345template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003346_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347void
3348basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3349{
3350 size_type __sz = size();
3351 if (__n > __sz)
3352 append(__n - __sz, __c);
3353 else
3354 __erase_to_end(__n);
3355}
3356
3357template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003358_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003359basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3360{
3361 size_type __sz = size();
3362 if (__n > __sz) {
3363 __append_default_init(__n - __sz);
3364 } else
3365 __erase_to_end(__n);
3366}
3367
3368template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003369inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003371basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003373 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003374 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3375 return __m - __alignment;
3376 } else {
3377 bool __uses_lsb = __endian_factor == 2;
3378 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3379 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380}
3381
3382template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003383_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384void
Marek Kurdejc9848142020-11-26 10:07:16 +01003385basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386{
Marek Kurdejc9848142020-11-26 10:07:16 +01003387 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003388 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003389
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003390 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3391 // and later (since P0966R1), however we provide consistent behavior in all Standard
3392 // modes because this function is instantiated in the shared library.
3393 if (__requested_capacity <= capacity())
3394 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003395
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003396 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003397 __target_capacity = __recommend(__target_capacity);
3398 if (__target_capacity == capacity()) return;
3399
3400 __shrink_or_extend(__target_capacity);
3401}
3402
3403template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003404inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003405void
3406basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3407{
3408 size_type __target_capacity = __recommend(size());
3409 if (__target_capacity == capacity()) return;
3410
3411 __shrink_or_extend(__target_capacity);
3412}
3413
3414template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003415inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003416void
3417basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3418{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003419 size_type __cap = capacity();
3420 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003421
3422 pointer __new_data, __p;
3423 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003424 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003426 __was_long = true;
3427 __now_long = false;
3428 __new_data = __get_short_pointer();
3429 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003431 else
3432 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003433 if (__target_capacity > __cap) {
3434 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3435 __new_data = __allocation.ptr;
3436 __target_capacity = __allocation.count - 1;
3437 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003438 else
3439 {
3440 #ifndef _LIBCPP_NO_EXCEPTIONS
3441 try
3442 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003443 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003444 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3445 __new_data = __allocation.ptr;
3446 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003447 #ifndef _LIBCPP_NO_EXCEPTIONS
3448 }
3449 catch (...)
3450 {
3451 return;
3452 }
3453 #else // _LIBCPP_NO_EXCEPTIONS
3454 if (__new_data == nullptr)
3455 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003456 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003457 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003458 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003459 __now_long = true;
3460 __was_long = __is_long();
3461 __p = __get_pointer();
3462 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003463 traits_type::copy(std::__to_address(__new_data),
3464 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003465 if (__was_long)
3466 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3467 if (__now_long)
3468 {
3469 __set_long_cap(__target_capacity+1);
3470 __set_long_size(__sz);
3471 __set_long_pointer(__new_data);
3472 }
3473 else
3474 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003475 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476}
3477
3478template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003479inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003481basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003483 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484 return *(data() + __pos);
3485}
3486
3487template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003488inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003490basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003492 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493 return *(__get_pointer() + __pos);
3494}
3495
3496template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003497_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3499basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3500{
3501 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003502 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503 return (*this)[__n];
3504}
3505
3506template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003507_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508typename basic_string<_CharT, _Traits, _Allocator>::reference
3509basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3510{
3511 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003512 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513 return (*this)[__n];
3514}
3515
3516template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003517inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003519basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003521 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522 return *__get_pointer();
3523}
3524
3525template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003526inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003528basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003530 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531 return *data();
3532}
3533
3534template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003535inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003537basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003539 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540 return *(__get_pointer() + size() - 1);
3541}
3542
3543template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003544inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003546basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003548 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549 return *(data() + size() - 1);
3550}
3551
3552template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003553_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003555basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556{
3557 size_type __sz = size();
3558 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003559 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003560 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561 traits_type::copy(__s, data() + __pos, __rlen);
3562 return __rlen;
3563}
3564
3565template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003566inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567basic_string<_CharT, _Traits, _Allocator>
3568basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3569{
3570 return basic_string(*this, __pos, __n, __alloc());
3571}
3572
3573template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003574inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575void
3576basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003577#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003578 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003579#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003580 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003581 __is_nothrow_swappable<allocator_type>::value)
3582#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583{
Nikolas Klauser98833542022-05-07 22:20:23 +02003584 if (!__is_long())
3585 std::__debug_db_invalidate_all(this);
3586 if (!__str.__is_long())
3587 std::__debug_db_invalidate_all(&__str);
3588 std::__debug_db_swap(this, &__str);
3589
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003590 _LIBCPP_ASSERT(
3591 __alloc_traits::propagate_on_container_swap::value ||
3592 __alloc_traits::is_always_equal::value ||
3593 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003594 std::swap(__r_.first(), __str.__r_.first());
3595 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003596}
3597
3598// find
3599
3600template <class _Traits>
3601struct _LIBCPP_HIDDEN __traits_eq
3602{
3603 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003604 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003605 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3606 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607};
3608
3609template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003610_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003612basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003613 size_type __pos,
3614 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003616 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003617 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003618 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619}
3620
3621template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003622inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003624basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3625 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003627 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003628 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629}
3630
3631template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003632template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003633_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003634__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003635<
3636 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3637 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003638>
Marshall Clowe46031a2018-07-02 18:41:15 +00003639basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003640 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003641{
Marshall Clowe46031a2018-07-02 18:41:15 +00003642 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003643 return __str_find<value_type, size_type, traits_type, npos>
3644 (data(), size(), __sv.data(), __pos, __sv.size());
3645}
3646
3647template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003648inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003649typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003650basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003651 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003653 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003654 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003655 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656}
3657
3658template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003659_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003661basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3662 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003664 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003665 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666}
3667
3668// rfind
3669
3670template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003671_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003673basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003674 size_type __pos,
3675 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003676{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003677 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003678 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003679 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680}
3681
3682template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003683inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003685basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3686 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003688 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003689 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690}
3691
3692template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003693template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003694_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003695__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003696<
3697 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3698 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003699>
Marshall Clowe46031a2018-07-02 18:41:15 +00003700basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003701 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702{
Marshall Clowe46031a2018-07-02 18:41:15 +00003703 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003704 return __str_rfind<value_type, size_type, traits_type, npos>
3705 (data(), size(), __sv.data(), __pos, __sv.size());
3706}
3707
3708template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003709inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003710typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003711basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003712 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003714 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003715 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003716 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717}
3718
3719template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003720_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003722basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3723 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003725 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003726 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727}
3728
3729// find_first_of
3730
3731template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003732_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003734basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003735 size_type __pos,
3736 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003738 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003739 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003740 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003741}
3742
3743template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003744inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003745typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003746basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3747 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003749 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003750 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751}
3752
3753template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003754template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003755_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003756__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003757<
3758 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3759 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003760>
Marshall Clowe46031a2018-07-02 18:41:15 +00003761basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003762 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003763{
Marshall Clowe46031a2018-07-02 18:41:15 +00003764 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003765 return __str_find_first_of<value_type, size_type, traits_type, npos>
3766 (data(), size(), __sv.data(), __pos, __sv.size());
3767}
3768
3769template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003770inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003771typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003772basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003773 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003775 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003776 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003777 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778}
3779
3780template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003781inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003782typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003783basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3784 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785{
3786 return find(__c, __pos);
3787}
3788
3789// find_last_of
3790
3791template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003792inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003793typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003794basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003795 size_type __pos,
3796 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003798 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003799 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003800 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801}
3802
3803template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003804inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003805typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003806basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3807 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003808{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003809 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003810 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811}
3812
3813template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003814template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003815_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003816__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003817<
3818 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3819 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003820>
Marshall Clowe46031a2018-07-02 18:41:15 +00003821basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003822 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003823{
Marshall Clowe46031a2018-07-02 18:41:15 +00003824 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003825 return __str_find_last_of<value_type, size_type, traits_type, npos>
3826 (data(), size(), __sv.data(), __pos, __sv.size());
3827}
3828
3829template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003830inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003831typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003832basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003833 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003835 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003836 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003837 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838}
3839
3840template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003841inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003843basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3844 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845{
3846 return rfind(__c, __pos);
3847}
3848
3849// find_first_not_of
3850
3851template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003852_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003854basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003855 size_type __pos,
3856 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003858 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003860 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861}
3862
3863template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003864inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003866basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3867 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003868{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003869 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003870 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003871}
3872
3873template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003874template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003875_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003876__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003877<
3878 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3879 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003880>
Marshall Clowe46031a2018-07-02 18:41:15 +00003881basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003882 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003883{
Marshall Clowe46031a2018-07-02 18:41:15 +00003884 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003885 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3886 (data(), size(), __sv.data(), __pos, __sv.size());
3887}
3888
3889template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003890inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003891typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003892basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003893 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003895 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003896 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003897 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898}
3899
3900template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003901inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003903basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3904 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003906 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003907 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908}
3909
3910// find_last_not_of
3911
3912template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003913_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003914typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003915basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003916 size_type __pos,
3917 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003919 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003920 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003921 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003922}
3923
3924template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003925inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003927basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3928 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003930 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003931 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932}
3933
3934template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003935template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003936_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003937__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003938<
3939 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3940 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003941>
Marshall Clowe46031a2018-07-02 18:41:15 +00003942basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003943 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003944{
Marshall Clowe46031a2018-07-02 18:41:15 +00003945 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003946 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3947 (data(), size(), __sv.data(), __pos, __sv.size());
3948}
3949
3950template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003951inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003952typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003953basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003954 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003956 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003957 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003958 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959}
3960
3961template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003962inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003964basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3965 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003967 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003968 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969}
3970
3971// compare
3972
3973template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003974template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003975_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003976__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003977<
3978 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3979 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003980>
zoecarver1997e0a2021-02-05 11:54:47 -08003981basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003982{
Marshall Clowe46031a2018-07-02 18:41:15 +00003983 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003984 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003985 size_t __rhs_sz = __sv.size();
3986 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003987 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003988 if (__result != 0)
3989 return __result;
3990 if (__lhs_sz < __rhs_sz)
3991 return -1;
3992 if (__lhs_sz > __rhs_sz)
3993 return 1;
3994 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995}
3996
3997template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003998inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004000basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004001{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004002 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003}
4004
4005template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004006inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004008basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4009 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00004010 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004011 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012{
Alp Tokerb8a95f52014-05-15 11:27:39 +00004013 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014 size_type __sz = size();
4015 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004016 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004017 size_type __rlen = std::min(__n1, __sz - __pos1);
4018 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019 if (__r == 0)
4020 {
4021 if (__rlen < __n2)
4022 __r = -1;
4023 else if (__rlen > __n2)
4024 __r = 1;
4025 }
4026 return __r;
4027}
4028
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004029template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00004030template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004031_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04004032__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00004033<
4034 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
4035 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004036>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004037basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4038 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00004039 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004040{
Marshall Clowe46031a2018-07-02 18:41:15 +00004041 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004042 return compare(__pos1, __n1, __sv.data(), __sv.size());
4043}
4044
4045template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004046inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004047int
4048basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4049 size_type __n1,
4050 const basic_string& __str) const
4051{
4052 return compare(__pos1, __n1, __str.data(), __str.size());
4053}
4054
4055template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00004056template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004057_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04004058__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00004059<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004060 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
4061 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00004062 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004063>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004064basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4065 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00004066 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004067 size_type __pos2,
4068 size_type __n2) const
4069{
Marshall Clow82513342016-09-24 22:45:42 +00004070 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004071 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
4072}
4073
4074template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004075_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004076int
4077basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4078 size_type __n1,
4079 const basic_string& __str,
4080 size_type __pos2,
4081 size_type __n2) const
4082{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004083 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004084}
4085
4086template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004087_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004088int
4089basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4090{
4091 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4092 return compare(0, npos, __s, traits_type::length(__s));
4093}
4094
4095template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004096_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004097int
4098basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4099 size_type __n1,
4100 const value_type* __s) const
4101{
4102 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4103 return compare(__pos1, __n1, __s, traits_type::length(__s));
4104}
4105
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106// __invariants
4107
4108template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004109inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110bool
4111basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4112{
4113 if (size() > capacity())
4114 return false;
4115 if (capacity() < __min_cap - 1)
4116 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004117 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004119 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120 return false;
4121 return true;
4122}
4123
Vedant Kumar55e007e2018-03-08 21:15:26 +00004124// __clear_and_shrink
4125
4126template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004127inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne173f29e2019-05-29 16:01:36 +00004128void
Marshall Clowe60a7182018-05-29 17:04:37 +00004129basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004130{
4131 clear();
4132 if(__is_long())
4133 {
4134 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4135 __set_long_cap(0);
4136 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004137 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004138 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004139}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004140
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141// operator==
4142
4143template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004144inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145bool
4146operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004147 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004148{
Mark de Weverb4830e62022-07-19 07:56:23 +02004149#if _LIBCPP_STD_VER > 17
4150 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4151#else
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004152 size_t __lhs_sz = __lhs.size();
4153 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4154 __rhs.data(),
4155 __lhs_sz) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004156#endif
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004157}
4158
4159template<class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004160inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004161bool
4162operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4163 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4164{
4165 size_t __lhs_sz = __lhs.size();
4166 if (__lhs_sz != __rhs.size())
4167 return false;
4168 const char* __lp = __lhs.data();
4169 const char* __rp = __rhs.data();
4170 if (__lhs.__is_long())
4171 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4172 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4173 if (*__lp != *__rp)
4174 return false;
4175 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176}
4177
Mark de Weverb4830e62022-07-19 07:56:23 +02004178#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004180inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004182operator==(const _CharT* __lhs,
4183 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004185 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4186 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4187 size_t __lhs_len = _Traits::length(__lhs);
4188 if (__lhs_len != __rhs.size()) return false;
4189 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190}
Mark de Weverb4830e62022-07-19 07:56:23 +02004191#endif // _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004194inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004196operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4197 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198{
Mark de Weverb4830e62022-07-19 07:56:23 +02004199#if _LIBCPP_STD_VER > 17
4200 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4201#else
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004202 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4203 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4204 size_t __rhs_len = _Traits::length(__rhs);
4205 if (__rhs_len != __lhs.size()) return false;
4206 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004207#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208}
4209
Mark de Weverb4830e62022-07-19 07:56:23 +02004210#if _LIBCPP_STD_VER > 17
4211
4212template <class _CharT, class _Traits, class _Allocator>
4213_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
4214 const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4215 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
4216 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4217}
4218
4219template <class _CharT, class _Traits, class _Allocator>
4220_LIBCPP_HIDE_FROM_ABI constexpr auto
4221operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
4222 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4223}
4224
4225#else // _LIBCPP_STD_VER > 17
4226
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004227template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004228inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229bool
4230operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004231 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004232{
4233 return !(__lhs == __rhs);
4234}
4235
4236template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004237inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004239operator!=(const _CharT* __lhs,
4240 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241{
4242 return !(__lhs == __rhs);
4243}
4244
4245template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004246inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004248operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4249 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004250{
4251 return !(__lhs == __rhs);
4252}
4253
4254// operator<
4255
4256template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004257inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258bool
4259operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004260 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004262 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263}
4264
4265template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004266inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004268operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4269 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004271 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272}
4273
4274template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004275inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004277operator< (const _CharT* __lhs,
4278 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279{
4280 return __rhs.compare(__lhs) > 0;
4281}
4282
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283// operator>
4284
4285template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004286inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287bool
4288operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004289 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290{
4291 return __rhs < __lhs;
4292}
4293
4294template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004295inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004297operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4298 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299{
4300 return __rhs < __lhs;
4301}
4302
4303template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004304inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004306operator> (const _CharT* __lhs,
4307 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308{
4309 return __rhs < __lhs;
4310}
4311
4312// operator<=
4313
4314template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004315inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004316bool
4317operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004318 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004319{
4320 return !(__rhs < __lhs);
4321}
4322
4323template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004324inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004326operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4327 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004328{
4329 return !(__rhs < __lhs);
4330}
4331
4332template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004333inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004335operator<=(const _CharT* __lhs,
4336 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004337{
4338 return !(__rhs < __lhs);
4339}
4340
4341// operator>=
4342
4343template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004344inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345bool
4346operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004347 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348{
4349 return !(__lhs < __rhs);
4350}
4351
4352template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004353inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004355operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4356 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357{
4358 return !(__lhs < __rhs);
4359}
4360
4361template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004362inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004364operator>=(const _CharT* __lhs,
4365 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004366{
4367 return !(__lhs < __rhs);
4368}
Mark de Weverb4830e62022-07-19 07:56:23 +02004369#endif // _LIBCPP_STD_VER > 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004370
4371// operator +
4372
4373template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004374_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004375basic_string<_CharT, _Traits, _Allocator>
4376operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4377 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4378{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004379 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004380 auto __lhs_sz = __lhs.size();
4381 auto __rhs_sz = __rhs.size();
4382 _String __r(__uninitialized_size_tag(),
4383 __lhs_sz + __rhs_sz,
4384 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4385 auto __ptr = std::__to_address(__r.__get_pointer());
4386 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4387 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4388 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004389 return __r;
4390}
4391
4392template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004393_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394basic_string<_CharT, _Traits, _Allocator>
4395operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4396{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004397 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004398 auto __lhs_sz = _Traits::length(__lhs);
4399 auto __rhs_sz = __rhs.size();
4400 _String __r(__uninitialized_size_tag(),
4401 __lhs_sz + __rhs_sz,
4402 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4403 auto __ptr = std::__to_address(__r.__get_pointer());
4404 _Traits::copy(__ptr, __lhs, __lhs_sz);
4405 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4406 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004407 return __r;
4408}
4409
4410template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004411_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004412basic_string<_CharT, _Traits, _Allocator>
4413operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4414{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004415 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004416 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004417 _String __r(__uninitialized_size_tag(),
4418 __rhs_sz + 1,
4419 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4420 auto __ptr = std::__to_address(__r.__get_pointer());
4421 _Traits::assign(__ptr, 1, __lhs);
4422 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4423 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424 return __r;
4425}
4426
4427template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004428inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004429basic_string<_CharT, _Traits, _Allocator>
4430operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4431{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004432 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004433 typename _String::size_type __lhs_sz = __lhs.size();
4434 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004435 _String __r(__uninitialized_size_tag(),
4436 __lhs_sz + __rhs_sz,
4437 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4438 auto __ptr = std::__to_address(__r.__get_pointer());
4439 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4440 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4441 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442 return __r;
4443}
4444
4445template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004446_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004447basic_string<_CharT, _Traits, _Allocator>
4448operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4449{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004450 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004451 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004452 _String __r(__uninitialized_size_tag(),
4453 __lhs_sz + 1,
4454 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4455 auto __ptr = std::__to_address(__r.__get_pointer());
4456 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4457 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4458 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459 return __r;
4460}
4461
Eric Fiselierfc92be82017-04-19 00:28:44 +00004462#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004463
4464template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004465inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466basic_string<_CharT, _Traits, _Allocator>
4467operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4468{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004469 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004470}
4471
4472template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004473inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004474basic_string<_CharT, _Traits, _Allocator>
4475operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4476{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004477 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004478}
4479
4480template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004481inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004482basic_string<_CharT, _Traits, _Allocator>
4483operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4484{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004485 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486}
4487
4488template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004489inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490basic_string<_CharT, _Traits, _Allocator>
4491operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4492{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004493 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004494}
4495
4496template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004497inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004498basic_string<_CharT, _Traits, _Allocator>
4499operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4500{
4501 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004502 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503}
4504
4505template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004506inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507basic_string<_CharT, _Traits, _Allocator>
4508operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4509{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004510 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004511}
4512
4513template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004514inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004515basic_string<_CharT, _Traits, _Allocator>
4516operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4517{
4518 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004519 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004520}
4521
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004522#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523
4524// swap
4525
4526template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004527inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004529swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004530 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4531 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004532{
4533 __lhs.swap(__rhs);
4534}
4535
Bruce Mitchener170d8972020-11-24 12:53:53 -05004536_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4537_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4538_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4539_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4540_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004541
Bruce Mitchener170d8972020-11-24 12:53:53 -05004542_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4543_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4544_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004545
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004546_LIBCPP_FUNC_VIS string to_string(int __val);
4547_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4548_LIBCPP_FUNC_VIS string to_string(long __val);
4549_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4550_LIBCPP_FUNC_VIS string to_string(long long __val);
4551_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4552_LIBCPP_FUNC_VIS string to_string(float __val);
4553_LIBCPP_FUNC_VIS string to_string(double __val);
4554_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004555
Louis Dionne89258142021-08-23 15:32:36 -04004556#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004557_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4558_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4559_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4560_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4561_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004562
Bruce Mitchener170d8972020-11-24 12:53:53 -05004563_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4564_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4565_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004566
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004567_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4568_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4569_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4570_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4571_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4572_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4573_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4574_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4575_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004576#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004577
Howard Hinnantc51e1022010-05-11 19:42:16 +00004578template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004579_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004580const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4581 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004582
Marshall Clow851b9ec2019-05-20 21:56:51 +00004583template <class _CharT, class _Allocator>
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004584struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004585{
4586 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004587 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4588 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004589};
4590
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004591template <class _Allocator>
4592struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
4593
4594#ifndef _LIBCPP_HAS_NO_CHAR8_T
4595template <class _Allocator>
4596struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
4597#endif
4598
4599template <class _Allocator>
4600struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
4601
4602template <class _Allocator>
4603struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
4604
4605#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4606template <class _Allocator>
4607struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
4608#endif
4609
Howard Hinnantdc095972011-07-18 15:51:59 +00004610template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004611_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004612operator<<(basic_ostream<_CharT, _Traits>& __os,
4613 const basic_string<_CharT, _Traits, _Allocator>& __str);
4614
4615template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004616_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004617operator>>(basic_istream<_CharT, _Traits>& __is,
4618 basic_string<_CharT, _Traits, _Allocator>& __str);
4619
4620template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004621_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004622getline(basic_istream<_CharT, _Traits>& __is,
4623 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4624
4625template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004626inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004627basic_istream<_CharT, _Traits>&
4628getline(basic_istream<_CharT, _Traits>& __is,
4629 basic_string<_CharT, _Traits, _Allocator>& __str);
4630
Howard Hinnantdc095972011-07-18 15:51:59 +00004631template<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, _CharT __dlm);
4636
4637template<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);
4642
Marshall Clow29b53f22018-12-14 18:49:35 +00004643#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004644template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004645inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004646 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4647 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4648 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004649 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004650 return __old_size - __str.size();
4651}
Marshall Clow29b53f22018-12-14 18:49:35 +00004652
Marek Kurdeja98b1412020-05-02 13:58:03 +02004653template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004654inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004655 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4656 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4657 _Predicate __pred) {
4658 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004659 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004660 __str.end());
4661 return __old_size - __str.size();
4662}
Marshall Clow29b53f22018-12-14 18:49:35 +00004663#endif
4664
Louis Dionne510450b2022-04-01 16:38:30 -04004665#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004666
4667template<class _CharT, class _Traits, class _Allocator>
4668bool
4669basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4670{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004671 return data() <= std::__to_address(__i->base()) &&
4672 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004673}
4674
4675template<class _CharT, class _Traits, class _Allocator>
4676bool
4677basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4678{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004679 return data() < std::__to_address(__i->base()) &&
4680 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004681}
4682
4683template<class _CharT, class _Traits, class _Allocator>
4684bool
4685basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4686{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004687 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004688 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004689}
4690
4691template<class _CharT, class _Traits, class _Allocator>
4692bool
4693basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4694{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004695 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004696 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004697}
4698
Louis Dionne510450b2022-04-01 16:38:30 -04004699#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004700
Louis Dionne173f29e2019-05-29 16:01:36 +00004701#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004702// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004703inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004704{
4705 inline namespace string_literals
4706 {
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004707 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004708 basic_string<char> operator "" s( const char *__str, size_t __len )
4709 {
4710 return basic_string<char> (__str, __len);
4711 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004712
Louis Dionne89258142021-08-23 15:32:36 -04004713#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004714 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004715 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4716 {
4717 return basic_string<wchar_t> (__str, __len);
4718 }
Louis Dionne89258142021-08-23 15:32:36 -04004719#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004720
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004721#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004722 inline _LIBCPP_HIDE_FROM_ABI constexpr
Nikolas Klauser5da956d2022-09-02 16:20:28 +02004723 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len)
Marshall Clow8732fed2018-12-11 04:35:44 +00004724 {
4725 return basic_string<char8_t> (__str, __len);
4726 }
4727#endif
4728
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004729 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004730 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4731 {
4732 return basic_string<char16_t> (__str, __len);
4733 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004734
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<char32_t> operator "" s( const char32_t *__str, size_t __len )
4737 {
4738 return basic_string<char32_t> (__str, __len);
4739 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004740 } // namespace string_literals
4741} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004742
4743#if _LIBCPP_STD_VER > 17
4744template <>
4745inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4746#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4747template <>
4748inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4749#endif
4750#endif
4751
Marshall Clowcba751f2013-07-23 17:05:24 +00004752#endif
4753
Howard Hinnantc51e1022010-05-11 19:42:16 +00004754_LIBCPP_END_NAMESPACE_STD
4755
Eric Fiselierf4433a32017-05-31 22:07:49 +00004756_LIBCPP_POP_MACROS
4757
Mark de Weveree5fe272022-09-02 17:53:28 +02004758#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
4759# include <algorithm>
4760# include <functional>
4761# include <iterator>
4762# include <new>
4763# include <typeinfo>
4764# include <utility>
4765# include <vector>
4766#endif
4767
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004768#endif // _LIBCPP_STRING