blob: 4e9bf64b5a3f8d39f5d65d1c6e97fee057c1e273 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
Mark de Weverb4830e62022-07-19 07:56:23 +020016#include <compare>
17#include <initializer_list>
18
Howard Hinnantc51e1022010-05-11 19:42:16 +000019namespace std
20{
21
22template <class stateT>
23class fpos
24{
25private:
26 stateT st;
27public:
28 fpos(streamoff = streamoff());
29
30 operator streamoff() const;
31
32 stateT state() const;
33 void state(stateT);
34
35 fpos& operator+=(streamoff);
36 fpos operator+ (streamoff) const;
37 fpos& operator-=(streamoff);
38 fpos operator- (streamoff) const;
39};
40
41template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
44template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
45
46template <class charT>
47struct char_traits
48{
Mark de Weverb4830e62022-07-19 07:56:23 +020049 using char_type = charT;
50 using int_type = ...;
51 using off_type = streamoff;
52 using pos_type = streampos;
53 using state_type = mbstate_t;
54 using comparison_category = strong_ordering; // Since C++20 only for the specializations
55 // char, wchar_t, char8_t, char16_t, and char32_t.
Howard Hinnantc51e1022010-05-11 19:42:16 +000056
Howard Hinnantaeb17d82011-05-29 19:57:12 +000057 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000058 static constexpr bool eq(char_type c1, char_type c2) noexcept;
59 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000060
61 static int compare(const char_type* s1, const char_type* s2, size_t n);
62 static size_t length(const char_type* s);
63 static const char_type* find(const char_type* s, size_t n, const char_type& a);
64 static char_type* move(char_type* s1, const char_type* s2, size_t n);
65 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
66 static char_type* assign(char_type* s, size_t n, char_type a);
67
Howard Hinnant270c00e2012-07-20 19:09:12 +000068 static constexpr int_type not_eof(int_type c) noexcept;
69 static constexpr char_type to_char_type(int_type c) noexcept;
70 static constexpr int_type to_int_type(char_type c) noexcept;
71 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
72 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000073};
74
75template <> struct char_traits<char>;
76template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010077template <> struct char_traits<char8_t>; // C++20
78template <> struct char_traits<char16_t>;
79template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
Howard Hinnant3b6579a2010-08-22 00:02:43 +000081template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000082class basic_string
83{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000084public:
85// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000086 typedef traits traits_type;
87 typedef typename traits_type::char_type value_type;
88 typedef Allocator allocator_type;
89 typedef typename allocator_type::size_type size_type;
90 typedef typename allocator_type::difference_type difference_type;
91 typedef typename allocator_type::reference reference;
92 typedef typename allocator_type::const_reference const_reference;
93 typedef typename allocator_type::pointer pointer;
94 typedef typename allocator_type::const_pointer const_pointer;
95 typedef implementation-defined iterator;
96 typedef implementation-defined const_iterator;
97 typedef std::reverse_iterator<iterator> reverse_iterator;
98 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
99
100 static const size_type npos = -1;
101
Howard Hinnant3e276872011-06-03 18:40:47 +0000102 basic_string()
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200103 noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20
104 explicit basic_string(const allocator_type& a); // constexpr since C++20
105 basic_string(const basic_string& str); // constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000106 basic_string(basic_string&& str)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200107 noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000108 basic_string(const basic_string& str, size_type pos,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200109 const allocator_type& a = allocator_type()); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000110 basic_string(const basic_string& str, size_type pos, size_type n,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200111 const Allocator& a = Allocator()); // constexpr since C++20
Marshall Clow78dbe462016-11-14 18:22:19 +0000112 template<class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200113 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17, constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000114 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200115 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17, constexpr since C++20
116 basic_string(const value_type* s, const allocator_type& a = allocator_type()); // constexpr since C++20
117 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type()); // constexpr since C++20
Marek Kurdej90d79712021-07-27 16:16:21 +0200118 basic_string(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200119 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type()); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000121 basic_string(InputIterator begin, InputIterator end,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200122 const allocator_type& a = allocator_type()); // constexpr since C++20
123 basic_string(initializer_list<value_type>, const Allocator& = Allocator()); // constexpr since C++20
124 basic_string(const basic_string&, const Allocator&); // constexpr since C++20
125 basic_string(basic_string&&, const Allocator&); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200127 ~basic_string(); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200129 operator basic_string_view<charT, traits>() const noexcept; // constexpr since C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000130
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200131 basic_string& operator=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000132 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200133 basic_string& operator=(const T& t); // C++17, constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000134 basic_string& operator=(basic_string&& str)
135 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000136 allocator_type::propagate_on_container_move_assignment::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200137 allocator_type::is_always_equal::value ); // C++17, constexpr since C++20
138 basic_string& operator=(const value_type* s); // constexpr since C++20
Marek Kurdej90d79712021-07-27 16:16:21 +0200139 basic_string& operator=(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200140 basic_string& operator=(value_type c); // constexpr since C++20
141 basic_string& operator=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200143 iterator begin() noexcept; // constexpr since C++20
144 const_iterator begin() const noexcept; // constexpr since C++20
145 iterator end() noexcept; // constexpr since C++20
146 const_iterator end() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200148 reverse_iterator rbegin() noexcept; // constexpr since C++20
149 const_reverse_iterator rbegin() const noexcept; // constexpr since C++20
150 reverse_iterator rend() noexcept; // constexpr since C++20
151 const_reverse_iterator rend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200153 const_iterator cbegin() const noexcept; // constexpr since C++20
154 const_iterator cend() const noexcept; // constexpr since C++20
155 const_reverse_iterator crbegin() const noexcept; // constexpr since C++20
156 const_reverse_iterator crend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200158 size_type size() const noexcept; // constexpr since C++20
159 size_type length() const noexcept; // constexpr since C++20
160 size_type max_size() const noexcept; // constexpr since C++20
161 size_type capacity() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000162
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200163 void resize(size_type n, value_type c); // constexpr since C++20
164 void resize(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100166 template<class Operation>
167 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
168
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200169 void reserve(size_type res_arg); // constexpr since C++20
Marek Kurdejc9848142020-11-26 10:07:16 +0100170 void reserve(); // deprecated in C++20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200171 void shrink_to_fit(); // constexpr since C++20
172 void clear() noexcept; // constexpr since C++20
173 bool empty() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200175 const_reference operator[](size_type pos) const; // constexpr since C++20
176 reference operator[](size_type pos); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200178 const_reference at(size_type n) const; // constexpr since C++20
179 reference at(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200181 basic_string& operator+=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000182 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200183 basic_string& operator+=(const T& t); // C++17, constexpr since C++20
184 basic_string& operator+=(const value_type* s); // constexpr since C++20
185 basic_string& operator+=(value_type c); // constexpr since C++20
186 basic_string& operator+=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200188 basic_string& append(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000189 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200190 basic_string& append(const T& t); // C++17, constexpr since C++20
191 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000192 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200193 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
194 basic_string& append(const value_type* s, size_type n); // constexpr since C++20
195 basic_string& append(const value_type* s); // constexpr since C++20
196 basic_string& append(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000197 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200198 basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
199 basic_string& append(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200201 void push_back(value_type c); // constexpr since C++20
202 void pop_back(); // constexpr since C++20
203 reference front(); // constexpr since C++20
204 const_reference front() const; // constexpr since C++20
205 reference back(); // constexpr since C++20
206 const_reference back() const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200208 basic_string& assign(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000209 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200210 basic_string& assign(const T& t); // C++17, constexpr since C++20
211 basic_string& assign(basic_string&& str); // constexpr since C++20
212 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000213 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200214 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
215 basic_string& assign(const value_type* s, size_type n); // constexpr since C++20
216 basic_string& assign(const value_type* s); // constexpr since C++20
217 basic_string& assign(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000218 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200219 basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
220 basic_string& assign(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200222 basic_string& insert(size_type pos1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000223 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200224 basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000225 basic_string& insert(size_type pos1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200226 size_type pos2, size_type n); // constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000227 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200228 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
229 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
230 basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
231 basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
232 iterator insert(const_iterator p, value_type c); // constexpr since C++20
233 iterator insert(const_iterator p, size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000234 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200235 iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20
236 iterator insert(const_iterator p, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200238 basic_string& erase(size_type pos = 0, size_type n = npos); // constexpr since C++20
239 iterator erase(const_iterator position); // constexpr since C++20
240 iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200242 basic_string& replace(size_type pos1, size_type n1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000243 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200244 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17, constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000245 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200246 size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000247 template <class T>
248 basic_string& replace(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200249 size_type pos2, size_type n); // C++17, constexpr since C++20
250 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
251 basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
252 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
253 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000254 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200255 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17, constexpr since C++20
256 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
257 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s); // constexpr since C++20
258 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000259 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200260 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
261 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200263 size_type copy(value_type* s, size_type n, size_type pos = 0) const; // constexpr since C++20
264 basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265
Howard Hinnant3e276872011-06-03 18:40:47 +0000266 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000267 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200268 allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200270 const value_type* c_str() const noexcept; // constexpr since C++20
271 const value_type* data() const noexcept; // constexpr since C++20
272 value_type* data() noexcept; // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200274 allocator_type get_allocator() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200276 size_type find(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000277 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200278 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
279 size_type find(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
280 size_type find(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
281 size_type find(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200283 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000284 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200285 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
286 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
287 size_type rfind(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
288 size_type rfind(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200290 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000291 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200292 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
293 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
294 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
295 size_type find_first_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200297 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000298 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200299 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension, constexpr since C++20
300 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
301 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
302 size_type find_last_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200304 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000305 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200306 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
307 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
308 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
309 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200311 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000312 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200313 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
314 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
315 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
316 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200318 int compare(const basic_string& str) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000319 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200320 int compare(const T& t) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
321 int compare(size_type pos1, size_type n1, const basic_string& str) const; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000322 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200323 int compare(size_type pos1, size_type n1, const T& t) const; // C++17, constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000324 int compare(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200325 size_type pos2, size_type n2=npos) const; // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000326 template <class T>
327 int compare(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200328 size_type pos2, size_type n2=npos) const; // C++17, constexpr since C++20
329 int compare(const value_type* s) const noexcept; // constexpr since C++20
330 int compare(size_type pos1, size_type n1, const value_type* s) const; // constexpr since C++20
331 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200333 constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
334 constexpr bool starts_with(charT c) const noexcept; // C++20
335 constexpr bool starts_with(const charT* s) const; // C++20
336 constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
337 constexpr bool ends_with(charT c) const noexcept; // C++20
338 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000339
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200340 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
341 constexpr bool contains(charT c) const noexcept; // C++2b
342 constexpr bool contains(const charT* s) const; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000343};
344
Marshall Clowa0563332018-02-08 06:34:03 +0000345template<class InputIterator,
346 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
347basic_string(InputIterator, InputIterator, Allocator = Allocator())
348 -> basic_string<typename iterator_traits<InputIterator>::value_type,
349 char_traits<typename iterator_traits<InputIterator>::value_type>,
350 Allocator>; // C++17
351
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352template<class charT, class traits, class Allocator>
353basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000354operator+(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200355 const basic_string<charT, traits, Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356
357template<class charT, class traits, class Allocator>
358basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200359operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
361template<class charT, class traits, class Allocator>
362basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200363operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
366basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200367operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368
369template<class charT, class traits, class Allocator>
370basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200371operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372
373template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000374bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200375 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200378bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
380template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200381bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000383template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000384bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200385 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200388bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200391bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392
393template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000394bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200395 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200398bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200401bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402
403template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000404bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200405 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200408bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200411bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412
413template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000414bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200415 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200418bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200421bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422
423template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000424bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200425 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200428bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200431bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Mark de Weverb4830e62022-07-19 07:56:23 +0200432
433template<class charT, class traits, class Allocator> // since C++20
434constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
435 const basic_string<charT, traits, Allocator>& rhs) noexcept;
436
437template<class charT, class traits, class Allocator> // since C++20
438constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
439 const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440
441template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000442void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000443 basic_string<charT, traits, Allocator>& rhs)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200444 noexcept(noexcept(lhs.swap(rhs))); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445
446template<class charT, class traits, class Allocator>
447basic_istream<charT, traits>&
448operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
449
450template<class charT, class traits, class Allocator>
451basic_ostream<charT, traits>&
452operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
453
454template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000455basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000456getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
457 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000458
459template<class charT, class traits, class Allocator>
460basic_istream<charT, traits>&
461getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
462
Marshall Clow29b53f22018-12-14 18:49:35 +0000463template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200464typename basic_string<charT, traits, Allocator>::size_type
465erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000466template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200467typename basic_string<charT, traits, Allocator>::size_type
468erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000469
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470typedef basic_string<char> string;
471typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100472typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000473typedef basic_string<char16_t> u16string;
474typedef basic_string<char32_t> u32string;
475
Bruce Mitchener170d8972020-11-24 12:53:53 -0500476int stoi (const string& str, size_t* idx = nullptr, int base = 10);
477long stol (const string& str, size_t* idx = nullptr, int base = 10);
478unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
479long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
480unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000481
Bruce Mitchener170d8972020-11-24 12:53:53 -0500482float stof (const string& str, size_t* idx = nullptr);
483double stod (const string& str, size_t* idx = nullptr);
484long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000485
486string to_string(int val);
487string to_string(unsigned val);
488string to_string(long val);
489string to_string(unsigned long val);
490string to_string(long long val);
491string to_string(unsigned long long val);
492string to_string(float val);
493string to_string(double val);
494string to_string(long double val);
495
Bruce Mitchener170d8972020-11-24 12:53:53 -0500496int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
497long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
498unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
499long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
500unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000501
Bruce Mitchener170d8972020-11-24 12:53:53 -0500502float stof (const wstring& str, size_t* idx = nullptr);
503double stod (const wstring& str, size_t* idx = nullptr);
504long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000505
506wstring to_wstring(int val);
507wstring to_wstring(unsigned val);
508wstring to_wstring(long val);
509wstring to_wstring(unsigned long val);
510wstring to_wstring(long long val);
511wstring to_wstring(unsigned long long val);
512wstring to_wstring(float val);
513wstring to_wstring(double val);
514wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515
516template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100517template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518template <> struct hash<u16string>;
519template <> struct hash<u32string>;
520template <> struct hash<wstring>;
521
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200522basic_string<char> operator "" s( const char *str, size_t len ); // C++14, constexpr since C++20
523basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14, constexpr since C++20
524constexpr basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
525basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14, constexpr since C++20
526basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14, constexpr since C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000527
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528} // std
529
530*/
531
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100532#include <__algorithm/max.h>
533#include <__algorithm/min.h>
534#include <__algorithm/remove.h>
535#include <__algorithm/remove_if.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400536#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400538#include <__debug>
Mark de Weverdf3e0d42021-09-26 15:47:42 +0200539#include <__format/enable_insertable.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200540#include <__functional/hash.h>
Nikolas Klauser24540d32022-05-21 00:45:51 +0200541#include <__functional/unary_function.h>
Nikolas Klauser80840272022-02-04 13:04:33 +0100542#include <__ios/fpos.h>
Nikolas Klauserfb1b0672022-06-10 19:53:10 +0200543#include <__iterator/distance.h>
544#include <__iterator/iterator_traits.h>
545#include <__iterator/reverse_iterator.h>
Louis Dionne77249522021-06-11 09:55:11 -0400546#include <__iterator/wrap_iter.h>
Nikolas Klauserc513eba2022-04-09 09:41:19 +0200547#include <__memory/allocate_at_least.h>
Nikolas Klauser0b5df932022-09-05 00:01:15 +0200548#include <__memory/allocator.h>
549#include <__memory/allocator_traits.h>
550#include <__memory/compressed_pair.h>
551#include <__memory/construct_at.h>
552#include <__memory/pointer_traits.h>
Nikolas Klauser35080b42022-07-26 16:13:56 +0200553#include <__memory/swap_allocator.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200554#include <__string/char_traits.h>
555#include <__string/extern_template_lists.h>
Nikolas Klauser0b5df932022-09-05 00:01:15 +0200556#include <__type_traits/is_allocator.h>
557#include <__type_traits/noexcept_move_assign_container.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100558#include <__utility/auto_cast.h>
559#include <__utility/move.h>
560#include <__utility/swap.h>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200561#include <__utility/unreachable.h>
562#include <climits>
Louis Dionne44962c32022-06-13 11:21:16 -0400563#include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400564#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400565#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400566#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400567#include <iosfwd>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200568#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400570#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000572#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000573
Louis Dionne89258142021-08-23 15:32:36 -0400574#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100575# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400576#endif
577
Nikolas Klausera0e0edb2022-06-16 22:43:46 +0200578// standard-mandated includes
579
580// [iterator.range]
581#include <__iterator/access.h>
582#include <__iterator/data.h>
583#include <__iterator/empty.h>
584#include <__iterator/reverse_access.h>
585#include <__iterator/size.h>
586
587// [string.syn]
588#include <compare>
589#include <initializer_list>
590
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000591#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500592# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000593#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594
Eric Fiselierf4433a32017-05-31 22:07:49 +0000595_LIBCPP_PUSH_MACROS
596#include <__undef_macros>
597
598
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599_LIBCPP_BEGIN_NAMESPACE_STD
600
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601// basic_string
602
603template<class _CharT, class _Traits, class _Allocator>
604basic_string<_CharT, _Traits, _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200605_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant944510a2011-06-14 19:58:17 +0000606operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
607 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608
609template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200610_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000612operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613
614template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200615_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000617operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618
619template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200620inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000622operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623
624template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200625_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000626basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000627operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000628
Louis Dionnedc496ec2021-06-08 17:25:08 -0400629extern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Shoaib Meenaiea363712017-07-29 02:54:41 +0000630
Marshall Clow039b2f02016-01-13 21:54:34 +0000631template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400632struct __string_is_trivial_iterator : public false_type {};
633
634template <class _Tp>
635struct __string_is_trivial_iterator<_Tp*>
636 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000637
Louis Dionne173f29e2019-05-29 16:01:36 +0000638template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400639struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
640 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000641
Marshall Clow82513342016-09-24 22:45:42 +0000642template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500643struct __can_be_converted_to_string_view : public _BoolConstant<
644 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
645 !is_convertible<const _Tp&, const _CharT*>::value
646 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000647
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400648#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800649typedef basic_string<char8_t> u8string;
650#endif
Richard Smith256954d2020-11-11 17:12:18 -0800651typedef basic_string<char16_t> u16string;
652typedef basic_string<char32_t> u32string;
Richard Smith256954d2020-11-11 17:12:18 -0800653
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200654struct __uninitialized_size_tag {};
655
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000656template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800657class
658 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400659#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800660 _LIBCPP_PREFERRED_NAME(u8string)
661#endif
Richard Smith256954d2020-11-11 17:12:18 -0800662 _LIBCPP_PREFERRED_NAME(u16string)
663 _LIBCPP_PREFERRED_NAME(u32string)
Richard Smith256954d2020-11-11 17:12:18 -0800664 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665{
666public:
667 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000668 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000670 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000672 typedef allocator_traits<allocator_type> __alloc_traits;
673 typedef typename __alloc_traits::size_type size_type;
674 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000675 typedef value_type& reference;
676 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000677 typedef typename __alloc_traits::pointer pointer;
678 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679
Marshall Clow79f33542018-03-21 00:36:05 +0000680 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
681 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
682 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
683 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000684 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000685 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000686 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000687
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 typedef __wrap_iter<pointer> iterator;
689 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200690 typedef std::reverse_iterator<iterator> reverse_iterator;
691 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692
693private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200694 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000695
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000696#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000697
698 struct __long
699 {
700 pointer __data_;
701 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200702 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
703 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000704 };
705
Howard Hinnant68bf1812013-04-30 21:44:48 +0000706 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
707 (sizeof(__long) - 1)/sizeof(value_type) : 2};
708
709 struct __short
710 {
711 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200712 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200713 unsigned char __size_ : 7;
714 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000715 };
716
Nikolas Klauser62060be2022-04-20 10:52:04 +0200717// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200718// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200719//
720// If the LSB is used to store the short-flag in the short string representation,
721// we have to multiply the size by two when it is stored and divide it by two when
722// it is loaded to make sure that we always store an even number. In the long string
723// representation, we can ignore this because we can assume that we always allocate
724// an even amount of value_types.
725//
726// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
727// This does not impact the short string representation, since we never need the MSB
728// for representing the size of a short string anyway.
729
730#ifdef _LIBCPP_BIG_ENDIAN
731 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000732#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200733 static const size_type __endian_factor = 1;
734#endif
735
736#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
737
738#ifdef _LIBCPP_BIG_ENDIAN
739 static const size_type __endian_factor = 1;
740#else
741 static const size_type __endian_factor = 2;
742#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000743
Xing Xue38b9fc42022-06-24 17:25:15 -0400744 // Attribute 'packed' is used to keep the layout compatible with the
745 // previous definition that did not use bit fields. This is because on
746 // some platforms bit fields have a default size rather than the actual
747 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 struct __long
749 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400750 struct _LIBCPP_PACKED {
751 size_type __is_long_ : 1;
752 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
753 };
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754 size_type __size_;
755 pointer __data_;
756 };
757
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
759 (sizeof(__long) - 1)/sizeof(value_type) : 2};
760
761 struct __short
762 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400763 struct _LIBCPP_PACKED {
764 unsigned char __is_long_ : 1;
765 unsigned char __size_ : 7;
766 };
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200767 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768 value_type __data_[__min_cap];
769 };
770
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400771#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000772
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200773 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
774
Howard Hinnant8ea98242013-08-23 17:37:05 +0000775 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776
Howard Hinnant8ea98242013-08-23 17:37:05 +0000777 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778
779 struct __raw
780 {
781 size_type __words[__n_words];
782 };
783
784 struct __rep
785 {
786 union
787 {
788 __long __l;
789 __short __s;
790 __raw __r;
791 };
792 };
793
794 __compressed_pair<__rep, allocator_type> __r_;
795
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200796 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
797 // don't initialize the characters. The contents of the string, including the null terminator, must be
798 // initialized separately.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200799 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200800 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200801 : __r_(__default_init_tag(), __a) {
802 if (__size > max_size())
803 __throw_length_error();
804 if (__fits_in_sso(__size)) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +0200805 __r_.first() = __rep();
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200806 __set_short_size(__size);
807 } else {
808 auto __capacity = __recommend(__size) + 1;
809 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200810 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200811 __set_long_cap(__capacity);
812 __set_long_pointer(__allocation);
813 __set_long_size(__size);
814 }
815 std::__debug_db_insert_c(this);
816 }
817
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818public:
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200819 _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200821 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
822 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
823 : __r_(__default_init_tag(), __default_init_tag()) {
824 std::__debug_db_insert_c(this);
825 __default_init();
826 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000827
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200828 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000829#if _LIBCPP_STD_VER <= 14
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200830 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +0000831#else
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200832 _NOEXCEPT
Marshall Clowa80abc72015-06-03 19:56:43 +0000833#endif
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200834 : __r_(__default_init_tag(), __a) {
835 std::__debug_db_insert_c(this);
836 __default_init();
837 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000838
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200839 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
840 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000841
Eric Fiselierfc92be82017-04-19 00:28:44 +0000842#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200843 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)
844# if _LIBCPP_STD_VER <= 14
845 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
846# else
847 _NOEXCEPT
848# endif
849 : __r_(std::move(__str.__r_)) {
850 __str.__default_init();
851 std::__debug_db_insert_c(this);
852 if (__is_long())
853 std::__debug_db_swap(this, &__str);
854 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000855
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200856 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)
857 : __r_(__default_init_tag(), __a) {
858 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
859 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
860 else {
861 if (__libcpp_is_constant_evaluated())
862 __r_.first() = __rep();
863 __r_.first() = __str.__r_.first();
864 __str.__default_init();
865 }
866 std::__debug_db_insert_c(this);
867 if (__is_long())
868 std::__debug_db_swap(this, &__str);
869 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400870#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000871
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200872 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
873 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s)
874 : __r_(__default_init_tag(), __default_init_tag()) {
875 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
876 __init(__s, traits_type::length(__s));
877 std::__debug_db_insert_c(this);
878 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000879
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200880 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
881 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, const _Allocator& __a);
Marshall Clowe46031a2018-07-02 18:41:15 +0000882
Marek Kurdej90d79712021-07-27 16:16:21 +0200883#if _LIBCPP_STD_VER > 20
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200884 basic_string(nullptr_t) = delete;
Marek Kurdej90d79712021-07-27 16:16:21 +0200885#endif
886
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200887 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n)
888 : __r_(__default_init_tag(), __default_init_tag()) {
889 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
890 __init(__s, __n);
891 std::__debug_db_insert_c(this);
892 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000893
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200894 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
895 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
896 : __r_(__default_init_tag(), __a) {
897 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
898 __init(__s, __n);
899 std::__debug_db_insert_c(this);
900 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000901
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200902 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c)
903 : __r_(__default_init_tag(), __default_init_tag()) {
904 __init(__n, __c);
905 std::__debug_db_insert_c(this);
906 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000907
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200908 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
909 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
Marshall Clowe46031a2018-07-02 18:41:15 +0000910
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200911 _LIBCPP_CONSTEXPR_SINCE_CXX20
912 basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000913
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200914 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
915 basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())
916 : __r_(__default_init_tag(), __a) {
917 size_type __str_sz = __str.size();
918 if (__pos > __str_sz)
919 __throw_out_of_range();
920 __init(__str.data() + __pos, __str_sz - __pos);
921 std::__debug_db_insert_c(this);
922 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000923
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200924 template <class _Tp,
925 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
926 !__is_same_uncvref<_Tp, basic_string>::value> >
927 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
928 basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type());
929
930 template <class _Tp,
931 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
932 !__is_same_uncvref<_Tp, basic_string>::value> >
933 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
934 const _Tp& __t);
935
936 template <class _Tp,
937 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
938 !__is_same_uncvref<_Tp, basic_string>::value> >
939 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
940 const _Tp& __t, const allocator_type& __a);
941
942 template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
943 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last)
944 : __r_(__default_init_tag(), __default_init_tag()) {
945 __init(__first, __last);
946 std::__debug_db_insert_c(this);
947 }
948
949 template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
950 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
951 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
952 : __r_(__default_init_tag(), __a) {
953 __init(__first, __last);
954 std::__debug_db_insert_c(this);
955 }
956
Eric Fiselierfc92be82017-04-19 00:28:44 +0000957#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200958 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il)
959 : __r_(__default_init_tag(), __default_init_tag()) {
960 __init(__il.begin(), __il.end());
961 std::__debug_db_insert_c(this);
962 }
963
964 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)
965 : __r_(__default_init_tag(), __a) {
966 __init(__il.begin(), __il.end());
967 std::__debug_db_insert_c(this);
968 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400969#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200971 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200973 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000974 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
975
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200976 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000977
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200978 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
979 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200980 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200981 __self_view __sv = __t;
982 return assign(__sv);
983 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000984
Eric Fiselierfc92be82017-04-19 00:28:44 +0000985#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200986 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +0000987 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000988 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200989 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierfc92be82017-04-19 00:28:44 +0000990 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200992 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200993 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200994#if _LIBCPP_STD_VER > 20
995 basic_string& operator=(nullptr_t) = delete;
996#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200997 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200999 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001000 iterator begin() _NOEXCEPT
1001 {return iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001002 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001003 const_iterator begin() const _NOEXCEPT
1004 {return const_iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001005 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001006 iterator end() _NOEXCEPT
1007 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001008 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001009 const_iterator end() const _NOEXCEPT
1010 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -04001011
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001012 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001013 reverse_iterator rbegin() _NOEXCEPT
1014 {return reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001015 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001016 const_reverse_iterator rbegin() const _NOEXCEPT
1017 {return const_reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001018 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001019 reverse_iterator rend() _NOEXCEPT
1020 {return reverse_iterator(begin());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001021 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001022 const_reverse_iterator rend() const _NOEXCEPT
1023 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001025 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001026 const_iterator cbegin() const _NOEXCEPT
1027 {return begin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001028 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001029 const_iterator cend() const _NOEXCEPT
1030 {return end();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001031 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001032 const_reverse_iterator crbegin() const _NOEXCEPT
1033 {return rbegin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001034 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001035 const_reverse_iterator crend() const _NOEXCEPT
1036 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001038 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001040 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {return size();}
1041 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT;
1042 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001043 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
1044 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001046 _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
1047 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001049 _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +01001050
1051#if _LIBCPP_STD_VER > 20
1052 template <class _Op>
1053 _LIBCPP_HIDE_FROM_ABI constexpr
1054 void resize_and_overwrite(size_type __n, _Op __op) {
1055 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001056 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +01001057 }
1058#endif
1059
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001060 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +00001061
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001062 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001063 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
1064 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001065
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001066 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowb7db4972017-11-15 20:02:27 +00001067 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001069 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001070 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001071 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001073 _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
1074 _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001076 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001077 return append(__str);
1078 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001079
1080 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001081 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001082 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001083 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001084 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1085 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001086 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001087 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001088 operator+=(const _Tp& __t) {
1089 __self_view __sv = __t; return append(__sv);
1090 }
1091
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001092 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001093 return append(__s);
1094 }
1095
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001096 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001097 push_back(__c);
1098 return *this;
1099 }
1100
Eric Fiselierfc92be82017-04-19 00:28:44 +00001101#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001102 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001103 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001104#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001106 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001108
1109 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001110 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001111 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001112 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1113 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001114 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001115 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001116 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001117 _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 +00001118
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 >
Marshall Clow82513342016-09-24 22:45:42 +00001127 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001128 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n);
1129 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s);
1130 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001131
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001132 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier451d5582018-11-26 20:15:38 +00001133 void __append_default_init(size_type __n);
1134
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001136 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001137 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001139 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001141 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001142 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001143 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001144 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001145 append(__temp.data(), __temp.size());
1146 return *this;
1147 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001149 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001150 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001152 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001154 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001155 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001156 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001157
Eric Fiselierfc92be82017-04-19 00:28:44 +00001158#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001159 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001161#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001163 _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
1164 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
1165 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT;
1166 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT;
1167 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT;
1168 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169
Marshall Clowe46031a2018-07-02 18:41:15 +00001170 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001171 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001172 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001173 <
1174 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1175 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001176 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001177 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001178 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001179 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001180#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001181 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001182 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001183 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001184 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001185#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001186 _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 +00001187 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001188 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001189 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001190 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001191 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1192 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001193 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001194 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001195 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001196 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n);
1197 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s);
1198 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001200 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001201 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001203 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001205 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206 assign(_InputIterator __first, _InputIterator __last);
1207 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001208 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001209 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001211 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001213 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001215#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001216 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001218#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001220 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001222
1223 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001224 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001225 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001226 <
1227 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1228 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001229 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001230 insert(size_type __pos1, const _Tp& __t)
1231 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1232
Marshall Clow82513342016-09-24 22:45:42 +00001233 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001234 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001235 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001236 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001237 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001238 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001239 >
Marshall Clow82513342016-09-24 22:45:42 +00001240 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001241 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001242 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001243 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1244 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s);
1245 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1246 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
1247 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1249 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001250 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001251 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001253 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001255 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1257 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001258 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001259 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001261 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001263 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001265#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001266 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1268 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001269#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001271 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1272 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273 iterator erase(const_iterator __pos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001274 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275 iterator erase(const_iterator __first, const_iterator __last);
1276
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001277 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001279
1280 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001281 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001282 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001283 <
1284 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1285 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001286 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001287 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 +02001288 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001289 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 +00001290 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001291 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001292 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001293 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001294 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001295 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001296 >
Marshall Clow82513342016-09-24 22:45:42 +00001297 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001298 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001299 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001300 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1301 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1302 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001303 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001304
1305 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001306 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001307 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001308 <
1309 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1310 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001311 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001312 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1313
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001314 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001315 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001316 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001317 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001319 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001321 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001322 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001324 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001326 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001327 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001328#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001329 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001330 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001332#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001334 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
1335 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1337
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001338 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001339 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001340#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001341 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001342#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001343 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001344 __is_nothrow_swappable<allocator_type>::value);
1345#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001347 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001348 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001349 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001350 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001351#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001352 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001353 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001354#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001356 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001357 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001359 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001360 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001361
1362 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001363 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001364 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001365 <
1366 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1367 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001368 >
zoecarver1997e0a2021-02-05 11:54:47 -08001369 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001370 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001371 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001372 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001373 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001374 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375
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 rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001378
1379 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001380 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001381 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001382 <
1383 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1384 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001385 >
zoecarver1997e0a2021-02-05 11:54:47 -08001386 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001387 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001388 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001389 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001390 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001391 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001393 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001394 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001395
1396 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001397 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001398 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001399 <
1400 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1401 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001402 >
zoecarver1997e0a2021-02-05 11:54:47 -08001403 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001404 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001405 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001406 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001407 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001408 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001409 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001411 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001412 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001413
1414 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001415 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001416 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001417 <
1418 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1419 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001420 >
zoecarver1997e0a2021-02-05 11:54:47 -08001421 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001422 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001423 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001424 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001425 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001426 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001427 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001429 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001430 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001431
1432 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001433 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001434 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001435 <
1436 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1437 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001438 >
zoecarver1997e0a2021-02-05 11:54:47 -08001439 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001440 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001441 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 +02001442 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001443 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001444 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001445 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1446
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001447 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001448 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001449
1450 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001451 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001452 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001453 <
1454 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1455 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001456 >
zoecarver1997e0a2021-02-05 11:54:47 -08001457 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001458 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001459 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 +02001460 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001461 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001462 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001463 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1464
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001465 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001466 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001467
1468 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001469 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001470 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001471 <
1472 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1473 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001474 >
zoecarver1997e0a2021-02-05 11:54:47 -08001475 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001476
1477 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001478 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001479 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001480 <
1481 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1482 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001483 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001484 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1485
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001486 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001488 _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001489 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1490 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001491
Marshall Clow82513342016-09-24 22:45:42 +00001492 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001493 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001494 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001495 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001496 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001497 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001498 >
Marshall Clow82513342016-09-24 22:45:42 +00001499 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 +02001500 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const value_type* __s) const _NOEXCEPT;
1501 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1502 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001503 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504
Marshall Clow18c293b2017-12-04 20:11:38 +00001505#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001506 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001507 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001508 { return __self_view(data(), size()).starts_with(__sv); }
1509
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001510 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001511 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001512 { return !empty() && _Traits::eq(front(), __c); }
1513
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001514 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001515 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001516 { return starts_with(__self_view(__s)); }
1517
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001518 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001519 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001520 { return __self_view(data(), size()).ends_with( __sv); }
1521
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001522 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001523 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001524 { return !empty() && _Traits::eq(back(), __c); }
1525
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001526 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001527 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001528 { return ends_with(__self_view(__s)); }
1529#endif
1530
Wim Leflere023c3542021-01-19 14:33:30 -05001531#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001532 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001533 bool contains(__self_view __sv) const noexcept
1534 { return __self_view(data(), size()).contains(__sv); }
1535
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001536 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001537 bool contains(value_type __c) const noexcept
1538 { return __self_view(data(), size()).contains(__c); }
1539
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001540 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001541 bool contains(const value_type* __s) const
1542 { return __self_view(data(), size()).contains(__s); }
1543#endif
1544
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001545 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001546
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001547 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001548
Louis Dionne510450b2022-04-01 16:38:30 -04001549#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001550
1551 bool __dereferenceable(const const_iterator* __i) const;
1552 bool __decrementable(const const_iterator* __i) const;
1553 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1554 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1555
Louis Dionne510450b2022-04-01 16:38:30 -04001556#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001557
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001559 template<class _Alloc>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001560 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001561 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1562 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1563
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001564 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001565
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001566 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001567 bool __is_long() const _NOEXCEPT {
1568 if (__libcpp_is_constant_evaluated())
1569 return true;
1570 return __r_.first().__s.__is_long_;
1571 }
1572
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001573 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __begin_lifetime(pointer __begin, size_type __n) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001574#if _LIBCPP_STD_VER > 17
1575 if (__libcpp_is_constant_evaluated()) {
1576 for (size_type __i = 0; __i != __n; ++__i)
1577 std::construct_at(std::addressof(__begin[__i]));
1578 }
1579#else
1580 (void)__begin;
1581 (void)__n;
1582#endif // _LIBCPP_STD_VER > 17
1583 }
1584
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001585 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __default_init() {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001586 __r_.first() = __rep();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001587 if (__libcpp_is_constant_evaluated()) {
1588 size_type __sz = __recommend(0) + 1;
1589 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1590 __begin_lifetime(__ptr, __sz);
1591 __set_long_pointer(__ptr);
1592 __set_long_cap(__sz);
1593 __set_long_size(0);
1594 }
1595 }
1596
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001597 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __deallocate_constexpr() {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001598 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1599 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1600 }
1601
Nikolas Klauser93826d12022-01-04 17:24:03 +01001602 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1603 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1604 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1605 }
1606
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001607 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001608 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001609 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1610 size_type __sz = size();
1611 size_type __cap = capacity();
1612 value_type* __p;
1613 if (__cap - __sz >= __n)
1614 {
1615 __p = std::__to_address(__get_pointer());
1616 size_type __n_move = __sz - __ip;
1617 if (__n_move != 0)
1618 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1619 }
1620 else
1621 {
1622 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1623 __p = std::__to_address(__get_long_pointer());
1624 }
1625 __sz += __n;
1626 __set_size(__sz);
1627 traits_type::assign(__p[__sz], value_type());
1628 for (__p += __ip; __first != __last; ++__p, ++__first)
1629 traits_type::assign(*__p, *__first);
1630
1631 return begin() + __ip;
1632 }
1633
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001634 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001635 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001637 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001638 void __set_short_size(size_type __s) _NOEXCEPT {
1639 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1640 __r_.first().__s.__size_ = __s;
1641 __r_.first().__s.__is_long_ = false;
1642 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001643
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001644 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001645 size_type __get_short_size() const _NOEXCEPT {
1646 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1647 return __r_.first().__s.__size_;
1648 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001649
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001650 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001651 void __set_long_size(size_type __s) _NOEXCEPT
1652 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001653 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001654 size_type __get_long_size() const _NOEXCEPT
1655 {return __r_.first().__l.__size_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001656 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001657 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1659
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001660 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001661 void __set_long_cap(size_type __s) _NOEXCEPT {
1662 __r_.first().__l.__cap_ = __s / __endian_factor;
1663 __r_.first().__l.__is_long_ = true;
1664 }
1665
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001666 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001667 size_type __get_long_cap() const _NOEXCEPT {
1668 return __r_.first().__l.__cap_ * __endian_factor;
1669 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001671 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001672 void __set_long_pointer(pointer __p) _NOEXCEPT
1673 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001674 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001675 pointer __get_long_pointer() _NOEXCEPT
1676 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001677 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001678 const_pointer __get_long_pointer() const _NOEXCEPT
1679 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001680 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001681 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001682 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001683 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001684 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001685 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001686 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001687 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001689 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001690 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1692
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693 template <size_type __a> static
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001694 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea382952013-08-14 18:00:20 +00001695 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001696 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 enum {__alignment = 16};
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001698 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001699 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001700 {
1701 if (__s < __min_cap) {
1702 if (__libcpp_is_constant_evaluated())
1703 return static_cast<size_type>(__min_cap);
1704 else
1705 return static_cast<size_type>(__min_cap) - 1;
1706 }
Marshall Clow80584522018-02-07 21:30:17 +00001707 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1708 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1709 if (__guess == __min_cap) ++__guess;
1710 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001711 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001713 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001714 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001715 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001716 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001717 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001719
Martijn Vels5e7c9752020-03-04 17:52:46 -05001720 // Slow path for the (inlined) copy constructor for 'long' strings.
1721 // Always externally instantiated and not inlined.
1722 // Requires that __s is zero terminated.
1723 // The main reason for this function to exist is because for unstable, we
1724 // want to allow inlining of the copy constructor. However, we don't want
1725 // to call the __init() functions as those are marked as inline which may
1726 // result in over-aggressive inlining by the compiler, where our aim is
1727 // to only inline the fast path code directly in the ctor.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001728 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001729
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001731 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001732 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001734 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1735 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 __init(_InputIterator __first, _InputIterator __last);
1737
1738 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001739 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001740 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001742 __is_cpp17_forward_iterator<_ForwardIterator>::value
1743 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744 __init(_ForwardIterator __first, _ForwardIterator __last);
1745
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001746 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001748 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001749 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1751 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001752 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753
Martijn Vels596e3de2020-02-26 15:55:49 -05001754 // __assign_no_alias is invoked for assignment operations where we
1755 // have proof that the input does not alias the current instance.
1756 // For example, operator=(basic_string) performs a 'self' check.
1757 template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001758 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001759
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001760 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 void __erase_to_end(size_type __pos);
1762
Martijn Velsa81fc792020-02-26 13:25:43 -05001763 // __erase_external_with_move is invoked for erase() invocations where
1764 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001765 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001766
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001767 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001768 void __copy_assign_alloc(const basic_string& __str)
1769 {__copy_assign_alloc(__str, integral_constant<bool,
1770 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1771
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001772 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001773 void __copy_assign_alloc(const basic_string& __str, true_type)
1774 {
Marshall Clowf258c202017-01-31 03:40:52 +00001775 if (__alloc() == __str.__alloc())
1776 __alloc() = __str.__alloc();
1777 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001778 {
Marshall Clowf258c202017-01-31 03:40:52 +00001779 if (!__str.__is_long())
1780 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001781 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001782 __alloc() = __str.__alloc();
1783 }
1784 else
1785 {
1786 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001787 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001788 __begin_lifetime(__allocation.ptr, __allocation.count);
Vedant Kumar55e007e2018-03-08 21:15:26 +00001789 __clear_and_shrink();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001790 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001791 __set_long_pointer(__allocation.ptr);
1792 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001793 __set_long_size(__str.size());
1794 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001795 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001796 }
1797
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001798 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001799 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001800 {}
1801
Eric Fiselierfc92be82017-04-19 00:28:44 +00001802#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001803 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001804 void __move_assign(basic_string& __str, false_type)
1805 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001806 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001807 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001808#if _LIBCPP_STD_VER > 14
1809 _NOEXCEPT;
1810#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001811 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001812#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001813#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001814
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001815 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001816 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001817 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001818 _NOEXCEPT_(
1819 !__alloc_traits::propagate_on_container_move_assignment::value ||
1820 is_nothrow_move_assignable<allocator_type>::value)
1821 {__move_assign_alloc(__str, integral_constant<bool,
1822 __alloc_traits::propagate_on_container_move_assignment::value>());}
1823
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001824 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc2734962011-09-02 20:42:31 +00001825 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001826 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1827 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001828 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001829 }
1830
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001831 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001832 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001833 _NOEXCEPT
1834 {}
1835
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001836 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s);
1837 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001838
1839 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1840 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1841 pointer __p = __is_long()
1842 ? (__set_long_size(__n), __get_long_pointer())
1843 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001844 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001845 traits_type::assign(__p[__n], value_type());
1846 return *this;
1847 }
1848
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001849 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001850 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001851 __set_size(__newsz);
1852 __invalidate_iterators_past(__newsz);
1853 traits_type::assign(__p[__newsz], value_type());
1854 return *this;
1855 }
1856
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001857 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001859 template<class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001860 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001861 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001862 // assume that the ranges overlap, because we can't check during constant evaluation
1863 if (__libcpp_is_constant_evaluated())
1864 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001865 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001866 return data() <= __p && __p <= data() + size();
1867 }
1868
Louis Dionned24191c2021-08-19 12:39:16 -04001869 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1870 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001871 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001872 }
1873
1874 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1875 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001876 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001877 }
1878
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001879 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const basic_string&);
1880 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const value_type*, const basic_string&);
1881 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(value_type, const basic_string&);
1882 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const value_type*);
1883 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884};
1885
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001886// These declarations must appear before any functions are implicitly used
1887// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001888#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001889#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001890 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001891# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001892 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001893# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001894#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001895 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001896# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001897 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001898# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001899#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001900#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001901
1902
Louis Dionned59f8a52021-08-17 11:59:07 -04001903#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001904template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001905 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001906 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001907 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1908 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001909 >
1910basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1911 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001912
1913template<class _CharT,
1914 class _Traits,
1915 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001916 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001917 >
1918explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1919 -> basic_string<_CharT, _Traits, _Allocator>;
1920
1921template<class _CharT,
1922 class _Traits,
1923 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001924 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001925 class _Sz = typename allocator_traits<_Allocator>::size_type
1926 >
1927basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1928 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001929#endif
1930
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001932inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001934basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935{
Louis Dionne510450b2022-04-01 16:38:30 -04001936#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001937 if (!__libcpp_is_constant_evaluated()) {
1938 __c_node* __c = __get_db()->__find_c_and_lock(this);
1939 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001941 const_pointer __new_last = __get_pointer() + __pos;
1942 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001944 --__p;
1945 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1946 if (__i->base() > __new_last)
1947 {
1948 (*__p)->__c_ = nullptr;
1949 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001950 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001951 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001953 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954 }
1955 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001956#else
1957 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04001958#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959}
1960
1961template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001962_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier815ed732016-09-16 00:00:48 +00001963void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1964 size_type __sz,
1965 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001967 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001968 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001970 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001972 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973 {
1974 __set_short_size(__sz);
1975 __p = __get_short_pointer();
1976 }
1977 else
1978 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001979 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1980 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001981 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001983 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984 __set_long_size(__sz);
1985 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001986 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 traits_type::assign(__p[__sz], value_type());
1988}
1989
1990template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001991_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001993basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001995 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001996 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001998 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002000 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001 {
2002 __set_short_size(__sz);
2003 __p = __get_short_pointer();
2004 }
2005 else
2006 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002007 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2008 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002009 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002011 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012 __set_long_size(__sz);
2013 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002014 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015 traits_type::assign(__p[__sz], value_type());
2016}
2017
2018template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002019template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002020_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002021basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002022 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002024 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002026 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027}
2028
2029template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002030_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002032 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033{
2034 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002035 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002037 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002038 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002039 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040}
2041
2042template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002043_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002044basic_string<_CharT, _Traits, _Allocator>::basic_string(
2045 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002046 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047{
2048 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002049 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002050 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002051 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002052 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002053 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054}
2055
Martijn Vels5e7c9752020-03-04 17:52:46 -05002056template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002057_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Vels5e7c9752020-03-04 17:52:46 -05002058void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2059 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002060 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002061 __r_.first() = __rep();
2062
Martijn Vels5e7c9752020-03-04 17:52:46 -05002063 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002064 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002065 __p = __get_short_pointer();
2066 __set_short_size(__sz);
2067 } else {
2068 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002069 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002070 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2071 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002072 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002073 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002074 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002075 __set_long_size(__sz);
2076 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002077 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002078}
2079
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002081_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082void
2083basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2084{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002085 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002086 __r_.first() = __rep();
2087
Howard Hinnantc51e1022010-05-11 19:42:16 +00002088 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002089 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002091 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092 {
2093 __set_short_size(__n);
2094 __p = __get_short_pointer();
2095 }
2096 else
2097 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002098 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2099 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002100 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002101 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002102 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103 __set_long_size(__n);
2104 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002105 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 traits_type::assign(__p[__n], value_type());
2107}
2108
2109template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002110template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002111_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002112basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002113 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114{
2115 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002116 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117}
2118
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002120_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002121basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2122 size_type __pos, size_type __n,
2123 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002124 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125{
2126 size_type __str_sz = __str.size();
2127 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002128 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002129 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2130 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 _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002135_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow78dbe462016-11-14 18:22:19 +00002136basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002137 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002138 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002139{
Marshall Clowe46031a2018-07-02 18:41:15 +00002140 __self_view __sv0 = __t;
2141 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002142 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002143 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002144}
2145
2146template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002147template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002148_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002149basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002150 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002151{
Marshall Clowe46031a2018-07-02 18:41:15 +00002152 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002153 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002154 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002155}
2156
2157template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002158template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002159_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002160basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002161 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002162{
Marshall Clowe46031a2018-07-02 18:41:15 +00002163 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002164 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002165 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002166}
2167
2168template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002170_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002171__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002173 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2174>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2176{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002177 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178#ifndef _LIBCPP_NO_EXCEPTIONS
2179 try
2180 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002181#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182 for (; __first != __last; ++__first)
2183 push_back(*__first);
2184#ifndef _LIBCPP_NO_EXCEPTIONS
2185 }
2186 catch (...)
2187 {
2188 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002189 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190 throw;
2191 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002192#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193}
2194
2195template <class _CharT, class _Traits, class _Allocator>
2196template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002197_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002198__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002200 __is_cpp17_forward_iterator<_ForwardIterator>::value
2201>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2203{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002204 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002205 __r_.first() = __rep();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002206 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002208 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002210 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 {
2212 __set_short_size(__sz);
2213 __p = __get_short_pointer();
2214 }
2215 else
2216 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002217 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2218 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002219 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002221 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 __set_long_size(__sz);
2223 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002224
2225#ifndef _LIBCPP_NO_EXCEPTIONS
2226 try
2227 {
2228#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002229 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 traits_type::assign(*__p, *__first);
2231 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002232#ifndef _LIBCPP_NO_EXCEPTIONS
2233 }
2234 catch (...)
2235 {
2236 if (__is_long())
2237 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2238 throw;
2239 }
2240#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241}
2242
2243template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002244_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2246{
Nikolas Klauser98833542022-05-07 22:20:23 +02002247 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002249 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250}
2251
2252template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002253_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254void
2255basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2256 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002257 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 +00002258{
2259 size_type __ms = max_size();
2260 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002261 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 pointer __old_p = __get_pointer();
2263 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002264 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002266 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2267 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002268 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002269 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002271 traits_type::copy(std::__to_address(__p),
2272 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002274 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2276 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002277 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2278 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002279 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002280 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002282 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2284 __set_long_size(__old_sz);
2285 traits_type::assign(__p[__old_sz], value_type());
2286}
2287
2288template <class _CharT, class _Traits, class _Allocator>
2289void
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002290_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2292 size_type __n_copy, size_type __n_del, size_type __n_add)
2293{
2294 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002295 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002296 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 pointer __old_p = __get_pointer();
2298 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002299 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002301 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2302 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002303 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002304 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002306 traits_type::copy(std::__to_address(__p),
2307 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2309 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002310 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2311 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002312 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002313 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2314 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002316 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317}
2318
2319// assign
2320
2321template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002322template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002323_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsb6a08b62020-04-10 18:36:31 -04002324basic_string<_CharT, _Traits, _Allocator>&
2325basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002326 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002327 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002328 if (__n < __cap) {
2329 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2330 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002331 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002332 traits_type::assign(__p[__n], value_type());
2333 __invalidate_iterators_past(__n);
2334 } else {
2335 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2336 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2337 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002338 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002339}
2340
2341template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002342_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002344basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2345 const value_type* __s, size_type __n) {
2346 size_type __cap = capacity();
2347 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002348 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002349 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002350 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002351 } else {
2352 size_type __sz = size();
2353 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002354 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002355 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002356}
2357
2358template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002359_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002360basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002361basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002363 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002364 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002365 ? __assign_short(__s, __n)
2366 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002367}
2368
2369template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002370_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371basic_string<_CharT, _Traits, _Allocator>&
2372basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2373{
2374 size_type __cap = capacity();
2375 if (__cap < __n)
2376 {
2377 size_type __sz = size();
2378 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2379 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002380 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002382 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383}
2384
2385template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002386_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387basic_string<_CharT, _Traits, _Allocator>&
2388basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2389{
2390 pointer __p;
2391 if (__is_long())
2392 {
2393 __p = __get_long_pointer();
2394 __set_long_size(1);
2395 }
2396 else
2397 {
2398 __p = __get_short_pointer();
2399 __set_short_size(1);
2400 }
2401 traits_type::assign(*__p, __c);
2402 traits_type::assign(*++__p, value_type());
2403 __invalidate_iterators_past(1);
2404 return *this;
2405}
2406
2407template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002408_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002409basic_string<_CharT, _Traits, _Allocator>&
2410basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2411{
Martijn Vels596e3de2020-02-26 15:55:49 -05002412 if (this != &__str) {
2413 __copy_assign_alloc(__str);
2414 if (!__is_long()) {
2415 if (!__str.__is_long()) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002416 __r_.first() = __str.__r_.first();
Martijn Vels596e3de2020-02-26 15:55:49 -05002417 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002418 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002419 }
2420 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002421 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002422 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002423 }
2424 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002425}
2426
Eric Fiselierfc92be82017-04-19 00:28:44 +00002427#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002428
2429template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002430inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002431void
2432basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002433 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002434{
2435 if (__alloc() != __str.__alloc())
2436 assign(__str);
2437 else
2438 __move_assign(__str, true_type());
2439}
2440
2441template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002442inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002443void
2444basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002445#if _LIBCPP_STD_VER > 14
2446 _NOEXCEPT
2447#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002448 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002449#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002450{
marshall2ed41622019-11-27 07:13:00 -08002451 if (__is_long()) {
2452 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2453 __get_long_cap());
2454#if _LIBCPP_STD_VER <= 14
2455 if (!is_nothrow_move_assignable<allocator_type>::value) {
2456 __set_short_size(0);
2457 traits_type::assign(__get_short_pointer()[0], value_type());
2458 }
2459#endif
2460 }
2461 __move_assign_alloc(__str);
2462 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002463 if (__libcpp_is_constant_evaluated()) {
2464 __str.__default_init();
2465 } else {
2466 __str.__set_short_size(0);
2467 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2468 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002469}
2470
2471template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002472inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002473basic_string<_CharT, _Traits, _Allocator>&
2474basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002475 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002476{
2477 __move_assign(__str, integral_constant<bool,
2478 __alloc_traits::propagate_on_container_move_assignment::value>());
2479 return *this;
2480}
2481
2482#endif
2483
2484template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002486_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002487__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002489 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002491>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2493{
Marshall Clow958362f2016-09-05 01:54:30 +00002494 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002495 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002496 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497}
2498
2499template <class _CharT, class _Traits, class _Allocator>
2500template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002501_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002502__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002504 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002506>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2508{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002510 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002511 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002512
2513 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2514 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002516 if (__cap < __n)
2517 {
2518 size_type __sz = size();
2519 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2520 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002521 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002522 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002523 traits_type::assign(*__p, *__first);
2524 traits_type::assign(*__p, value_type());
2525 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002526 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527 }
2528 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002529 {
2530 const basic_string __temp(__first, __last, __alloc());
2531 assign(__temp.data(), __temp.size());
2532 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533 return *this;
2534}
2535
2536template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002537_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538basic_string<_CharT, _Traits, _Allocator>&
2539basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2540{
2541 size_type __sz = __str.size();
2542 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002543 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002544 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545}
2546
2547template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002548template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002549_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002550__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002551<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002552 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2553 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002554 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002555>
Marshall Clow82513342016-09-24 22:45:42 +00002556basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002557{
Marshall Clow82513342016-09-24 22:45:42 +00002558 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002559 size_type __sz = __sv.size();
2560 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002561 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002562 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002563}
2564
2565
2566template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002567_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002569basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2570 return __assign_external(__s, traits_type::length(__s));
2571}
2572
2573template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002574_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002575basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002576basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002578 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002579 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002580 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002581 ? __assign_short(__s, traits_type::length(__s))
2582 : __assign_external(__s, traits_type::length(__s)))
2583 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585// append
2586
2587template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002588_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002590basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002592 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 size_type __cap = capacity();
2594 size_type __sz = size();
2595 if (__cap - __sz >= __n)
2596 {
2597 if (__n)
2598 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002599 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600 traits_type::copy(__p + __sz, __s, __n);
2601 __sz += __n;
2602 __set_size(__sz);
2603 traits_type::assign(__p[__sz], value_type());
2604 }
2605 }
2606 else
2607 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2608 return *this;
2609}
2610
2611template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002612_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613basic_string<_CharT, _Traits, _Allocator>&
2614basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2615{
2616 if (__n)
2617 {
2618 size_type __cap = capacity();
2619 size_type __sz = size();
2620 if (__cap - __sz < __n)
2621 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2622 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002623 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 __sz += __n;
2625 __set_size(__sz);
2626 traits_type::assign(__p[__sz], value_type());
2627 }
2628 return *this;
2629}
2630
2631template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002632_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002633basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2634{
2635 if (__n)
2636 {
2637 size_type __cap = capacity();
2638 size_type __sz = size();
2639 if (__cap - __sz < __n)
2640 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2641 pointer __p = __get_pointer();
2642 __sz += __n;
2643 __set_size(__sz);
2644 traits_type::assign(__p[__sz], value_type());
2645 }
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 +00002650void
2651basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2652{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002653 bool __is_short = !__is_long();
2654 size_type __cap;
2655 size_type __sz;
2656 if (__is_short)
2657 {
2658 __cap = __min_cap - 1;
2659 __sz = __get_short_size();
2660 }
2661 else
2662 {
2663 __cap = __get_long_cap() - 1;
2664 __sz = __get_long_size();
2665 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002667 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002669 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002670 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002671 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002672 if (__is_short)
2673 {
2674 __p = __get_short_pointer() + __sz;
2675 __set_short_size(__sz+1);
2676 }
2677 else
2678 {
2679 __p = __get_long_pointer() + __sz;
2680 __set_long_size(__sz+1);
2681 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 traits_type::assign(*__p, __c);
2683 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684}
2685
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686template <class _CharT, class _Traits, class _Allocator>
2687template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002688_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002689__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002690<
2691 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2692 basic_string<_CharT, _Traits, _Allocator>&
2693>
2694basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002695 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696{
2697 size_type __sz = size();
2698 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002699 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700 if (__n)
2701 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002702 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2703 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002704 {
2705 if (__cap - __sz < __n)
2706 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2707 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002708 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002709 traits_type::assign(*__p, *__first);
2710 traits_type::assign(*__p, value_type());
2711 __set_size(__sz + __n);
2712 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002713 else
2714 {
2715 const basic_string __temp(__first, __last, __alloc());
2716 append(__temp.data(), __temp.size());
2717 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718 }
2719 return *this;
2720}
2721
2722template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002723inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724basic_string<_CharT, _Traits, _Allocator>&
2725basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2726{
2727 return append(__str.data(), __str.size());
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 +00002732basic_string<_CharT, _Traits, _Allocator>&
2733basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2734{
2735 size_type __sz = __str.size();
2736 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002737 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002738 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739}
2740
2741template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002742template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002743_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002744 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002745 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002746 __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 +00002747 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002748 >
Marshall Clow82513342016-09-24 22:45:42 +00002749basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002750{
Marshall Clow82513342016-09-24 22:45:42 +00002751 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002752 size_type __sz = __sv.size();
2753 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002754 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002755 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002756}
2757
2758template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002759_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002761basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002763 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 return append(__s, traits_type::length(__s));
2765}
2766
2767// insert
2768
2769template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002770_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002772basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002774 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775 size_type __sz = size();
2776 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002777 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002779 if (__libcpp_is_constant_evaluated()) {
2780 if (__cap - __sz >= __n)
2781 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2782 else
2783 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2784 return *this;
2785 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 if (__cap - __sz >= __n)
2787 {
2788 if (__n)
2789 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002790 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 size_type __n_move = __sz - __pos;
2792 if (__n_move != 0)
2793 {
2794 if (__p + __pos <= __s && __s < __p + __sz)
2795 __s += __n;
2796 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2797 }
2798 traits_type::move(__p + __pos, __s, __n);
2799 __sz += __n;
2800 __set_size(__sz);
2801 traits_type::assign(__p[__sz], value_type());
2802 }
2803 }
2804 else
2805 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2806 return *this;
2807}
2808
2809template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002810_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811basic_string<_CharT, _Traits, _Allocator>&
2812basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2813{
2814 size_type __sz = size();
2815 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002816 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817 if (__n)
2818 {
2819 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002820 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 if (__cap - __sz >= __n)
2822 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002823 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 size_type __n_move = __sz - __pos;
2825 if (__n_move != 0)
2826 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2827 }
2828 else
2829 {
2830 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002831 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 }
2833 traits_type::assign(__p + __pos, __n, __c);
2834 __sz += __n;
2835 __set_size(__sz);
2836 traits_type::assign(__p[__sz], value_type());
2837 }
2838 return *this;
2839}
2840
2841template <class _CharT, class _Traits, class _Allocator>
2842template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002843_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002844__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002846 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002847 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002848>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2850{
Nikolas Klausereed25832021-12-15 01:32:30 +01002851 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2852 "string::insert(iterator, range) called with an iterator not"
2853 " referring to this string");
2854 const basic_string __temp(__first, __last, __alloc());
2855 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856}
2857
2858template <class _CharT, class _Traits, class _Allocator>
2859template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002860_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002861__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002863 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002865>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2867{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002868 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2869 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002870
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002872 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2873 if (__n == 0)
2874 return begin() + __ip;
2875
2876 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002878 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002880 else
2881 {
2882 const basic_string __temp(__first, __last, __alloc());
2883 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2884 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885}
2886
2887template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002888inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889basic_string<_CharT, _Traits, _Allocator>&
2890basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2891{
2892 return insert(__pos1, __str.data(), __str.size());
2893}
2894
2895template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002896_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897basic_string<_CharT, _Traits, _Allocator>&
2898basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2899 size_type __pos2, size_type __n)
2900{
2901 size_type __str_sz = __str.size();
2902 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002903 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002904 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905}
2906
2907template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002908template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002909_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002910__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002911<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002912 __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 +00002913 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002914>
Marshall Clow82513342016-09-24 22:45:42 +00002915basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002916 size_type __pos2, size_type __n)
2917{
Marshall Clow82513342016-09-24 22:45:42 +00002918 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002919 size_type __str_sz = __sv.size();
2920 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002921 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002922 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002923}
2924
2925template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002926_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002928basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002929{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002930 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 return insert(__pos, __s, traits_type::length(__s));
2932}
2933
2934template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002935_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936typename basic_string<_CharT, _Traits, _Allocator>::iterator
2937basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2938{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002939 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2940 "string::insert(iterator, character) called with an iterator not"
2941 " referring to this string");
2942
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 size_type __ip = static_cast<size_type>(__pos - begin());
2944 size_type __sz = size();
2945 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002946 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 if (__cap == __sz)
2948 {
2949 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002950 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951 }
2952 else
2953 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002954 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955 size_type __n_move = __sz - __ip;
2956 if (__n_move != 0)
2957 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2958 }
2959 traits_type::assign(__p[__ip], __c);
2960 traits_type::assign(__p[++__sz], value_type());
2961 __set_size(__sz);
2962 return begin() + static_cast<difference_type>(__ip);
2963}
2964
2965template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002966inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967typename basic_string<_CharT, _Traits, _Allocator>::iterator
2968basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2969{
Nikolas Klausereed25832021-12-15 01:32:30 +01002970 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2971 "string::insert(iterator, n, value) called with an iterator not"
2972 " referring to this string");
2973 difference_type __p = __pos - begin();
2974 insert(static_cast<size_type>(__p), __n, __c);
2975 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976}
2977
2978// replace
2979
2980template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002981_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002983basic_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 +00002984 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002986 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 size_type __sz = size();
2988 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002989 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002990 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991 size_type __cap = capacity();
2992 if (__cap - __sz + __n1 >= __n2)
2993 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002994 if (__libcpp_is_constant_evaluated()) {
2995 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
2996 return *this;
2997 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002998 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999 if (__n1 != __n2)
3000 {
3001 size_type __n_move = __sz - __pos - __n1;
3002 if (__n_move != 0)
3003 {
3004 if (__n1 > __n2)
3005 {
3006 traits_type::move(__p + __pos, __s, __n2);
3007 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003008 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009 }
3010 if (__p + __pos < __s && __s < __p + __sz)
3011 {
3012 if (__p + __pos + __n1 <= __s)
3013 __s += __n2 - __n1;
3014 else // __p + __pos < __s < __p + __pos + __n1
3015 {
3016 traits_type::move(__p + __pos, __s, __n1);
3017 __pos += __n1;
3018 __s += __n2;
3019 __n2 -= __n1;
3020 __n1 = 0;
3021 }
3022 }
3023 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3024 }
3025 }
3026 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003027 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028 }
3029 else
3030 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3031 return *this;
3032}
3033
3034template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003035_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036basic_string<_CharT, _Traits, _Allocator>&
3037basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3038{
3039 size_type __sz = size();
3040 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003041 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003042 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003044 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045 if (__cap - __sz + __n1 >= __n2)
3046 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003047 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048 if (__n1 != __n2)
3049 {
3050 size_type __n_move = __sz - __pos - __n1;
3051 if (__n_move != 0)
3052 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3053 }
3054 }
3055 else
3056 {
3057 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003058 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059 }
3060 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003061 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062}
3063
3064template <class _CharT, class _Traits, class _Allocator>
3065template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003066_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003067__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003069 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003071>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003072basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 _InputIterator __j1, _InputIterator __j2)
3074{
Marshall Clow958362f2016-09-05 01:54:30 +00003075 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003076 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077}
3078
3079template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003080inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081basic_string<_CharT, _Traits, _Allocator>&
3082basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3083{
3084 return replace(__pos1, __n1, __str.data(), __str.size());
3085}
3086
3087template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003088_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089basic_string<_CharT, _Traits, _Allocator>&
3090basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3091 size_type __pos2, size_type __n2)
3092{
3093 size_type __str_sz = __str.size();
3094 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003095 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003096 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097}
3098
3099template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003100template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003101_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003102__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003103<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003104 __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 +00003105 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003106>
Marshall Clow82513342016-09-24 22:45:42 +00003107basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003108 size_type __pos2, size_type __n2)
3109{
Marshall Clow82513342016-09-24 22:45:42 +00003110 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003111 size_type __str_sz = __sv.size();
3112 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003113 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003114 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003115}
3116
3117template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003118_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003119basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003120basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003122 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123 return replace(__pos, __n1, __s, traits_type::length(__s));
3124}
3125
3126template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003127inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003129basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130{
3131 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3132 __str.data(), __str.size());
3133}
3134
3135template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003136inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003138basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139{
3140 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3141}
3142
3143template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003144inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003146basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147{
3148 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3149}
3150
3151template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003152inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003154basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155{
3156 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3157}
3158
3159// erase
3160
Martijn Velsa81fc792020-02-26 13:25:43 -05003161// 'externally instantiated' erase() implementation, called when __n != npos.
3162// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003164_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003165void
3166basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3167 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169 if (__n)
3170 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003171 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003172 value_type* __p = std::__to_address(__get_pointer());
3173 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174 size_type __n_move = __sz - __pos - __n;
3175 if (__n_move != 0)
3176 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003177 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003179}
3180
3181template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003182_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003183basic_string<_CharT, _Traits, _Allocator>&
3184basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3185 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003186 if (__pos > size())
3187 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003188 if (__n == npos) {
3189 __erase_to_end(__pos);
3190 } else {
3191 __erase_external_with_move(__pos, __n);
3192 }
3193 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194}
3195
3196template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003197inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198typename basic_string<_CharT, _Traits, _Allocator>::iterator
3199basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3200{
Nikolas Klausereed25832021-12-15 01:32:30 +01003201 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3202 "string::erase(iterator) called with an iterator not"
3203 " referring to this string");
3204
3205 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3206 iterator __b = begin();
3207 size_type __r = static_cast<size_type>(__pos - __b);
3208 erase(__r, 1);
3209 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210}
3211
3212template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003213inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214typename basic_string<_CharT, _Traits, _Allocator>::iterator
3215basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3216{
Nikolas Klausereed25832021-12-15 01:32:30 +01003217 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3218 "string::erase(iterator, iterator) called with an iterator not"
3219 " referring to this string");
3220
3221 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3222 iterator __b = begin();
3223 size_type __r = static_cast<size_type>(__first - __b);
3224 erase(__r, static_cast<size_type>(__last - __first));
3225 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226}
3227
3228template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003229inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230void
3231basic_string<_CharT, _Traits, _Allocator>::pop_back()
3232{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003233 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003234 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235}
3236
3237template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003238inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003240basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003242 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243 if (__is_long())
3244 {
3245 traits_type::assign(*__get_long_pointer(), value_type());
3246 __set_long_size(0);
3247 }
3248 else
3249 {
3250 traits_type::assign(*__get_short_pointer(), value_type());
3251 __set_short_size(0);
3252 }
3253}
3254
3255template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003256inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257void
3258basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3259{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003260 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261}
3262
3263template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003264_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265void
3266basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3267{
3268 size_type __sz = size();
3269 if (__n > __sz)
3270 append(__n - __sz, __c);
3271 else
3272 __erase_to_end(__n);
3273}
3274
3275template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003276_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003277basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3278{
3279 size_type __sz = size();
3280 if (__n > __sz) {
3281 __append_default_init(__n - __sz);
3282 } else
3283 __erase_to_end(__n);
3284}
3285
3286template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003287inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003289basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003291 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003292 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3293 return __m - __alignment;
3294 } else {
3295 bool __uses_lsb = __endian_factor == 2;
3296 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3297 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298}
3299
3300template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003301_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302void
Marek Kurdejc9848142020-11-26 10:07:16 +01003303basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304{
Marek Kurdejc9848142020-11-26 10:07:16 +01003305 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003306 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003307
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003308 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3309 // and later (since P0966R1), however we provide consistent behavior in all Standard
3310 // modes because this function is instantiated in the shared library.
3311 if (__requested_capacity <= capacity())
3312 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003313
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003314 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003315 __target_capacity = __recommend(__target_capacity);
3316 if (__target_capacity == capacity()) return;
3317
3318 __shrink_or_extend(__target_capacity);
3319}
3320
3321template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003322inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003323void
3324basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3325{
3326 size_type __target_capacity = __recommend(size());
3327 if (__target_capacity == capacity()) return;
3328
3329 __shrink_or_extend(__target_capacity);
3330}
3331
3332template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003333inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003334void
3335basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3336{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337 size_type __cap = capacity();
3338 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003339
3340 pointer __new_data, __p;
3341 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003342 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003344 __was_long = true;
3345 __now_long = false;
3346 __new_data = __get_short_pointer();
3347 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003349 else
3350 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003351 if (__target_capacity > __cap) {
3352 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3353 __new_data = __allocation.ptr;
3354 __target_capacity = __allocation.count - 1;
3355 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003356 else
3357 {
3358 #ifndef _LIBCPP_NO_EXCEPTIONS
3359 try
3360 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003361 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003362 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3363 __new_data = __allocation.ptr;
3364 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003365 #ifndef _LIBCPP_NO_EXCEPTIONS
3366 }
3367 catch (...)
3368 {
3369 return;
3370 }
3371 #else // _LIBCPP_NO_EXCEPTIONS
3372 if (__new_data == nullptr)
3373 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003374 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003375 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003376 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003377 __now_long = true;
3378 __was_long = __is_long();
3379 __p = __get_pointer();
3380 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003381 traits_type::copy(std::__to_address(__new_data),
3382 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003383 if (__was_long)
3384 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3385 if (__now_long)
3386 {
3387 __set_long_cap(__target_capacity+1);
3388 __set_long_size(__sz);
3389 __set_long_pointer(__new_data);
3390 }
3391 else
3392 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003393 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394}
3395
3396template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003397inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003399basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003401 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003402 return *(data() + __pos);
3403}
3404
3405template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003406inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003408basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003410 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411 return *(__get_pointer() + __pos);
3412}
3413
3414template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003415_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3417basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3418{
3419 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003420 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003421 return (*this)[__n];
3422}
3423
3424template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003425_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426typename basic_string<_CharT, _Traits, _Allocator>::reference
3427basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3428{
3429 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003430 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431 return (*this)[__n];
3432}
3433
3434template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003435inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003437basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003439 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440 return *__get_pointer();
3441}
3442
3443template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003444inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003446basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003448 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449 return *data();
3450}
3451
3452template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003453inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003455basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003457 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458 return *(__get_pointer() + size() - 1);
3459}
3460
3461template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003462inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003464basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003466 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003467 return *(data() + size() - 1);
3468}
3469
3470template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003471_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003473basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474{
3475 size_type __sz = size();
3476 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003477 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003478 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479 traits_type::copy(__s, data() + __pos, __rlen);
3480 return __rlen;
3481}
3482
3483template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003484inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485basic_string<_CharT, _Traits, _Allocator>
3486basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3487{
3488 return basic_string(*this, __pos, __n, __alloc());
3489}
3490
3491template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003492inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493void
3494basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003495#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003496 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003497#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003498 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003499 __is_nothrow_swappable<allocator_type>::value)
3500#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501{
Nikolas Klauser98833542022-05-07 22:20:23 +02003502 if (!__is_long())
3503 std::__debug_db_invalidate_all(this);
3504 if (!__str.__is_long())
3505 std::__debug_db_invalidate_all(&__str);
3506 std::__debug_db_swap(this, &__str);
3507
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003508 _LIBCPP_ASSERT(
3509 __alloc_traits::propagate_on_container_swap::value ||
3510 __alloc_traits::is_always_equal::value ||
3511 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003512 std::swap(__r_.first(), __str.__r_.first());
3513 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514}
3515
3516// find
3517
3518template <class _Traits>
3519struct _LIBCPP_HIDDEN __traits_eq
3520{
3521 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003522 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003523 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3524 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525};
3526
3527template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003528_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003530basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003531 size_type __pos,
3532 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003534 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003535 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003536 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537}
3538
3539template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003540inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003542basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3543 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003545 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003546 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547}
3548
3549template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003550template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003551_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003552__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003553<
3554 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3555 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003556>
Marshall Clowe46031a2018-07-02 18:41:15 +00003557basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003558 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003559{
Marshall Clowe46031a2018-07-02 18:41:15 +00003560 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003561 return __str_find<value_type, size_type, traits_type, npos>
3562 (data(), size(), __sv.data(), __pos, __sv.size());
3563}
3564
3565template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003566inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003567typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003568basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003569 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003571 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003572 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003573 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574}
3575
3576template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003577_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003579basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3580 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003582 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003583 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584}
3585
3586// rfind
3587
3588template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003589_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003591basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003592 size_type __pos,
3593 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003595 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003596 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003597 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598}
3599
3600template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003601inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003603basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3604 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003606 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003607 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608}
3609
3610template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003611template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003612_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003613__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003614<
3615 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3616 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003617>
Marshall Clowe46031a2018-07-02 18:41:15 +00003618basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003619 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003620{
Marshall Clowe46031a2018-07-02 18:41:15 +00003621 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003622 return __str_rfind<value_type, size_type, traits_type, npos>
3623 (data(), size(), __sv.data(), __pos, __sv.size());
3624}
3625
3626template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003627inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003628typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003629basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003630 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003632 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003633 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003634 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635}
3636
3637template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003638_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003640basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3641 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003643 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003644 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645}
3646
3647// find_first_of
3648
3649template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003650_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003652basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003653 size_type __pos,
3654 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003656 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003657 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003658 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659}
3660
3661template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003662inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003664basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3665 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003667 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003668 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669}
3670
3671template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003672template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003673_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003674__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003675<
3676 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3677 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003678>
Marshall Clowe46031a2018-07-02 18:41:15 +00003679basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003680 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003681{
Marshall Clowe46031a2018-07-02 18:41:15 +00003682 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003683 return __str_find_first_of<value_type, size_type, traits_type, npos>
3684 (data(), size(), __sv.data(), __pos, __sv.size());
3685}
3686
3687template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003688inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003689typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003690basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003691 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003693 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003695 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696}
3697
3698template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003699inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003701basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3702 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703{
3704 return find(__c, __pos);
3705}
3706
3707// find_last_of
3708
3709template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003710inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003712basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003713 size_type __pos,
3714 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003716 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003717 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003718 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719}
3720
3721template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003722inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003724basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3725 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003727 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003728 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729}
3730
3731template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003732template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003733_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003734__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003735<
3736 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3737 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003738>
Marshall Clowe46031a2018-07-02 18:41:15 +00003739basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003740 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003741{
Marshall Clowe46031a2018-07-02 18:41:15 +00003742 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003743 return __str_find_last_of<value_type, size_type, traits_type, npos>
3744 (data(), size(), __sv.data(), __pos, __sv.size());
3745}
3746
3747template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003748inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003749typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003750basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003751 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003752{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003753 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003754 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003755 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756}
3757
3758template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003759inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003760typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003761basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3762 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763{
3764 return rfind(__c, __pos);
3765}
3766
3767// find_first_not_of
3768
3769template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003770_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003772basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003773 size_type __pos,
3774 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003776 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003777 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003778 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779}
3780
3781template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003782inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003784basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3785 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003787 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003788 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789}
3790
3791template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003792template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003793_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003794__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003795<
3796 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3797 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003798>
Marshall Clowe46031a2018-07-02 18:41:15 +00003799basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003800 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003801{
Marshall Clowe46031a2018-07-02 18:41:15 +00003802 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003803 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3804 (data(), size(), __sv.data(), __pos, __sv.size());
3805}
3806
3807template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003808inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003809typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003810basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003811 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003813 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003814 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003815 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816}
3817
3818template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003819inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003821basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3822 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003824 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003825 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003826}
3827
3828// find_last_not_of
3829
3830template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003831_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003833basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003834 size_type __pos,
3835 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003837 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003838 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003839 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840}
3841
3842template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003843inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003845basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3846 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003848 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003849 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003850}
3851
3852template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003853template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003854_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003855__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003856<
3857 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3858 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003859>
Marshall Clowe46031a2018-07-02 18:41:15 +00003860basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003861 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003862{
Marshall Clowe46031a2018-07-02 18:41:15 +00003863 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003864 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3865 (data(), size(), __sv.data(), __pos, __sv.size());
3866}
3867
3868template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003869inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003870typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003871basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003872 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003874 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003875 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003876 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877}
3878
3879template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003880inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003882basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3883 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003885 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003886 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887}
3888
3889// compare
3890
3891template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003892template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003893_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003894__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003895<
3896 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3897 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003898>
zoecarver1997e0a2021-02-05 11:54:47 -08003899basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900{
Marshall Clowe46031a2018-07-02 18:41:15 +00003901 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003902 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003903 size_t __rhs_sz = __sv.size();
3904 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003905 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003906 if (__result != 0)
3907 return __result;
3908 if (__lhs_sz < __rhs_sz)
3909 return -1;
3910 if (__lhs_sz > __rhs_sz)
3911 return 1;
3912 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913}
3914
3915template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003916inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003918basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003919{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003920 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921}
3922
3923template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003924inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003926basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3927 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003928 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003929 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003931 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932 size_type __sz = size();
3933 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003934 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003935 size_type __rlen = std::min(__n1, __sz - __pos1);
3936 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937 if (__r == 0)
3938 {
3939 if (__rlen < __n2)
3940 __r = -1;
3941 else if (__rlen > __n2)
3942 __r = 1;
3943 }
3944 return __r;
3945}
3946
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003947template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003948template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003949_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003950__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003951<
3952 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3953 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003954>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003955basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3956 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003957 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003958{
Marshall Clowe46031a2018-07-02 18:41:15 +00003959 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003960 return compare(__pos1, __n1, __sv.data(), __sv.size());
3961}
3962
3963template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003964inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003965int
3966basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3967 size_type __n1,
3968 const basic_string& __str) const
3969{
3970 return compare(__pos1, __n1, __str.data(), __str.size());
3971}
3972
3973template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +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 Clow82513342016-09-24 22:45:42 +00003977<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003978 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3979 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003980 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003981>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003982basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3983 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003984 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003985 size_type __pos2,
3986 size_type __n2) const
3987{
Marshall Clow82513342016-09-24 22:45:42 +00003988 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003989 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3990}
3991
3992template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003993_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003994int
3995basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3996 size_type __n1,
3997 const basic_string& __str,
3998 size_type __pos2,
3999 size_type __n2) const
4000{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004001 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004002}
4003
4004template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004005_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004006int
4007basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4008{
4009 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4010 return compare(0, npos, __s, traits_type::length(__s));
4011}
4012
4013template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004014_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004015int
4016basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4017 size_type __n1,
4018 const value_type* __s) const
4019{
4020 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4021 return compare(__pos1, __n1, __s, traits_type::length(__s));
4022}
4023
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024// __invariants
4025
4026template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004027inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028bool
4029basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4030{
4031 if (size() > capacity())
4032 return false;
4033 if (capacity() < __min_cap - 1)
4034 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004035 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004037 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038 return false;
4039 return true;
4040}
4041
Vedant Kumar55e007e2018-03-08 21:15:26 +00004042// __clear_and_shrink
4043
4044template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004045inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne173f29e2019-05-29 16:01:36 +00004046void
Marshall Clowe60a7182018-05-29 17:04:37 +00004047basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004048{
4049 clear();
4050 if(__is_long())
4051 {
4052 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4053 __set_long_cap(0);
4054 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004055 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004056 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004057}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004058
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059// operator==
4060
4061template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004062inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063bool
4064operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004065 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066{
Mark de Weverb4830e62022-07-19 07:56:23 +02004067#if _LIBCPP_STD_VER > 17
4068 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4069#else
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004070 size_t __lhs_sz = __lhs.size();
4071 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4072 __rhs.data(),
4073 __lhs_sz) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004074#endif
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004075}
4076
4077template<class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004078inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004079bool
4080operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4081 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4082{
4083 size_t __lhs_sz = __lhs.size();
4084 if (__lhs_sz != __rhs.size())
4085 return false;
4086 const char* __lp = __lhs.data();
4087 const char* __rp = __rhs.data();
4088 if (__lhs.__is_long())
4089 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4090 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4091 if (*__lp != *__rp)
4092 return false;
4093 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004094}
4095
Mark de Weverb4830e62022-07-19 07:56:23 +02004096#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004098inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004100operator==(const _CharT* __lhs,
4101 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004103 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4104 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4105 size_t __lhs_len = _Traits::length(__lhs);
4106 if (__lhs_len != __rhs.size()) return false;
4107 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108}
Mark de Weverb4830e62022-07-19 07:56:23 +02004109#endif // _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004112inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004114operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4115 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116{
Mark de Weverb4830e62022-07-19 07:56:23 +02004117#if _LIBCPP_STD_VER > 17
4118 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4119#else
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004120 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4121 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4122 size_t __rhs_len = _Traits::length(__rhs);
4123 if (__rhs_len != __lhs.size()) return false;
4124 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004125#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126}
4127
Mark de Weverb4830e62022-07-19 07:56:23 +02004128#if _LIBCPP_STD_VER > 17
4129
4130template <class _CharT, class _Traits, class _Allocator>
4131_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
4132 const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4133 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
4134 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4135}
4136
4137template <class _CharT, class _Traits, class _Allocator>
4138_LIBCPP_HIDE_FROM_ABI constexpr auto
4139operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
4140 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4141}
4142
4143#else // _LIBCPP_STD_VER > 17
4144
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004145template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004146inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147bool
4148operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004149 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150{
4151 return !(__lhs == __rhs);
4152}
4153
4154template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004155inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004157operator!=(const _CharT* __lhs,
4158 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159{
4160 return !(__lhs == __rhs);
4161}
4162
4163template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004164inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004166operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4167 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168{
4169 return !(__lhs == __rhs);
4170}
4171
4172// operator<
4173
4174template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004175inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176bool
4177operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004178 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004180 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181}
4182
4183template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004184inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004186operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4187 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004189 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190}
4191
4192template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004193inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004195operator< (const _CharT* __lhs,
4196 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197{
4198 return __rhs.compare(__lhs) > 0;
4199}
4200
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201// operator>
4202
4203template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004204inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205bool
4206operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004207 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208{
4209 return __rhs < __lhs;
4210}
4211
4212template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004213inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004215operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4216 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217{
4218 return __rhs < __lhs;
4219}
4220
4221template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004222inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004224operator> (const _CharT* __lhs,
4225 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226{
4227 return __rhs < __lhs;
4228}
4229
4230// operator<=
4231
4232template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004233inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234bool
4235operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004236 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237{
4238 return !(__rhs < __lhs);
4239}
4240
4241template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004242inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004244operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4245 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246{
4247 return !(__rhs < __lhs);
4248}
4249
4250template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004251inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004253operator<=(const _CharT* __lhs,
4254 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255{
4256 return !(__rhs < __lhs);
4257}
4258
4259// operator>=
4260
4261template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004262inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263bool
4264operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004265 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004266{
4267 return !(__lhs < __rhs);
4268}
4269
4270template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004271inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004273operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4274 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275{
4276 return !(__lhs < __rhs);
4277}
4278
4279template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004280inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004282operator>=(const _CharT* __lhs,
4283 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284{
4285 return !(__lhs < __rhs);
4286}
Mark de Weverb4830e62022-07-19 07:56:23 +02004287#endif // _LIBCPP_STD_VER > 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288
4289// operator +
4290
4291template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004292_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293basic_string<_CharT, _Traits, _Allocator>
4294operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4295 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4296{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004297 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004298 auto __lhs_sz = __lhs.size();
4299 auto __rhs_sz = __rhs.size();
4300 _String __r(__uninitialized_size_tag(),
4301 __lhs_sz + __rhs_sz,
4302 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4303 auto __ptr = std::__to_address(__r.__get_pointer());
4304 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4305 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4306 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307 return __r;
4308}
4309
4310template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004311_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004312basic_string<_CharT, _Traits, _Allocator>
4313operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4314{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004315 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004316 auto __lhs_sz = _Traits::length(__lhs);
4317 auto __rhs_sz = __rhs.size();
4318 _String __r(__uninitialized_size_tag(),
4319 __lhs_sz + __rhs_sz,
4320 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4321 auto __ptr = std::__to_address(__r.__get_pointer());
4322 _Traits::copy(__ptr, __lhs, __lhs_sz);
4323 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4324 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325 return __r;
4326}
4327
4328template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004329_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330basic_string<_CharT, _Traits, _Allocator>
4331operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4332{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004333 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004334 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004335 _String __r(__uninitialized_size_tag(),
4336 __rhs_sz + 1,
4337 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4338 auto __ptr = std::__to_address(__r.__get_pointer());
4339 _Traits::assign(__ptr, 1, __lhs);
4340 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4341 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342 return __r;
4343}
4344
4345template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004346inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347basic_string<_CharT, _Traits, _Allocator>
4348operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4349{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004350 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004351 typename _String::size_type __lhs_sz = __lhs.size();
4352 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004353 _String __r(__uninitialized_size_tag(),
4354 __lhs_sz + __rhs_sz,
4355 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4356 auto __ptr = std::__to_address(__r.__get_pointer());
4357 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4358 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4359 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360 return __r;
4361}
4362
4363template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004364_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365basic_string<_CharT, _Traits, _Allocator>
4366operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4367{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004368 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004369 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004370 _String __r(__uninitialized_size_tag(),
4371 __lhs_sz + 1,
4372 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4373 auto __ptr = std::__to_address(__r.__get_pointer());
4374 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4375 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4376 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377 return __r;
4378}
4379
Eric Fiselierfc92be82017-04-19 00:28:44 +00004380#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004381
4382template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004383inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384basic_string<_CharT, _Traits, _Allocator>
4385operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4386{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004387 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388}
4389
4390template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004391inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004392basic_string<_CharT, _Traits, _Allocator>
4393operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4394{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004395 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004396}
4397
4398template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004399inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004400basic_string<_CharT, _Traits, _Allocator>
4401operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4402{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004403 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404}
4405
4406template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004407inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004408basic_string<_CharT, _Traits, _Allocator>
4409operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4410{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004411 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004412}
4413
4414template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004415inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416basic_string<_CharT, _Traits, _Allocator>
4417operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4418{
4419 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004420 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004421}
4422
4423template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004424inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004425basic_string<_CharT, _Traits, _Allocator>
4426operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4427{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004428 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004429}
4430
4431template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004432inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004433basic_string<_CharT, _Traits, _Allocator>
4434operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4435{
4436 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004437 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438}
4439
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004440#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441
4442// swap
4443
4444template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004445inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004447swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004448 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4449 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004450{
4451 __lhs.swap(__rhs);
4452}
4453
Bruce Mitchener170d8972020-11-24 12:53:53 -05004454_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4455_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4456_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4457_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4458_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004459
Bruce Mitchener170d8972020-11-24 12:53:53 -05004460_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4461_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4462_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004463
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004464_LIBCPP_FUNC_VIS string to_string(int __val);
4465_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4466_LIBCPP_FUNC_VIS string to_string(long __val);
4467_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4468_LIBCPP_FUNC_VIS string to_string(long long __val);
4469_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4470_LIBCPP_FUNC_VIS string to_string(float __val);
4471_LIBCPP_FUNC_VIS string to_string(double __val);
4472_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004473
Louis Dionne89258142021-08-23 15:32:36 -04004474#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004475_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4476_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4477_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4478_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4479_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004480
Bruce Mitchener170d8972020-11-24 12:53:53 -05004481_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4482_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4483_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004484
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004485_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4486_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4487_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4488_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4489_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4490_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4491_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4492_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4493_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004494#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004495
Howard Hinnantc51e1022010-05-11 19:42:16 +00004496template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004497_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004498const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4499 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004500
Marshall Clow851b9ec2019-05-20 21:56:51 +00004501template <class _CharT, class _Allocator>
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004502struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503{
4504 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004505 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4506 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507};
4508
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004509template <class _Allocator>
4510struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
4511
4512#ifndef _LIBCPP_HAS_NO_CHAR8_T
4513template <class _Allocator>
4514struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
4515#endif
4516
4517template <class _Allocator>
4518struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
4519
4520template <class _Allocator>
4521struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
4522
4523#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4524template <class _Allocator>
4525struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
4526#endif
4527
Howard Hinnantdc095972011-07-18 15:51:59 +00004528template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004529_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004530operator<<(basic_ostream<_CharT, _Traits>& __os,
4531 const basic_string<_CharT, _Traits, _Allocator>& __str);
4532
4533template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004534_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004535operator>>(basic_istream<_CharT, _Traits>& __is,
4536 basic_string<_CharT, _Traits, _Allocator>& __str);
4537
4538template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004539_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004540getline(basic_istream<_CharT, _Traits>& __is,
4541 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4542
4543template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004544inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004545basic_istream<_CharT, _Traits>&
4546getline(basic_istream<_CharT, _Traits>& __is,
4547 basic_string<_CharT, _Traits, _Allocator>& __str);
4548
Howard Hinnantdc095972011-07-18 15:51:59 +00004549template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004550inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004551basic_istream<_CharT, _Traits>&
4552getline(basic_istream<_CharT, _Traits>&& __is,
4553 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4554
4555template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004556inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004557basic_istream<_CharT, _Traits>&
4558getline(basic_istream<_CharT, _Traits>&& __is,
4559 basic_string<_CharT, _Traits, _Allocator>& __str);
4560
Marshall Clow29b53f22018-12-14 18:49:35 +00004561#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004562template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004563inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004564 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4565 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4566 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004567 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004568 return __old_size - __str.size();
4569}
Marshall Clow29b53f22018-12-14 18:49:35 +00004570
Marek Kurdeja98b1412020-05-02 13:58:03 +02004571template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004572inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004573 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4574 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4575 _Predicate __pred) {
4576 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004577 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004578 __str.end());
4579 return __old_size - __str.size();
4580}
Marshall Clow29b53f22018-12-14 18:49:35 +00004581#endif
4582
Louis Dionne510450b2022-04-01 16:38:30 -04004583#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004584
4585template<class _CharT, class _Traits, class _Allocator>
4586bool
4587basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4588{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004589 return data() <= std::__to_address(__i->base()) &&
4590 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004591}
4592
4593template<class _CharT, class _Traits, class _Allocator>
4594bool
4595basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4596{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004597 return data() < std::__to_address(__i->base()) &&
4598 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004599}
4600
4601template<class _CharT, class _Traits, class _Allocator>
4602bool
4603basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4604{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004605 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004606 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004607}
4608
4609template<class _CharT, class _Traits, class _Allocator>
4610bool
4611basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4612{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004613 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004614 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004615}
4616
Louis Dionne510450b2022-04-01 16:38:30 -04004617#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004618
Louis Dionne173f29e2019-05-29 16:01:36 +00004619#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004620// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004621inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004622{
4623 inline namespace string_literals
4624 {
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004625 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004626 basic_string<char> operator "" s( const char *__str, size_t __len )
4627 {
4628 return basic_string<char> (__str, __len);
4629 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004630
Louis Dionne89258142021-08-23 15:32:36 -04004631#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004632 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004633 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4634 {
4635 return basic_string<wchar_t> (__str, __len);
4636 }
Louis Dionne89258142021-08-23 15:32:36 -04004637#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004638
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004639#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004640 inline _LIBCPP_HIDE_FROM_ABI constexpr
Nikolas Klauser5da956d2022-09-02 16:20:28 +02004641 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len)
Marshall Clow8732fed2018-12-11 04:35:44 +00004642 {
4643 return basic_string<char8_t> (__str, __len);
4644 }
4645#endif
4646
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004647 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004648 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4649 {
4650 return basic_string<char16_t> (__str, __len);
4651 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004652
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004653 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004654 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4655 {
4656 return basic_string<char32_t> (__str, __len);
4657 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004658 } // namespace string_literals
4659} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004660
4661#if _LIBCPP_STD_VER > 17
4662template <>
4663inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4664#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4665template <>
4666inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4667#endif
4668#endif
4669
Marshall Clowcba751f2013-07-23 17:05:24 +00004670#endif
4671
Howard Hinnantc51e1022010-05-11 19:42:16 +00004672_LIBCPP_END_NAMESPACE_STD
4673
Eric Fiselierf4433a32017-05-31 22:07:49 +00004674_LIBCPP_POP_MACROS
4675
Mark de Weveree5fe272022-09-02 17:53:28 +02004676#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
4677# include <algorithm>
4678# include <functional>
4679# include <iterator>
4680# include <new>
4681# include <typeinfo>
4682# include <utility>
4683# include <vector>
4684#endif
4685
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004686#endif // _LIBCPP_STRING