blob: 6010177ab5845e6bd8be3de6549e30c0ded66e8a [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
Mark de Weverb4830e62022-07-19 07:56:23 +020016#include <compare>
17#include <initializer_list>
18
Howard Hinnantc51e1022010-05-11 19:42:16 +000019namespace std
20{
21
22template <class stateT>
23class fpos
24{
25private:
26 stateT st;
27public:
28 fpos(streamoff = streamoff());
29
30 operator streamoff() const;
31
32 stateT state() const;
33 void state(stateT);
34
35 fpos& operator+=(streamoff);
36 fpos operator+ (streamoff) const;
37 fpos& operator-=(streamoff);
38 fpos operator- (streamoff) const;
39};
40
41template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
44template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
45
46template <class charT>
47struct char_traits
48{
Mark de Weverb4830e62022-07-19 07:56:23 +020049 using char_type = charT;
50 using int_type = ...;
51 using off_type = streamoff;
52 using pos_type = streampos;
53 using state_type = mbstate_t;
54 using comparison_category = strong_ordering; // Since C++20 only for the specializations
55 // char, wchar_t, char8_t, char16_t, and char32_t.
Howard Hinnantc51e1022010-05-11 19:42:16 +000056
Howard Hinnantaeb17d82011-05-29 19:57:12 +000057 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000058 static constexpr bool eq(char_type c1, char_type c2) noexcept;
59 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000060
61 static int compare(const char_type* s1, const char_type* s2, size_t n);
62 static size_t length(const char_type* s);
63 static const char_type* find(const char_type* s, size_t n, const char_type& a);
64 static char_type* move(char_type* s1, const char_type* s2, size_t n);
65 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
66 static char_type* assign(char_type* s, size_t n, char_type a);
67
Howard Hinnant270c00e2012-07-20 19:09:12 +000068 static constexpr int_type not_eof(int_type c) noexcept;
69 static constexpr char_type to_char_type(int_type c) noexcept;
70 static constexpr int_type to_int_type(char_type c) noexcept;
71 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
72 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000073};
74
75template <> struct char_traits<char>;
76template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010077template <> struct char_traits<char8_t>; // C++20
78template <> struct char_traits<char16_t>;
79template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
Howard Hinnant3b6579a2010-08-22 00:02:43 +000081template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000082class basic_string
83{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000084public:
85// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000086 typedef traits traits_type;
87 typedef typename traits_type::char_type value_type;
88 typedef Allocator allocator_type;
89 typedef typename allocator_type::size_type size_type;
90 typedef typename allocator_type::difference_type difference_type;
91 typedef typename allocator_type::reference reference;
92 typedef typename allocator_type::const_reference const_reference;
93 typedef typename allocator_type::pointer pointer;
94 typedef typename allocator_type::const_pointer const_pointer;
95 typedef implementation-defined iterator;
96 typedef implementation-defined const_iterator;
97 typedef std::reverse_iterator<iterator> reverse_iterator;
98 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
99
100 static const size_type npos = -1;
101
Howard Hinnant3e276872011-06-03 18:40:47 +0000102 basic_string()
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200103 noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20
104 explicit basic_string(const allocator_type& a); // constexpr since C++20
105 basic_string(const basic_string& str); // constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000106 basic_string(basic_string&& str)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200107 noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000108 basic_string(const basic_string& str, size_type pos,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200109 const allocator_type& a = allocator_type()); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000110 basic_string(const basic_string& str, size_type pos, size_type n,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200111 const Allocator& a = Allocator()); // constexpr since C++20
Marshall Clow78dbe462016-11-14 18:22:19 +0000112 template<class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200113 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17, constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000114 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200115 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17, constexpr since C++20
116 basic_string(const value_type* s, const allocator_type& a = allocator_type()); // constexpr since C++20
117 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type()); // constexpr since C++20
Marek Kurdej90d79712021-07-27 16:16:21 +0200118 basic_string(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200119 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type()); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000121 basic_string(InputIterator begin, InputIterator end,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200122 const allocator_type& a = allocator_type()); // constexpr since C++20
123 basic_string(initializer_list<value_type>, const Allocator& = Allocator()); // constexpr since C++20
124 basic_string(const basic_string&, const Allocator&); // constexpr since C++20
125 basic_string(basic_string&&, const Allocator&); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200127 ~basic_string(); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200129 operator basic_string_view<charT, traits>() const noexcept; // constexpr since C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000130
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200131 basic_string& operator=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000132 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200133 basic_string& operator=(const T& t); // C++17, constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000134 basic_string& operator=(basic_string&& str)
135 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000136 allocator_type::propagate_on_container_move_assignment::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200137 allocator_type::is_always_equal::value ); // C++17, constexpr since C++20
138 basic_string& operator=(const value_type* s); // constexpr since C++20
Marek Kurdej90d79712021-07-27 16:16:21 +0200139 basic_string& operator=(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200140 basic_string& operator=(value_type c); // constexpr since C++20
141 basic_string& operator=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200143 iterator begin() noexcept; // constexpr since C++20
144 const_iterator begin() const noexcept; // constexpr since C++20
145 iterator end() noexcept; // constexpr since C++20
146 const_iterator end() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200148 reverse_iterator rbegin() noexcept; // constexpr since C++20
149 const_reverse_iterator rbegin() const noexcept; // constexpr since C++20
150 reverse_iterator rend() noexcept; // constexpr since C++20
151 const_reverse_iterator rend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200153 const_iterator cbegin() const noexcept; // constexpr since C++20
154 const_iterator cend() const noexcept; // constexpr since C++20
155 const_reverse_iterator crbegin() const noexcept; // constexpr since C++20
156 const_reverse_iterator crend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200158 size_type size() const noexcept; // constexpr since C++20
159 size_type length() const noexcept; // constexpr since C++20
160 size_type max_size() const noexcept; // constexpr since C++20
161 size_type capacity() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000162
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200163 void resize(size_type n, value_type c); // constexpr since C++20
164 void resize(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100166 template<class Operation>
167 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
168
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200169 void reserve(size_type res_arg); // constexpr since C++20
Marek Kurdejc9848142020-11-26 10:07:16 +0100170 void reserve(); // deprecated in C++20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200171 void shrink_to_fit(); // constexpr since C++20
172 void clear() noexcept; // constexpr since C++20
173 bool empty() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200175 const_reference operator[](size_type pos) const; // constexpr since C++20
176 reference operator[](size_type pos); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200178 const_reference at(size_type n) const; // constexpr since C++20
179 reference at(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200181 basic_string& operator+=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000182 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200183 basic_string& operator+=(const T& t); // C++17, constexpr since C++20
184 basic_string& operator+=(const value_type* s); // constexpr since C++20
185 basic_string& operator+=(value_type c); // constexpr since C++20
186 basic_string& operator+=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200188 basic_string& append(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000189 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200190 basic_string& append(const T& t); // C++17, constexpr since C++20
191 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000192 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200193 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
194 basic_string& append(const value_type* s, size_type n); // constexpr since C++20
195 basic_string& append(const value_type* s); // constexpr since C++20
196 basic_string& append(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000197 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200198 basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
199 basic_string& append(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200201 void push_back(value_type c); // constexpr since C++20
202 void pop_back(); // constexpr since C++20
203 reference front(); // constexpr since C++20
204 const_reference front() const; // constexpr since C++20
205 reference back(); // constexpr since C++20
206 const_reference back() const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200208 basic_string& assign(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000209 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200210 basic_string& assign(const T& t); // C++17, constexpr since C++20
211 basic_string& assign(basic_string&& str); // constexpr since C++20
212 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000213 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200214 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
215 basic_string& assign(const value_type* s, size_type n); // constexpr since C++20
216 basic_string& assign(const value_type* s); // constexpr since C++20
217 basic_string& assign(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000218 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200219 basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
220 basic_string& assign(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200222 basic_string& insert(size_type pos1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000223 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200224 basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000225 basic_string& insert(size_type pos1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200226 size_type pos2, size_type n); // constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000227 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200228 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
229 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
230 basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
231 basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
232 iterator insert(const_iterator p, value_type c); // constexpr since C++20
233 iterator insert(const_iterator p, size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000234 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200235 iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20
236 iterator insert(const_iterator p, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200238 basic_string& erase(size_type pos = 0, size_type n = npos); // constexpr since C++20
239 iterator erase(const_iterator position); // constexpr since C++20
240 iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200242 basic_string& replace(size_type pos1, size_type n1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000243 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200244 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17, constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000245 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200246 size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000247 template <class T>
248 basic_string& replace(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200249 size_type pos2, size_type n); // C++17, constexpr since C++20
250 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
251 basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
252 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
253 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000254 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200255 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17, constexpr since C++20
256 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
257 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s); // constexpr since C++20
258 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000259 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200260 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
261 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200263 size_type copy(value_type* s, size_type n, size_type pos = 0) const; // constexpr since C++20
264 basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265
Howard Hinnant3e276872011-06-03 18:40:47 +0000266 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000267 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200268 allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200270 const value_type* c_str() const noexcept; // constexpr since C++20
271 const value_type* data() const noexcept; // constexpr since C++20
272 value_type* data() noexcept; // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200274 allocator_type get_allocator() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200276 size_type find(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000277 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200278 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
279 size_type find(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
280 size_type find(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
281 size_type find(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200283 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000284 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200285 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
286 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
287 size_type rfind(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
288 size_type rfind(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200290 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000291 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200292 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
293 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
294 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
295 size_type find_first_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200297 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000298 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200299 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension, constexpr since C++20
300 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
301 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
302 size_type find_last_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200304 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000305 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200306 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
307 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
308 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
309 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200311 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000312 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200313 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
314 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
315 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
316 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200318 int compare(const basic_string& str) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000319 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200320 int compare(const T& t) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
321 int compare(size_type pos1, size_type n1, const basic_string& str) const; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000322 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200323 int compare(size_type pos1, size_type n1, const T& t) const; // C++17, constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000324 int compare(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200325 size_type pos2, size_type n2=npos) const; // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000326 template <class T>
327 int compare(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200328 size_type pos2, size_type n2=npos) const; // C++17, constexpr since C++20
329 int compare(const value_type* s) const noexcept; // constexpr since C++20
330 int compare(size_type pos1, size_type n1, const value_type* s) const; // constexpr since C++20
331 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200333 constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
334 constexpr bool starts_with(charT c) const noexcept; // C++20
335 constexpr bool starts_with(const charT* s) const; // C++20
336 constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
337 constexpr bool ends_with(charT c) const noexcept; // C++20
338 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000339
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200340 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
341 constexpr bool contains(charT c) const noexcept; // C++2b
342 constexpr bool contains(const charT* s) const; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000343};
344
Marshall Clowa0563332018-02-08 06:34:03 +0000345template<class InputIterator,
346 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
347basic_string(InputIterator, InputIterator, Allocator = Allocator())
348 -> basic_string<typename iterator_traits<InputIterator>::value_type,
349 char_traits<typename iterator_traits<InputIterator>::value_type>,
350 Allocator>; // C++17
351
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352template<class charT, class traits, class Allocator>
353basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000354operator+(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200355 const basic_string<charT, traits, Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356
357template<class charT, class traits, class Allocator>
358basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200359operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
361template<class charT, class traits, class Allocator>
362basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200363operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
366basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200367operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368
369template<class charT, class traits, class Allocator>
370basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200371operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372
373template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000374bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200375 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200378bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
380template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200381bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000383template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000384bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200385 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200388bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200391bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392
393template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000394bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200395 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200398bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200401bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402
403template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000404bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200405 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200408bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200411bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412
413template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000414bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200415 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200418bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200421bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422
423template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000424bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200425 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200428bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200431bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Mark de Weverb4830e62022-07-19 07:56:23 +0200432
433template<class charT, class traits, class Allocator> // since C++20
434constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
435 const basic_string<charT, traits, Allocator>& rhs) noexcept;
436
437template<class charT, class traits, class Allocator> // since C++20
438constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
439 const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440
441template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000442void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000443 basic_string<charT, traits, Allocator>& rhs)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200444 noexcept(noexcept(lhs.swap(rhs))); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445
446template<class charT, class traits, class Allocator>
447basic_istream<charT, traits>&
448operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
449
450template<class charT, class traits, class Allocator>
451basic_ostream<charT, traits>&
452operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
453
454template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000455basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000456getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
457 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000458
459template<class charT, class traits, class Allocator>
460basic_istream<charT, traits>&
461getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
462
Marshall Clow29b53f22018-12-14 18:49:35 +0000463template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200464typename basic_string<charT, traits, Allocator>::size_type
465erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000466template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200467typename basic_string<charT, traits, Allocator>::size_type
468erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000469
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470typedef basic_string<char> string;
471typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100472typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000473typedef basic_string<char16_t> u16string;
474typedef basic_string<char32_t> u32string;
475
Bruce Mitchener170d8972020-11-24 12:53:53 -0500476int stoi (const string& str, size_t* idx = nullptr, int base = 10);
477long stol (const string& str, size_t* idx = nullptr, int base = 10);
478unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
479long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
480unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000481
Bruce Mitchener170d8972020-11-24 12:53:53 -0500482float stof (const string& str, size_t* idx = nullptr);
483double stod (const string& str, size_t* idx = nullptr);
484long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000485
486string to_string(int val);
487string to_string(unsigned val);
488string to_string(long val);
489string to_string(unsigned long val);
490string to_string(long long val);
491string to_string(unsigned long long val);
492string to_string(float val);
493string to_string(double val);
494string to_string(long double val);
495
Bruce Mitchener170d8972020-11-24 12:53:53 -0500496int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
497long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
498unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
499long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
500unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000501
Bruce Mitchener170d8972020-11-24 12:53:53 -0500502float stof (const wstring& str, size_t* idx = nullptr);
503double stod (const wstring& str, size_t* idx = nullptr);
504long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000505
506wstring to_wstring(int val);
507wstring to_wstring(unsigned val);
508wstring to_wstring(long val);
509wstring to_wstring(unsigned long val);
510wstring to_wstring(long long val);
511wstring to_wstring(unsigned long long val);
512wstring to_wstring(float val);
513wstring to_wstring(double val);
514wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515
516template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100517template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518template <> struct hash<u16string>;
519template <> struct hash<u32string>;
520template <> struct hash<wstring>;
521
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200522basic_string<char> operator "" s( const char *str, size_t len ); // C++14, constexpr since C++20
523basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14, constexpr since C++20
524constexpr basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
525basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14, constexpr since C++20
526basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14, constexpr since C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000527
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528} // std
529
530*/
531
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100532#include <__algorithm/max.h>
533#include <__algorithm/min.h>
534#include <__algorithm/remove.h>
535#include <__algorithm/remove_if.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400536#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400538#include <__debug>
Mark de Weverdf3e0d42021-09-26 15:47:42 +0200539#include <__format/enable_insertable.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200540#include <__functional/hash.h>
Nikolas Klauser24540d32022-05-21 00:45:51 +0200541#include <__functional/unary_function.h>
Nikolas Klauser80840272022-02-04 13:04:33 +0100542#include <__ios/fpos.h>
Nikolas Klauserfb1b0672022-06-10 19:53:10 +0200543#include <__iterator/distance.h>
544#include <__iterator/iterator_traits.h>
545#include <__iterator/reverse_iterator.h>
Louis Dionne77249522021-06-11 09:55:11 -0400546#include <__iterator/wrap_iter.h>
Nikolas Klauserc513eba2022-04-09 09:41:19 +0200547#include <__memory/allocate_at_least.h>
Nikolas Klauser35080b42022-07-26 16:13:56 +0200548#include <__memory/swap_allocator.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200549#include <__string/char_traits.h>
550#include <__string/extern_template_lists.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100551#include <__utility/auto_cast.h>
552#include <__utility/move.h>
553#include <__utility/swap.h>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200554#include <__utility/unreachable.h>
555#include <climits>
Louis Dionne44962c32022-06-13 11:21:16 -0400556#include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400557#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400558#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400559#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400560#include <iosfwd>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200561#include <limits>
Vitaly Buka5a807d92022-09-02 19:35:10 -0700562#include <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400564#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000566#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000567
Louis Dionne89258142021-08-23 15:32:36 -0400568#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100569# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400570#endif
571
Mark de Weverf6df9e82022-08-20 10:34:26 +0200572#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
Louis Dionne22355cc2022-06-27 15:53:41 -0400573# include <algorithm>
574# include <functional>
575# include <iterator>
576# include <new>
577# include <typeinfo>
578# include <utility>
579# include <vector>
580#endif
581
Nikolas Klausera0e0edb2022-06-16 22:43:46 +0200582// standard-mandated includes
583
584// [iterator.range]
585#include <__iterator/access.h>
586#include <__iterator/data.h>
587#include <__iterator/empty.h>
588#include <__iterator/reverse_access.h>
589#include <__iterator/size.h>
590
591// [string.syn]
592#include <compare>
593#include <initializer_list>
594
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000595#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500596# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000597#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598
Eric Fiselierf4433a32017-05-31 22:07:49 +0000599_LIBCPP_PUSH_MACROS
600#include <__undef_macros>
601
602
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603_LIBCPP_BEGIN_NAMESPACE_STD
604
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605// basic_string
606
607template<class _CharT, class _Traits, class _Allocator>
608basic_string<_CharT, _Traits, _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200609_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant944510a2011-06-14 19:58:17 +0000610operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
611 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612
613template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200614_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000616operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617
618template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200619_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000621operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622
623template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200624inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000626operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627
628template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200629_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000631operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632
Louis Dionnedc496ec2021-06-08 17:25:08 -0400633extern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Shoaib Meenaiea363712017-07-29 02:54:41 +0000634
Marshall Clow039b2f02016-01-13 21:54:34 +0000635template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400636struct __string_is_trivial_iterator : public false_type {};
637
638template <class _Tp>
639struct __string_is_trivial_iterator<_Tp*>
640 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000641
Louis Dionne173f29e2019-05-29 16:01:36 +0000642template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400643struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
644 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000645
Marshall Clow82513342016-09-24 22:45:42 +0000646template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500647struct __can_be_converted_to_string_view : public _BoolConstant<
648 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
649 !is_convertible<const _Tp&, const _CharT*>::value
650 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000651
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400652#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800653typedef basic_string<char8_t> u8string;
654#endif
Richard Smith256954d2020-11-11 17:12:18 -0800655typedef basic_string<char16_t> u16string;
656typedef basic_string<char32_t> u32string;
Richard Smith256954d2020-11-11 17:12:18 -0800657
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200658struct __uninitialized_size_tag {};
659
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000660template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800661class
662 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400663#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800664 _LIBCPP_PREFERRED_NAME(u8string)
665#endif
Richard Smith256954d2020-11-11 17:12:18 -0800666 _LIBCPP_PREFERRED_NAME(u16string)
667 _LIBCPP_PREFERRED_NAME(u32string)
Richard Smith256954d2020-11-11 17:12:18 -0800668 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669{
670public:
671 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000672 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000674 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000676 typedef allocator_traits<allocator_type> __alloc_traits;
677 typedef typename __alloc_traits::size_type size_type;
678 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000679 typedef value_type& reference;
680 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000681 typedef typename __alloc_traits::pointer pointer;
682 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000683
Marshall Clow79f33542018-03-21 00:36:05 +0000684 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
685 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
686 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
687 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000688 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000689 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000690 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000691
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 typedef __wrap_iter<pointer> iterator;
693 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200694 typedef std::reverse_iterator<iterator> reverse_iterator;
695 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696
697private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200698 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000699
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000700#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000701
702 struct __long
703 {
704 pointer __data_;
705 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200706 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
707 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000708 };
709
Howard Hinnant68bf1812013-04-30 21:44:48 +0000710 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
711 (sizeof(__long) - 1)/sizeof(value_type) : 2};
712
713 struct __short
714 {
715 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200716 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200717 unsigned char __size_ : 7;
718 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000719 };
720
Nikolas Klauser62060be2022-04-20 10:52:04 +0200721// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200722// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200723//
724// If the LSB is used to store the short-flag in the short string representation,
725// we have to multiply the size by two when it is stored and divide it by two when
726// it is loaded to make sure that we always store an even number. In the long string
727// representation, we can ignore this because we can assume that we always allocate
728// an even amount of value_types.
729//
730// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
731// This does not impact the short string representation, since we never need the MSB
732// for representing the size of a short string anyway.
733
734#ifdef _LIBCPP_BIG_ENDIAN
735 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000736#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200737 static const size_type __endian_factor = 1;
738#endif
739
740#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
741
742#ifdef _LIBCPP_BIG_ENDIAN
743 static const size_type __endian_factor = 1;
744#else
745 static const size_type __endian_factor = 2;
746#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000747
Xing Xue38b9fc42022-06-24 17:25:15 -0400748 // Attribute 'packed' is used to keep the layout compatible with the
749 // previous definition that did not use bit fields. This is because on
750 // some platforms bit fields have a default size rather than the actual
751 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 struct __long
753 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400754 struct _LIBCPP_PACKED {
755 size_type __is_long_ : 1;
756 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
757 };
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758 size_type __size_;
759 pointer __data_;
760 };
761
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
763 (sizeof(__long) - 1)/sizeof(value_type) : 2};
764
765 struct __short
766 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400767 struct _LIBCPP_PACKED {
768 unsigned char __is_long_ : 1;
769 unsigned char __size_ : 7;
770 };
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200771 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772 value_type __data_[__min_cap];
773 };
774
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400775#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000776
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200777 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
778
Howard Hinnant8ea98242013-08-23 17:37:05 +0000779 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780
Howard Hinnant8ea98242013-08-23 17:37:05 +0000781 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782
783 struct __raw
784 {
785 size_type __words[__n_words];
786 };
787
788 struct __rep
789 {
790 union
791 {
792 __long __l;
793 __short __s;
794 __raw __r;
795 };
796 };
797
798 __compressed_pair<__rep, allocator_type> __r_;
799
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200800 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
801 // don't initialize the characters. The contents of the string, including the null terminator, must be
802 // initialized separately.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200803 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200804 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200805 : __r_(__default_init_tag(), __a) {
806 if (__size > max_size())
807 __throw_length_error();
808 if (__fits_in_sso(__size)) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +0200809 __r_.first() = __rep();
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200810 __set_short_size(__size);
811 } else {
812 auto __capacity = __recommend(__size) + 1;
813 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200814 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200815 __set_long_cap(__capacity);
816 __set_long_pointer(__allocation);
817 __set_long_size(__size);
818 }
819 std::__debug_db_insert_c(this);
820 }
821
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200823 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824 static const size_type npos = -1;
825
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200826 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
Howard Hinnant3e276872011-06-03 18:40:47 +0000827 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000828
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200829 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000830#if _LIBCPP_STD_VER <= 14
831 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
832#else
833 _NOEXCEPT;
834#endif
835
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200836 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
837 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000838
Eric Fiselierfc92be82017-04-19 00:28:44 +0000839#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200840 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +0000841 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000842#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000843 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000844#else
845 _NOEXCEPT;
846#endif
847
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200848 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400850#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000851
Louis Dionne9ce598d2021-09-08 09:14:43 -0400852 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200853 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500854 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000855 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
856 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200857 std::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000858 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000859
Louis Dionne9ce598d2021-09-08 09:14:43 -0400860 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200861 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000862 basic_string(const _CharT* __s, const _Allocator& __a);
863
Marek Kurdej90d79712021-07-27 16:16:21 +0200864#if _LIBCPP_STD_VER > 20
865 basic_string(nullptr_t) = delete;
866#endif
867
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200868 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000869 basic_string(const _CharT* __s, size_type __n);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200870 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000871 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200872 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000873 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000874
Louis Dionne9ce598d2021-09-08 09:14:43 -0400875 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200876 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000877 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
878
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200879 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow83445802016-04-07 18:13:41 +0000880 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000881 const _Allocator& __a = _Allocator());
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200882 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow83445802016-04-07 18:13:41 +0000883 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000884 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000885
Louis Dionne9ce598d2021-09-08 09:14:43 -0400886 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200887 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000888 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500889 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000890
Louis Dionne9ce598d2021-09-08 09:14:43 -0400891 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500892 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200893 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000894 explicit basic_string(const _Tp& __t);
895
Louis Dionne9ce598d2021-09-08 09:14:43 -0400896 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200897 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +0000898 explicit basic_string(const _Tp& __t, const allocator_type& __a);
899
Louis Dionne9ce598d2021-09-08 09:14:43 -0400900 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200901 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400903 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200904 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000906#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200907 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000908 basic_string(initializer_list<_CharT> __il);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200909 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +0000910 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400911#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200913 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200915 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000916 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
917
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200918 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000919
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200920 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
921 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200922 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200923 __self_view __sv = __t;
924 return assign(__sv);
925 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000926
Eric Fiselierfc92be82017-04-19 00:28:44 +0000927#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200928 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +0000929 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000930 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200931 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierfc92be82017-04-19 00:28:44 +0000932 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200934 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200935 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200936#if _LIBCPP_STD_VER > 20
937 basic_string& operator=(nullptr_t) = delete;
938#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200939 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200941 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000942 iterator begin() _NOEXCEPT
943 {return iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200944 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000945 const_iterator begin() const _NOEXCEPT
946 {return const_iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200947 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000948 iterator end() _NOEXCEPT
949 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200950 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +0000951 const_iterator end() const _NOEXCEPT
952 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -0400953
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200954 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000955 reverse_iterator rbegin() _NOEXCEPT
956 {return reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200957 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000958 const_reverse_iterator rbegin() const _NOEXCEPT
959 {return const_reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200960 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000961 reverse_iterator rend() _NOEXCEPT
962 {return reverse_iterator(begin());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200963 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000964 const_reverse_iterator rend() const _NOEXCEPT
965 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200967 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000968 const_iterator cbegin() const _NOEXCEPT
969 {return begin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200970 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000971 const_iterator cend() const _NOEXCEPT
972 {return end();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200973 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000974 const_reverse_iterator crbegin() const _NOEXCEPT
975 {return rbegin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200976 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000977 const_reverse_iterator crend() const _NOEXCEPT
978 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200980 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200982 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {return size();}
983 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT;
984 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200985 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
986 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200988 _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
989 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200991 _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100992
993#if _LIBCPP_STD_VER > 20
994 template <class _Op>
995 _LIBCPP_HIDE_FROM_ABI constexpr
996 void resize_and_overwrite(size_type __n, _Op __op) {
997 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200998 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100999 }
1000#endif
1001
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001002 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +00001003
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001004 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001005 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
1006 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001007
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001008 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowb7db4972017-11-15 20:02:27 +00001009 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001011 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001012 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001013 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001015 _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
1016 _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001018 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001019 return append(__str);
1020 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001021
1022 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001023 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001024 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001025 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1027 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001028 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001029 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001030 operator+=(const _Tp& __t) {
1031 __self_view __sv = __t; return append(__sv);
1032 }
1033
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001034 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001035 return append(__s);
1036 }
1037
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001038 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001039 push_back(__c);
1040 return *this;
1041 }
1042
Eric Fiselierfc92be82017-04-19 00:28:44 +00001043#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001044 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001045 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001046#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001048 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001050
1051 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001052 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001053 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001054 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1055 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001056 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001057 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001058 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001059 _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 +00001060
Marshall Clow62953962016-10-03 23:40:48 +00001061 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001062 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001063 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001064 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001065 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1066 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001067 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001068 >
Marshall Clow82513342016-09-24 22:45:42 +00001069 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001070 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n);
1071 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s);
1072 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001073
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001074 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier451d5582018-11-26 20:15:38 +00001075 void __append_default_init(size_type __n);
1076
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001078 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001079 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001081 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001083 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001084 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001085 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001086 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001087 append(__temp.data(), __temp.size());
1088 return *this;
1089 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001091 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001092 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001094 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001096 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001097 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001098 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001099
Eric Fiselierfc92be82017-04-19 00:28:44 +00001100#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001101 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001103#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001105 _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
1106 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
1107 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT;
1108 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT;
1109 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT;
1110 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111
Marshall Clowe46031a2018-07-02 18:41:15 +00001112 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001113 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001114 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001115 <
1116 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1117 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001118 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001119 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001120 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001121 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001122#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001123 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001124 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001125 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001126 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001127#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001128 _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 +00001129 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001130 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001131 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001132 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1134 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001135 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001136 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001137 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001138 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n);
1139 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s);
1140 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001142 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001143 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001145 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001147 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 assign(_InputIterator __first, _InputIterator __last);
1149 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001150 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001151 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001153 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001155 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001157#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001158 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001160#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001162 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001164
1165 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001166 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001167 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001168 <
1169 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1170 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001171 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001172 insert(size_type __pos1, const _Tp& __t)
1173 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1174
Marshall Clow82513342016-09-24 22:45:42 +00001175 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001176 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001177 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001178 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001179 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001180 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001181 >
Marshall Clow82513342016-09-24 22:45:42 +00001182 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001183 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001184 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001185 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1186 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s);
1187 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1188 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
1189 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1191 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001192 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001193 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001195 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001197 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1199 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001200 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001201 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001203 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001205 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001207#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001208 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1210 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001211#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001213 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1214 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215 iterator erase(const_iterator __pos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001216 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 iterator erase(const_iterator __first, const_iterator __last);
1218
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001219 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001221
1222 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001223 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001224 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001225 <
1226 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1227 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001228 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001229 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 +02001230 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001231 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 +00001232 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001233 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001234 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001235 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001236 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001237 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001238 >
Marshall Clow82513342016-09-24 22:45:42 +00001239 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001240 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001241 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001242 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1243 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1244 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001245 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001246
1247 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001248 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001249 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001250 <
1251 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1252 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001253 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001254 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1255
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001256 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001257 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001258 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001259 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001260 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001261 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001263 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001264 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001266 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001268 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001269 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001270#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001271 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001272 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001274#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001276 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
1277 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1279
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001280 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001281 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001282#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001283 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001284#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001285 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001286 __is_nothrow_swappable<allocator_type>::value);
1287#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001289 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001290 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001291 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001292 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001293#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001294 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001295 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001296#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001298 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001299 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001301 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001302 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001303
1304 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001305 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001306 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001307 <
1308 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1309 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001310 >
zoecarver1997e0a2021-02-05 11:54:47 -08001311 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001312 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001313 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001314 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001315 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001316 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001319 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001320
1321 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001322 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001323 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001324 <
1325 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1326 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001327 >
zoecarver1997e0a2021-02-05 11:54:47 -08001328 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001329 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001330 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001331 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001332 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001333 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001335 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001336 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001337
1338 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001339 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001340 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001341 <
1342 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1343 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001344 >
zoecarver1997e0a2021-02-05 11:54:47 -08001345 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001346 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001347 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001348 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001349 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001350 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001351 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001353 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001354 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001355
1356 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001357 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001358 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001359 <
1360 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1361 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001362 >
zoecarver1997e0a2021-02-05 11:54:47 -08001363 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001364 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001365 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001366 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001367 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001368 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001369 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001371 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001372 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001373
1374 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001375 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001376 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001377 <
1378 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1379 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001380 >
zoecarver1997e0a2021-02-05 11:54:47 -08001381 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001382 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001383 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 +02001384 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001385 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001386 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001387 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1388
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001389 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001390 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001391
1392 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001393 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001394 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001395 <
1396 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1397 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001398 >
zoecarver1997e0a2021-02-05 11:54:47 -08001399 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001400 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001401 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 +02001402 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001403 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001404 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001405 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1406
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001407 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001408 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001409
1410 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001411 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001412 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001413 <
1414 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1415 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001416 >
zoecarver1997e0a2021-02-05 11:54:47 -08001417 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001418
1419 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001420 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001421 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001422 <
1423 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1424 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001425 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001426 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1427
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001428 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001430 _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001431 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1432 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001433
Marshall Clow82513342016-09-24 22:45:42 +00001434 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001435 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001436 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001437 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001438 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001439 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001440 >
Marshall Clow82513342016-09-24 22:45:42 +00001441 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 +02001442 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const value_type* __s) const _NOEXCEPT;
1443 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1444 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001445 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446
Marshall Clow18c293b2017-12-04 20:11:38 +00001447#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001448 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001449 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001450 { return __self_view(data(), size()).starts_with(__sv); }
1451
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001452 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001453 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001454 { return !empty() && _Traits::eq(front(), __c); }
1455
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001456 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001457 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001458 { return starts_with(__self_view(__s)); }
1459
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001460 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001461 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001462 { return __self_view(data(), size()).ends_with( __sv); }
1463
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001464 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001465 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001466 { return !empty() && _Traits::eq(back(), __c); }
1467
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001468 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001469 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001470 { return ends_with(__self_view(__s)); }
1471#endif
1472
Wim Leflere023c3542021-01-19 14:33:30 -05001473#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001474 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001475 bool contains(__self_view __sv) const noexcept
1476 { return __self_view(data(), size()).contains(__sv); }
1477
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001478 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001479 bool contains(value_type __c) const noexcept
1480 { return __self_view(data(), size()).contains(__c); }
1481
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001482 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001483 bool contains(const value_type* __s) const
1484 { return __self_view(data(), size()).contains(__s); }
1485#endif
1486
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001487 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001488
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001489 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001490
Louis Dionne510450b2022-04-01 16:38:30 -04001491#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001492
1493 bool __dereferenceable(const const_iterator* __i) const;
1494 bool __decrementable(const const_iterator* __i) const;
1495 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1496 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1497
Louis Dionne510450b2022-04-01 16:38:30 -04001498#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001499
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001501 template<class _Alloc>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001502 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001503 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1504 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1505
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001506 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001507
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001508 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001509 bool __is_long() const _NOEXCEPT {
1510 if (__libcpp_is_constant_evaluated())
1511 return true;
1512 return __r_.first().__s.__is_long_;
1513 }
1514
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001515 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __begin_lifetime(pointer __begin, size_type __n) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001516#if _LIBCPP_STD_VER > 17
1517 if (__libcpp_is_constant_evaluated()) {
1518 for (size_type __i = 0; __i != __n; ++__i)
1519 std::construct_at(std::addressof(__begin[__i]));
1520 }
1521#else
1522 (void)__begin;
1523 (void)__n;
1524#endif // _LIBCPP_STD_VER > 17
1525 }
1526
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001527 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __default_init() {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001528 __r_.first() = __rep();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001529 if (__libcpp_is_constant_evaluated()) {
1530 size_type __sz = __recommend(0) + 1;
1531 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1532 __begin_lifetime(__ptr, __sz);
1533 __set_long_pointer(__ptr);
1534 __set_long_cap(__sz);
1535 __set_long_size(0);
1536 }
1537 }
1538
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001539 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __deallocate_constexpr() {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001540 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1541 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1542 }
1543
Nikolas Klauser93826d12022-01-04 17:24:03 +01001544 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1545 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1546 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1547 }
1548
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001549 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001550 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001551 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1552 size_type __sz = size();
1553 size_type __cap = capacity();
1554 value_type* __p;
1555 if (__cap - __sz >= __n)
1556 {
1557 __p = std::__to_address(__get_pointer());
1558 size_type __n_move = __sz - __ip;
1559 if (__n_move != 0)
1560 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1561 }
1562 else
1563 {
1564 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1565 __p = std::__to_address(__get_long_pointer());
1566 }
1567 __sz += __n;
1568 __set_size(__sz);
1569 traits_type::assign(__p[__sz], value_type());
1570 for (__p += __ip; __first != __last; ++__p, ++__first)
1571 traits_type::assign(*__p, *__first);
1572
1573 return begin() + __ip;
1574 }
1575
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001576 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001577 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001579 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001580 void __set_short_size(size_type __s) _NOEXCEPT {
1581 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1582 __r_.first().__s.__size_ = __s;
1583 __r_.first().__s.__is_long_ = false;
1584 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001585
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001586 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001587 size_type __get_short_size() const _NOEXCEPT {
1588 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1589 return __r_.first().__s.__size_;
1590 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001591
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001592 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001593 void __set_long_size(size_type __s) _NOEXCEPT
1594 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001595 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001596 size_type __get_long_size() const _NOEXCEPT
1597 {return __r_.first().__l.__size_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001598 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001599 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1601
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001602 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001603 void __set_long_cap(size_type __s) _NOEXCEPT {
1604 __r_.first().__l.__cap_ = __s / __endian_factor;
1605 __r_.first().__l.__is_long_ = true;
1606 }
1607
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001608 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001609 size_type __get_long_cap() const _NOEXCEPT {
1610 return __r_.first().__l.__cap_ * __endian_factor;
1611 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001613 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001614 void __set_long_pointer(pointer __p) _NOEXCEPT
1615 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001616 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001617 pointer __get_long_pointer() _NOEXCEPT
1618 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001619 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001620 const_pointer __get_long_pointer() const _NOEXCEPT
1621 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001622 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001623 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001624 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001625 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001626 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001627 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001628 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001629 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001631 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001632 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1634
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635 template <size_type __a> static
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001636 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea382952013-08-14 18:00:20 +00001637 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001638 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 enum {__alignment = 16};
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001640 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001641 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001642 {
1643 if (__s < __min_cap) {
1644 if (__libcpp_is_constant_evaluated())
1645 return static_cast<size_type>(__min_cap);
1646 else
1647 return static_cast<size_type>(__min_cap) - 1;
1648 }
Marshall Clow80584522018-02-07 21:30:17 +00001649 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1650 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1651 if (__guess == __min_cap) ++__guess;
1652 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001653 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001655 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001656 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001657 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001658 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001659 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001661
Martijn Vels5e7c9752020-03-04 17:52:46 -05001662 // Slow path for the (inlined) copy constructor for 'long' strings.
1663 // Always externally instantiated and not inlined.
1664 // Requires that __s is zero terminated.
1665 // The main reason for this function to exist is because for unstable, we
1666 // want to allow inlining of the copy constructor. However, we don't want
1667 // to call the __init() functions as those are marked as inline which may
1668 // result in over-aggressive inlining by the compiler, where our aim is
1669 // to only inline the fast path code directly in the ctor.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001670 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001671
Howard Hinnantc51e1022010-05-11 19:42:16 +00001672 template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001673 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001674 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001676 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1677 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678 __init(_InputIterator __first, _InputIterator __last);
1679
1680 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001681 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001682 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001684 __is_cpp17_forward_iterator<_ForwardIterator>::value
1685 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686 __init(_ForwardIterator __first, _ForwardIterator __last);
1687
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001688 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001690 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001691 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1693 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001694 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695
Martijn Vels596e3de2020-02-26 15:55:49 -05001696 // __assign_no_alias is invoked for assignment operations where we
1697 // have proof that the input does not alias the current instance.
1698 // For example, operator=(basic_string) performs a 'self' check.
1699 template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001700 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001701
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001702 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 void __erase_to_end(size_type __pos);
1704
Martijn Velsa81fc792020-02-26 13:25:43 -05001705 // __erase_external_with_move is invoked for erase() invocations where
1706 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001707 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001708
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001709 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001710 void __copy_assign_alloc(const basic_string& __str)
1711 {__copy_assign_alloc(__str, integral_constant<bool,
1712 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1713
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001714 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001715 void __copy_assign_alloc(const basic_string& __str, true_type)
1716 {
Marshall Clowf258c202017-01-31 03:40:52 +00001717 if (__alloc() == __str.__alloc())
1718 __alloc() = __str.__alloc();
1719 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001720 {
Marshall Clowf258c202017-01-31 03:40:52 +00001721 if (!__str.__is_long())
1722 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001723 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001724 __alloc() = __str.__alloc();
1725 }
1726 else
1727 {
1728 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001729 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001730 __begin_lifetime(__allocation.ptr, __allocation.count);
Vedant Kumar55e007e2018-03-08 21:15:26 +00001731 __clear_and_shrink();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001732 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001733 __set_long_pointer(__allocation.ptr);
1734 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001735 __set_long_size(__str.size());
1736 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001737 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001738 }
1739
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001740 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001741 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001742 {}
1743
Eric Fiselierfc92be82017-04-19 00:28:44 +00001744#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001745 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001746 void __move_assign(basic_string& __str, false_type)
1747 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001748 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001749 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001750#if _LIBCPP_STD_VER > 14
1751 _NOEXCEPT;
1752#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001753 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001754#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001755#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001756
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001757 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001758 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001759 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001760 _NOEXCEPT_(
1761 !__alloc_traits::propagate_on_container_move_assignment::value ||
1762 is_nothrow_move_assignable<allocator_type>::value)
1763 {__move_assign_alloc(__str, integral_constant<bool,
1764 __alloc_traits::propagate_on_container_move_assignment::value>());}
1765
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001766 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc2734962011-09-02 20:42:31 +00001767 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001768 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1769 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001770 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001771 }
1772
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001773 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001774 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001775 _NOEXCEPT
1776 {}
1777
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001778 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s);
1779 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001780
1781 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1782 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1783 pointer __p = __is_long()
1784 ? (__set_long_size(__n), __get_long_pointer())
1785 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001786 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001787 traits_type::assign(__p[__n], value_type());
1788 return *this;
1789 }
1790
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001791 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001792 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001793 __set_size(__newsz);
1794 __invalidate_iterators_past(__newsz);
1795 traits_type::assign(__p[__newsz], value_type());
1796 return *this;
1797 }
1798
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001799 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001801 template<class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001802 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001803 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001804 // assume that the ranges overlap, because we can't check during constant evaluation
1805 if (__libcpp_is_constant_evaluated())
1806 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001807 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001808 return data() <= __p && __p <= data() + size();
1809 }
1810
Louis Dionned24191c2021-08-19 12:39:16 -04001811 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1812 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001813 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001814 }
1815
1816 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1817 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001818 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001819 }
1820
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001821 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const basic_string&);
1822 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const value_type*, const basic_string&);
1823 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(value_type, const basic_string&);
1824 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const value_type*);
1825 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826};
1827
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001828// These declarations must appear before any functions are implicitly used
1829// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001830#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001831#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001832 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001833# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001834 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001835# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001836#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001837 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001838# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001839 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001840# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001841#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001842#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001843
1844
Louis Dionned59f8a52021-08-17 11:59:07 -04001845#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001846template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001847 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001848 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001849 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1850 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001851 >
1852basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1853 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001854
1855template<class _CharT,
1856 class _Traits,
1857 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001858 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001859 >
1860explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1861 -> basic_string<_CharT, _Traits, _Allocator>;
1862
1863template<class _CharT,
1864 class _Traits,
1865 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001866 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001867 class _Sz = typename allocator_traits<_Allocator>::size_type
1868 >
1869basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1870 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001871#endif
1872
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001874inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001876basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877{
Louis Dionne510450b2022-04-01 16:38:30 -04001878#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001879 if (!__libcpp_is_constant_evaluated()) {
1880 __c_node* __c = __get_db()->__find_c_and_lock(this);
1881 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001883 const_pointer __new_last = __get_pointer() + __pos;
1884 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001886 --__p;
1887 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1888 if (__i->base() > __new_last)
1889 {
1890 (*__p)->__c_ = nullptr;
1891 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001892 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001893 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001895 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 }
1897 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001898#else
1899 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04001900#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901}
1902
1903template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001904inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001905basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001906 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001907 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001909 std::__debug_db_insert_c(this);
1910 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911}
1912
1913template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001914inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001916#if _LIBCPP_STD_VER <= 14
1917 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1918#else
1919 _NOEXCEPT
1920#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001921: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001923 std::__debug_db_insert_c(this);
1924 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925}
1926
1927template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001928_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier815ed732016-09-16 00:00:48 +00001929void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1930 size_type __sz,
1931 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001933 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001934 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001936 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001938 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939 {
1940 __set_short_size(__sz);
1941 __p = __get_short_pointer();
1942 }
1943 else
1944 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001945 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1946 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001947 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001949 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950 __set_long_size(__sz);
1951 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001952 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953 traits_type::assign(__p[__sz], value_type());
1954}
1955
1956template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001957_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001959basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001960{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001961 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001962 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001964 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001966 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967 {
1968 __set_short_size(__sz);
1969 __p = __get_short_pointer();
1970 }
1971 else
1972 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001973 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1974 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001975 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001977 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978 __set_long_size(__sz);
1979 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001980 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 traits_type::assign(__p[__sz], value_type());
1982}
1983
1984template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001985template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001986_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00001987basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001988 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001990 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001992 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993}
1994
1995template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001996inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00001997basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001998 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999{
Nikolas Klauserf2807732022-01-11 00:33:35 +01002000 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
2001 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002002 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003}
2004
2005template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002006inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002007basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002008 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002010 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002012 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013}
2014
2015template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002016_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002018 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002019{
2020 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002021 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002023 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002024 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002025 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026}
2027
2028template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002029_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002030basic_string<_CharT, _Traits, _Allocator>::basic_string(
2031 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002032 : __r_(__default_init_tag(), __a)
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
Martijn Vels5e7c9752020-03-04 17:52:46 -05002042template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002043_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Vels5e7c9752020-03-04 17:52:46 -05002044void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2045 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002046 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002047 __r_.first() = __rep();
2048
Martijn Vels5e7c9752020-03-04 17:52:46 -05002049 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002050 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002051 __p = __get_short_pointer();
2052 __set_short_size(__sz);
2053 } else {
2054 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002055 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002056 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2057 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002058 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002059 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002060 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002061 __set_long_size(__sz);
2062 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002063 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002064}
2065
Eric Fiselierfc92be82017-04-19 00:28:44 +00002066#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067
2068template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002069inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00002070basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00002071#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00002072 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00002073#else
2074 _NOEXCEPT
2075#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002076 : __r_(std::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002077{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002078 __str.__default_init();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002079 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002080 if (__is_long())
2081 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082}
2083
2084template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002085inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002087 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002088{
Marshall Clowd2d24692014-07-17 15:32:20 +00002089 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002090 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002091 else
2092 {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002093 if (__libcpp_is_constant_evaluated())
2094 __r_.first() = __rep();
2095 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002096 __str.__default_init();
Marshall Clowd2d24692014-07-17 15:32:20 +00002097 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002098 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002099 if (__is_long())
2100 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002101}
2102
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002103#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104
2105template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002106_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107void
2108basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2109{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002110 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002111 __r_.first() = __rep();
2112
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002114 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002116 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117 {
2118 __set_short_size(__n);
2119 __p = __get_short_pointer();
2120 }
2121 else
2122 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002123 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2124 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002125 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002127 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128 __set_long_size(__n);
2129 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002130 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 traits_type::assign(__p[__n], value_type());
2132}
2133
2134template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002135inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002136basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002137 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138{
2139 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002140 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141}
2142
2143template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002144template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002145_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002146basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002147 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148{
2149 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002150 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151}
2152
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002154_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002155basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2156 size_type __pos, size_type __n,
2157 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002158 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159{
2160 size_type __str_sz = __str.size();
2161 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002162 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002163 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2164 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165}
2166
2167template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002168inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow83445802016-04-07 18:13:41 +00002169basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002170 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002171 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002172{
2173 size_type __str_sz = __str.size();
2174 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002175 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002176 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002177 std::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002178}
2179
2180template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002181template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002182_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow78dbe462016-11-14 18:22:19 +00002183basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002184 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002185 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002186{
Marshall Clowe46031a2018-07-02 18:41:15 +00002187 __self_view __sv0 = __t;
2188 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002189 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002190 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002191}
2192
2193template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002194template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002195_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002196basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002197 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002198{
Marshall Clowe46031a2018-07-02 18:41:15 +00002199 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002200 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002201 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002202}
2203
2204template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002205template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002206_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002207basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002208 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002209{
Marshall Clowe46031a2018-07-02 18:41:15 +00002210 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002211 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002212 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002213}
2214
2215template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002217_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002218__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002220 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2221>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2223{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002224 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225#ifndef _LIBCPP_NO_EXCEPTIONS
2226 try
2227 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002228#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229 for (; __first != __last; ++__first)
2230 push_back(*__first);
2231#ifndef _LIBCPP_NO_EXCEPTIONS
2232 }
2233 catch (...)
2234 {
2235 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002236 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 throw;
2238 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002239#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240}
2241
2242template <class _CharT, class _Traits, class _Allocator>
2243template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002244_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002245__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002247 __is_cpp17_forward_iterator<_ForwardIterator>::value
2248>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2250{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002251 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002252 __r_.first() = __rep();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002253 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002255 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002257 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 {
2259 __set_short_size(__sz);
2260 __p = __get_short_pointer();
2261 }
2262 else
2263 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002264 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2265 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002266 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002268 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 __set_long_size(__sz);
2270 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002271
2272#ifndef _LIBCPP_NO_EXCEPTIONS
2273 try
2274 {
2275#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002276 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 traits_type::assign(*__p, *__first);
2278 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002279#ifndef _LIBCPP_NO_EXCEPTIONS
2280 }
2281 catch (...)
2282 {
2283 if (__is_long())
2284 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2285 throw;
2286 }
2287#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288}
2289
2290template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002291template<class _InputIterator, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002292inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002293basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002294 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295{
2296 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002297 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298}
2299
2300template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002301template<class _InputIterator, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002302inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2304 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002305 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306{
2307 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002308 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309}
2310
Eric Fiselierfc92be82017-04-19 00:28:44 +00002311#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002312
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002314inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002315basic_string<_CharT, _Traits, _Allocator>::basic_string(
2316 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002317 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318{
2319 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002320 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321}
2322
2323template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002324inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002325basic_string<_CharT, _Traits, _Allocator>::basic_string(
2326 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002327 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328{
2329 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002330 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331}
2332
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002333#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002334
Howard Hinnantc51e1022010-05-11 19:42:16 +00002335template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002336_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2338{
Nikolas Klauser98833542022-05-07 22:20:23 +02002339 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002341 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342}
2343
2344template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002345_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346void
2347basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2348 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002349 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 +00002350{
2351 size_type __ms = max_size();
2352 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002353 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354 pointer __old_p = __get_pointer();
2355 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002356 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002357 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002358 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2359 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002360 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002361 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002363 traits_type::copy(std::__to_address(__p),
2364 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002366 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002367 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2368 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002369 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2370 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002371 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002372 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002374 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2376 __set_long_size(__old_sz);
2377 traits_type::assign(__p[__old_sz], value_type());
2378}
2379
2380template <class _CharT, class _Traits, class _Allocator>
2381void
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002382_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2384 size_type __n_copy, size_type __n_del, size_type __n_add)
2385{
2386 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002387 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002388 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389 pointer __old_p = __get_pointer();
2390 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002391 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002393 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2394 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002395 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002396 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002398 traits_type::copy(std::__to_address(__p),
2399 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2401 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002402 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2403 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002404 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002405 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2406 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002408 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409}
2410
2411// assign
2412
2413template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002414template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002415_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsb6a08b62020-04-10 18:36:31 -04002416basic_string<_CharT, _Traits, _Allocator>&
2417basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002418 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002419 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002420 if (__n < __cap) {
2421 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2422 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002423 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002424 traits_type::assign(__p[__n], value_type());
2425 __invalidate_iterators_past(__n);
2426 } else {
2427 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2428 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2429 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002430 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002431}
2432
2433template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002434_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002436basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2437 const value_type* __s, size_type __n) {
2438 size_type __cap = capacity();
2439 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002440 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002441 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002442 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002443 } else {
2444 size_type __sz = size();
2445 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002446 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002447 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002448}
2449
2450template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002451_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002452basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002453basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002455 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002456 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002457 ? __assign_short(__s, __n)
2458 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459}
2460
2461template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002462_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463basic_string<_CharT, _Traits, _Allocator>&
2464basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2465{
2466 size_type __cap = capacity();
2467 if (__cap < __n)
2468 {
2469 size_type __sz = size();
2470 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2471 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002472 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002474 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475}
2476
2477template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002478_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479basic_string<_CharT, _Traits, _Allocator>&
2480basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2481{
2482 pointer __p;
2483 if (__is_long())
2484 {
2485 __p = __get_long_pointer();
2486 __set_long_size(1);
2487 }
2488 else
2489 {
2490 __p = __get_short_pointer();
2491 __set_short_size(1);
2492 }
2493 traits_type::assign(*__p, __c);
2494 traits_type::assign(*++__p, value_type());
2495 __invalidate_iterators_past(1);
2496 return *this;
2497}
2498
2499template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002500_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002501basic_string<_CharT, _Traits, _Allocator>&
2502basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2503{
Martijn Vels596e3de2020-02-26 15:55:49 -05002504 if (this != &__str) {
2505 __copy_assign_alloc(__str);
2506 if (!__is_long()) {
2507 if (!__str.__is_long()) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002508 __r_.first() = __str.__r_.first();
Martijn Vels596e3de2020-02-26 15:55:49 -05002509 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002510 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002511 }
2512 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002513 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002514 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002515 }
2516 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002517}
2518
Eric Fiselierfc92be82017-04-19 00:28:44 +00002519#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002520
2521template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002522inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002523void
2524basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002525 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002526{
2527 if (__alloc() != __str.__alloc())
2528 assign(__str);
2529 else
2530 __move_assign(__str, true_type());
2531}
2532
2533template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002534inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002535void
2536basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002537#if _LIBCPP_STD_VER > 14
2538 _NOEXCEPT
2539#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002540 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002541#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002542{
marshall2ed41622019-11-27 07:13:00 -08002543 if (__is_long()) {
2544 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2545 __get_long_cap());
2546#if _LIBCPP_STD_VER <= 14
2547 if (!is_nothrow_move_assignable<allocator_type>::value) {
2548 __set_short_size(0);
2549 traits_type::assign(__get_short_pointer()[0], value_type());
2550 }
2551#endif
2552 }
2553 __move_assign_alloc(__str);
2554 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002555 if (__libcpp_is_constant_evaluated()) {
2556 __str.__default_init();
2557 } else {
2558 __str.__set_short_size(0);
2559 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2560 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002561}
2562
2563template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002564inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002565basic_string<_CharT, _Traits, _Allocator>&
2566basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002567 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002568{
2569 __move_assign(__str, integral_constant<bool,
2570 __alloc_traits::propagate_on_container_move_assignment::value>());
2571 return *this;
2572}
2573
2574#endif
2575
2576template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002578_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002579__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002581 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002583>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2585{
Marshall Clow958362f2016-09-05 01:54:30 +00002586 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002587 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002588 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589}
2590
2591template <class _CharT, class _Traits, class _Allocator>
2592template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002593_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002594__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002596 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002598>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2600{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002602 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002603 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002604
2605 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2606 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002608 if (__cap < __n)
2609 {
2610 size_type __sz = size();
2611 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2612 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002613 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002614 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002615 traits_type::assign(*__p, *__first);
2616 traits_type::assign(*__p, value_type());
2617 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002618 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619 }
2620 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002621 {
2622 const basic_string __temp(__first, __last, __alloc());
2623 assign(__temp.data(), __temp.size());
2624 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625 return *this;
2626}
2627
2628template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002629_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630basic_string<_CharT, _Traits, _Allocator>&
2631basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2632{
2633 size_type __sz = __str.size();
2634 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002635 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002636 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637}
2638
2639template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002640template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002641_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002642__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002643<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002644 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2645 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002646 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002647>
Marshall Clow82513342016-09-24 22:45:42 +00002648basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002649{
Marshall Clow82513342016-09-24 22:45:42 +00002650 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002651 size_type __sz = __sv.size();
2652 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002653 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002654 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002655}
2656
2657
2658template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002659_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002661basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2662 return __assign_external(__s, traits_type::length(__s));
2663}
2664
2665template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002666_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002667basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002668basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002670 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002671 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002672 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002673 ? __assign_short(__s, traits_type::length(__s))
2674 : __assign_external(__s, traits_type::length(__s)))
2675 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677// append
2678
2679template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002680_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002682basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002684 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 size_type __cap = capacity();
2686 size_type __sz = size();
2687 if (__cap - __sz >= __n)
2688 {
2689 if (__n)
2690 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002691 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 traits_type::copy(__p + __sz, __s, __n);
2693 __sz += __n;
2694 __set_size(__sz);
2695 traits_type::assign(__p[__sz], value_type());
2696 }
2697 }
2698 else
2699 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2700 return *this;
2701}
2702
2703template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002704_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705basic_string<_CharT, _Traits, _Allocator>&
2706basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2707{
2708 if (__n)
2709 {
2710 size_type __cap = capacity();
2711 size_type __sz = size();
2712 if (__cap - __sz < __n)
2713 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2714 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002715 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716 __sz += __n;
2717 __set_size(__sz);
2718 traits_type::assign(__p[__sz], value_type());
2719 }
2720 return *this;
2721}
2722
2723template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002724_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002725basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2726{
2727 if (__n)
2728 {
2729 size_type __cap = capacity();
2730 size_type __sz = size();
2731 if (__cap - __sz < __n)
2732 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2733 pointer __p = __get_pointer();
2734 __sz += __n;
2735 __set_size(__sz);
2736 traits_type::assign(__p[__sz], value_type());
2737 }
2738}
2739
2740template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002741_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742void
2743basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2744{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002745 bool __is_short = !__is_long();
2746 size_type __cap;
2747 size_type __sz;
2748 if (__is_short)
2749 {
2750 __cap = __min_cap - 1;
2751 __sz = __get_short_size();
2752 }
2753 else
2754 {
2755 __cap = __get_long_cap() - 1;
2756 __sz = __get_long_size();
2757 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002759 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002761 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002762 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002763 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002764 if (__is_short)
2765 {
2766 __p = __get_short_pointer() + __sz;
2767 __set_short_size(__sz+1);
2768 }
2769 else
2770 {
2771 __p = __get_long_pointer() + __sz;
2772 __set_long_size(__sz+1);
2773 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774 traits_type::assign(*__p, __c);
2775 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776}
2777
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778template <class _CharT, class _Traits, class _Allocator>
2779template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002780_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002781__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002782<
2783 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2784 basic_string<_CharT, _Traits, _Allocator>&
2785>
2786basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002787 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788{
2789 size_type __sz = size();
2790 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002791 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 if (__n)
2793 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002794 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2795 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002796 {
2797 if (__cap - __sz < __n)
2798 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2799 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002800 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002801 traits_type::assign(*__p, *__first);
2802 traits_type::assign(*__p, value_type());
2803 __set_size(__sz + __n);
2804 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002805 else
2806 {
2807 const basic_string __temp(__first, __last, __alloc());
2808 append(__temp.data(), __temp.size());
2809 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810 }
2811 return *this;
2812}
2813
2814template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002815inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816basic_string<_CharT, _Traits, _Allocator>&
2817basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2818{
2819 return append(__str.data(), __str.size());
2820}
2821
2822template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002823_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824basic_string<_CharT, _Traits, _Allocator>&
2825basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2826{
2827 size_type __sz = __str.size();
2828 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002829 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002830 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831}
2832
2833template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002834template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002835_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002836 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002837 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002838 __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 +00002839 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002840 >
Marshall Clow82513342016-09-24 22:45:42 +00002841basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002842{
Marshall Clow82513342016-09-24 22:45:42 +00002843 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002844 size_type __sz = __sv.size();
2845 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002846 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002847 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002848}
2849
2850template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002851_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002853basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002855 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 return append(__s, traits_type::length(__s));
2857}
2858
2859// insert
2860
2861template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002862_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002864basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002866 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 size_type __sz = size();
2868 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002869 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002871 if (__libcpp_is_constant_evaluated()) {
2872 if (__cap - __sz >= __n)
2873 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2874 else
2875 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2876 return *this;
2877 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 if (__cap - __sz >= __n)
2879 {
2880 if (__n)
2881 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002882 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883 size_type __n_move = __sz - __pos;
2884 if (__n_move != 0)
2885 {
2886 if (__p + __pos <= __s && __s < __p + __sz)
2887 __s += __n;
2888 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2889 }
2890 traits_type::move(__p + __pos, __s, __n);
2891 __sz += __n;
2892 __set_size(__sz);
2893 traits_type::assign(__p[__sz], value_type());
2894 }
2895 }
2896 else
2897 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2898 return *this;
2899}
2900
2901template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002902_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903basic_string<_CharT, _Traits, _Allocator>&
2904basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2905{
2906 size_type __sz = size();
2907 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002908 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 if (__n)
2910 {
2911 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002912 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913 if (__cap - __sz >= __n)
2914 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002915 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916 size_type __n_move = __sz - __pos;
2917 if (__n_move != 0)
2918 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2919 }
2920 else
2921 {
2922 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002923 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924 }
2925 traits_type::assign(__p + __pos, __n, __c);
2926 __sz += __n;
2927 __set_size(__sz);
2928 traits_type::assign(__p[__sz], value_type());
2929 }
2930 return *this;
2931}
2932
2933template <class _CharT, class _Traits, class _Allocator>
2934template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002935_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002936__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002938 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002939 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002940>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2942{
Nikolas Klausereed25832021-12-15 01:32:30 +01002943 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2944 "string::insert(iterator, range) called with an iterator not"
2945 " referring to this string");
2946 const basic_string __temp(__first, __last, __alloc());
2947 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948}
2949
2950template <class _CharT, class _Traits, class _Allocator>
2951template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002952_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002953__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002955 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002957>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2959{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002960 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2961 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002962
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002964 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2965 if (__n == 0)
2966 return begin() + __ip;
2967
2968 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002970 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002972 else
2973 {
2974 const basic_string __temp(__first, __last, __alloc());
2975 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2976 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977}
2978
2979template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002980inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981basic_string<_CharT, _Traits, _Allocator>&
2982basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2983{
2984 return insert(__pos1, __str.data(), __str.size());
2985}
2986
2987template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002988_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989basic_string<_CharT, _Traits, _Allocator>&
2990basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2991 size_type __pos2, size_type __n)
2992{
2993 size_type __str_sz = __str.size();
2994 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002995 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002996 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997}
2998
2999template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003000template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003001_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003002__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003003<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003004 __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 +00003005 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003006>
Marshall Clow82513342016-09-24 22:45:42 +00003007basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003008 size_type __pos2, size_type __n)
3009{
Marshall Clow82513342016-09-24 22:45:42 +00003010 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003011 size_type __str_sz = __sv.size();
3012 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003013 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003014 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003015}
3016
3017template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003018_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003020basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003022 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023 return insert(__pos, __s, traits_type::length(__s));
3024}
3025
3026template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003027_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028typename basic_string<_CharT, _Traits, _Allocator>::iterator
3029basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
3030{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05003031 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3032 "string::insert(iterator, character) called with an iterator not"
3033 " referring to this string");
3034
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 size_type __ip = static_cast<size_type>(__pos - begin());
3036 size_type __sz = size();
3037 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003038 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039 if (__cap == __sz)
3040 {
3041 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003042 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043 }
3044 else
3045 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003046 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047 size_type __n_move = __sz - __ip;
3048 if (__n_move != 0)
3049 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3050 }
3051 traits_type::assign(__p[__ip], __c);
3052 traits_type::assign(__p[++__sz], value_type());
3053 __set_size(__sz);
3054 return begin() + static_cast<difference_type>(__ip);
3055}
3056
3057template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003058inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059typename basic_string<_CharT, _Traits, _Allocator>::iterator
3060basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3061{
Nikolas Klausereed25832021-12-15 01:32:30 +01003062 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3063 "string::insert(iterator, n, value) called with an iterator not"
3064 " referring to this string");
3065 difference_type __p = __pos - begin();
3066 insert(static_cast<size_type>(__p), __n, __c);
3067 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068}
3069
3070// replace
3071
3072template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003073_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003075basic_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 +00003076 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003078 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079 size_type __sz = size();
3080 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003081 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003082 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083 size_type __cap = capacity();
3084 if (__cap - __sz + __n1 >= __n2)
3085 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003086 if (__libcpp_is_constant_evaluated()) {
3087 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3088 return *this;
3089 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003090 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 if (__n1 != __n2)
3092 {
3093 size_type __n_move = __sz - __pos - __n1;
3094 if (__n_move != 0)
3095 {
3096 if (__n1 > __n2)
3097 {
3098 traits_type::move(__p + __pos, __s, __n2);
3099 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003100 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 }
3102 if (__p + __pos < __s && __s < __p + __sz)
3103 {
3104 if (__p + __pos + __n1 <= __s)
3105 __s += __n2 - __n1;
3106 else // __p + __pos < __s < __p + __pos + __n1
3107 {
3108 traits_type::move(__p + __pos, __s, __n1);
3109 __pos += __n1;
3110 __s += __n2;
3111 __n2 -= __n1;
3112 __n1 = 0;
3113 }
3114 }
3115 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3116 }
3117 }
3118 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003119 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120 }
3121 else
3122 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3123 return *this;
3124}
3125
3126template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003127_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128basic_string<_CharT, _Traits, _Allocator>&
3129basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3130{
3131 size_type __sz = size();
3132 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003133 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003134 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003136 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137 if (__cap - __sz + __n1 >= __n2)
3138 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003139 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140 if (__n1 != __n2)
3141 {
3142 size_type __n_move = __sz - __pos - __n1;
3143 if (__n_move != 0)
3144 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3145 }
3146 }
3147 else
3148 {
3149 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003150 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 }
3152 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003153 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154}
3155
3156template <class _CharT, class _Traits, class _Allocator>
3157template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003158_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003159__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003161 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003163>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003164basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 _InputIterator __j1, _InputIterator __j2)
3166{
Marshall Clow958362f2016-09-05 01:54:30 +00003167 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003168 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169}
3170
3171template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003172inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173basic_string<_CharT, _Traits, _Allocator>&
3174basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3175{
3176 return replace(__pos1, __n1, __str.data(), __str.size());
3177}
3178
3179template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003180_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003181basic_string<_CharT, _Traits, _Allocator>&
3182basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3183 size_type __pos2, size_type __n2)
3184{
3185 size_type __str_sz = __str.size();
3186 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003187 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003188 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189}
3190
3191template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003192template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003193_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003194__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003195<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003196 __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 +00003197 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003198>
Marshall Clow82513342016-09-24 22:45:42 +00003199basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003200 size_type __pos2, size_type __n2)
3201{
Marshall Clow82513342016-09-24 22:45:42 +00003202 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003203 size_type __str_sz = __sv.size();
3204 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003205 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003206 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003207}
3208
3209template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003210_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003211basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003212basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003214 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215 return replace(__pos, __n1, __s, traits_type::length(__s));
3216}
3217
3218template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003219inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003221basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222{
3223 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3224 __str.data(), __str.size());
3225}
3226
3227template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003228inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003230basic_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 +00003231{
3232 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3233}
3234
3235template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003236inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003238basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239{
3240 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3241}
3242
3243template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003244inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003246basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247{
3248 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3249}
3250
3251// erase
3252
Martijn Velsa81fc792020-02-26 13:25:43 -05003253// 'externally instantiated' erase() implementation, called when __n != npos.
3254// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003256_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003257void
3258basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3259 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261 if (__n)
3262 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003263 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003264 value_type* __p = std::__to_address(__get_pointer());
3265 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266 size_type __n_move = __sz - __pos - __n;
3267 if (__n_move != 0)
3268 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003269 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003271}
3272
3273template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003274_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003275basic_string<_CharT, _Traits, _Allocator>&
3276basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3277 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003278 if (__pos > size())
3279 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003280 if (__n == npos) {
3281 __erase_to_end(__pos);
3282 } else {
3283 __erase_external_with_move(__pos, __n);
3284 }
3285 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286}
3287
3288template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003289inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290typename basic_string<_CharT, _Traits, _Allocator>::iterator
3291basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3292{
Nikolas Klausereed25832021-12-15 01:32:30 +01003293 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3294 "string::erase(iterator) called with an iterator not"
3295 " referring to this string");
3296
3297 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3298 iterator __b = begin();
3299 size_type __r = static_cast<size_type>(__pos - __b);
3300 erase(__r, 1);
3301 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302}
3303
3304template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003305inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003306typename basic_string<_CharT, _Traits, _Allocator>::iterator
3307basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3308{
Nikolas Klausereed25832021-12-15 01:32:30 +01003309 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3310 "string::erase(iterator, iterator) called with an iterator not"
3311 " referring to this string");
3312
3313 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3314 iterator __b = begin();
3315 size_type __r = static_cast<size_type>(__first - __b);
3316 erase(__r, static_cast<size_type>(__last - __first));
3317 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318}
3319
3320template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003321inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322void
3323basic_string<_CharT, _Traits, _Allocator>::pop_back()
3324{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003325 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003326 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327}
3328
3329template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003330inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003332basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003334 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003335 if (__is_long())
3336 {
3337 traits_type::assign(*__get_long_pointer(), value_type());
3338 __set_long_size(0);
3339 }
3340 else
3341 {
3342 traits_type::assign(*__get_short_pointer(), value_type());
3343 __set_short_size(0);
3344 }
3345}
3346
3347template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003348inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349void
3350basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3351{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003352 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353}
3354
3355template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003356_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357void
3358basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3359{
3360 size_type __sz = size();
3361 if (__n > __sz)
3362 append(__n - __sz, __c);
3363 else
3364 __erase_to_end(__n);
3365}
3366
3367template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003368_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003369basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3370{
3371 size_type __sz = size();
3372 if (__n > __sz) {
3373 __append_default_init(__n - __sz);
3374 } else
3375 __erase_to_end(__n);
3376}
3377
3378template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003379inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003381basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003383 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003384 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3385 return __m - __alignment;
3386 } else {
3387 bool __uses_lsb = __endian_factor == 2;
3388 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3389 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390}
3391
3392template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003393_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394void
Marek Kurdejc9848142020-11-26 10:07:16 +01003395basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396{
Marek Kurdejc9848142020-11-26 10:07:16 +01003397 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003398 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003399
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003400 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3401 // and later (since P0966R1), however we provide consistent behavior in all Standard
3402 // modes because this function is instantiated in the shared library.
3403 if (__requested_capacity <= capacity())
3404 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003405
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003406 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003407 __target_capacity = __recommend(__target_capacity);
3408 if (__target_capacity == capacity()) return;
3409
3410 __shrink_or_extend(__target_capacity);
3411}
3412
3413template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003414inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003415void
3416basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3417{
3418 size_type __target_capacity = __recommend(size());
3419 if (__target_capacity == capacity()) return;
3420
3421 __shrink_or_extend(__target_capacity);
3422}
3423
3424template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003425inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003426void
3427basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3428{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003429 size_type __cap = capacity();
3430 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003431
3432 pointer __new_data, __p;
3433 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003434 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003436 __was_long = true;
3437 __now_long = false;
3438 __new_data = __get_short_pointer();
3439 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003441 else
3442 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003443 if (__target_capacity > __cap) {
3444 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3445 __new_data = __allocation.ptr;
3446 __target_capacity = __allocation.count - 1;
3447 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003448 else
3449 {
3450 #ifndef _LIBCPP_NO_EXCEPTIONS
3451 try
3452 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003453 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003454 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3455 __new_data = __allocation.ptr;
3456 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003457 #ifndef _LIBCPP_NO_EXCEPTIONS
3458 }
3459 catch (...)
3460 {
3461 return;
3462 }
3463 #else // _LIBCPP_NO_EXCEPTIONS
3464 if (__new_data == nullptr)
3465 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003466 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003467 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003468 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003469 __now_long = true;
3470 __was_long = __is_long();
3471 __p = __get_pointer();
3472 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003473 traits_type::copy(std::__to_address(__new_data),
3474 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003475 if (__was_long)
3476 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3477 if (__now_long)
3478 {
3479 __set_long_cap(__target_capacity+1);
3480 __set_long_size(__sz);
3481 __set_long_pointer(__new_data);
3482 }
3483 else
3484 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003485 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486}
3487
3488template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003489inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003491basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003493 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494 return *(data() + __pos);
3495}
3496
3497template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003498inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003500basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003502 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503 return *(__get_pointer() + __pos);
3504}
3505
3506template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003507_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3509basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3510{
3511 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003512 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513 return (*this)[__n];
3514}
3515
3516template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003517_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518typename basic_string<_CharT, _Traits, _Allocator>::reference
3519basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3520{
3521 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003522 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523 return (*this)[__n];
3524}
3525
3526template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003527inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003529basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003531 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532 return *__get_pointer();
3533}
3534
3535template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003536inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003538basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003540 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541 return *data();
3542}
3543
3544template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003545inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003547basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003549 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550 return *(__get_pointer() + size() - 1);
3551}
3552
3553template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003554inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003556basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003558 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559 return *(data() + size() - 1);
3560}
3561
3562template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003563_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003565basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566{
3567 size_type __sz = size();
3568 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003569 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003570 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571 traits_type::copy(__s, data() + __pos, __rlen);
3572 return __rlen;
3573}
3574
3575template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003576inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577basic_string<_CharT, _Traits, _Allocator>
3578basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3579{
3580 return basic_string(*this, __pos, __n, __alloc());
3581}
3582
3583template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003584inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585void
3586basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003587#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003588 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003589#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003590 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003591 __is_nothrow_swappable<allocator_type>::value)
3592#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593{
Nikolas Klauser98833542022-05-07 22:20:23 +02003594 if (!__is_long())
3595 std::__debug_db_invalidate_all(this);
3596 if (!__str.__is_long())
3597 std::__debug_db_invalidate_all(&__str);
3598 std::__debug_db_swap(this, &__str);
3599
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003600 _LIBCPP_ASSERT(
3601 __alloc_traits::propagate_on_container_swap::value ||
3602 __alloc_traits::is_always_equal::value ||
3603 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003604 std::swap(__r_.first(), __str.__r_.first());
3605 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606}
3607
3608// find
3609
3610template <class _Traits>
3611struct _LIBCPP_HIDDEN __traits_eq
3612{
3613 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003614 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003615 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3616 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617};
3618
3619template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003620_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003622basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003623 size_type __pos,
3624 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003626 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003627 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003628 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629}
3630
3631template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003632inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003634basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3635 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003637 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003638 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639}
3640
3641template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003642template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003643_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003644__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003645<
3646 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3647 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003648>
Marshall Clowe46031a2018-07-02 18:41:15 +00003649basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003650 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003651{
Marshall Clowe46031a2018-07-02 18:41:15 +00003652 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003653 return __str_find<value_type, size_type, traits_type, npos>
3654 (data(), size(), __sv.data(), __pos, __sv.size());
3655}
3656
3657template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003658inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003659typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003660basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003661 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003663 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003664 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003665 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666}
3667
3668template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003669_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003671basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3672 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003674 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003675 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003676}
3677
3678// rfind
3679
3680template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003681_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003683basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003684 size_type __pos,
3685 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003687 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003688 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003689 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690}
3691
3692template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003693inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003695basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3696 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003698 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003699 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700}
3701
3702template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003703template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003704_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003705__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003706<
3707 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3708 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003709>
Marshall Clowe46031a2018-07-02 18:41:15 +00003710basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003711 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003712{
Marshall Clowe46031a2018-07-02 18:41:15 +00003713 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003714 return __str_rfind<value_type, size_type, traits_type, npos>
3715 (data(), size(), __sv.data(), __pos, __sv.size());
3716}
3717
3718template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003719inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003720typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003721basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003722 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003724 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003725 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003726 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727}
3728
3729template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003730_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003731typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003732basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3733 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003735 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003736 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737}
3738
3739// find_first_of
3740
3741template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003742_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003744basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003745 size_type __pos,
3746 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003748 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003749 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003750 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751}
3752
3753template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003754inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003756basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3757 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003759 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003760 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761}
3762
3763template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003764template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003765_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003766__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003767<
3768 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3769 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003770>
Marshall Clowe46031a2018-07-02 18:41:15 +00003771basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003772 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003773{
Marshall Clowe46031a2018-07-02 18:41:15 +00003774 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003775 return __str_find_first_of<value_type, size_type, traits_type, npos>
3776 (data(), size(), __sv.data(), __pos, __sv.size());
3777}
3778
3779template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003780inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003781typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003782basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003783 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003785 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003786 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003787 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788}
3789
3790template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003791inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003793basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3794 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795{
3796 return find(__c, __pos);
3797}
3798
3799// find_last_of
3800
3801template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003802inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003804basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003805 size_type __pos,
3806 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003808 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003809 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003810 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811}
3812
3813template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003814inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003816basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3817 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003819 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003820 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821}
3822
3823template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003824template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003825_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003826__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003827<
3828 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3829 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003830>
Marshall Clowe46031a2018-07-02 18:41:15 +00003831basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003832 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003833{
Marshall Clowe46031a2018-07-02 18:41:15 +00003834 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003835 return __str_find_last_of<value_type, size_type, traits_type, npos>
3836 (data(), size(), __sv.data(), __pos, __sv.size());
3837}
3838
3839template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003840inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003841typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003842basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003843 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003845 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003846 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003847 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848}
3849
3850template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003851inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003852typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003853basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3854 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855{
3856 return rfind(__c, __pos);
3857}
3858
3859// find_first_not_of
3860
3861template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003862_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003864basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003865 size_type __pos,
3866 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003868 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003869 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003870 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003871}
3872
3873template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003874inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003876basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3877 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003879 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003880 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881}
3882
3883template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003884template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003885_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003886__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003887<
3888 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3889 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003890>
Marshall Clowe46031a2018-07-02 18:41:15 +00003891basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003892 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003893{
Marshall Clowe46031a2018-07-02 18:41:15 +00003894 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003895 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3896 (data(), size(), __sv.data(), __pos, __sv.size());
3897}
3898
3899template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003900inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003901typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003902basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003903 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003905 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003906 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003907 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908}
3909
3910template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003911inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003913basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3914 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003916 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003917 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918}
3919
3920// find_last_not_of
3921
3922template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003923_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003925basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003926 size_type __pos,
3927 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003929 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003930 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003931 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932}
3933
3934template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003935inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003937basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3938 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003940 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003941 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942}
3943
3944template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003945template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003946_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003947__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003948<
3949 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3950 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003951>
Marshall Clowe46031a2018-07-02 18:41:15 +00003952basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003953 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003954{
Marshall Clowe46031a2018-07-02 18:41:15 +00003955 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003956 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3957 (data(), size(), __sv.data(), __pos, __sv.size());
3958}
3959
3960template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003961inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003962typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003963basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003964 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003966 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003967 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003968 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969}
3970
3971template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003972inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003974basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3975 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003977 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003978 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979}
3980
3981// compare
3982
3983template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003984template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003985_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003986__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003987<
3988 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3989 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003990>
zoecarver1997e0a2021-02-05 11:54:47 -08003991basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003992{
Marshall Clowe46031a2018-07-02 18:41:15 +00003993 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003994 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003995 size_t __rhs_sz = __sv.size();
3996 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003997 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003998 if (__result != 0)
3999 return __result;
4000 if (__lhs_sz < __rhs_sz)
4001 return -1;
4002 if (__lhs_sz > __rhs_sz)
4003 return 1;
4004 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005}
4006
4007template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004008inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004010basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004012 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013}
4014
4015template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004016inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004018basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4019 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00004020 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004021 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022{
Alp Tokerb8a95f52014-05-15 11:27:39 +00004023 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024 size_type __sz = size();
4025 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004026 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004027 size_type __rlen = std::min(__n1, __sz - __pos1);
4028 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029 if (__r == 0)
4030 {
4031 if (__rlen < __n2)
4032 __r = -1;
4033 else if (__rlen > __n2)
4034 __r = 1;
4035 }
4036 return __r;
4037}
4038
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004039template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00004040template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004041_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04004042__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00004043<
4044 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
4045 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004046>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004047basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4048 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00004049 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004050{
Marshall Clowe46031a2018-07-02 18:41:15 +00004051 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004052 return compare(__pos1, __n1, __sv.data(), __sv.size());
4053}
4054
4055template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004056inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004057int
4058basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4059 size_type __n1,
4060 const basic_string& __str) const
4061{
4062 return compare(__pos1, __n1, __str.data(), __str.size());
4063}
4064
4065template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00004066template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004067_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04004068__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00004069<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004070 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
4071 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00004072 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004073>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004074basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4075 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00004076 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004077 size_type __pos2,
4078 size_type __n2) const
4079{
Marshall Clow82513342016-09-24 22:45:42 +00004080 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004081 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
4082}
4083
4084template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004085_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004086int
4087basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4088 size_type __n1,
4089 const basic_string& __str,
4090 size_type __pos2,
4091 size_type __n2) const
4092{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004093 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004094}
4095
4096template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004097_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004098int
4099basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4100{
4101 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4102 return compare(0, npos, __s, traits_type::length(__s));
4103}
4104
4105template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004106_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004107int
4108basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4109 size_type __n1,
4110 const value_type* __s) const
4111{
4112 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4113 return compare(__pos1, __n1, __s, traits_type::length(__s));
4114}
4115
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116// __invariants
4117
4118template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004119inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120bool
4121basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4122{
4123 if (size() > capacity())
4124 return false;
4125 if (capacity() < __min_cap - 1)
4126 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004127 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004129 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130 return false;
4131 return true;
4132}
4133
Vedant Kumar55e007e2018-03-08 21:15:26 +00004134// __clear_and_shrink
4135
4136template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004137inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne173f29e2019-05-29 16:01:36 +00004138void
Marshall Clowe60a7182018-05-29 17:04:37 +00004139basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004140{
4141 clear();
4142 if(__is_long())
4143 {
4144 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4145 __set_long_cap(0);
4146 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004147 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004148 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004149}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004150
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151// operator==
4152
4153template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004154inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155bool
4156operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004157 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158{
Mark de Weverb4830e62022-07-19 07:56:23 +02004159#if _LIBCPP_STD_VER > 17
4160 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4161#else
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004162 size_t __lhs_sz = __lhs.size();
4163 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4164 __rhs.data(),
4165 __lhs_sz) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004166#endif
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004167}
4168
4169template<class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004170inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004171bool
4172operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4173 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4174{
4175 size_t __lhs_sz = __lhs.size();
4176 if (__lhs_sz != __rhs.size())
4177 return false;
4178 const char* __lp = __lhs.data();
4179 const char* __rp = __rhs.data();
4180 if (__lhs.__is_long())
4181 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4182 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4183 if (*__lp != *__rp)
4184 return false;
4185 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186}
4187
Mark de Weverb4830e62022-07-19 07:56:23 +02004188#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004189template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004190inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004192operator==(const _CharT* __lhs,
4193 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004195 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4196 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4197 size_t __lhs_len = _Traits::length(__lhs);
4198 if (__lhs_len != __rhs.size()) return false;
4199 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200}
Mark de Weverb4830e62022-07-19 07:56:23 +02004201#endif // _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004204inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004206operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4207 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208{
Mark de Weverb4830e62022-07-19 07:56:23 +02004209#if _LIBCPP_STD_VER > 17
4210 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4211#else
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004212 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4213 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4214 size_t __rhs_len = _Traits::length(__rhs);
4215 if (__rhs_len != __lhs.size()) return false;
4216 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004217#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218}
4219
Mark de Weverb4830e62022-07-19 07:56:23 +02004220#if _LIBCPP_STD_VER > 17
4221
4222template <class _CharT, class _Traits, class _Allocator>
4223_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
4224 const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4225 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
4226 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4227}
4228
4229template <class _CharT, class _Traits, class _Allocator>
4230_LIBCPP_HIDE_FROM_ABI constexpr auto
4231operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
4232 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4233}
4234
4235#else // _LIBCPP_STD_VER > 17
4236
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004237template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004238inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239bool
4240operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004241 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242{
4243 return !(__lhs == __rhs);
4244}
4245
4246template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004247inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004249operator!=(const _CharT* __lhs,
4250 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251{
4252 return !(__lhs == __rhs);
4253}
4254
4255template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004256inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004258operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4259 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260{
4261 return !(__lhs == __rhs);
4262}
4263
4264// operator<
4265
4266template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004267inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268bool
4269operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004270 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004272 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273}
4274
4275template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004276inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004277bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004278operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4279 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004281 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282}
4283
4284template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004285inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004287operator< (const _CharT* __lhs,
4288 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289{
4290 return __rhs.compare(__lhs) > 0;
4291}
4292
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293// operator>
4294
4295template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004296inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297bool
4298operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004299 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004300{
4301 return __rhs < __lhs;
4302}
4303
4304template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004305inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004307operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4308 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004309{
4310 return __rhs < __lhs;
4311}
4312
4313template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004314inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004316operator> (const _CharT* __lhs,
4317 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318{
4319 return __rhs < __lhs;
4320}
4321
4322// operator<=
4323
4324template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004325inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326bool
4327operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004328 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329{
4330 return !(__rhs < __lhs);
4331}
4332
4333template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004334inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004336operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4337 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338{
4339 return !(__rhs < __lhs);
4340}
4341
4342template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004343inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004345operator<=(const _CharT* __lhs,
4346 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347{
4348 return !(__rhs < __lhs);
4349}
4350
4351// operator>=
4352
4353template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004354inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355bool
4356operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004357 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358{
4359 return !(__lhs < __rhs);
4360}
4361
4362template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004363inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004365operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4366 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367{
4368 return !(__lhs < __rhs);
4369}
4370
4371template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004372inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004374operator>=(const _CharT* __lhs,
4375 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376{
4377 return !(__lhs < __rhs);
4378}
Mark de Weverb4830e62022-07-19 07:56:23 +02004379#endif // _LIBCPP_STD_VER > 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380
4381// operator +
4382
4383template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004384_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385basic_string<_CharT, _Traits, _Allocator>
4386operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4387 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4388{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004389 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004390 auto __lhs_sz = __lhs.size();
4391 auto __rhs_sz = __rhs.size();
4392 _String __r(__uninitialized_size_tag(),
4393 __lhs_sz + __rhs_sz,
4394 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4395 auto __ptr = std::__to_address(__r.__get_pointer());
4396 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4397 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4398 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004399 return __r;
4400}
4401
4402template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004403_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404basic_string<_CharT, _Traits, _Allocator>
4405operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4406{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004407 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004408 auto __lhs_sz = _Traits::length(__lhs);
4409 auto __rhs_sz = __rhs.size();
4410 _String __r(__uninitialized_size_tag(),
4411 __lhs_sz + __rhs_sz,
4412 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4413 auto __ptr = std::__to_address(__r.__get_pointer());
4414 _Traits::copy(__ptr, __lhs, __lhs_sz);
4415 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4416 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417 return __r;
4418}
4419
4420template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004421_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422basic_string<_CharT, _Traits, _Allocator>
4423operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4424{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004425 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004426 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004427 _String __r(__uninitialized_size_tag(),
4428 __rhs_sz + 1,
4429 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4430 auto __ptr = std::__to_address(__r.__get_pointer());
4431 _Traits::assign(__ptr, 1, __lhs);
4432 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4433 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434 return __r;
4435}
4436
4437template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004438inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004439basic_string<_CharT, _Traits, _Allocator>
4440operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4441{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004442 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004443 typename _String::size_type __lhs_sz = __lhs.size();
4444 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004445 _String __r(__uninitialized_size_tag(),
4446 __lhs_sz + __rhs_sz,
4447 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4448 auto __ptr = std::__to_address(__r.__get_pointer());
4449 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4450 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4451 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452 return __r;
4453}
4454
4455template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004456_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004457basic_string<_CharT, _Traits, _Allocator>
4458operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4459{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004460 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004461 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004462 _String __r(__uninitialized_size_tag(),
4463 __lhs_sz + 1,
4464 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4465 auto __ptr = std::__to_address(__r.__get_pointer());
4466 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4467 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4468 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004469 return __r;
4470}
4471
Eric Fiselierfc92be82017-04-19 00:28:44 +00004472#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004473
4474template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004475inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004476basic_string<_CharT, _Traits, _Allocator>
4477operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4478{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004479 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004480}
4481
4482template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004483inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484basic_string<_CharT, _Traits, _Allocator>
4485operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4486{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004487 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004488}
4489
4490template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004491inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004492basic_string<_CharT, _Traits, _Allocator>
4493operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4494{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004495 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004496}
4497
4498template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004499inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004500basic_string<_CharT, _Traits, _Allocator>
4501operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4502{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004503 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004504}
4505
4506template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004507inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004508basic_string<_CharT, _Traits, _Allocator>
4509operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4510{
4511 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004512 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004513}
4514
4515template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004516inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004517basic_string<_CharT, _Traits, _Allocator>
4518operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4519{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004520 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521}
4522
4523template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004524inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004525basic_string<_CharT, _Traits, _Allocator>
4526operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4527{
4528 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004529 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530}
4531
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004532#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533
4534// swap
4535
4536template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004537inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004539swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004540 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4541 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004542{
4543 __lhs.swap(__rhs);
4544}
4545
Bruce Mitchener170d8972020-11-24 12:53:53 -05004546_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4547_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4548_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4549_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4550_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004551
Bruce Mitchener170d8972020-11-24 12:53:53 -05004552_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4553_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4554_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004555
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004556_LIBCPP_FUNC_VIS string to_string(int __val);
4557_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4558_LIBCPP_FUNC_VIS string to_string(long __val);
4559_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4560_LIBCPP_FUNC_VIS string to_string(long long __val);
4561_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4562_LIBCPP_FUNC_VIS string to_string(float __val);
4563_LIBCPP_FUNC_VIS string to_string(double __val);
4564_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004565
Louis Dionne89258142021-08-23 15:32:36 -04004566#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004567_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4568_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4569_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4570_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4571_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004572
Bruce Mitchener170d8972020-11-24 12:53:53 -05004573_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4574_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4575_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004576
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004577_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4578_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4579_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4580_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4581_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4582_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4583_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4584_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4585_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004586#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004587
Howard Hinnantc51e1022010-05-11 19:42:16 +00004588template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004589_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004590const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4591 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592
Marshall Clow851b9ec2019-05-20 21:56:51 +00004593template <class _CharT, class _Allocator>
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004594struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004595{
4596 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004597 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4598 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004599};
4600
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004601template <class _Allocator>
4602struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
4603
4604#ifndef _LIBCPP_HAS_NO_CHAR8_T
4605template <class _Allocator>
4606struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
4607#endif
4608
4609template <class _Allocator>
4610struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
4611
4612template <class _Allocator>
4613struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
4614
4615#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4616template <class _Allocator>
4617struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
4618#endif
4619
Howard Hinnantdc095972011-07-18 15:51:59 +00004620template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004621_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004622operator<<(basic_ostream<_CharT, _Traits>& __os,
4623 const basic_string<_CharT, _Traits, _Allocator>& __str);
4624
4625template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004626_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004627operator>>(basic_istream<_CharT, _Traits>& __is,
4628 basic_string<_CharT, _Traits, _Allocator>& __str);
4629
4630template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004631_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004632getline(basic_istream<_CharT, _Traits>& __is,
4633 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4634
4635template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004636inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004637basic_istream<_CharT, _Traits>&
4638getline(basic_istream<_CharT, _Traits>& __is,
4639 basic_string<_CharT, _Traits, _Allocator>& __str);
4640
Howard Hinnantdc095972011-07-18 15:51:59 +00004641template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004642inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004643basic_istream<_CharT, _Traits>&
4644getline(basic_istream<_CharT, _Traits>&& __is,
4645 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4646
4647template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004648inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004649basic_istream<_CharT, _Traits>&
4650getline(basic_istream<_CharT, _Traits>&& __is,
4651 basic_string<_CharT, _Traits, _Allocator>& __str);
4652
Marshall Clow29b53f22018-12-14 18:49:35 +00004653#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004654template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004655inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004656 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4657 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4658 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004659 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004660 return __old_size - __str.size();
4661}
Marshall Clow29b53f22018-12-14 18:49:35 +00004662
Marek Kurdeja98b1412020-05-02 13:58:03 +02004663template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004664inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004665 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4666 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4667 _Predicate __pred) {
4668 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004669 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004670 __str.end());
4671 return __old_size - __str.size();
4672}
Marshall Clow29b53f22018-12-14 18:49:35 +00004673#endif
4674
Louis Dionne510450b2022-04-01 16:38:30 -04004675#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004676
4677template<class _CharT, class _Traits, class _Allocator>
4678bool
4679basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4680{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004681 return data() <= std::__to_address(__i->base()) &&
4682 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004683}
4684
4685template<class _CharT, class _Traits, class _Allocator>
4686bool
4687basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4688{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004689 return data() < std::__to_address(__i->base()) &&
4690 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004691}
4692
4693template<class _CharT, class _Traits, class _Allocator>
4694bool
4695basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4696{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004697 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004698 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004699}
4700
4701template<class _CharT, class _Traits, class _Allocator>
4702bool
4703basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4704{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004705 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004706 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004707}
4708
Louis Dionne510450b2022-04-01 16:38:30 -04004709#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004710
Louis Dionne173f29e2019-05-29 16:01:36 +00004711#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004712// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004713inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004714{
4715 inline namespace string_literals
4716 {
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004717 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004718 basic_string<char> operator "" s( const char *__str, size_t __len )
4719 {
4720 return basic_string<char> (__str, __len);
4721 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004722
Louis Dionne89258142021-08-23 15:32:36 -04004723#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004724 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004725 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4726 {
4727 return basic_string<wchar_t> (__str, __len);
4728 }
Louis Dionne89258142021-08-23 15:32:36 -04004729#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004730
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004731#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004732 inline _LIBCPP_HIDE_FROM_ABI constexpr
Nikolas Klauser5da956d2022-09-02 16:20:28 +02004733 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len)
Marshall Clow8732fed2018-12-11 04:35:44 +00004734 {
4735 return basic_string<char8_t> (__str, __len);
4736 }
4737#endif
4738
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004739 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004740 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4741 {
4742 return basic_string<char16_t> (__str, __len);
4743 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004744
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004745 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004746 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4747 {
4748 return basic_string<char32_t> (__str, __len);
4749 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004750 } // namespace string_literals
4751} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004752
4753#if _LIBCPP_STD_VER > 17
4754template <>
4755inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4756#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4757template <>
4758inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4759#endif
4760#endif
4761
Marshall Clowcba751f2013-07-23 17:05:24 +00004762#endif
4763
Howard Hinnantc51e1022010-05-11 19:42:16 +00004764_LIBCPP_END_NAMESPACE_STD
4765
Eric Fiselierf4433a32017-05-31 22:07:49 +00004766_LIBCPP_POP_MACROS
4767
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004768#endif // _LIBCPP_STRING