blob: bae6884347e031c3a630b5c0af0417d8f5b7cd46 [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 Weverb4830e62022-07-19 07:56:23 +0200378bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, 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 Weverb4830e62022-07-19 07:56:23 +0200385 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200388bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200391bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20, 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 Weverb4830e62022-07-19 07:56:23 +0200395 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200398bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200401bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, 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 Weverb4830e62022-07-19 07:56:23 +0200405 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200408bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200411bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, 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 Weverb4830e62022-07-19 07:56:23 +0200415 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200418bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200421bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, 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 Weverb4830e62022-07-19 07:56:23 +0200425 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200428bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Mark de Weverb4830e62022-07-19 07:56:23 +0200431bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20, removed in C++20
432
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>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562#include <memory>
563#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
Louis Dionne22355cc2022-06-27 15:53:41 -0400572#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
573# 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 Klausera9ad6702022-08-13 13:23:16 +0200609_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klausera9ad6702022-08-13 13:23:16 +0200614_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klausera9ad6702022-08-13 13:23:16 +0200619_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200624inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klausera9ad6702022-08-13 13:23:16 +0200629_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +0200803 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
804 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)) {
809 __zero();
810 __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 Klauser1d5108d2022-04-29 11:17:58 +0200826 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser1d5108d2022-04-29 11:17:58 +0200829 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser80b3cf32022-04-27 10:11:44 +0200836 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string(const basic_string& __str);
837 _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser1d5108d2022-04-29 11:17:58 +0200840 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200848 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200853 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200861 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200868 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000869 basic_string(const _CharT* __s, size_type __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200870 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000871 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200872 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200876 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000877 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
878
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200879 _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200882 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +0200887 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +0200893 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +0200897 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200901 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200904 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200907 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000908 basic_string(initializer_list<_CharT> __il);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200909 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +0200913 inline _LIBCPP_CONSTEXPR_AFTER_CXX17 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200915 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000916 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
917
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200918 _LIBCPP_CONSTEXPR_AFTER_CXX17 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> >
922 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(const _Tp& __t) {
923 __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 Klauser1d5108d2022-04-29 11:17:58 +0200928 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200931 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200934 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +0200939 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200941 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000942 iterator begin() _NOEXCEPT
943 {return iterator(this, __get_pointer());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200944 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000945 const_iterator begin() const _NOEXCEPT
946 {return const_iterator(this, __get_pointer());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200947 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000948 iterator end() _NOEXCEPT
949 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200950 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200954 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000955 reverse_iterator rbegin() _NOEXCEPT
956 {return reverse_iterator(end());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200957 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000958 const_reverse_iterator rbegin() const _NOEXCEPT
959 {return const_reverse_iterator(end());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200960 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000961 reverse_iterator rend() _NOEXCEPT
962 {return reverse_iterator(begin());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200963 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200967 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000968 const_iterator cbegin() const _NOEXCEPT
969 {return begin();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200970 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000971 const_iterator cend() const _NOEXCEPT
972 {return end();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200973 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000974 const_reverse_iterator crbegin() const _NOEXCEPT
975 {return rbegin();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200976 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +0200980 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200982 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type length() const _NOEXCEPT {return size();}
983 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type max_size() const _NOEXCEPT;
984 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser80b3cf32022-04-27 10:11:44 +0200988 _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200989 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200991 _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser1d5108d2022-04-29 11:17:58 +02001002 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 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(); }
1005 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void shrink_to_fit() _NOEXCEPT;
1006 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001007
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001008 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowb7db4972017-11-15 20:02:27 +00001009 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001011 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001012 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001013 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001015 _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference at(size_type __n) const;
1016 _LIBCPP_CONSTEXPR_AFTER_CXX17 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001018 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser80b3cf32022-04-27 10:11:44 +02001023 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001034 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001035 return append(__s);
1036 }
1037
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001038 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser1d5108d2022-04-29 11:17:58 +02001044 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001048 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001052 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001059 _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser80b3cf32022-04-27 10:11:44 +02001062 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001070 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s, size_type __n);
1071 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s);
1072 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001073
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001074 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001084 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001097 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001101 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001105 _LIBCPP_CONSTEXPR_AFTER_CXX17 void push_back(value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001106 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void pop_back();
1107 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference front() _NOEXCEPT;
1108 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference front() const _NOEXCEPT;
1109 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference back() _NOEXCEPT;
1110 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser80b3cf32022-04-27 10:11:44 +02001113 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001120 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001123 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001128 _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser80b3cf32022-04-27 10:11:44 +02001130 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001138 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s, size_type __n);
1139 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s);
1140 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001142 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001150 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001158 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001162 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001166 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001176 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001183 _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001185 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1186 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s);
1187 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1188 _LIBCPP_CONSTEXPR_AFTER_CXX17 iterator insert(const_iterator __pos, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001189 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1191 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001192 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001200 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001208 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001213 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001214 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215 iterator erase(const_iterator __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001216 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 iterator erase(const_iterator __first, const_iterator __last);
1218
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001219 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001223 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001230 _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001233 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001240 _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001242 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1243 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001244 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001248 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001256 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001258 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001259 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001260 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001263 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001271 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001276 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001277 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1279
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001280 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001289 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001290 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001291 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1292 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 Klauser1d5108d2022-04-29 11:17:58 +02001294 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1295 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 Klauser1d5108d2022-04-29 11:17:58 +02001298 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001301 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001305 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001312 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001313 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001314 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001315 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001316 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001322 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001329 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001330 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001331 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001332 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001333 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001335 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001339 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001346 _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001348 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001349 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001350 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001353 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001357 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001364 _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001366 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001367 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001368 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001371 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001375 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001382 _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001384 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001386 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001389 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001393 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001400 _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001402 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001404 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001407 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001411 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001420 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001428 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001430 _LIBCPP_CONSTEXPR_AFTER_CXX17
1431 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 Klauser1d5108d2022-04-29 11:17:58 +02001435 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02001442 _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(const value_type* __s) const _NOEXCEPT;
1443 _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1444 _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001487 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001488
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001489 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 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 Klauser1d5108d2022-04-29 11:17:58 +02001502 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001506 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001507
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001508 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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
1515 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __begin_lifetime(pointer __begin, size_type __n) {
1516#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
1527 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __default_init() {
1528 __zero();
1529 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
1539 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __deallocate_constexpr() {
1540 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>
1550 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1551 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 Klauser80b3cf32022-04-27 10:11:44 +02001576 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1577 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001579 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001586 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001592 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001593 void __set_long_size(size_type __s) _NOEXCEPT
1594 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001595 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001596 size_type __get_long_size() const _NOEXCEPT
1597 {return __r_.first().__l.__size_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001598 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001602 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001608 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001613 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001614 void __set_long_pointer(pointer __p) _NOEXCEPT
1615 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001616 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001617 pointer __get_long_pointer() _NOEXCEPT
1618 {return __r_.first().__l.__data_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001619 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001620 const_pointer __get_long_pointer() const _NOEXCEPT
1621 {return __r_.first().__l.__data_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001622 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001625 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001628 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser1d5108d2022-04-29 11:17:58 +02001631 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
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
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001635 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001636 void __zero() _NOEXCEPT {
1637 __r_.first() = __rep();
1638 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639
1640 template <size_type __a> static
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001641 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea382952013-08-14 18:00:20 +00001642 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001643 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 enum {__alignment = 16};
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001645 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001646 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001647 {
1648 if (__s < __min_cap) {
1649 if (__libcpp_is_constant_evaluated())
1650 return static_cast<size_type>(__min_cap);
1651 else
1652 return static_cast<size_type>(__min_cap) - 1;
1653 }
Marshall Clow80584522018-02-07 21:30:17 +00001654 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1655 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1656 if (__guess == __min_cap) ++__guess;
1657 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001658 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001660 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001661 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001662 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001663 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001664 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001666
Martijn Vels5e7c9752020-03-04 17:52:46 -05001667 // Slow path for the (inlined) copy constructor for 'long' strings.
1668 // Always externally instantiated and not inlined.
1669 // Requires that __s is zero terminated.
1670 // The main reason for this function to exist is because for unstable, we
1671 // want to allow inlining of the copy constructor. However, we don't want
1672 // to call the __init() functions as those are marked as inline which may
1673 // result in over-aggressive inlining by the compiler, where our aim is
1674 // to only inline the fast path code directly in the ctor.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001675 _LIBCPP_CONSTEXPR_AFTER_CXX17 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001676
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677 template <class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001678 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001679 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001681 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1682 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683 __init(_InputIterator __first, _InputIterator __last);
1684
1685 template <class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001686 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001687 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001689 __is_cpp17_forward_iterator<_ForwardIterator>::value
1690 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691 __init(_ForwardIterator __first, _ForwardIterator __last);
1692
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001693 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001695 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001696 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1698 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001699 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700
Martijn Vels596e3de2020-02-26 15:55:49 -05001701 // __assign_no_alias is invoked for assignment operations where we
1702 // have proof that the input does not alias the current instance.
1703 // For example, operator=(basic_string) performs a 'self' check.
1704 template <bool __is_short>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001705 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001706
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001707 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 void __erase_to_end(size_type __pos);
1709
Martijn Velsa81fc792020-02-26 13:25:43 -05001710 // __erase_external_with_move is invoked for erase() invocations where
1711 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001712 _LIBCPP_CONSTEXPR_AFTER_CXX17 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001713
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001714 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001715 void __copy_assign_alloc(const basic_string& __str)
1716 {__copy_assign_alloc(__str, integral_constant<bool,
1717 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1718
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001719 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001720 void __copy_assign_alloc(const basic_string& __str, true_type)
1721 {
Marshall Clowf258c202017-01-31 03:40:52 +00001722 if (__alloc() == __str.__alloc())
1723 __alloc() = __str.__alloc();
1724 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001725 {
Marshall Clowf258c202017-01-31 03:40:52 +00001726 if (!__str.__is_long())
1727 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001728 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001729 __alloc() = __str.__alloc();
1730 }
1731 else
1732 {
1733 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001734 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001735 __begin_lifetime(__allocation.ptr, __allocation.count);
Vedant Kumar55e007e2018-03-08 21:15:26 +00001736 __clear_and_shrink();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001737 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001738 __set_long_pointer(__allocation.ptr);
1739 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001740 __set_long_size(__str.size());
1741 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001742 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001743 }
1744
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001745 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant28b24882011-12-01 20:21:04 +00001746 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001747 {}
1748
Eric Fiselierfc92be82017-04-19 00:28:44 +00001749#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001750 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001751 void __move_assign(basic_string& __str, false_type)
1752 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001753 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001754 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001755#if _LIBCPP_STD_VER > 14
1756 _NOEXCEPT;
1757#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001758 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001759#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001760#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001761
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001762 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001763 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001764 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001765 _NOEXCEPT_(
1766 !__alloc_traits::propagate_on_container_move_assignment::value ||
1767 is_nothrow_move_assignable<allocator_type>::value)
1768 {__move_assign_alloc(__str, integral_constant<bool,
1769 __alloc_traits::propagate_on_container_move_assignment::value>());}
1770
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001771 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc2734962011-09-02 20:42:31 +00001772 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001773 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1774 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001775 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001776 }
1777
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001778 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant28b24882011-12-01 20:21:04 +00001779 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001780 _NOEXCEPT
1781 {}
1782
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001783 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s);
1784 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001785
1786 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1787 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1788 pointer __p = __is_long()
1789 ? (__set_long_size(__n), __get_long_pointer())
1790 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001791 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001792 traits_type::assign(__p[__n], value_type());
1793 return *this;
1794 }
1795
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001796 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1797 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001798 __set_size(__newsz);
1799 __invalidate_iterators_past(__newsz);
1800 traits_type::assign(__p[__newsz], value_type());
1801 return *this;
1802 }
1803
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001804 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001806 template<class _Tp>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001807 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001808 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001809 // assume that the ranges overlap, because we can't check during constant evaluation
1810 if (__libcpp_is_constant_evaluated())
1811 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001812 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001813 return data() <= __p && __p <= data() + size();
1814 }
1815
Louis Dionned24191c2021-08-19 12:39:16 -04001816 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1817 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001818 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001819 }
1820
1821 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1822 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001823 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001824 }
1825
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001826 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const basic_string&);
1827 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const value_type*, const basic_string&);
1828 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(value_type, const basic_string&);
1829 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const value_type*);
1830 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831};
1832
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001833// These declarations must appear before any functions are implicitly used
1834// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001835#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001836#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001837 _LIBCPP_STRING_UNSTABLE_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_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001840# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001841#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001842 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001843# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001844 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001845# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001846#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001847#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001848
1849
Louis Dionned59f8a52021-08-17 11:59:07 -04001850#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001851template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001852 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001853 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001854 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1855 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001856 >
1857basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1858 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001859
1860template<class _CharT,
1861 class _Traits,
1862 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001863 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001864 >
1865explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1866 -> basic_string<_CharT, _Traits, _Allocator>;
1867
1868template<class _CharT,
1869 class _Traits,
1870 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001871 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001872 class _Sz = typename allocator_traits<_Allocator>::size_type
1873 >
1874basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1875 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001876#endif
1877
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001879inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001881basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882{
Louis Dionne510450b2022-04-01 16:38:30 -04001883#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001884 if (!__libcpp_is_constant_evaluated()) {
1885 __c_node* __c = __get_db()->__find_c_and_lock(this);
1886 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001888 const_pointer __new_last = __get_pointer() + __pos;
1889 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001891 --__p;
1892 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1893 if (__i->base() > __new_last)
1894 {
1895 (*__p)->__c_ = nullptr;
1896 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001897 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001898 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001900 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 }
1902 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001903#else
1904 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04001905#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906}
1907
1908template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001909inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001910basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001911 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001912 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001914 std::__debug_db_insert_c(this);
1915 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916}
1917
1918template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001919inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001921#if _LIBCPP_STD_VER <= 14
1922 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1923#else
1924 _NOEXCEPT
1925#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001926: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001928 std::__debug_db_insert_c(this);
1929 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930}
1931
1932template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001933_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier815ed732016-09-16 00:00:48 +00001934void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1935 size_type __sz,
1936 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001938 if (__libcpp_is_constant_evaluated())
1939 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001941 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001943 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944 {
1945 __set_short_size(__sz);
1946 __p = __get_short_pointer();
1947 }
1948 else
1949 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001950 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1951 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001952 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001954 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 __set_long_size(__sz);
1956 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001957 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958 traits_type::assign(__p[__sz], value_type());
1959}
1960
1961template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001962_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001964basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001966 if (__libcpp_is_constant_evaluated())
1967 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001969 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001971 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972 {
1973 __set_short_size(__sz);
1974 __p = __get_short_pointer();
1975 }
1976 else
1977 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001978 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1979 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001980 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001982 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983 __set_long_size(__sz);
1984 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001985 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 traits_type::assign(__p[__sz], value_type());
1987}
1988
1989template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001990template <class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001991_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001992basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001993 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001995 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001997 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998}
1999
2000template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002001inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002002basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002003 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004{
Nikolas Klauserf2807732022-01-11 00:33:35 +01002005 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
2006 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002007 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008}
2009
2010template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002011inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002012basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002013 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002015 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002017 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018}
2019
2020template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002021_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002023 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024{
2025 if (!__str.__is_long())
2026 __r_.first().__r = __str.__r_.first().__r;
2027 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002028 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002029 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002030 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031}
2032
2033template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002034_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002035basic_string<_CharT, _Traits, _Allocator>::basic_string(
2036 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002037 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038{
2039 if (!__str.__is_long())
2040 __r_.first().__r = __str.__r_.first().__r;
2041 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002042 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002043 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002044 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045}
2046
Martijn Vels5e7c9752020-03-04 17:52:46 -05002047template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002048_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Vels5e7c9752020-03-04 17:52:46 -05002049void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2050 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002051 if (__libcpp_is_constant_evaluated())
2052 __zero();
Martijn Vels5e7c9752020-03-04 17:52:46 -05002053 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002054 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002055 __p = __get_short_pointer();
2056 __set_short_size(__sz);
2057 } else {
2058 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002059 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002060 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2061 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002062 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002063 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002064 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002065 __set_long_size(__sz);
2066 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002067 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002068}
2069
Eric Fiselierfc92be82017-04-19 00:28:44 +00002070#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071
2072template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002073inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00002074basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00002075#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00002076 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00002077#else
2078 _NOEXCEPT
2079#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002080 : __r_(std::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002082 __str.__default_init();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002083 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002084 if (__is_long())
2085 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086}
2087
2088template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002089inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002091 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092{
Marshall Clowd2d24692014-07-17 15:32:20 +00002093 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002094 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002095 else
2096 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002097 if (__libcpp_is_constant_evaluated()) {
2098 __zero();
2099 __r_.first().__l = __str.__r_.first().__l;
2100 } else {
2101 __r_.first().__r = __str.__r_.first().__r;
2102 }
2103 __str.__default_init();
Marshall Clowd2d24692014-07-17 15:32:20 +00002104 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002105 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002106 if (__is_long())
2107 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108}
2109
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002110#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111
2112template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002113_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114void
2115basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2116{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002117 if (__libcpp_is_constant_evaluated())
2118 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002120 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002122 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123 {
2124 __set_short_size(__n);
2125 __p = __get_short_pointer();
2126 }
2127 else
2128 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002129 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2130 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002131 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002133 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134 __set_long_size(__n);
2135 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002136 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137 traits_type::assign(__p[__n], value_type());
2138}
2139
2140template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002141inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002142basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002143 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144{
2145 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002146 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147}
2148
2149template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002150template <class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002151_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002152basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002153 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154{
2155 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002156 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157}
2158
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002160_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002161basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2162 size_type __pos, size_type __n,
2163 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002164 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165{
2166 size_type __str_sz = __str.size();
2167 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002168 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002169 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2170 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002171}
2172
2173template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002174inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +00002175basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002176 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002177 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002178{
2179 size_type __str_sz = __str.size();
2180 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002181 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002182 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002183 std::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002184}
2185
2186template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002187template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002188_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow78dbe462016-11-14 18:22:19 +00002189basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002190 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002191 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002192{
Marshall Clowe46031a2018-07-02 18:41:15 +00002193 __self_view __sv0 = __t;
2194 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002195 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002196 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002197}
2198
2199template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002200template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002201_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +00002202basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002203 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002204{
Marshall Clowe46031a2018-07-02 18:41:15 +00002205 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002206 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002207 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002208}
2209
2210template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002211template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002212_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +00002213basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002214 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002215{
Marshall Clowe46031a2018-07-02 18:41:15 +00002216 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002217 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002218 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002219}
2220
2221template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222template <class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002223_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002224__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002226 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2227>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2229{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002230 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231#ifndef _LIBCPP_NO_EXCEPTIONS
2232 try
2233 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002234#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 for (; __first != __last; ++__first)
2236 push_back(*__first);
2237#ifndef _LIBCPP_NO_EXCEPTIONS
2238 }
2239 catch (...)
2240 {
2241 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002242 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 throw;
2244 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002245#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246}
2247
2248template <class _CharT, class _Traits, class _Allocator>
2249template <class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002250_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002251__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002253 __is_cpp17_forward_iterator<_ForwardIterator>::value
2254>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2256{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002257 if (__libcpp_is_constant_evaluated())
2258 __zero();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002259 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002261 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002263 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 {
2265 __set_short_size(__sz);
2266 __p = __get_short_pointer();
2267 }
2268 else
2269 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002270 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2271 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002272 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002274 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 __set_long_size(__sz);
2276 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002277
2278#ifndef _LIBCPP_NO_EXCEPTIONS
2279 try
2280 {
2281#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002282 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 traits_type::assign(*__p, *__first);
2284 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002285#ifndef _LIBCPP_NO_EXCEPTIONS
2286 }
2287 catch (...)
2288 {
2289 if (__is_long())
2290 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2291 throw;
2292 }
2293#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294}
2295
2296template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002297template<class _InputIterator, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002298inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002300 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301{
2302 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002303 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304}
2305
2306template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002307template<class _InputIterator, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002308inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2310 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002311 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312{
2313 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002314 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315}
2316
Eric Fiselierfc92be82017-04-19 00:28:44 +00002317#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002318
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002320inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002321basic_string<_CharT, _Traits, _Allocator>::basic_string(
2322 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002323 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324{
2325 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002326 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327}
2328
2329template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002330inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002331basic_string<_CharT, _Traits, _Allocator>::basic_string(
2332 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002333 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334{
2335 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002336 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337}
2338
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002339#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002340
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002342_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2344{
Nikolas Klauser98833542022-05-07 22:20:23 +02002345 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002347 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348}
2349
2350template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002351_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352void
2353basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2354 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002355 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 +00002356{
2357 size_type __ms = max_size();
2358 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002359 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360 pointer __old_p = __get_pointer();
2361 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002362 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002364 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2365 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002366 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002367 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002369 traits_type::copy(std::__to_address(__p),
2370 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002372 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2374 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002375 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2376 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002377 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002378 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002380 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2382 __set_long_size(__old_sz);
2383 traits_type::assign(__p[__old_sz], value_type());
2384}
2385
2386template <class _CharT, class _Traits, class _Allocator>
2387void
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002388_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2390 size_type __n_copy, size_type __n_del, size_type __n_add)
2391{
2392 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002393 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002394 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395 pointer __old_p = __get_pointer();
2396 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002397 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002399 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2400 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002401 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002402 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002404 traits_type::copy(std::__to_address(__p),
2405 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002406 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2407 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002408 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2409 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002410 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002411 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2412 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002414 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415}
2416
2417// assign
2418
2419template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002420template <bool __is_short>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002421_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsb6a08b62020-04-10 18:36:31 -04002422basic_string<_CharT, _Traits, _Allocator>&
2423basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002424 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002425 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002426 if (__n < __cap) {
2427 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2428 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002429 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002430 traits_type::assign(__p[__n], value_type());
2431 __invalidate_iterators_past(__n);
2432 } else {
2433 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2434 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2435 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002436 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002437}
2438
2439template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002440_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002442basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2443 const value_type* __s, size_type __n) {
2444 size_type __cap = capacity();
2445 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002446 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002447 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002448 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002449 } else {
2450 size_type __sz = size();
2451 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002452 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002453 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002454}
2455
2456template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002457_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsda7d94f2020-06-19 14:24:03 -04002458basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002459basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002461 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002462 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002463 ? __assign_short(__s, __n)
2464 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465}
2466
2467template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002468_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469basic_string<_CharT, _Traits, _Allocator>&
2470basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2471{
2472 size_type __cap = capacity();
2473 if (__cap < __n)
2474 {
2475 size_type __sz = size();
2476 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2477 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002478 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002480 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481}
2482
2483template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002484_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485basic_string<_CharT, _Traits, _Allocator>&
2486basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2487{
2488 pointer __p;
2489 if (__is_long())
2490 {
2491 __p = __get_long_pointer();
2492 __set_long_size(1);
2493 }
2494 else
2495 {
2496 __p = __get_short_pointer();
2497 __set_short_size(1);
2498 }
2499 traits_type::assign(*__p, __c);
2500 traits_type::assign(*++__p, value_type());
2501 __invalidate_iterators_past(1);
2502 return *this;
2503}
2504
2505template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002506_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002507basic_string<_CharT, _Traits, _Allocator>&
2508basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2509{
Martijn Vels596e3de2020-02-26 15:55:49 -05002510 if (this != &__str) {
2511 __copy_assign_alloc(__str);
2512 if (!__is_long()) {
2513 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002514 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002515 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002516 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002517 }
2518 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002519 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002520 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002521 }
2522 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002523}
2524
Eric Fiselierfc92be82017-04-19 00:28:44 +00002525#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002526
2527template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002528inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002529void
2530basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002531 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002532{
2533 if (__alloc() != __str.__alloc())
2534 assign(__str);
2535 else
2536 __move_assign(__str, true_type());
2537}
2538
2539template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002540inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002541void
2542basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002543#if _LIBCPP_STD_VER > 14
2544 _NOEXCEPT
2545#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002546 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002547#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002548{
marshall2ed41622019-11-27 07:13:00 -08002549 if (__is_long()) {
2550 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2551 __get_long_cap());
2552#if _LIBCPP_STD_VER <= 14
2553 if (!is_nothrow_move_assignable<allocator_type>::value) {
2554 __set_short_size(0);
2555 traits_type::assign(__get_short_pointer()[0], value_type());
2556 }
2557#endif
2558 }
2559 __move_assign_alloc(__str);
2560 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002561 if (__libcpp_is_constant_evaluated()) {
2562 __str.__default_init();
2563 } else {
2564 __str.__set_short_size(0);
2565 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2566 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002567}
2568
2569template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002570inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002571basic_string<_CharT, _Traits, _Allocator>&
2572basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002573 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002574{
2575 __move_assign(__str, integral_constant<bool,
2576 __alloc_traits::propagate_on_container_move_assignment::value>());
2577 return *this;
2578}
2579
2580#endif
2581
2582template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002584_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002585__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002587 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002589>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2591{
Marshall Clow958362f2016-09-05 01:54:30 +00002592 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002593 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002594 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595}
2596
2597template <class _CharT, class _Traits, class _Allocator>
2598template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002599_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002600__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002602 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002604>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2606{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002608 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002609 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002610
2611 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2612 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002614 if (__cap < __n)
2615 {
2616 size_type __sz = size();
2617 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2618 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002619 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002620 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002621 traits_type::assign(*__p, *__first);
2622 traits_type::assign(*__p, value_type());
2623 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002624 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625 }
2626 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002627 {
2628 const basic_string __temp(__first, __last, __alloc());
2629 assign(__temp.data(), __temp.size());
2630 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631 return *this;
2632}
2633
2634template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002635_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636basic_string<_CharT, _Traits, _Allocator>&
2637basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2638{
2639 size_type __sz = __str.size();
2640 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002641 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002642 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643}
2644
2645template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002646template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002647_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002648__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002649<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002650 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2651 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002652 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002653>
Marshall Clow82513342016-09-24 22:45:42 +00002654basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002655{
Marshall Clow82513342016-09-24 22:45:42 +00002656 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002657 size_type __sz = __sv.size();
2658 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002659 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002660 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002661}
2662
2663
2664template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002665_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002667basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2668 return __assign_external(__s, traits_type::length(__s));
2669}
2670
2671template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002672_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsda7d94f2020-06-19 14:24:03 -04002673basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002674basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002676 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002677 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002678 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002679 ? __assign_short(__s, traits_type::length(__s))
2680 : __assign_external(__s, traits_type::length(__s)))
2681 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683// append
2684
2685template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002686_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002688basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002690 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 size_type __cap = capacity();
2692 size_type __sz = size();
2693 if (__cap - __sz >= __n)
2694 {
2695 if (__n)
2696 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002697 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 traits_type::copy(__p + __sz, __s, __n);
2699 __sz += __n;
2700 __set_size(__sz);
2701 traits_type::assign(__p[__sz], value_type());
2702 }
2703 }
2704 else
2705 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2706 return *this;
2707}
2708
2709template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002710_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711basic_string<_CharT, _Traits, _Allocator>&
2712basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2713{
2714 if (__n)
2715 {
2716 size_type __cap = capacity();
2717 size_type __sz = size();
2718 if (__cap - __sz < __n)
2719 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2720 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002721 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722 __sz += __n;
2723 __set_size(__sz);
2724 traits_type::assign(__p[__sz], value_type());
2725 }
2726 return *this;
2727}
2728
2729template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002730_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002731basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2732{
2733 if (__n)
2734 {
2735 size_type __cap = capacity();
2736 size_type __sz = size();
2737 if (__cap - __sz < __n)
2738 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2739 pointer __p = __get_pointer();
2740 __sz += __n;
2741 __set_size(__sz);
2742 traits_type::assign(__p[__sz], value_type());
2743 }
2744}
2745
2746template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002747_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748void
2749basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2750{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002751 bool __is_short = !__is_long();
2752 size_type __cap;
2753 size_type __sz;
2754 if (__is_short)
2755 {
2756 __cap = __min_cap - 1;
2757 __sz = __get_short_size();
2758 }
2759 else
2760 {
2761 __cap = __get_long_cap() - 1;
2762 __sz = __get_long_size();
2763 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002765 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002767 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002768 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002769 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002770 if (__is_short)
2771 {
2772 __p = __get_short_pointer() + __sz;
2773 __set_short_size(__sz+1);
2774 }
2775 else
2776 {
2777 __p = __get_long_pointer() + __sz;
2778 __set_long_size(__sz+1);
2779 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780 traits_type::assign(*__p, __c);
2781 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782}
2783
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784template <class _CharT, class _Traits, class _Allocator>
2785template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002786_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002787__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002788<
2789 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2790 basic_string<_CharT, _Traits, _Allocator>&
2791>
2792basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002793 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794{
2795 size_type __sz = size();
2796 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002797 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 if (__n)
2799 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002800 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2801 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002802 {
2803 if (__cap - __sz < __n)
2804 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2805 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002806 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002807 traits_type::assign(*__p, *__first);
2808 traits_type::assign(*__p, value_type());
2809 __set_size(__sz + __n);
2810 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002811 else
2812 {
2813 const basic_string __temp(__first, __last, __alloc());
2814 append(__temp.data(), __temp.size());
2815 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 }
2817 return *this;
2818}
2819
2820template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002821inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822basic_string<_CharT, _Traits, _Allocator>&
2823basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2824{
2825 return append(__str.data(), __str.size());
2826}
2827
2828template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002829_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830basic_string<_CharT, _Traits, _Allocator>&
2831basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2832{
2833 size_type __sz = __str.size();
2834 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002835 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002836 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837}
2838
2839template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002840template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002841_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002842 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002843 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002844 __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 +00002845 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002846 >
Marshall Clow82513342016-09-24 22:45:42 +00002847basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002848{
Marshall Clow82513342016-09-24 22:45:42 +00002849 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002850 size_type __sz = __sv.size();
2851 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002852 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002853 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002854}
2855
2856template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002857_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002859basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002861 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862 return append(__s, traits_type::length(__s));
2863}
2864
2865// insert
2866
2867template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002868_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002870basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002872 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 size_type __sz = size();
2874 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002875 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002877 if (__libcpp_is_constant_evaluated()) {
2878 if (__cap - __sz >= __n)
2879 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2880 else
2881 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2882 return *this;
2883 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 if (__cap - __sz >= __n)
2885 {
2886 if (__n)
2887 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002888 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889 size_type __n_move = __sz - __pos;
2890 if (__n_move != 0)
2891 {
2892 if (__p + __pos <= __s && __s < __p + __sz)
2893 __s += __n;
2894 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2895 }
2896 traits_type::move(__p + __pos, __s, __n);
2897 __sz += __n;
2898 __set_size(__sz);
2899 traits_type::assign(__p[__sz], value_type());
2900 }
2901 }
2902 else
2903 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2904 return *this;
2905}
2906
2907template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002908_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909basic_string<_CharT, _Traits, _Allocator>&
2910basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2911{
2912 size_type __sz = size();
2913 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002914 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915 if (__n)
2916 {
2917 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002918 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919 if (__cap - __sz >= __n)
2920 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002921 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922 size_type __n_move = __sz - __pos;
2923 if (__n_move != 0)
2924 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2925 }
2926 else
2927 {
2928 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002929 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930 }
2931 traits_type::assign(__p + __pos, __n, __c);
2932 __sz += __n;
2933 __set_size(__sz);
2934 traits_type::assign(__p[__sz], value_type());
2935 }
2936 return *this;
2937}
2938
2939template <class _CharT, class _Traits, class _Allocator>
2940template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002941_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002942__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002944 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002945 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002946>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2948{
Nikolas Klausereed25832021-12-15 01:32:30 +01002949 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2950 "string::insert(iterator, range) called with an iterator not"
2951 " referring to this string");
2952 const basic_string __temp(__first, __last, __alloc());
2953 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954}
2955
2956template <class _CharT, class _Traits, class _Allocator>
2957template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002958_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002959__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002961 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002963>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2965{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002966 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2967 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002968
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002970 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2971 if (__n == 0)
2972 return begin() + __ip;
2973
2974 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002976 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002978 else
2979 {
2980 const basic_string __temp(__first, __last, __alloc());
2981 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2982 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983}
2984
2985template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002986inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987basic_string<_CharT, _Traits, _Allocator>&
2988basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2989{
2990 return insert(__pos1, __str.data(), __str.size());
2991}
2992
2993template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002994_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995basic_string<_CharT, _Traits, _Allocator>&
2996basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2997 size_type __pos2, size_type __n)
2998{
2999 size_type __str_sz = __str.size();
3000 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003001 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003002 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003003}
3004
3005template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003006template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003007_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003008__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003009<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003010 __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 +00003011 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003012>
Marshall Clow82513342016-09-24 22:45:42 +00003013basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003014 size_type __pos2, size_type __n)
3015{
Marshall Clow82513342016-09-24 22:45:42 +00003016 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003017 size_type __str_sz = __sv.size();
3018 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003019 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003020 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003021}
3022
3023template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003024_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003026basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003027{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003028 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029 return insert(__pos, __s, traits_type::length(__s));
3030}
3031
3032template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003033_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034typename basic_string<_CharT, _Traits, _Allocator>::iterator
3035basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
3036{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05003037 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3038 "string::insert(iterator, character) called with an iterator not"
3039 " referring to this string");
3040
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041 size_type __ip = static_cast<size_type>(__pos - begin());
3042 size_type __sz = size();
3043 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003044 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045 if (__cap == __sz)
3046 {
3047 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003048 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049 }
3050 else
3051 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003052 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053 size_type __n_move = __sz - __ip;
3054 if (__n_move != 0)
3055 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3056 }
3057 traits_type::assign(__p[__ip], __c);
3058 traits_type::assign(__p[++__sz], value_type());
3059 __set_size(__sz);
3060 return begin() + static_cast<difference_type>(__ip);
3061}
3062
3063template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003064inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065typename basic_string<_CharT, _Traits, _Allocator>::iterator
3066basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3067{
Nikolas Klausereed25832021-12-15 01:32:30 +01003068 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3069 "string::insert(iterator, n, value) called with an iterator not"
3070 " referring to this string");
3071 difference_type __p = __pos - begin();
3072 insert(static_cast<size_type>(__p), __n, __c);
3073 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074}
3075
3076// replace
3077
3078template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003079_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003081basic_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 +00003082 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003084 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085 size_type __sz = size();
3086 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003087 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003088 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089 size_type __cap = capacity();
3090 if (__cap - __sz + __n1 >= __n2)
3091 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003092 if (__libcpp_is_constant_evaluated()) {
3093 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3094 return *this;
3095 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003096 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 if (__n1 != __n2)
3098 {
3099 size_type __n_move = __sz - __pos - __n1;
3100 if (__n_move != 0)
3101 {
3102 if (__n1 > __n2)
3103 {
3104 traits_type::move(__p + __pos, __s, __n2);
3105 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003106 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107 }
3108 if (__p + __pos < __s && __s < __p + __sz)
3109 {
3110 if (__p + __pos + __n1 <= __s)
3111 __s += __n2 - __n1;
3112 else // __p + __pos < __s < __p + __pos + __n1
3113 {
3114 traits_type::move(__p + __pos, __s, __n1);
3115 __pos += __n1;
3116 __s += __n2;
3117 __n2 -= __n1;
3118 __n1 = 0;
3119 }
3120 }
3121 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3122 }
3123 }
3124 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003125 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126 }
3127 else
3128 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3129 return *this;
3130}
3131
3132template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003133_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134basic_string<_CharT, _Traits, _Allocator>&
3135basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3136{
3137 size_type __sz = size();
3138 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003139 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003140 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003142 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143 if (__cap - __sz + __n1 >= __n2)
3144 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003145 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 if (__n1 != __n2)
3147 {
3148 size_type __n_move = __sz - __pos - __n1;
3149 if (__n_move != 0)
3150 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3151 }
3152 }
3153 else
3154 {
3155 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003156 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 }
3158 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003159 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160}
3161
3162template <class _CharT, class _Traits, class _Allocator>
3163template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003164_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003165__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003167 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003169>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003170basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171 _InputIterator __j1, _InputIterator __j2)
3172{
Marshall Clow958362f2016-09-05 01:54:30 +00003173 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003174 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175}
3176
3177template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003178inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179basic_string<_CharT, _Traits, _Allocator>&
3180basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3181{
3182 return replace(__pos1, __n1, __str.data(), __str.size());
3183}
3184
3185template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003186_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187basic_string<_CharT, _Traits, _Allocator>&
3188basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3189 size_type __pos2, size_type __n2)
3190{
3191 size_type __str_sz = __str.size();
3192 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003193 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003194 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195}
3196
3197template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003198template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003199_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003200__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003201<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003202 __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 +00003203 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003204>
Marshall Clow82513342016-09-24 22:45:42 +00003205basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003206 size_type __pos2, size_type __n2)
3207{
Marshall Clow82513342016-09-24 22:45:42 +00003208 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003209 size_type __str_sz = __sv.size();
3210 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003211 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003212 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003213}
3214
3215template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003216_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003217basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003218basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003220 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221 return replace(__pos, __n1, __s, traits_type::length(__s));
3222}
3223
3224template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003225inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003227basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003228{
3229 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3230 __str.data(), __str.size());
3231}
3232
3233template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003234inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003236basic_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 +00003237{
3238 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3239}
3240
3241template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003242inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003244basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245{
3246 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3247}
3248
3249template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003250inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003252basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253{
3254 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3255}
3256
3257// erase
3258
Martijn Velsa81fc792020-02-26 13:25:43 -05003259// 'externally instantiated' erase() implementation, called when __n != npos.
3260// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003262_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsa81fc792020-02-26 13:25:43 -05003263void
3264basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3265 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267 if (__n)
3268 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003269 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003270 value_type* __p = std::__to_address(__get_pointer());
3271 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272 size_type __n_move = __sz - __pos - __n;
3273 if (__n_move != 0)
3274 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003275 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003277}
3278
3279template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003280_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsa81fc792020-02-26 13:25:43 -05003281basic_string<_CharT, _Traits, _Allocator>&
3282basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3283 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003284 if (__pos > size())
3285 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003286 if (__n == npos) {
3287 __erase_to_end(__pos);
3288 } else {
3289 __erase_external_with_move(__pos, __n);
3290 }
3291 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003292}
3293
3294template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003295inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296typename basic_string<_CharT, _Traits, _Allocator>::iterator
3297basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3298{
Nikolas Klausereed25832021-12-15 01:32:30 +01003299 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3300 "string::erase(iterator) called with an iterator not"
3301 " referring to this string");
3302
3303 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3304 iterator __b = begin();
3305 size_type __r = static_cast<size_type>(__pos - __b);
3306 erase(__r, 1);
3307 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308}
3309
3310template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003311inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312typename basic_string<_CharT, _Traits, _Allocator>::iterator
3313basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3314{
Nikolas Klausereed25832021-12-15 01:32:30 +01003315 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3316 "string::erase(iterator, iterator) called with an iterator not"
3317 " referring to this string");
3318
3319 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3320 iterator __b = begin();
3321 size_type __r = static_cast<size_type>(__first - __b);
3322 erase(__r, static_cast<size_type>(__last - __first));
3323 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324}
3325
3326template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003327inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328void
3329basic_string<_CharT, _Traits, _Allocator>::pop_back()
3330{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003331 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003332 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333}
3334
3335template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003336inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003338basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003340 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341 if (__is_long())
3342 {
3343 traits_type::assign(*__get_long_pointer(), value_type());
3344 __set_long_size(0);
3345 }
3346 else
3347 {
3348 traits_type::assign(*__get_short_pointer(), value_type());
3349 __set_short_size(0);
3350 }
3351}
3352
3353template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003354inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003355void
3356basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3357{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003358 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359}
3360
3361template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003362_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363void
3364basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3365{
3366 size_type __sz = size();
3367 if (__n > __sz)
3368 append(__n - __sz, __c);
3369 else
3370 __erase_to_end(__n);
3371}
3372
3373template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003374_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003375basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3376{
3377 size_type __sz = size();
3378 if (__n > __sz) {
3379 __append_default_init(__n - __sz);
3380 } else
3381 __erase_to_end(__n);
3382}
3383
3384template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003385inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003387basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003389 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003390 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3391 return __m - __alignment;
3392 } else {
3393 bool __uses_lsb = __endian_factor == 2;
3394 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3395 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396}
3397
3398template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003399_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400void
Marek Kurdejc9848142020-11-26 10:07:16 +01003401basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003402{
Marek Kurdejc9848142020-11-26 10:07:16 +01003403 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003404 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003405
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003406 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3407 // and later (since P0966R1), however we provide consistent behavior in all Standard
3408 // modes because this function is instantiated in the shared library.
3409 if (__requested_capacity <= capacity())
3410 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003411
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003412 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003413 __target_capacity = __recommend(__target_capacity);
3414 if (__target_capacity == capacity()) return;
3415
3416 __shrink_or_extend(__target_capacity);
3417}
3418
3419template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003420inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marek Kurdejc9848142020-11-26 10:07:16 +01003421void
3422basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3423{
3424 size_type __target_capacity = __recommend(size());
3425 if (__target_capacity == capacity()) return;
3426
3427 __shrink_or_extend(__target_capacity);
3428}
3429
3430template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003431inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marek Kurdejc9848142020-11-26 10:07:16 +01003432void
3433basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3434{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435 size_type __cap = capacity();
3436 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003437
3438 pointer __new_data, __p;
3439 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003440 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003442 __was_long = true;
3443 __now_long = false;
3444 __new_data = __get_short_pointer();
3445 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003447 else
3448 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003449 if (__target_capacity > __cap) {
3450 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3451 __new_data = __allocation.ptr;
3452 __target_capacity = __allocation.count - 1;
3453 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003454 else
3455 {
3456 #ifndef _LIBCPP_NO_EXCEPTIONS
3457 try
3458 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003459 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003460 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3461 __new_data = __allocation.ptr;
3462 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003463 #ifndef _LIBCPP_NO_EXCEPTIONS
3464 }
3465 catch (...)
3466 {
3467 return;
3468 }
3469 #else // _LIBCPP_NO_EXCEPTIONS
3470 if (__new_data == nullptr)
3471 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003472 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003473 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003474 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003475 __now_long = true;
3476 __was_long = __is_long();
3477 __p = __get_pointer();
3478 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003479 traits_type::copy(std::__to_address(__new_data),
3480 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003481 if (__was_long)
3482 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3483 if (__now_long)
3484 {
3485 __set_long_cap(__target_capacity+1);
3486 __set_long_size(__sz);
3487 __set_long_pointer(__new_data);
3488 }
3489 else
3490 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003491 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492}
3493
3494template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003495inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003497basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003499 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500 return *(data() + __pos);
3501}
3502
3503template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003504inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003506basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003508 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509 return *(__get_pointer() + __pos);
3510}
3511
3512template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003513_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3515basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3516{
3517 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003518 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519 return (*this)[__n];
3520}
3521
3522template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003523_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524typename basic_string<_CharT, _Traits, _Allocator>::reference
3525basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3526{
3527 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003528 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529 return (*this)[__n];
3530}
3531
3532template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003533inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003535basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003537 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538 return *__get_pointer();
3539}
3540
3541template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003542inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003544basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003546 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547 return *data();
3548}
3549
3550template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003551inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003553basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003555 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556 return *(__get_pointer() + size() - 1);
3557}
3558
3559template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003560inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003562basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003564 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565 return *(data() + size() - 1);
3566}
3567
3568template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003569_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003571basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572{
3573 size_type __sz = size();
3574 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003575 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003576 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577 traits_type::copy(__s, data() + __pos, __rlen);
3578 return __rlen;
3579}
3580
3581template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003582inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583basic_string<_CharT, _Traits, _Allocator>
3584basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3585{
3586 return basic_string(*this, __pos, __n, __alloc());
3587}
3588
3589template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003590inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591void
3592basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003593#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003594 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003595#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003596 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003597 __is_nothrow_swappable<allocator_type>::value)
3598#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599{
Nikolas Klauser98833542022-05-07 22:20:23 +02003600 if (!__is_long())
3601 std::__debug_db_invalidate_all(this);
3602 if (!__str.__is_long())
3603 std::__debug_db_invalidate_all(&__str);
3604 std::__debug_db_swap(this, &__str);
3605
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003606 _LIBCPP_ASSERT(
3607 __alloc_traits::propagate_on_container_swap::value ||
3608 __alloc_traits::is_always_equal::value ||
3609 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003610 std::swap(__r_.first(), __str.__r_.first());
3611 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612}
3613
3614// find
3615
3616template <class _Traits>
3617struct _LIBCPP_HIDDEN __traits_eq
3618{
3619 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003620 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003621 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3622 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623};
3624
3625template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003626_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003628basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003629 size_type __pos,
3630 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003632 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003633 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003634 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635}
3636
3637template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003638inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003640basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3641 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003643 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003644 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645}
3646
3647template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003648template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003649_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003650__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003651<
3652 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3653 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003654>
Marshall Clowe46031a2018-07-02 18:41:15 +00003655basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003656 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003657{
Marshall Clowe46031a2018-07-02 18:41:15 +00003658 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003659 return __str_find<value_type, size_type, traits_type, npos>
3660 (data(), size(), __sv.data(), __pos, __sv.size());
3661}
3662
3663template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003664inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003665typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003666basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003667 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003669 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003670 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003671 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672}
3673
3674template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003675_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003676typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003677basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3678 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003680 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003681 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682}
3683
3684// rfind
3685
3686template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003687_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003689basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003690 size_type __pos,
3691 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003693 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003695 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696}
3697
3698template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003699inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003701basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3702 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003704 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003705 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706}
3707
3708template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003709template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003710_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003711__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003712<
3713 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3714 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003715>
Marshall Clowe46031a2018-07-02 18:41:15 +00003716basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003717 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003718{
Marshall Clowe46031a2018-07-02 18:41:15 +00003719 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003720 return __str_rfind<value_type, size_type, traits_type, npos>
3721 (data(), size(), __sv.data(), __pos, __sv.size());
3722}
3723
3724template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003725inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003726typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003727basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003728 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003730 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003731 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003732 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733}
3734
3735template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003736_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003738basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3739 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003741 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003742 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743}
3744
3745// find_first_of
3746
3747template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003748_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003750basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003751 size_type __pos,
3752 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003754 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003755 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003756 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003757}
3758
3759template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003760inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003762basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3763 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003765 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003766 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767}
3768
3769template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003770template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003771_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003772__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003773<
3774 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3775 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003776>
Marshall Clowe46031a2018-07-02 18:41:15 +00003777basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003778 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003779{
Marshall Clowe46031a2018-07-02 18:41:15 +00003780 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003781 return __str_find_first_of<value_type, size_type, traits_type, npos>
3782 (data(), size(), __sv.data(), __pos, __sv.size());
3783}
3784
3785template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003786inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003787typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003788basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003789 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003791 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003792 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003793 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794}
3795
3796template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003797inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003799basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3800 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801{
3802 return find(__c, __pos);
3803}
3804
3805// find_last_of
3806
3807template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003808inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003810basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003811 size_type __pos,
3812 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003814 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003815 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003816 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817}
3818
3819template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003820inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003822basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3823 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003825 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003826 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827}
3828
3829template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003830template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003831_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003832__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003833<
3834 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3835 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003836>
Marshall Clowe46031a2018-07-02 18:41:15 +00003837basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003838 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003839{
Marshall Clowe46031a2018-07-02 18:41:15 +00003840 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003841 return __str_find_last_of<value_type, size_type, traits_type, npos>
3842 (data(), size(), __sv.data(), __pos, __sv.size());
3843}
3844
3845template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003846inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003847typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003848basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003849 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003850{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003851 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003852 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003853 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854}
3855
3856template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003857inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003859basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3860 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861{
3862 return rfind(__c, __pos);
3863}
3864
3865// find_first_not_of
3866
3867template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003868_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003869typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003870basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003871 size_type __pos,
3872 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003874 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003875 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003876 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877}
3878
3879template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003880inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003882basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3883 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003885 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003886 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887}
3888
3889template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003890template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003891_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003892__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003893<
3894 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3895 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003896>
Marshall Clowe46031a2018-07-02 18:41:15 +00003897basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003898 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003899{
Marshall Clowe46031a2018-07-02 18:41:15 +00003900 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003901 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3902 (data(), size(), __sv.data(), __pos, __sv.size());
3903}
3904
3905template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003906inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003907typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003908basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003909 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003910{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003911 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003912 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003913 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003914}
3915
3916template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003917inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003919basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3920 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003922 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003923 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924}
3925
3926// find_last_not_of
3927
3928template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003929_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003931basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003932 size_type __pos,
3933 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003934{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003935 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003936 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003937 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003938}
3939
3940template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003941inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003943basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3944 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003946 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003947 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003948}
3949
3950template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003951template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003952_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003953__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003954<
3955 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3956 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003957>
Marshall Clowe46031a2018-07-02 18:41:15 +00003958basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003959 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003960{
Marshall Clowe46031a2018-07-02 18:41:15 +00003961 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003962 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3963 (data(), size(), __sv.data(), __pos, __sv.size());
3964}
3965
3966template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003967inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003968typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003969basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003970 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003972 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003973 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003974 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975}
3976
3977template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003978inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003980basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3981 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003982{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003983 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003984 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985}
3986
3987// compare
3988
3989template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003990template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003991_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003992__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003993<
3994 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3995 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003996>
zoecarver1997e0a2021-02-05 11:54:47 -08003997basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998{
Marshall Clowe46031a2018-07-02 18:41:15 +00003999 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00004000 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004001 size_t __rhs_sz = __sv.size();
4002 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004003 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00004004 if (__result != 0)
4005 return __result;
4006 if (__lhs_sz < __rhs_sz)
4007 return -1;
4008 if (__lhs_sz > __rhs_sz)
4009 return 1;
4010 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011}
4012
4013template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004014inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004016basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004018 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019}
4020
4021template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004022inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004024basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4025 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00004026 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004027 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028{
Alp Tokerb8a95f52014-05-15 11:27:39 +00004029 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030 size_type __sz = size();
4031 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004032 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004033 size_type __rlen = std::min(__n1, __sz - __pos1);
4034 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035 if (__r == 0)
4036 {
4037 if (__rlen < __n2)
4038 __r = -1;
4039 else if (__rlen > __n2)
4040 __r = 1;
4041 }
4042 return __r;
4043}
4044
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004045template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00004046template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004047_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04004048__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00004049<
4050 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
4051 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004052>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004053basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4054 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00004055 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004056{
Marshall Clowe46031a2018-07-02 18:41:15 +00004057 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004058 return compare(__pos1, __n1, __sv.data(), __sv.size());
4059}
4060
4061template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004062inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004063int
4064basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4065 size_type __n1,
4066 const basic_string& __str) const
4067{
4068 return compare(__pos1, __n1, __str.data(), __str.size());
4069}
4070
4071template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00004072template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004073_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04004074__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00004075<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004076 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
4077 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00004078 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004079>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004080basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4081 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00004082 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004083 size_type __pos2,
4084 size_type __n2) const
4085{
Marshall Clow82513342016-09-24 22:45:42 +00004086 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004087 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
4088}
4089
4090template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004091_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004092int
4093basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4094 size_type __n1,
4095 const basic_string& __str,
4096 size_type __pos2,
4097 size_type __n2) const
4098{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004099 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004100}
4101
4102template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004103_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004104int
4105basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4106{
4107 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4108 return compare(0, npos, __s, traits_type::length(__s));
4109}
4110
4111template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004112_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004113int
4114basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4115 size_type __n1,
4116 const value_type* __s) const
4117{
4118 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4119 return compare(__pos1, __n1, __s, traits_type::length(__s));
4120}
4121
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122// __invariants
4123
4124template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004125inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126bool
4127basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4128{
4129 if (size() > capacity())
4130 return false;
4131 if (capacity() < __min_cap - 1)
4132 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004133 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004135 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136 return false;
4137 return true;
4138}
4139
Vedant Kumar55e007e2018-03-08 21:15:26 +00004140// __clear_and_shrink
4141
4142template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004143inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne173f29e2019-05-29 16:01:36 +00004144void
Marshall Clowe60a7182018-05-29 17:04:37 +00004145basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004146{
4147 clear();
4148 if(__is_long())
4149 {
4150 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4151 __set_long_cap(0);
4152 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004153 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004154 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004155}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004156
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157// operator==
4158
4159template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004160inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161bool
4162operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004163 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004164{
Mark de Weverb4830e62022-07-19 07:56:23 +02004165#if _LIBCPP_STD_VER > 17
4166 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4167#else
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004168 size_t __lhs_sz = __lhs.size();
4169 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4170 __rhs.data(),
4171 __lhs_sz) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004172#endif
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004173}
4174
4175template<class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004176inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004177bool
4178operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4179 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4180{
4181 size_t __lhs_sz = __lhs.size();
4182 if (__lhs_sz != __rhs.size())
4183 return false;
4184 const char* __lp = __lhs.data();
4185 const char* __rp = __rhs.data();
4186 if (__lhs.__is_long())
4187 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4188 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4189 if (*__lp != *__rp)
4190 return false;
4191 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192}
4193
Mark de Weverb4830e62022-07-19 07:56:23 +02004194#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004196inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004198operator==(const _CharT* __lhs,
4199 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004201 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4202 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4203 size_t __lhs_len = _Traits::length(__lhs);
4204 if (__lhs_len != __rhs.size()) return false;
4205 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206}
Mark de Weverb4830e62022-07-19 07:56:23 +02004207#endif // _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004210inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004212operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4213 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214{
Mark de Weverb4830e62022-07-19 07:56:23 +02004215#if _LIBCPP_STD_VER > 17
4216 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4217#else
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004218 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4219 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4220 size_t __rhs_len = _Traits::length(__rhs);
4221 if (__rhs_len != __lhs.size()) return false;
4222 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004223#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224}
4225
Mark de Weverb4830e62022-07-19 07:56:23 +02004226#if _LIBCPP_STD_VER > 17
4227
4228template <class _CharT, class _Traits, class _Allocator>
4229_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
4230 const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4231 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
4232 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4233}
4234
4235template <class _CharT, class _Traits, class _Allocator>
4236_LIBCPP_HIDE_FROM_ABI constexpr auto
4237operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
4238 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4239}
4240
4241#else // _LIBCPP_STD_VER > 17
4242
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004243template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004244inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245bool
4246operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004247 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248{
4249 return !(__lhs == __rhs);
4250}
4251
4252template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004253inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004255operator!=(const _CharT* __lhs,
4256 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257{
4258 return !(__lhs == __rhs);
4259}
4260
4261template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004262inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004264operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4265 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004266{
4267 return !(__lhs == __rhs);
4268}
4269
4270// operator<
4271
4272template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004273inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274bool
4275operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004276 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004277{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004278 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279}
4280
4281template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004282inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004284operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4285 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004287 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288}
4289
4290template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004291inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004293operator< (const _CharT* __lhs,
4294 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004295{
4296 return __rhs.compare(__lhs) > 0;
4297}
4298
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299// operator>
4300
4301template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004302inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004303bool
4304operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004305 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306{
4307 return __rhs < __lhs;
4308}
4309
4310template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004311inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004312bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004313operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4314 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315{
4316 return __rhs < __lhs;
4317}
4318
4319template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004320inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004322operator> (const _CharT* __lhs,
4323 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324{
4325 return __rhs < __lhs;
4326}
4327
4328// operator<=
4329
4330template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004331inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004332bool
4333operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004334 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335{
4336 return !(__rhs < __lhs);
4337}
4338
4339template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004340inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004342operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4343 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344{
4345 return !(__rhs < __lhs);
4346}
4347
4348template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004349inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004351operator<=(const _CharT* __lhs,
4352 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004353{
4354 return !(__rhs < __lhs);
4355}
4356
4357// operator>=
4358
4359template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004360inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004361bool
4362operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004363 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364{
4365 return !(__lhs < __rhs);
4366}
4367
4368template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004369inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004370bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004371operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4372 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373{
4374 return !(__lhs < __rhs);
4375}
4376
4377template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004378inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004380operator>=(const _CharT* __lhs,
4381 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382{
4383 return !(__lhs < __rhs);
4384}
Mark de Weverb4830e62022-07-19 07:56:23 +02004385#endif // _LIBCPP_STD_VER > 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386
4387// operator +
4388
4389template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004390_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004391basic_string<_CharT, _Traits, _Allocator>
4392operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4393 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4394{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004395 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004396 auto __lhs_sz = __lhs.size();
4397 auto __rhs_sz = __rhs.size();
4398 _String __r(__uninitialized_size_tag(),
4399 __lhs_sz + __rhs_sz,
4400 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4401 auto __ptr = std::__to_address(__r.__get_pointer());
4402 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4403 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4404 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004405 return __r;
4406}
4407
4408template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004409_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004410basic_string<_CharT, _Traits, _Allocator>
4411operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4412{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004413 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004414 auto __lhs_sz = _Traits::length(__lhs);
4415 auto __rhs_sz = __rhs.size();
4416 _String __r(__uninitialized_size_tag(),
4417 __lhs_sz + __rhs_sz,
4418 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4419 auto __ptr = std::__to_address(__r.__get_pointer());
4420 _Traits::copy(__ptr, __lhs, __lhs_sz);
4421 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4422 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423 return __r;
4424}
4425
4426template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004427_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428basic_string<_CharT, _Traits, _Allocator>
4429operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4430{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004431 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004432 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004433 _String __r(__uninitialized_size_tag(),
4434 __rhs_sz + 1,
4435 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4436 auto __ptr = std::__to_address(__r.__get_pointer());
4437 _Traits::assign(__ptr, 1, __lhs);
4438 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4439 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440 return __r;
4441}
4442
4443template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004444inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004445basic_string<_CharT, _Traits, _Allocator>
4446operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4447{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004448 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004449 typename _String::size_type __lhs_sz = __lhs.size();
4450 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004451 _String __r(__uninitialized_size_tag(),
4452 __lhs_sz + __rhs_sz,
4453 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4454 auto __ptr = std::__to_address(__r.__get_pointer());
4455 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4456 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4457 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004458 return __r;
4459}
4460
4461template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004462_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004463basic_string<_CharT, _Traits, _Allocator>
4464operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4465{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004466 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004467 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004468 _String __r(__uninitialized_size_tag(),
4469 __lhs_sz + 1,
4470 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4471 auto __ptr = std::__to_address(__r.__get_pointer());
4472 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4473 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4474 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475 return __r;
4476}
4477
Eric Fiselierfc92be82017-04-19 00:28:44 +00004478#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479
4480template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004481inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004482basic_string<_CharT, _Traits, _Allocator>
4483operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4484{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004485 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486}
4487
4488template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004489inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490basic_string<_CharT, _Traits, _Allocator>
4491operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4492{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004493 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004494}
4495
4496template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004497inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004498basic_string<_CharT, _Traits, _Allocator>
4499operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4500{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004501 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004502}
4503
4504template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004505inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004506basic_string<_CharT, _Traits, _Allocator>
4507operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4508{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004509 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004510}
4511
4512template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004513inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004514basic_string<_CharT, _Traits, _Allocator>
4515operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4516{
4517 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004518 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004519}
4520
4521template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004522inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523basic_string<_CharT, _Traits, _Allocator>
4524operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4525{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004526 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004527}
4528
4529template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004530inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004531basic_string<_CharT, _Traits, _Allocator>
4532operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4533{
4534 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004535 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536}
4537
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004538#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004539
4540// swap
4541
4542template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004543inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004544void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004545swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004546 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4547 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004548{
4549 __lhs.swap(__rhs);
4550}
4551
Bruce Mitchener170d8972020-11-24 12:53:53 -05004552_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4553_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4554_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4555_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4556_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004557
Bruce Mitchener170d8972020-11-24 12:53:53 -05004558_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4559_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4560_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004561
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004562_LIBCPP_FUNC_VIS string to_string(int __val);
4563_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4564_LIBCPP_FUNC_VIS string to_string(long __val);
4565_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4566_LIBCPP_FUNC_VIS string to_string(long long __val);
4567_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4568_LIBCPP_FUNC_VIS string to_string(float __val);
4569_LIBCPP_FUNC_VIS string to_string(double __val);
4570_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004571
Louis Dionne89258142021-08-23 15:32:36 -04004572#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004573_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4574_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4575_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4576_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4577_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004578
Bruce Mitchener170d8972020-11-24 12:53:53 -05004579_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4580_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4581_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004582
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004583_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4584_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4585_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4586_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4587_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4588_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4589_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4590_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4591_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004592#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004593
Howard Hinnantc51e1022010-05-11 19:42:16 +00004594template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004595_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004596const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4597 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004598
Marshall Clow851b9ec2019-05-20 21:56:51 +00004599template <class _CharT, class _Allocator>
4600struct _LIBCPP_TEMPLATE_VIS
4601 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
Nikolas Klauser672ee852022-06-22 10:11:14 +02004602 : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004603{
4604 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004605 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4606 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004607};
4608
Howard Hinnantdc095972011-07-18 15:51:59 +00004609template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004610_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004611operator<<(basic_ostream<_CharT, _Traits>& __os,
4612 const basic_string<_CharT, _Traits, _Allocator>& __str);
4613
4614template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004615_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004616operator>>(basic_istream<_CharT, _Traits>& __is,
4617 basic_string<_CharT, _Traits, _Allocator>& __str);
4618
4619template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004620_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004621getline(basic_istream<_CharT, _Traits>& __is,
4622 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4623
4624template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004625inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004626basic_istream<_CharT, _Traits>&
4627getline(basic_istream<_CharT, _Traits>& __is,
4628 basic_string<_CharT, _Traits, _Allocator>& __str);
4629
Howard Hinnantdc095972011-07-18 15:51:59 +00004630template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004631inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004632basic_istream<_CharT, _Traits>&
4633getline(basic_istream<_CharT, _Traits>&& __is,
4634 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4635
4636template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004637inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004638basic_istream<_CharT, _Traits>&
4639getline(basic_istream<_CharT, _Traits>&& __is,
4640 basic_string<_CharT, _Traits, _Allocator>& __str);
4641
Marshall Clow29b53f22018-12-14 18:49:35 +00004642#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004643template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004644inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004645 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4646 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4647 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004648 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004649 return __old_size - __str.size();
4650}
Marshall Clow29b53f22018-12-14 18:49:35 +00004651
Marek Kurdeja98b1412020-05-02 13:58:03 +02004652template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004653inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004654 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4655 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4656 _Predicate __pred) {
4657 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004658 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004659 __str.end());
4660 return __old_size - __str.size();
4661}
Marshall Clow29b53f22018-12-14 18:49:35 +00004662#endif
4663
Louis Dionne510450b2022-04-01 16:38:30 -04004664#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004665
4666template<class _CharT, class _Traits, class _Allocator>
4667bool
4668basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4669{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004670 return data() <= std::__to_address(__i->base()) &&
4671 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004672}
4673
4674template<class _CharT, class _Traits, class _Allocator>
4675bool
4676basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4677{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004678 return data() < std::__to_address(__i->base()) &&
4679 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004680}
4681
4682template<class _CharT, class _Traits, class _Allocator>
4683bool
4684basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4685{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004686 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004687 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004688}
4689
4690template<class _CharT, class _Traits, class _Allocator>
4691bool
4692basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4693{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004694 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004695 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004696}
4697
Louis Dionne510450b2022-04-01 16:38:30 -04004698#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004699
Louis Dionne173f29e2019-05-29 16:01:36 +00004700#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004701// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004702inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004703{
4704 inline namespace string_literals
4705 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004706 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004707 basic_string<char> operator "" s( const char *__str, size_t __len )
4708 {
4709 return basic_string<char> (__str, __len);
4710 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004711
Louis Dionne89258142021-08-23 15:32:36 -04004712#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004713 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004714 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4715 {
4716 return basic_string<wchar_t> (__str, __len);
4717 }
Louis Dionne89258142021-08-23 15:32:36 -04004718#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004719
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004720#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004721 inline _LIBCPP_HIDE_FROM_ABI constexpr
Marshall Clow8732fed2018-12-11 04:35:44 +00004722 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4723 {
4724 return basic_string<char8_t> (__str, __len);
4725 }
4726#endif
4727
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004728 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004729 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4730 {
4731 return basic_string<char16_t> (__str, __len);
4732 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004733
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004734 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004735 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4736 {
4737 return basic_string<char32_t> (__str, __len);
4738 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004739 } // namespace string_literals
4740} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004741
4742#if _LIBCPP_STD_VER > 17
4743template <>
4744inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4745#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4746template <>
4747inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4748#endif
4749#endif
4750
Marshall Clowcba751f2013-07-23 17:05:24 +00004751#endif
4752
Howard Hinnantc51e1022010-05-11 19:42:16 +00004753_LIBCPP_END_NAMESPACE_STD
4754
Eric Fiselierf4433a32017-05-31 22:07:49 +00004755_LIBCPP_POP_MACROS
4756
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004757#endif // _LIBCPP_STRING