blob: 726bba3156f6e6ad55f4d98586c20529b5cadbf6 [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
Nikolas Klauser1b531cf2022-08-09 13:17:30 +0200112 constexpr basic_string(
113 basic_string&& str, size_type pos, const Allocator& a = Allocator()); // since C++23
114 constexpr basic_string(
115 basic_string&& str, size_type pos, size_type n, const Allocator& a = Allocator()); // since C++23
Marshall Clow78dbe462016-11-14 18:22:19 +0000116 template<class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200117 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 +0000118 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200119 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17, constexpr since C++20
120 basic_string(const value_type* s, const allocator_type& a = allocator_type()); // constexpr since C++20
121 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 +0200122 basic_string(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200123 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 +0000124 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000125 basic_string(InputIterator begin, InputIterator end,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200126 const allocator_type& a = allocator_type()); // constexpr since C++20
127 basic_string(initializer_list<value_type>, const Allocator& = Allocator()); // constexpr since C++20
128 basic_string(const basic_string&, const Allocator&); // constexpr since C++20
129 basic_string(basic_string&&, const Allocator&); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200131 ~basic_string(); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200133 operator basic_string_view<charT, traits>() const noexcept; // constexpr since C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000134
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200135 basic_string& operator=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000136 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200137 basic_string& operator=(const T& t); // C++17, constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000138 basic_string& operator=(basic_string&& str)
139 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000140 allocator_type::propagate_on_container_move_assignment::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200141 allocator_type::is_always_equal::value ); // C++17, constexpr since C++20
142 basic_string& operator=(const value_type* s); // constexpr since C++20
Marek Kurdej90d79712021-07-27 16:16:21 +0200143 basic_string& operator=(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200144 basic_string& operator=(value_type c); // constexpr since C++20
145 basic_string& operator=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200147 iterator begin() noexcept; // constexpr since C++20
148 const_iterator begin() const noexcept; // constexpr since C++20
149 iterator end() noexcept; // constexpr since C++20
150 const_iterator end() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200152 reverse_iterator rbegin() noexcept; // constexpr since C++20
153 const_reverse_iterator rbegin() const noexcept; // constexpr since C++20
154 reverse_iterator rend() noexcept; // constexpr since C++20
155 const_reverse_iterator rend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200157 const_iterator cbegin() const noexcept; // constexpr since C++20
158 const_iterator cend() const noexcept; // constexpr since C++20
159 const_reverse_iterator crbegin() const noexcept; // constexpr since C++20
160 const_reverse_iterator crend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200162 size_type size() const noexcept; // constexpr since C++20
163 size_type length() const noexcept; // constexpr since C++20
164 size_type max_size() const noexcept; // constexpr since C++20
165 size_type capacity() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200167 void resize(size_type n, value_type c); // constexpr since C++20
168 void resize(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100170 template<class Operation>
171 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
172
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200173 void reserve(size_type res_arg); // constexpr since C++20
Marek Kurdejc9848142020-11-26 10:07:16 +0100174 void reserve(); // deprecated in C++20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200175 void shrink_to_fit(); // constexpr since C++20
176 void clear() noexcept; // constexpr since C++20
177 bool empty() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200179 const_reference operator[](size_type pos) const; // constexpr since C++20
180 reference operator[](size_type pos); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000181
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200182 const_reference at(size_type n) const; // constexpr since C++20
183 reference at(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200185 basic_string& operator+=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000186 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200187 basic_string& operator+=(const T& t); // C++17, constexpr since C++20
188 basic_string& operator+=(const value_type* s); // constexpr since C++20
189 basic_string& operator+=(value_type c); // constexpr since C++20
190 basic_string& operator+=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200192 basic_string& append(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000193 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200194 basic_string& append(const T& t); // C++17, constexpr since C++20
195 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 +0000196 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200197 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
198 basic_string& append(const value_type* s, size_type n); // constexpr since C++20
199 basic_string& append(const value_type* s); // constexpr since C++20
200 basic_string& append(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000201 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200202 basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
203 basic_string& append(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200205 void push_back(value_type c); // constexpr since C++20
206 void pop_back(); // constexpr since C++20
207 reference front(); // constexpr since C++20
208 const_reference front() const; // constexpr since C++20
209 reference back(); // constexpr since C++20
210 const_reference back() const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200212 basic_string& assign(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000213 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200214 basic_string& assign(const T& t); // C++17, constexpr since C++20
215 basic_string& assign(basic_string&& str); // constexpr since C++20
216 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 +0000217 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200218 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
219 basic_string& assign(const value_type* s, size_type n); // constexpr since C++20
220 basic_string& assign(const value_type* s); // constexpr since C++20
221 basic_string& assign(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000222 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200223 basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
224 basic_string& assign(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200226 basic_string& insert(size_type pos1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000227 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200228 basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 basic_string& insert(size_type pos1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200230 size_type pos2, size_type n); // constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000231 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200232 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
233 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
234 basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
235 basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
236 iterator insert(const_iterator p, value_type c); // constexpr since C++20
237 iterator insert(const_iterator p, size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000238 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200239 iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20
240 iterator insert(const_iterator p, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200242 basic_string& erase(size_type pos = 0, size_type n = npos); // constexpr since C++20
243 iterator erase(const_iterator position); // constexpr since C++20
244 iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200246 basic_string& replace(size_type pos1, size_type n1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000247 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200248 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 +0000249 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200250 size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000251 template <class T>
252 basic_string& replace(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200253 size_type pos2, size_type n); // C++17, constexpr since C++20
254 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
255 basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
256 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
257 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000258 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200259 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17, constexpr since C++20
260 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
261 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s); // constexpr since C++20
262 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 +0000263 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200264 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
265 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200267 size_type copy(value_type* s, size_type n, size_type pos = 0) const; // constexpr since C++20
Nikolas Klauser1b531cf2022-08-09 13:17:30 +0200268 basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr in C++20, removed in C++23
269 basic_string substr(size_type pos = 0, size_type n = npos) const&; // since C++23
270 constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&; // since C++23
Howard Hinnant3e276872011-06-03 18:40:47 +0000271 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000272 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200273 allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200275 const value_type* c_str() const noexcept; // constexpr since C++20
276 const value_type* data() const noexcept; // constexpr since C++20
277 value_type* data() noexcept; // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200279 allocator_type get_allocator() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200281 size_type find(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000282 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200283 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
284 size_type find(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
285 size_type find(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
286 size_type find(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200288 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000289 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200290 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
291 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
292 size_type rfind(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
293 size_type rfind(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200295 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 +0000296 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200297 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
298 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
299 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
300 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 +0000301
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200302 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 +0000303 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200304 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
305 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
306 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
307 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 +0000308
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200309 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 +0000310 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200311 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
312 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
313 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
314 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 +0000315
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200316 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 +0000317 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200318 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
319 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
320 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
321 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 +0000322
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200323 int compare(const basic_string& str) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000324 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200325 int compare(const T& t) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
326 int compare(size_type pos1, size_type n1, const basic_string& str) const; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000327 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200328 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 +0000329 int compare(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200330 size_type pos2, size_type n2=npos) const; // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000331 template <class T>
332 int compare(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200333 size_type pos2, size_type n2=npos) const; // C++17, constexpr since C++20
334 int compare(const value_type* s) const noexcept; // constexpr since C++20
335 int compare(size_type pos1, size_type n1, const value_type* s) const; // constexpr since C++20
336 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 +0000337
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200338 constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
339 constexpr bool starts_with(charT c) const noexcept; // C++20
340 constexpr bool starts_with(const charT* s) const; // C++20
341 constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
342 constexpr bool ends_with(charT c) const noexcept; // C++20
343 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000344
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200345 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
346 constexpr bool contains(charT c) const noexcept; // C++2b
347 constexpr bool contains(const charT* s) const; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348};
349
Marshall Clowa0563332018-02-08 06:34:03 +0000350template<class InputIterator,
351 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
352basic_string(InputIterator, InputIterator, Allocator = Allocator())
353 -> basic_string<typename iterator_traits<InputIterator>::value_type,
354 char_traits<typename iterator_traits<InputIterator>::value_type>,
355 Allocator>; // C++17
356
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357template<class charT, class traits, class Allocator>
358basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000359operator+(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200360 const basic_string<charT, traits, Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200364operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200368operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369
370template<class charT, class traits, class Allocator>
371basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200372operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
375basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200376operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377
378template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000379bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200380 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200383bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200386bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000388template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000389bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200390 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200393bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394
395template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200396bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397
398template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000399bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200400 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200403bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200406bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407
408template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000409bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200410 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
412template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200413bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414
415template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200416bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417
418template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000419bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200420 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200423bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424
425template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200426bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427
428template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000429bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Mark de Weveraf1968a2022-08-14 14:14:09 +0200430 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431
432template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200433bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +0200436bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
Mark de Weverb4830e62022-07-19 07:56:23 +0200437
438template<class charT, class traits, class Allocator> // since C++20
439constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
440 const basic_string<charT, traits, Allocator>& rhs) noexcept;
441
442template<class charT, class traits, class Allocator> // since C++20
443constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
444 const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445
446template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000447void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000448 basic_string<charT, traits, Allocator>& rhs)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200449 noexcept(noexcept(lhs.swap(rhs))); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000450
451template<class charT, class traits, class Allocator>
452basic_istream<charT, traits>&
453operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
454
455template<class charT, class traits, class Allocator>
456basic_ostream<charT, traits>&
457operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
458
459template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000460basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000461getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
462 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000463
464template<class charT, class traits, class Allocator>
465basic_istream<charT, traits>&
466getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
467
Marshall Clow29b53f22018-12-14 18:49:35 +0000468template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200469typename basic_string<charT, traits, Allocator>::size_type
470erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000471template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200472typename basic_string<charT, traits, Allocator>::size_type
473erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000474
Howard Hinnantc51e1022010-05-11 19:42:16 +0000475typedef basic_string<char> string;
476typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100477typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000478typedef basic_string<char16_t> u16string;
479typedef basic_string<char32_t> u32string;
480
Bruce Mitchener170d8972020-11-24 12:53:53 -0500481int stoi (const string& str, size_t* idx = nullptr, int base = 10);
482long stol (const string& str, size_t* idx = nullptr, int base = 10);
483unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
484long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
485unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000486
Bruce Mitchener170d8972020-11-24 12:53:53 -0500487float stof (const string& str, size_t* idx = nullptr);
488double stod (const string& str, size_t* idx = nullptr);
489long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
491string to_string(int val);
492string to_string(unsigned val);
493string to_string(long val);
494string to_string(unsigned long val);
495string to_string(long long val);
496string to_string(unsigned long long val);
497string to_string(float val);
498string to_string(double val);
499string to_string(long double val);
500
Bruce Mitchener170d8972020-11-24 12:53:53 -0500501int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
502long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
503unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
504long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
505unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000506
Bruce Mitchener170d8972020-11-24 12:53:53 -0500507float stof (const wstring& str, size_t* idx = nullptr);
508double stod (const wstring& str, size_t* idx = nullptr);
509long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000510
511wstring to_wstring(int val);
512wstring to_wstring(unsigned val);
513wstring to_wstring(long val);
514wstring to_wstring(unsigned long val);
515wstring to_wstring(long long val);
516wstring to_wstring(unsigned long long val);
517wstring to_wstring(float val);
518wstring to_wstring(double val);
519wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520
521template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100522template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523template <> struct hash<u16string>;
524template <> struct hash<u32string>;
525template <> struct hash<wstring>;
526
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200527basic_string<char> operator "" s( const char *str, size_t len ); // C++14, constexpr since C++20
528basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14, constexpr since C++20
529constexpr basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
530basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14, constexpr since C++20
531basic_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 +0000532
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533} // std
534
535*/
536
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100537#include <__algorithm/max.h>
538#include <__algorithm/min.h>
539#include <__algorithm/remove.h>
540#include <__algorithm/remove_if.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400541#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400543#include <__debug>
Mark de Weverdf3e0d42021-09-26 15:47:42 +0200544#include <__format/enable_insertable.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200545#include <__functional/hash.h>
Nikolas Klauser24540d32022-05-21 00:45:51 +0200546#include <__functional/unary_function.h>
Nikolas Klauser713bfda2022-10-13 01:03:58 +0200547#include <__fwd/string.h>
Nikolas Klauser80840272022-02-04 13:04:33 +0100548#include <__ios/fpos.h>
Nikolas Klauserfb1b0672022-06-10 19:53:10 +0200549#include <__iterator/distance.h>
550#include <__iterator/iterator_traits.h>
551#include <__iterator/reverse_iterator.h>
Louis Dionne77249522021-06-11 09:55:11 -0400552#include <__iterator/wrap_iter.h>
Nikolas Klauserc513eba2022-04-09 09:41:19 +0200553#include <__memory/allocate_at_least.h>
Nikolas Klauser0b5df932022-09-05 00:01:15 +0200554#include <__memory/allocator.h>
555#include <__memory/allocator_traits.h>
556#include <__memory/compressed_pair.h>
557#include <__memory/construct_at.h>
558#include <__memory/pointer_traits.h>
Nikolas Klauser35080b42022-07-26 16:13:56 +0200559#include <__memory/swap_allocator.h>
Arthur O'Dwyerebf2d342022-10-06 16:53:30 -0400560#include <__memory_resource/polymorphic_allocator.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200561#include <__string/char_traits.h>
562#include <__string/extern_template_lists.h>
Nikolas Klauser0b5df932022-09-05 00:01:15 +0200563#include <__type_traits/is_allocator.h>
564#include <__type_traits/noexcept_move_assign_container.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100565#include <__utility/auto_cast.h>
566#include <__utility/move.h>
567#include <__utility/swap.h>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200568#include <__utility/unreachable.h>
569#include <climits>
Louis Dionne44962c32022-06-13 11:21:16 -0400570#include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400571#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400572#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400573#include <cstring>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200574#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400576#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000578#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000579
Louis Dionne89258142021-08-23 15:32:36 -0400580#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100581# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400582#endif
583
Nikolas Klausera0e0edb2022-06-16 22:43:46 +0200584// standard-mandated includes
585
586// [iterator.range]
587#include <__iterator/access.h>
588#include <__iterator/data.h>
589#include <__iterator/empty.h>
590#include <__iterator/reverse_access.h>
591#include <__iterator/size.h>
592
593// [string.syn]
594#include <compare>
595#include <initializer_list>
596
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000597#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500598# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000599#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600
Eric Fiselierf4433a32017-05-31 22:07:49 +0000601_LIBCPP_PUSH_MACROS
602#include <__undef_macros>
603
604
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605_LIBCPP_BEGIN_NAMESPACE_STD
606
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607// basic_string
608
609template<class _CharT, class _Traits, class _Allocator>
610basic_string<_CharT, _Traits, _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200611_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant944510a2011-06-14 19:58:17 +0000612operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
613 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614
615template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200616_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000618operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619
620template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200621_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000623operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624
625template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200626inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000628operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629
630template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200631_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000633operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634
Louis Dionnedc496ec2021-06-08 17:25:08 -0400635extern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Shoaib Meenaiea363712017-07-29 02:54:41 +0000636
Marshall Clow039b2f02016-01-13 21:54:34 +0000637template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400638struct __string_is_trivial_iterator : public false_type {};
639
640template <class _Tp>
641struct __string_is_trivial_iterator<_Tp*>
642 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000643
Louis Dionne173f29e2019-05-29 16:01:36 +0000644template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400645struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
646 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000647
Marshall Clow82513342016-09-24 22:45:42 +0000648template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500649struct __can_be_converted_to_string_view : public _BoolConstant<
650 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
651 !is_convertible<const _Tp&, const _CharT*>::value
652 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000653
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200654struct __uninitialized_size_tag {};
655
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000656template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser713bfda2022-10-13 01:03:58 +0200657class basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658{
659public:
660 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000661 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000663 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000665 typedef allocator_traits<allocator_type> __alloc_traits;
666 typedef typename __alloc_traits::size_type size_type;
667 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000668 typedef value_type& reference;
669 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000670 typedef typename __alloc_traits::pointer pointer;
671 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672
Marshall Clow79f33542018-03-21 00:36:05 +0000673 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
674 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
675 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
676 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000677 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000678 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000679 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000680
Nikolas Klausereb116e22022-10-08 22:17:32 +0200681 static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
682 "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
683 "original allocator");
684
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685 typedef __wrap_iter<pointer> iterator;
686 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200687 typedef std::reverse_iterator<iterator> reverse_iterator;
688 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689
690private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200691 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000692
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000693#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000694
695 struct __long
696 {
697 pointer __data_;
698 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200699 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
700 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000701 };
702
Howard Hinnant68bf1812013-04-30 21:44:48 +0000703 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
704 (sizeof(__long) - 1)/sizeof(value_type) : 2};
705
706 struct __short
707 {
708 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200709 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200710 unsigned char __size_ : 7;
711 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000712 };
713
Nikolas Klauser62060be2022-04-20 10:52:04 +0200714// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200715// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200716//
717// If the LSB is used to store the short-flag in the short string representation,
718// we have to multiply the size by two when it is stored and divide it by two when
719// it is loaded to make sure that we always store an even number. In the long string
720// representation, we can ignore this because we can assume that we always allocate
721// an even amount of value_types.
722//
723// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
724// This does not impact the short string representation, since we never need the MSB
725// for representing the size of a short string anyway.
726
727#ifdef _LIBCPP_BIG_ENDIAN
728 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000729#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200730 static const size_type __endian_factor = 1;
731#endif
732
733#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
734
735#ifdef _LIBCPP_BIG_ENDIAN
736 static const size_type __endian_factor = 1;
737#else
738 static const size_type __endian_factor = 2;
739#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000740
Xing Xue38b9fc42022-06-24 17:25:15 -0400741 // Attribute 'packed' is used to keep the layout compatible with the
742 // previous definition that did not use bit fields. This is because on
743 // some platforms bit fields have a default size rather than the actual
744 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 struct __long
746 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400747 struct _LIBCPP_PACKED {
748 size_type __is_long_ : 1;
749 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
750 };
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751 size_type __size_;
752 pointer __data_;
753 };
754
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
756 (sizeof(__long) - 1)/sizeof(value_type) : 2};
757
758 struct __short
759 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400760 struct _LIBCPP_PACKED {
761 unsigned char __is_long_ : 1;
762 unsigned char __size_ : 7;
763 };
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200764 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 value_type __data_[__min_cap];
766 };
767
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400768#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000769
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200770 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
771
Howard Hinnant8ea98242013-08-23 17:37:05 +0000772 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773
Howard Hinnant8ea98242013-08-23 17:37:05 +0000774 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775
776 struct __raw
777 {
778 size_type __words[__n_words];
779 };
780
781 struct __rep
782 {
783 union
784 {
785 __long __l;
786 __short __s;
787 __raw __r;
788 };
789 };
790
791 __compressed_pair<__rep, allocator_type> __r_;
792
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200793 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
794 // don't initialize the characters. The contents of the string, including the null terminator, must be
795 // initialized separately.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200796 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200797 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200798 : __r_(__default_init_tag(), __a) {
799 if (__size > max_size())
800 __throw_length_error();
801 if (__fits_in_sso(__size)) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +0200802 __r_.first() = __rep();
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200803 __set_short_size(__size);
804 } else {
805 auto __capacity = __recommend(__size) + 1;
806 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200807 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200808 __set_long_cap(__capacity);
809 __set_long_pointer(__allocation);
810 __set_long_size(__size);
811 }
812 std::__debug_db_insert_c(this);
813 }
814
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815public:
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200816 _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200818 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
819 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
820 : __r_(__default_init_tag(), __default_init_tag()) {
821 std::__debug_db_insert_c(this);
822 __default_init();
823 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000824
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200825 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000826#if _LIBCPP_STD_VER <= 14
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200827 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +0000828#else
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200829 _NOEXCEPT
Marshall Clowa80abc72015-06-03 19:56:43 +0000830#endif
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200831 : __r_(__default_init_tag(), __a) {
832 std::__debug_db_insert_c(this);
833 __default_init();
834 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000835
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200836 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
837 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000838
Eric Fiselierfc92be82017-04-19 00:28:44 +0000839#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200840 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)
841# if _LIBCPP_STD_VER <= 14
842 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
843# else
844 _NOEXCEPT
845# endif
846 : __r_(std::move(__str.__r_)) {
847 __str.__default_init();
848 std::__debug_db_insert_c(this);
849 if (__is_long())
850 std::__debug_db_swap(this, &__str);
851 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000852
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200853 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)
854 : __r_(__default_init_tag(), __a) {
855 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
856 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
857 else {
858 if (__libcpp_is_constant_evaluated())
859 __r_.first() = __rep();
860 __r_.first() = __str.__r_.first();
861 __str.__default_init();
862 }
863 std::__debug_db_insert_c(this);
864 if (__is_long())
865 std::__debug_db_swap(this, &__str);
866 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400867#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000868
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200869 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
870 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s)
871 : __r_(__default_init_tag(), __default_init_tag()) {
872 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
873 __init(__s, traits_type::length(__s));
874 std::__debug_db_insert_c(this);
875 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000876
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200877 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
878 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, const _Allocator& __a);
Marshall Clowe46031a2018-07-02 18:41:15 +0000879
Marek Kurdej90d79712021-07-27 16:16:21 +0200880#if _LIBCPP_STD_VER > 20
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200881 basic_string(nullptr_t) = delete;
Marek Kurdej90d79712021-07-27 16:16:21 +0200882#endif
883
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200884 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n)
885 : __r_(__default_init_tag(), __default_init_tag()) {
886 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
887 __init(__s, __n);
888 std::__debug_db_insert_c(this);
889 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000890
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200891 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
892 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
893 : __r_(__default_init_tag(), __a) {
894 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
895 __init(__s, __n);
896 std::__debug_db_insert_c(this);
897 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000898
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200899 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c)
900 : __r_(__default_init_tag(), __default_init_tag()) {
901 __init(__n, __c);
902 std::__debug_db_insert_c(this);
903 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000904
Nikolas Klauser1b531cf2022-08-09 13:17:30 +0200905#if _LIBCPP_STD_VER > 20
906 _LIBCPP_HIDE_FROM_ABI constexpr
907 basic_string(basic_string&& __str, size_type __pos, const _Allocator& __alloc = _Allocator())
908 : basic_string(std::move(__str), __pos, npos, __alloc) {}
909
910 _LIBCPP_HIDE_FROM_ABI constexpr
911 basic_string(basic_string&& __str, size_type __pos, size_type __n, const _Allocator& __alloc = _Allocator())
912 : __r_(__default_init_tag(), __alloc) {
913 if (__pos > __str.size())
914 __throw_out_of_range();
915
916 auto __len = std::min<size_type>(__n, __str.size() - __pos);
917 if (__alloc_traits::is_always_equal::value || __alloc == __str.__alloc()) {
918 __r_.first() = __str.__r_.first();
919 __str.__default_init();
920
921 _Traits::move(data(), data() + __pos, __len);
922 __set_size(__len);
923 _Traits::assign(data()[__len], value_type());
924 } else {
925 // Perform a copy because the allocators are not compatible.
926 __init(__str.data() + __pos, __len);
927 }
928
929 std::__debug_db_insert_c(this);
930 if (__is_long())
931 std::__debug_db_swap(this, &__str);
932 }
933#endif
934
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200935 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
936 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
Marshall Clowe46031a2018-07-02 18:41:15 +0000937
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200938 _LIBCPP_CONSTEXPR_SINCE_CXX20
939 basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000940
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200941 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
942 basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())
943 : __r_(__default_init_tag(), __a) {
944 size_type __str_sz = __str.size();
945 if (__pos > __str_sz)
946 __throw_out_of_range();
947 __init(__str.data() + __pos, __str_sz - __pos);
948 std::__debug_db_insert_c(this);
949 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000950
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200951 template <class _Tp,
952 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
953 !__is_same_uncvref<_Tp, basic_string>::value> >
954 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
955 basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type());
956
957 template <class _Tp,
958 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
959 !__is_same_uncvref<_Tp, basic_string>::value> >
960 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
961 const _Tp& __t);
962
963 template <class _Tp,
964 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
965 !__is_same_uncvref<_Tp, basic_string>::value> >
966 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
967 const _Tp& __t, const allocator_type& __a);
968
969 template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
970 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last)
971 : __r_(__default_init_tag(), __default_init_tag()) {
972 __init(__first, __last);
973 std::__debug_db_insert_c(this);
974 }
975
976 template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
977 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
978 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
979 : __r_(__default_init_tag(), __a) {
980 __init(__first, __last);
981 std::__debug_db_insert_c(this);
982 }
983
Eric Fiselierfc92be82017-04-19 00:28:44 +0000984#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200985 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il)
986 : __r_(__default_init_tag(), __default_init_tag()) {
987 __init(__il.begin(), __il.end());
988 std::__debug_db_insert_c(this);
989 }
990
991 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)
992 : __r_(__default_init_tag(), __a) {
993 __init(__il.begin(), __il.end());
994 std::__debug_db_insert_c(this);
995 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400996#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200998 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001000 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001001 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
1002
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001003 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +00001004
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001005 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1006 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001007 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001008 __self_view __sv = __t;
1009 return assign(__sv);
1010 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001011
Eric Fiselierfc92be82017-04-19 00:28:44 +00001012#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001013 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001014 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001015 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001016 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierfc92be82017-04-19 00:28:44 +00001017 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001019 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001020 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +02001021#if _LIBCPP_STD_VER > 20
1022 basic_string& operator=(nullptr_t) = delete;
1023#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001024 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001026 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001027 iterator begin() _NOEXCEPT
1028 {return iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001029 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001030 const_iterator begin() const _NOEXCEPT
1031 {return const_iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001032 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001033 iterator end() _NOEXCEPT
1034 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001035 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001036 const_iterator end() const _NOEXCEPT
1037 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -04001038
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001039 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001040 reverse_iterator rbegin() _NOEXCEPT
1041 {return reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001042 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001043 const_reverse_iterator rbegin() const _NOEXCEPT
1044 {return const_reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001045 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001046 reverse_iterator rend() _NOEXCEPT
1047 {return reverse_iterator(begin());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001048 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001049 const_reverse_iterator rend() const _NOEXCEPT
1050 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001052 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001053 const_iterator cbegin() const _NOEXCEPT
1054 {return begin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001055 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001056 const_iterator cend() const _NOEXCEPT
1057 {return end();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001058 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001059 const_reverse_iterator crbegin() const _NOEXCEPT
1060 {return rbegin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001061 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001062 const_reverse_iterator crend() const _NOEXCEPT
1063 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001065 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001067 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {return size();}
1068 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT;
1069 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001070 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
1071 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001073 _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
1074 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001076 _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +01001077
1078#if _LIBCPP_STD_VER > 20
1079 template <class _Op>
1080 _LIBCPP_HIDE_FROM_ABI constexpr
1081 void resize_and_overwrite(size_type __n, _Op __op) {
1082 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001083 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +01001084 }
1085#endif
1086
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001087 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +00001088
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001089 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001090 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
1091 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001092
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001093 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowb7db4972017-11-15 20:02:27 +00001094 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001096 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001097 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001098 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001100 _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
1101 _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001103 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001104 return append(__str);
1105 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001106
1107 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001108 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001109 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001110 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001111 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1112 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001113 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001115 operator+=(const _Tp& __t) {
1116 __self_view __sv = __t; return append(__sv);
1117 }
1118
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001119 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001120 return append(__s);
1121 }
1122
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001123 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001124 push_back(__c);
1125 return *this;
1126 }
1127
Eric Fiselierfc92be82017-04-19 00:28:44 +00001128#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001129 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001130 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001131#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001133 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001135
1136 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001137 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001138 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001139 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1140 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001141 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001142 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001143 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001144 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001145
Marshall Clow62953962016-10-03 23:40:48 +00001146 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001147 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001148 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001149 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001150 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1151 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001152 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001153 >
Marshall Clow82513342016-09-24 22:45:42 +00001154 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001155 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n);
1156 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s);
1157 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001158
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001159 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier451d5582018-11-26 20:15:38 +00001160 void __append_default_init(size_type __n);
1161
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001163 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001164 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001166 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001168 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001169 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001170 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001171 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001172 append(__temp.data(), __temp.size());
1173 return *this;
1174 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001176 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001177 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001179 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001181 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001182 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001183 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001184
Eric Fiselierfc92be82017-04-19 00:28:44 +00001185#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001186 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001188#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001190 _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
1191 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
1192 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT;
1193 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT;
1194 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT;
1195 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196
Marshall Clowe46031a2018-07-02 18:41:15 +00001197 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001198 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001199 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001200 <
1201 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1202 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001203 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001204 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001205 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001206 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001207#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001208 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001209 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001210 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001211 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001212#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001213 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001214 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001215 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001216 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001217 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001218 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1219 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001220 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001221 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001222 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001223 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n);
1224 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s);
1225 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001227 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001228 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001230 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001232 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233 assign(_InputIterator __first, _InputIterator __last);
1234 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001235 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001236 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001238 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001240 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001242#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001243 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001245#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001247 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001249
1250 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001251 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001252 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001253 <
1254 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1255 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001256 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001257 insert(size_type __pos1, const _Tp& __t)
1258 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1259
Marshall Clow82513342016-09-24 22:45:42 +00001260 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001261 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001262 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001263 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001264 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001265 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001266 >
Marshall Clow82513342016-09-24 22:45:42 +00001267 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001268 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001269 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001270 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1271 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s);
1272 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1273 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
1274 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1276 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001277 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001278 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001280 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001282 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1284 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001285 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001286 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001288 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001290 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001292#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001293 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1295 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001296#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001298 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1299 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300 iterator erase(const_iterator __pos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001301 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302 iterator erase(const_iterator __first, const_iterator __last);
1303
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001304 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001306
1307 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001308 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001309 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001310 <
1311 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1312 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001313 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001314 replace(size_type __pos1, size_type __n1, const _Tp& __t) { __self_view __sv = __t; return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001315 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001316 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 +00001317 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001318 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001319 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001320 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001321 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001322 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001323 >
Marshall Clow82513342016-09-24 22:45:42 +00001324 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001325 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001326 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001327 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1328 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1329 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001330 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001331
1332 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001333 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001334 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001335 <
1336 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1337 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001338 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001339 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1340
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001341 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001342 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001343 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001344 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001345 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001346 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001348 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001349 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001351 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001353 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001354 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001355#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001356 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001357 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001359#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001361 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Nikolas Klauser1b531cf2022-08-09 13:17:30 +02001362
1363 // TODO: Maybe don't pass in the allocator. See https://llvm.org/PR57190
1364#if _LIBCPP_STD_VER <= 20
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001365 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1b531cf2022-08-09 13:17:30 +02001366 basic_string substr(size_type __pos = 0, size_type __n = npos) const {
1367 return basic_string(*this, __pos, __n, __alloc());
1368 }
1369#else
1370 _LIBCPP_HIDE_FROM_ABI constexpr
1371 basic_string substr(size_type __pos = 0, size_type __n = npos) const& {
1372 return basic_string(*this, __pos, __n, __alloc());
1373 }
1374
1375 _LIBCPP_HIDE_FROM_ABI constexpr
1376 basic_string substr(size_type __pos = 0, size_type __n = npos) && {
1377 return basic_string(std::move(*this), __pos, __n, __alloc());
1378 }
1379#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001381 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001382 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001383#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001384 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001385#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001386 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001387 __is_nothrow_swappable<allocator_type>::value);
1388#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001390 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001391 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001392 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001393 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001394#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001395 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001396 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001397#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001398
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001399 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001400 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001402 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001403 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001404
1405 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001406 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001407 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001408 <
1409 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1410 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001411 >
zoecarver1997e0a2021-02-05 11:54:47 -08001412 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001413 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001414 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001415 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001416 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001417 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001419 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001420 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001421
1422 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001423 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001424 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001425 <
1426 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1427 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001428 >
zoecarver1997e0a2021-02-05 11:54:47 -08001429 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001430 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001431 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001432 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001433 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001434 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001436 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001437 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001438
1439 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001440 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001441 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001442 <
1443 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1444 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001445 >
zoecarver1997e0a2021-02-05 11:54:47 -08001446 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001447 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001448 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001449 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001450 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001451 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001452 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001454 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001455 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001456
1457 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001458 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001459 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001460 <
1461 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1462 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001463 >
zoecarver1997e0a2021-02-05 11:54:47 -08001464 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001465 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001466 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001467 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001468 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001469 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001470 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001472 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001473 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001474
1475 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001476 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001477 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001478 <
1479 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1480 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001481 >
zoecarver1997e0a2021-02-05 11:54:47 -08001482 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001483 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001484 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001485 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001486 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001487 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001488 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1489
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001490 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001491 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001492
1493 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001494 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001495 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001496 <
1497 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1498 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001499 >
zoecarver1997e0a2021-02-05 11:54:47 -08001500 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001501 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001502 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001503 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001504 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001505 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001506 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1507
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001508 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001509 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001510
1511 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001512 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001513 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001514 <
1515 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1516 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001517 >
zoecarver1997e0a2021-02-05 11:54:47 -08001518 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001519
1520 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001521 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001522 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001523 <
1524 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1525 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001526 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001527 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1528
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001529 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001531 _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001532 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1533 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001534
Marshall Clow82513342016-09-24 22:45:42 +00001535 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001536 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001537 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001538 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001539 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001540 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001541 >
Marshall Clow82513342016-09-24 22:45:42 +00001542 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001543 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const value_type* __s) const _NOEXCEPT;
1544 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1545 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001546 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547
Marshall Clow18c293b2017-12-04 20:11:38 +00001548#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001549 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001550 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001551 { return __self_view(data(), size()).starts_with(__sv); }
1552
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001553 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001554 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001555 { return !empty() && _Traits::eq(front(), __c); }
1556
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001557 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001558 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001559 { return starts_with(__self_view(__s)); }
1560
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001561 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001562 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001563 { return __self_view(data(), size()).ends_with( __sv); }
1564
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001565 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001566 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001567 { return !empty() && _Traits::eq(back(), __c); }
1568
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001569 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001570 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001571 { return ends_with(__self_view(__s)); }
1572#endif
1573
Wim Leflere023c3542021-01-19 14:33:30 -05001574#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001575 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001576 bool contains(__self_view __sv) const noexcept
1577 { return __self_view(data(), size()).contains(__sv); }
1578
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001579 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001580 bool contains(value_type __c) const noexcept
1581 { return __self_view(data(), size()).contains(__c); }
1582
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001583 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001584 bool contains(const value_type* __s) const
1585 { return __self_view(data(), size()).contains(__s); }
1586#endif
1587
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001588 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001589
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001590 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001591
Louis Dionne510450b2022-04-01 16:38:30 -04001592#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001593
1594 bool __dereferenceable(const const_iterator* __i) const;
1595 bool __decrementable(const const_iterator* __i) const;
1596 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1597 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1598
Louis Dionne510450b2022-04-01 16:38:30 -04001599#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001600
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001602 template<class _Alloc>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001603 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001604 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1605 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1606
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001607 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001608
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001609 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001610 bool __is_long() const _NOEXCEPT {
1611 if (__libcpp_is_constant_evaluated())
1612 return true;
1613 return __r_.first().__s.__is_long_;
1614 }
1615
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001616 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __begin_lifetime(pointer __begin, size_type __n) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001617#if _LIBCPP_STD_VER > 17
1618 if (__libcpp_is_constant_evaluated()) {
1619 for (size_type __i = 0; __i != __n; ++__i)
1620 std::construct_at(std::addressof(__begin[__i]));
1621 }
1622#else
1623 (void)__begin;
1624 (void)__n;
1625#endif // _LIBCPP_STD_VER > 17
1626 }
1627
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001628 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __default_init() {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001629 __r_.first() = __rep();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001630 if (__libcpp_is_constant_evaluated()) {
1631 size_type __sz = __recommend(0) + 1;
1632 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1633 __begin_lifetime(__ptr, __sz);
1634 __set_long_pointer(__ptr);
1635 __set_long_cap(__sz);
1636 __set_long_size(0);
1637 }
1638 }
1639
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001640 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __deallocate_constexpr() {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001641 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1642 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1643 }
1644
Nikolas Klauser93826d12022-01-04 17:24:03 +01001645 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1646 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1647 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1648 }
1649
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001650 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001651 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001652 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1653 size_type __sz = size();
1654 size_type __cap = capacity();
1655 value_type* __p;
1656 if (__cap - __sz >= __n)
1657 {
1658 __p = std::__to_address(__get_pointer());
1659 size_type __n_move = __sz - __ip;
1660 if (__n_move != 0)
1661 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1662 }
1663 else
1664 {
1665 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1666 __p = std::__to_address(__get_long_pointer());
1667 }
1668 __sz += __n;
1669 __set_size(__sz);
1670 traits_type::assign(__p[__sz], value_type());
1671 for (__p += __ip; __first != __last; ++__p, ++__first)
1672 traits_type::assign(*__p, *__first);
1673
1674 return begin() + __ip;
1675 }
1676
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001677 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001678 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001680 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001681 void __set_short_size(size_type __s) _NOEXCEPT {
1682 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1683 __r_.first().__s.__size_ = __s;
1684 __r_.first().__s.__is_long_ = false;
1685 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001686
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001687 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001688 size_type __get_short_size() const _NOEXCEPT {
1689 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1690 return __r_.first().__s.__size_;
1691 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001692
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001693 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001694 void __set_long_size(size_type __s) _NOEXCEPT
1695 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001696 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001697 size_type __get_long_size() const _NOEXCEPT
1698 {return __r_.first().__l.__size_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001699 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001700 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1702
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001703 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001704 void __set_long_cap(size_type __s) _NOEXCEPT {
1705 __r_.first().__l.__cap_ = __s / __endian_factor;
1706 __r_.first().__l.__is_long_ = true;
1707 }
1708
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001709 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001710 size_type __get_long_cap() const _NOEXCEPT {
1711 return __r_.first().__l.__cap_ * __endian_factor;
1712 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001714 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001715 void __set_long_pointer(pointer __p) _NOEXCEPT
1716 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001717 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001718 pointer __get_long_pointer() _NOEXCEPT
1719 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001720 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001721 const_pointer __get_long_pointer() const _NOEXCEPT
1722 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001723 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001724 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001725 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001726 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001727 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001728 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001729 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001730 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001732 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001733 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1735
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 template <size_type __a> static
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001737 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea382952013-08-14 18:00:20 +00001738 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001739 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 enum {__alignment = 16};
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001741 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001742 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001743 {
1744 if (__s < __min_cap) {
1745 if (__libcpp_is_constant_evaluated())
1746 return static_cast<size_type>(__min_cap);
1747 else
1748 return static_cast<size_type>(__min_cap) - 1;
1749 }
Marshall Clow80584522018-02-07 21:30:17 +00001750 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1751 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1752 if (__guess == __min_cap) ++__guess;
1753 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001754 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001756 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001757 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001758 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001759 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001760 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001762
Martijn Vels5e7c9752020-03-04 17:52:46 -05001763 // Slow path for the (inlined) copy constructor for 'long' strings.
1764 // Always externally instantiated and not inlined.
1765 // Requires that __s is zero terminated.
1766 // The main reason for this function to exist is because for unstable, we
1767 // want to allow inlining of the copy constructor. However, we don't want
1768 // to call the __init() functions as those are marked as inline which may
1769 // result in over-aggressive inlining by the compiler, where our aim is
1770 // to only inline the fast path code directly in the ctor.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001771 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001772
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001774 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001775 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001777 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1778 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 __init(_InputIterator __first, _InputIterator __last);
1780
1781 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001782 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001783 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001785 __is_cpp17_forward_iterator<_ForwardIterator>::value
1786 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 __init(_ForwardIterator __first, _ForwardIterator __last);
1788
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001789 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001791 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001792 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1794 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001795 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796
Martijn Vels596e3de2020-02-26 15:55:49 -05001797 // __assign_no_alias is invoked for assignment operations where we
1798 // have proof that the input does not alias the current instance.
1799 // For example, operator=(basic_string) performs a 'self' check.
1800 template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001801 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001802
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001803 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 void __erase_to_end(size_type __pos);
1805
Martijn Velsa81fc792020-02-26 13:25:43 -05001806 // __erase_external_with_move is invoked for erase() invocations where
1807 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001808 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001809
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001810 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001811 void __copy_assign_alloc(const basic_string& __str)
1812 {__copy_assign_alloc(__str, integral_constant<bool,
1813 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1814
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001815 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001816 void __copy_assign_alloc(const basic_string& __str, true_type)
1817 {
Marshall Clowf258c202017-01-31 03:40:52 +00001818 if (__alloc() == __str.__alloc())
1819 __alloc() = __str.__alloc();
1820 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001821 {
Marshall Clowf258c202017-01-31 03:40:52 +00001822 if (!__str.__is_long())
1823 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001824 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001825 __alloc() = __str.__alloc();
1826 }
1827 else
1828 {
1829 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001830 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001831 __begin_lifetime(__allocation.ptr, __allocation.count);
Nikolas Klauser1821ec32022-10-01 15:37:24 +02001832 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001833 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001834 __set_long_pointer(__allocation.ptr);
1835 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001836 __set_long_size(__str.size());
1837 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001838 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001839 }
1840
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001841 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001842 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001843 {}
1844
Eric Fiselierfc92be82017-04-19 00:28:44 +00001845#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001846 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001847 void __move_assign(basic_string& __str, false_type)
1848 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001849 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001850 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001851#if _LIBCPP_STD_VER > 14
1852 _NOEXCEPT;
1853#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001854 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001855#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001856#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001857
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001858 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001859 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001860 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001861 _NOEXCEPT_(
1862 !__alloc_traits::propagate_on_container_move_assignment::value ||
1863 is_nothrow_move_assignable<allocator_type>::value)
1864 {__move_assign_alloc(__str, integral_constant<bool,
1865 __alloc_traits::propagate_on_container_move_assignment::value>());}
1866
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001867 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc2734962011-09-02 20:42:31 +00001868 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001869 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1870 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001871 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001872 }
1873
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001874 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001875 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001876 _NOEXCEPT
1877 {}
1878
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001879 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s);
1880 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001881
1882 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1883 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1884 pointer __p = __is_long()
1885 ? (__set_long_size(__n), __get_long_pointer())
1886 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001887 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001888 traits_type::assign(__p[__n], value_type());
1889 return *this;
1890 }
1891
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001892 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001893 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001894 __set_size(__newsz);
1895 __invalidate_iterators_past(__newsz);
1896 traits_type::assign(__p[__newsz], value_type());
1897 return *this;
1898 }
1899
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001900 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001902 template<class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001903 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001904 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001905 // assume that the ranges overlap, because we can't check during constant evaluation
1906 if (__libcpp_is_constant_evaluated())
1907 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001908 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001909 return data() <= __p && __p <= data() + size();
1910 }
1911
Louis Dionned24191c2021-08-19 12:39:16 -04001912 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1913 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001914 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001915 }
1916
1917 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1918 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001919 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001920 }
1921
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001922 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const basic_string&);
1923 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const value_type*, const basic_string&);
1924 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(value_type, const basic_string&);
1925 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const value_type*);
1926 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927};
1928
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001929// These declarations must appear before any functions are implicitly used
1930// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001931#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001932#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001933 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001934# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001935 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001936# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001937#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001938 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001939# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001940 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001941# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001942#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001943#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001944
1945
Louis Dionned59f8a52021-08-17 11:59:07 -04001946#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001947template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001948 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001949 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001950 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1951 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001952 >
1953basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1954 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001955
1956template<class _CharT,
1957 class _Traits,
1958 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001959 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001960 >
1961explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1962 -> basic_string<_CharT, _Traits, _Allocator>;
1963
1964template<class _CharT,
1965 class _Traits,
1966 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001967 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001968 class _Sz = typename allocator_traits<_Allocator>::size_type
1969 >
1970basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1971 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001972#endif
1973
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001975inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001977basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978{
Louis Dionne510450b2022-04-01 16:38:30 -04001979#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001980 if (!__libcpp_is_constant_evaluated()) {
1981 __c_node* __c = __get_db()->__find_c_and_lock(this);
1982 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001984 const_pointer __new_last = __get_pointer() + __pos;
1985 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001987 --__p;
1988 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1989 if (__i->base() > __new_last)
1990 {
1991 (*__p)->__c_ = nullptr;
1992 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001993 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001994 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001996 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997 }
1998 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001999#else
2000 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04002001#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002}
2003
2004template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002005_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier815ed732016-09-16 00:00:48 +00002006void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
2007 size_type __sz,
2008 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002010 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002011 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002013 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002015 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016 {
2017 __set_short_size(__sz);
2018 __p = __get_short_pointer();
2019 }
2020 else
2021 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002022 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
2023 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002024 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002026 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027 __set_long_size(__sz);
2028 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002029 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030 traits_type::assign(__p[__sz], value_type());
2031}
2032
2033template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002034_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035void
Howard Hinnantd17880b2013-06-28 16:59:19 +00002036basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002038 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002039 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002041 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002043 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044 {
2045 __set_short_size(__sz);
2046 __p = __get_short_pointer();
2047 }
2048 else
2049 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002050 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2051 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002052 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002054 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055 __set_long_size(__sz);
2056 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002057 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058 traits_type::assign(__p[__sz], value_type());
2059}
2060
2061template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002062template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002063_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002064basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002065 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002066{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002067 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002069 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070}
2071
2072template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002073_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002075 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076{
2077 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002078 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002080 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002081 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002082 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083}
2084
2085template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002086_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002087basic_string<_CharT, _Traits, _Allocator>::basic_string(
2088 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002089 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090{
2091 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002092 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002094 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002095 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002096 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097}
2098
Martijn Vels5e7c9752020-03-04 17:52:46 -05002099template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002100_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Vels5e7c9752020-03-04 17:52:46 -05002101void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2102 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002103 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002104 __r_.first() = __rep();
2105
Martijn Vels5e7c9752020-03-04 17:52:46 -05002106 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002107 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002108 __p = __get_short_pointer();
2109 __set_short_size(__sz);
2110 } else {
2111 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002112 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002113 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2114 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002115 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002116 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002117 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002118 __set_long_size(__sz);
2119 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002120 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002121}
2122
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002124_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125void
2126basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2127{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002128 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002129 __r_.first() = __rep();
2130
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002132 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002134 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135 {
2136 __set_short_size(__n);
2137 __p = __get_short_pointer();
2138 }
2139 else
2140 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002141 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2142 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002143 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002145 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146 __set_long_size(__n);
2147 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002148 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 traits_type::assign(__p[__n], value_type());
2150}
2151
2152template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002153template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002154_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002155basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002156 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157{
2158 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002159 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160}
2161
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002163_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002164basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2165 size_type __pos, size_type __n,
2166 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002167 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168{
2169 size_type __str_sz = __str.size();
2170 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002171 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002172 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2173 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174}
2175
2176template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002177template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002178_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow78dbe462016-11-14 18:22:19 +00002179basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002180 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002181 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002182{
Marshall Clowe46031a2018-07-02 18:41:15 +00002183 __self_view __sv0 = __t;
2184 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002185 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002186 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002187}
2188
2189template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002190template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002191_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002192basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002193 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002194{
Marshall Clowe46031a2018-07-02 18:41:15 +00002195 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002196 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002197 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002198}
2199
2200template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002201template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002202_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002203basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002204 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002205{
Marshall Clowe46031a2018-07-02 18:41:15 +00002206 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002207 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002208 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002209}
2210
2211template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002213_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002214__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002216 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2217>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2219{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002220 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221#ifndef _LIBCPP_NO_EXCEPTIONS
2222 try
2223 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002224#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225 for (; __first != __last; ++__first)
2226 push_back(*__first);
2227#ifndef _LIBCPP_NO_EXCEPTIONS
2228 }
2229 catch (...)
2230 {
2231 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002232 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233 throw;
2234 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002235#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236}
2237
2238template <class _CharT, class _Traits, class _Allocator>
2239template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002240_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002241__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002243 __is_cpp17_forward_iterator<_ForwardIterator>::value
2244>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2246{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002247 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002248 __r_.first() = __rep();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002249 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002251 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002253 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 {
2255 __set_short_size(__sz);
2256 __p = __get_short_pointer();
2257 }
2258 else
2259 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002260 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2261 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002262 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002264 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265 __set_long_size(__sz);
2266 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002267
2268#ifndef _LIBCPP_NO_EXCEPTIONS
2269 try
2270 {
2271#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002272 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 traits_type::assign(*__p, *__first);
2274 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002275#ifndef _LIBCPP_NO_EXCEPTIONS
2276 }
2277 catch (...)
2278 {
2279 if (__is_long())
2280 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2281 throw;
2282 }
2283#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284}
2285
2286template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002287_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2289{
Nikolas Klauser98833542022-05-07 22:20:23 +02002290 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002292 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002293}
2294
2295template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002296_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297void
2298basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2299 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002300 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 +00002301{
2302 size_type __ms = max_size();
2303 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002304 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305 pointer __old_p = __get_pointer();
2306 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002307 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002309 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2310 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002311 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002312 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002314 traits_type::copy(std::__to_address(__p),
2315 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002317 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2319 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002320 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2321 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002322 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002323 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002325 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2327 __set_long_size(__old_sz);
2328 traits_type::assign(__p[__old_sz], value_type());
2329}
2330
2331template <class _CharT, class _Traits, class _Allocator>
2332void
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002333_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2335 size_type __n_copy, size_type __n_del, size_type __n_add)
2336{
2337 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002338 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002339 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340 pointer __old_p = __get_pointer();
2341 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002342 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002344 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2345 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002346 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002347 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002349 traits_type::copy(std::__to_address(__p),
2350 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2352 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002353 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2354 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002355 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002356 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2357 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002359 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360}
2361
2362// assign
2363
2364template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002365template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002366_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsb6a08b62020-04-10 18:36:31 -04002367basic_string<_CharT, _Traits, _Allocator>&
2368basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002369 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002370 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002371 if (__n < __cap) {
2372 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2373 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002374 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002375 traits_type::assign(__p[__n], value_type());
2376 __invalidate_iterators_past(__n);
2377 } else {
2378 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2379 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2380 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002381 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002382}
2383
2384template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002385_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002387basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2388 const value_type* __s, size_type __n) {
2389 size_type __cap = capacity();
2390 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002391 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002392 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002393 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002394 } else {
2395 size_type __sz = size();
2396 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002397 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002398 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002399}
2400
2401template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002402_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002403basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002404basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002406 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002407 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002408 ? __assign_short(__s, __n)
2409 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410}
2411
2412template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002413_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414basic_string<_CharT, _Traits, _Allocator>&
2415basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2416{
2417 size_type __cap = capacity();
2418 if (__cap < __n)
2419 {
2420 size_type __sz = size();
2421 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2422 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002423 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002425 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426}
2427
2428template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002429_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430basic_string<_CharT, _Traits, _Allocator>&
2431basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2432{
2433 pointer __p;
2434 if (__is_long())
2435 {
2436 __p = __get_long_pointer();
2437 __set_long_size(1);
2438 }
2439 else
2440 {
2441 __p = __get_short_pointer();
2442 __set_short_size(1);
2443 }
2444 traits_type::assign(*__p, __c);
2445 traits_type::assign(*++__p, value_type());
2446 __invalidate_iterators_past(1);
2447 return *this;
2448}
2449
2450template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002451_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002452basic_string<_CharT, _Traits, _Allocator>&
2453basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2454{
Martijn Vels596e3de2020-02-26 15:55:49 -05002455 if (this != &__str) {
2456 __copy_assign_alloc(__str);
2457 if (!__is_long()) {
2458 if (!__str.__is_long()) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002459 __r_.first() = __str.__r_.first();
Martijn Vels596e3de2020-02-26 15:55:49 -05002460 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002461 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002462 }
2463 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002464 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002465 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002466 }
2467 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002468}
2469
Eric Fiselierfc92be82017-04-19 00:28:44 +00002470#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002471
2472template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002473inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002474void
2475basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002476 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002477{
2478 if (__alloc() != __str.__alloc())
2479 assign(__str);
2480 else
2481 __move_assign(__str, true_type());
2482}
2483
2484template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002485inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002486void
2487basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002488#if _LIBCPP_STD_VER > 14
2489 _NOEXCEPT
2490#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002491 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002492#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002493{
marshall2ed41622019-11-27 07:13:00 -08002494 if (__is_long()) {
2495 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2496 __get_long_cap());
2497#if _LIBCPP_STD_VER <= 14
2498 if (!is_nothrow_move_assignable<allocator_type>::value) {
2499 __set_short_size(0);
2500 traits_type::assign(__get_short_pointer()[0], value_type());
2501 }
2502#endif
2503 }
2504 __move_assign_alloc(__str);
2505 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002506 if (__libcpp_is_constant_evaluated()) {
2507 __str.__default_init();
2508 } else {
2509 __str.__set_short_size(0);
2510 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2511 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002512}
2513
2514template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002515inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002516basic_string<_CharT, _Traits, _Allocator>&
2517basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002518 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002519{
2520 __move_assign(__str, integral_constant<bool,
2521 __alloc_traits::propagate_on_container_move_assignment::value>());
2522 return *this;
2523}
2524
2525#endif
2526
2527template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002529_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002530__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002532 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002534>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2536{
Marshall Clow958362f2016-09-05 01:54:30 +00002537 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002538 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002539 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540}
2541
2542template <class _CharT, class _Traits, class _Allocator>
2543template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002544_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002545__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002547 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002549>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2551{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002553 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002554 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002555
2556 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2557 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002559 if (__cap < __n)
2560 {
2561 size_type __sz = size();
2562 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2563 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002564 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002565 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002566 traits_type::assign(*__p, *__first);
2567 traits_type::assign(*__p, value_type());
2568 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002569 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570 }
2571 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002572 {
2573 const basic_string __temp(__first, __last, __alloc());
2574 assign(__temp.data(), __temp.size());
2575 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576 return *this;
2577}
2578
2579template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002580_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581basic_string<_CharT, _Traits, _Allocator>&
2582basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2583{
2584 size_type __sz = __str.size();
2585 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002586 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002587 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588}
2589
2590template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002591template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002592_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002593__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002594<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002595 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2596 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002597 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002598>
Marshall Clow82513342016-09-24 22:45:42 +00002599basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002600{
Marshall Clow82513342016-09-24 22:45:42 +00002601 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002602 size_type __sz = __sv.size();
2603 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002604 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002605 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002606}
2607
2608
2609template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002610_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002612basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2613 return __assign_external(__s, traits_type::length(__s));
2614}
2615
2616template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002617_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002618basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002619basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002621 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002622 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002623 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002624 ? __assign_short(__s, traits_type::length(__s))
2625 : __assign_external(__s, traits_type::length(__s)))
2626 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628// append
2629
2630template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002631_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002632basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002633basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002635 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636 size_type __cap = capacity();
2637 size_type __sz = size();
2638 if (__cap - __sz >= __n)
2639 {
2640 if (__n)
2641 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002642 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 traits_type::copy(__p + __sz, __s, __n);
2644 __sz += __n;
2645 __set_size(__sz);
2646 traits_type::assign(__p[__sz], value_type());
2647 }
2648 }
2649 else
2650 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2651 return *this;
2652}
2653
2654template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002655_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656basic_string<_CharT, _Traits, _Allocator>&
2657basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2658{
2659 if (__n)
2660 {
2661 size_type __cap = capacity();
2662 size_type __sz = size();
2663 if (__cap - __sz < __n)
2664 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2665 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002666 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667 __sz += __n;
2668 __set_size(__sz);
2669 traits_type::assign(__p[__sz], value_type());
2670 }
2671 return *this;
2672}
2673
2674template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002675_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002676basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2677{
2678 if (__n)
2679 {
2680 size_type __cap = capacity();
2681 size_type __sz = size();
2682 if (__cap - __sz < __n)
2683 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2684 pointer __p = __get_pointer();
2685 __sz += __n;
2686 __set_size(__sz);
2687 traits_type::assign(__p[__sz], value_type());
2688 }
2689}
2690
2691template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002692_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693void
2694basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2695{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002696 bool __is_short = !__is_long();
2697 size_type __cap;
2698 size_type __sz;
2699 if (__is_short)
2700 {
2701 __cap = __min_cap - 1;
2702 __sz = __get_short_size();
2703 }
2704 else
2705 {
2706 __cap = __get_long_cap() - 1;
2707 __sz = __get_long_size();
2708 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002710 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002712 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002713 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002714 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002715 if (__is_short)
2716 {
2717 __p = __get_short_pointer() + __sz;
2718 __set_short_size(__sz+1);
2719 }
2720 else
2721 {
2722 __p = __get_long_pointer() + __sz;
2723 __set_long_size(__sz+1);
2724 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725 traits_type::assign(*__p, __c);
2726 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727}
2728
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729template <class _CharT, class _Traits, class _Allocator>
2730template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002731_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002732__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002733<
2734 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2735 basic_string<_CharT, _Traits, _Allocator>&
2736>
2737basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002738 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739{
2740 size_type __sz = size();
2741 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002742 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743 if (__n)
2744 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002745 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2746 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002747 {
2748 if (__cap - __sz < __n)
2749 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2750 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002751 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002752 traits_type::assign(*__p, *__first);
2753 traits_type::assign(*__p, value_type());
2754 __set_size(__sz + __n);
2755 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002756 else
2757 {
2758 const basic_string __temp(__first, __last, __alloc());
2759 append(__temp.data(), __temp.size());
2760 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761 }
2762 return *this;
2763}
2764
2765template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002766inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767basic_string<_CharT, _Traits, _Allocator>&
2768basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2769{
2770 return append(__str.data(), __str.size());
2771}
2772
2773template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002774_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775basic_string<_CharT, _Traits, _Allocator>&
2776basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2777{
2778 size_type __sz = __str.size();
2779 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002780 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002781 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782}
2783
2784template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002785template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002786_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002787 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002788 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002789 __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 +00002790 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002791 >
Marshall Clow82513342016-09-24 22:45:42 +00002792basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002793{
Marshall Clow82513342016-09-24 22:45:42 +00002794 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002795 size_type __sz = __sv.size();
2796 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002797 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002798 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002799}
2800
2801template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002802_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002804basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002806 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 return append(__s, traits_type::length(__s));
2808}
2809
2810// insert
2811
2812template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002813_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002815basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002817 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818 size_type __sz = size();
2819 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002820 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002822 if (__libcpp_is_constant_evaluated()) {
2823 if (__cap - __sz >= __n)
2824 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2825 else
2826 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2827 return *this;
2828 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829 if (__cap - __sz >= __n)
2830 {
2831 if (__n)
2832 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002833 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 size_type __n_move = __sz - __pos;
2835 if (__n_move != 0)
2836 {
2837 if (__p + __pos <= __s && __s < __p + __sz)
2838 __s += __n;
2839 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2840 }
2841 traits_type::move(__p + __pos, __s, __n);
2842 __sz += __n;
2843 __set_size(__sz);
2844 traits_type::assign(__p[__sz], value_type());
2845 }
2846 }
2847 else
2848 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2849 return *this;
2850}
2851
2852template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002853_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854basic_string<_CharT, _Traits, _Allocator>&
2855basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2856{
2857 size_type __sz = size();
2858 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002859 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 if (__n)
2861 {
2862 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002863 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864 if (__cap - __sz >= __n)
2865 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002866 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 size_type __n_move = __sz - __pos;
2868 if (__n_move != 0)
2869 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2870 }
2871 else
2872 {
2873 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002874 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875 }
2876 traits_type::assign(__p + __pos, __n, __c);
2877 __sz += __n;
2878 __set_size(__sz);
2879 traits_type::assign(__p[__sz], value_type());
2880 }
2881 return *this;
2882}
2883
2884template <class _CharT, class _Traits, class _Allocator>
2885template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002886_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002887__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002889 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002890 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002891>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2893{
Nikolas Klausereed25832021-12-15 01:32:30 +01002894 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2895 "string::insert(iterator, range) called with an iterator not"
2896 " referring to this string");
2897 const basic_string __temp(__first, __last, __alloc());
2898 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899}
2900
2901template <class _CharT, class _Traits, class _Allocator>
2902template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002903_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002904__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002906 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002908>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2910{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002911 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2912 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002913
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002915 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2916 if (__n == 0)
2917 return begin() + __ip;
2918
2919 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002921 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002923 else
2924 {
2925 const basic_string __temp(__first, __last, __alloc());
2926 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2927 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928}
2929
2930template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002931inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932basic_string<_CharT, _Traits, _Allocator>&
2933basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2934{
2935 return insert(__pos1, __str.data(), __str.size());
2936}
2937
2938template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002939_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940basic_string<_CharT, _Traits, _Allocator>&
2941basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2942 size_type __pos2, size_type __n)
2943{
2944 size_type __str_sz = __str.size();
2945 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002946 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002947 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948}
2949
2950template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002951template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002952_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002953__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002954<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002955 __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 +00002956 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002957>
Marshall Clow82513342016-09-24 22:45:42 +00002958basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002959 size_type __pos2, size_type __n)
2960{
Marshall Clow82513342016-09-24 22:45:42 +00002961 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002962 size_type __str_sz = __sv.size();
2963 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002964 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002965 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002966}
2967
2968template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002969_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002971basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002973 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974 return insert(__pos, __s, traits_type::length(__s));
2975}
2976
2977template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002978_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979typename basic_string<_CharT, _Traits, _Allocator>::iterator
2980basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2981{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002982 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2983 "string::insert(iterator, character) called with an iterator not"
2984 " referring to this string");
2985
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986 size_type __ip = static_cast<size_type>(__pos - begin());
2987 size_type __sz = size();
2988 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002989 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 if (__cap == __sz)
2991 {
2992 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002993 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994 }
2995 else
2996 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002997 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998 size_type __n_move = __sz - __ip;
2999 if (__n_move != 0)
3000 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3001 }
3002 traits_type::assign(__p[__ip], __c);
3003 traits_type::assign(__p[++__sz], value_type());
3004 __set_size(__sz);
3005 return begin() + static_cast<difference_type>(__ip);
3006}
3007
3008template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003009inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010typename basic_string<_CharT, _Traits, _Allocator>::iterator
3011basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3012{
Nikolas Klausereed25832021-12-15 01:32:30 +01003013 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3014 "string::insert(iterator, n, value) called with an iterator not"
3015 " referring to this string");
3016 difference_type __p = __pos - begin();
3017 insert(static_cast<size_type>(__p), __n, __c);
3018 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019}
3020
3021// replace
3022
3023template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003024_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003026basic_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 +00003027 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003029 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003030 size_type __sz = size();
3031 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003032 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003033 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034 size_type __cap = capacity();
3035 if (__cap - __sz + __n1 >= __n2)
3036 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003037 if (__libcpp_is_constant_evaluated()) {
3038 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3039 return *this;
3040 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003041 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042 if (__n1 != __n2)
3043 {
3044 size_type __n_move = __sz - __pos - __n1;
3045 if (__n_move != 0)
3046 {
3047 if (__n1 > __n2)
3048 {
3049 traits_type::move(__p + __pos, __s, __n2);
3050 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003051 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052 }
3053 if (__p + __pos < __s && __s < __p + __sz)
3054 {
3055 if (__p + __pos + __n1 <= __s)
3056 __s += __n2 - __n1;
3057 else // __p + __pos < __s < __p + __pos + __n1
3058 {
3059 traits_type::move(__p + __pos, __s, __n1);
3060 __pos += __n1;
3061 __s += __n2;
3062 __n2 -= __n1;
3063 __n1 = 0;
3064 }
3065 }
3066 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3067 }
3068 }
3069 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003070 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003071 }
3072 else
3073 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3074 return *this;
3075}
3076
3077template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003078_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079basic_string<_CharT, _Traits, _Allocator>&
3080basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3081{
3082 size_type __sz = size();
3083 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003084 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003085 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003087 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088 if (__cap - __sz + __n1 >= __n2)
3089 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003090 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 if (__n1 != __n2)
3092 {
3093 size_type __n_move = __sz - __pos - __n1;
3094 if (__n_move != 0)
3095 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3096 }
3097 }
3098 else
3099 {
3100 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003101 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102 }
3103 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003104 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105}
3106
3107template <class _CharT, class _Traits, class _Allocator>
3108template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003109_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003110__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003112 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003114>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003115basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 _InputIterator __j1, _InputIterator __j2)
3117{
Marshall Clow958362f2016-09-05 01:54:30 +00003118 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003119 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120}
3121
3122template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003123inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124basic_string<_CharT, _Traits, _Allocator>&
3125basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3126{
3127 return replace(__pos1, __n1, __str.data(), __str.size());
3128}
3129
3130template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003131_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132basic_string<_CharT, _Traits, _Allocator>&
3133basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3134 size_type __pos2, size_type __n2)
3135{
3136 size_type __str_sz = __str.size();
3137 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003138 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003139 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140}
3141
3142template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003143template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003144_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003145__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003146<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003147 __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 +00003148 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003149>
Marshall Clow82513342016-09-24 22:45:42 +00003150basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003151 size_type __pos2, size_type __n2)
3152{
Marshall Clow82513342016-09-24 22:45:42 +00003153 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003154 size_type __str_sz = __sv.size();
3155 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003156 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003157 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003158}
3159
3160template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003161_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003162basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003163basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003165 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166 return replace(__pos, __n1, __s, traits_type::length(__s));
3167}
3168
3169template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003170inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003172basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173{
3174 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3175 __str.data(), __str.size());
3176}
3177
3178template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003179inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003181basic_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 +00003182{
3183 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3184}
3185
3186template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003187inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003189basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190{
3191 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3192}
3193
3194template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003195inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003197basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198{
3199 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3200}
3201
3202// erase
3203
Martijn Velsa81fc792020-02-26 13:25:43 -05003204// 'externally instantiated' erase() implementation, called when __n != npos.
3205// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003207_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003208void
3209basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3210 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212 if (__n)
3213 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003214 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003215 value_type* __p = std::__to_address(__get_pointer());
3216 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217 size_type __n_move = __sz - __pos - __n;
3218 if (__n_move != 0)
3219 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003220 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003222}
3223
3224template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003225_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003226basic_string<_CharT, _Traits, _Allocator>&
3227basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3228 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003229 if (__pos > size())
3230 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003231 if (__n == npos) {
3232 __erase_to_end(__pos);
3233 } else {
3234 __erase_external_with_move(__pos, __n);
3235 }
3236 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237}
3238
3239template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003240inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241typename basic_string<_CharT, _Traits, _Allocator>::iterator
3242basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3243{
Nikolas Klausereed25832021-12-15 01:32:30 +01003244 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3245 "string::erase(iterator) called with an iterator not"
3246 " referring to this string");
3247
3248 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3249 iterator __b = begin();
3250 size_type __r = static_cast<size_type>(__pos - __b);
3251 erase(__r, 1);
3252 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253}
3254
3255template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003256inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257typename basic_string<_CharT, _Traits, _Allocator>::iterator
3258basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3259{
Nikolas Klausereed25832021-12-15 01:32:30 +01003260 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3261 "string::erase(iterator, iterator) called with an iterator not"
3262 " referring to this string");
3263
3264 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3265 iterator __b = begin();
3266 size_type __r = static_cast<size_type>(__first - __b);
3267 erase(__r, static_cast<size_type>(__last - __first));
3268 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269}
3270
3271template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003272inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273void
3274basic_string<_CharT, _Traits, _Allocator>::pop_back()
3275{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003276 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003277 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278}
3279
3280template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003281inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003283basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003284{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003285 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286 if (__is_long())
3287 {
3288 traits_type::assign(*__get_long_pointer(), value_type());
3289 __set_long_size(0);
3290 }
3291 else
3292 {
3293 traits_type::assign(*__get_short_pointer(), value_type());
3294 __set_short_size(0);
3295 }
3296}
3297
3298template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003299inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300void
3301basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3302{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003303 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304}
3305
3306template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003307_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308void
3309basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3310{
3311 size_type __sz = size();
3312 if (__n > __sz)
3313 append(__n - __sz, __c);
3314 else
3315 __erase_to_end(__n);
3316}
3317
3318template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003319_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003320basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3321{
3322 size_type __sz = size();
3323 if (__n > __sz) {
3324 __append_default_init(__n - __sz);
3325 } else
3326 __erase_to_end(__n);
3327}
3328
3329template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003330inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003332basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003334 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003335 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3336 return __m - __alignment;
3337 } else {
3338 bool __uses_lsb = __endian_factor == 2;
3339 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3340 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341}
3342
3343template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003344_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345void
Marek Kurdejc9848142020-11-26 10:07:16 +01003346basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347{
Marek Kurdejc9848142020-11-26 10:07:16 +01003348 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003349 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003350
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003351 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3352 // and later (since P0966R1), however we provide consistent behavior in all Standard
3353 // modes because this function is instantiated in the shared library.
3354 if (__requested_capacity <= capacity())
3355 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003356
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003357 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003358 __target_capacity = __recommend(__target_capacity);
3359 if (__target_capacity == capacity()) return;
3360
3361 __shrink_or_extend(__target_capacity);
3362}
3363
3364template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003365inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003366void
3367basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3368{
3369 size_type __target_capacity = __recommend(size());
3370 if (__target_capacity == capacity()) return;
3371
3372 __shrink_or_extend(__target_capacity);
3373}
3374
3375template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003376inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003377void
3378basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3379{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380 size_type __cap = capacity();
3381 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003382
3383 pointer __new_data, __p;
3384 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003385 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003387 __was_long = true;
3388 __now_long = false;
3389 __new_data = __get_short_pointer();
3390 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003392 else
3393 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003394 if (__target_capacity > __cap) {
3395 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3396 __new_data = __allocation.ptr;
3397 __target_capacity = __allocation.count - 1;
3398 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003399 else
3400 {
3401 #ifndef _LIBCPP_NO_EXCEPTIONS
3402 try
3403 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003404 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003405 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3406 __new_data = __allocation.ptr;
3407 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003408 #ifndef _LIBCPP_NO_EXCEPTIONS
3409 }
3410 catch (...)
3411 {
3412 return;
3413 }
3414 #else // _LIBCPP_NO_EXCEPTIONS
3415 if (__new_data == nullptr)
3416 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003417 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003418 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003419 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003420 __now_long = true;
3421 __was_long = __is_long();
3422 __p = __get_pointer();
3423 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003424 traits_type::copy(std::__to_address(__new_data),
3425 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003426 if (__was_long)
3427 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3428 if (__now_long)
3429 {
3430 __set_long_cap(__target_capacity+1);
3431 __set_long_size(__sz);
3432 __set_long_pointer(__new_data);
3433 }
3434 else
3435 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003436 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437}
3438
3439template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003440inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003442basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003444 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445 return *(data() + __pos);
3446}
3447
3448template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003449inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003451basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003453 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454 return *(__get_pointer() + __pos);
3455}
3456
3457template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003458_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3460basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3461{
3462 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003463 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464 return (*this)[__n];
3465}
3466
3467template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003468_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469typename basic_string<_CharT, _Traits, _Allocator>::reference
3470basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3471{
3472 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003473 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474 return (*this)[__n];
3475}
3476
3477template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003478inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003480basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003482 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483 return *__get_pointer();
3484}
3485
3486template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003487inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003489basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003491 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492 return *data();
3493}
3494
3495template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003496inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003498basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003500 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501 return *(__get_pointer() + size() - 1);
3502}
3503
3504template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003505inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003507basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003509 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510 return *(data() + size() - 1);
3511}
3512
3513template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003514_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003516basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517{
3518 size_type __sz = size();
3519 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003520 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003521 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522 traits_type::copy(__s, data() + __pos, __rlen);
3523 return __rlen;
3524}
3525
3526template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003527inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528void
3529basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003530#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003531 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003532#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003533 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003534 __is_nothrow_swappable<allocator_type>::value)
3535#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536{
Nikolas Klauser98833542022-05-07 22:20:23 +02003537 if (!__is_long())
3538 std::__debug_db_invalidate_all(this);
3539 if (!__str.__is_long())
3540 std::__debug_db_invalidate_all(&__str);
3541 std::__debug_db_swap(this, &__str);
3542
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003543 _LIBCPP_ASSERT(
3544 __alloc_traits::propagate_on_container_swap::value ||
3545 __alloc_traits::is_always_equal::value ||
3546 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003547 std::swap(__r_.first(), __str.__r_.first());
3548 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549}
3550
3551// find
3552
3553template <class _Traits>
3554struct _LIBCPP_HIDDEN __traits_eq
3555{
3556 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003557 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003558 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3559 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003560};
3561
3562template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003563_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003565basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003566 size_type __pos,
3567 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003569 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003570 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003571 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572}
3573
3574template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003575inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003577basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3578 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003580 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003581 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582}
3583
3584template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003585template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003586_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003587__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003588<
3589 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3590 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003591>
Marshall Clowe46031a2018-07-02 18:41:15 +00003592basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003593 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003594{
Marshall Clowe46031a2018-07-02 18:41:15 +00003595 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003596 return __str_find<value_type, size_type, traits_type, npos>
3597 (data(), size(), __sv.data(), __pos, __sv.size());
3598}
3599
3600template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003601inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003602typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003603basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003604 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003606 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003607 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003608 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609}
3610
3611template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003612_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003614basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3615 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003617 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003618 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619}
3620
3621// rfind
3622
3623template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003624_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003626basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003627 size_type __pos,
3628 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003630 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003631 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003632 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633}
3634
3635template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003636inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003638basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3639 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003640{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003641 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003642 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643}
3644
3645template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003646template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003647_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003648__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003649<
3650 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3651 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003652>
Marshall Clowe46031a2018-07-02 18:41:15 +00003653basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003654 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003655{
Marshall Clowe46031a2018-07-02 18:41:15 +00003656 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003657 return __str_rfind<value_type, size_type, traits_type, npos>
3658 (data(), size(), __sv.data(), __pos, __sv.size());
3659}
3660
3661template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003662inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003663typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003664basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003665 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003667 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003668 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003669 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670}
3671
3672template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003673_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003675basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3676 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003678 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003679 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680}
3681
3682// find_first_of
3683
3684template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003685_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003687basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003688 size_type __pos,
3689 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003691 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003692 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003693 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694}
3695
3696template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003697inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003698typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003699basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3700 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003703 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704}
3705
3706template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003707template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003708_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003709__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003710<
3711 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3712 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003713>
Marshall Clowe46031a2018-07-02 18:41:15 +00003714basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003715 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003716{
Marshall Clowe46031a2018-07-02 18:41:15 +00003717 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003718 return __str_find_first_of<value_type, size_type, traits_type, npos>
3719 (data(), size(), __sv.data(), __pos, __sv.size());
3720}
3721
3722template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003723inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003724typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003725basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003726 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003728 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003729 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003730 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003731}
3732
3733template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003734inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003736basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3737 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738{
3739 return find(__c, __pos);
3740}
3741
3742// find_last_of
3743
3744template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003745inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003746typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003747basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003748 size_type __pos,
3749 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003751 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003752 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003753 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754}
3755
3756template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003757inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003759basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3760 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003762 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003763 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764}
3765
3766template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003767template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003768_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003769__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003770<
3771 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3772 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003773>
Marshall Clowe46031a2018-07-02 18:41:15 +00003774basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003775 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003776{
Marshall Clowe46031a2018-07-02 18:41:15 +00003777 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003778 return __str_find_last_of<value_type, size_type, traits_type, npos>
3779 (data(), size(), __sv.data(), __pos, __sv.size());
3780}
3781
3782template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003783inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003784typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003785basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003786 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003788 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003789 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003790 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791}
3792
3793template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003794inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003796basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3797 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798{
3799 return rfind(__c, __pos);
3800}
3801
3802// find_first_not_of
3803
3804template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003805_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003807basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003808 size_type __pos,
3809 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003811 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003812 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003813 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814}
3815
3816template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003817inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003819basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3820 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003822 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003823 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824}
3825
3826template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003827template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003828_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003829__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003830<
3831 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3832 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003833>
Marshall Clowe46031a2018-07-02 18:41:15 +00003834basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003835 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003836{
Marshall Clowe46031a2018-07-02 18:41:15 +00003837 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003838 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3839 (data(), size(), __sv.data(), __pos, __sv.size());
3840}
3841
3842template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003843inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003844typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003845basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003846 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003848 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003849 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003850 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851}
3852
3853template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003854inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003856basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3857 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003860 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861}
3862
3863// find_last_not_of
3864
3865template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003866_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003868basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003869 size_type __pos,
3870 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003871{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003872 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003873 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003874 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875}
3876
3877template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003878inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003880basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3881 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003883 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003884 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003885}
3886
3887template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003888template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003889_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003890__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003891<
3892 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3893 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003894>
Marshall Clowe46031a2018-07-02 18:41:15 +00003895basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003896 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003897{
Marshall Clowe46031a2018-07-02 18:41:15 +00003898 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003899 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3900 (data(), size(), __sv.data(), __pos, __sv.size());
3901}
3902
3903template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003904inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003905typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003906basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003907 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003909 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003910 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003911 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912}
3913
3914template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003915inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003916typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003917basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3918 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003919{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003920 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003921 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003922}
3923
3924// compare
3925
3926template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003927template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003928_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003929__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003930<
3931 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3932 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003933>
zoecarver1997e0a2021-02-05 11:54:47 -08003934basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935{
Marshall Clowe46031a2018-07-02 18:41:15 +00003936 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003937 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003938 size_t __rhs_sz = __sv.size();
3939 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003940 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003941 if (__result != 0)
3942 return __result;
3943 if (__lhs_sz < __rhs_sz)
3944 return -1;
3945 if (__lhs_sz > __rhs_sz)
3946 return 1;
3947 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003948}
3949
3950template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003951inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003953basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003955 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003956}
3957
3958template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003959inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003961basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3962 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003963 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003964 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003966 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967 size_type __sz = size();
3968 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003969 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003970 size_type __rlen = std::min(__n1, __sz - __pos1);
3971 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972 if (__r == 0)
3973 {
3974 if (__rlen < __n2)
3975 __r = -1;
3976 else if (__rlen > __n2)
3977 __r = 1;
3978 }
3979 return __r;
3980}
3981
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003982template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003983template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003984_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003985__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003986<
3987 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3988 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003989>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003990basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3991 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003992 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003993{
Marshall Clowe46031a2018-07-02 18:41:15 +00003994 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003995 return compare(__pos1, __n1, __sv.data(), __sv.size());
3996}
3997
3998template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003999inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004000int
4001basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4002 size_type __n1,
4003 const basic_string& __str) const
4004{
4005 return compare(__pos1, __n1, __str.data(), __str.size());
4006}
4007
4008template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00004009template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004010_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04004011__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00004012<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004013 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
4014 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00004015 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004016>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004017basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4018 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00004019 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004020 size_type __pos2,
4021 size_type __n2) const
4022{
Marshall Clow82513342016-09-24 22:45:42 +00004023 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004024 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
4025}
4026
4027template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004028_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004029int
4030basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4031 size_type __n1,
4032 const basic_string& __str,
4033 size_type __pos2,
4034 size_type __n2) const
4035{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004036 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004037}
4038
4039template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004040_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004041int
4042basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4043{
4044 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4045 return compare(0, npos, __s, traits_type::length(__s));
4046}
4047
4048template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004049_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004050int
4051basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4052 size_type __n1,
4053 const value_type* __s) const
4054{
4055 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4056 return compare(__pos1, __n1, __s, traits_type::length(__s));
4057}
4058
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059// __invariants
4060
4061template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004062inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063bool
4064basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4065{
4066 if (size() > capacity())
4067 return false;
4068 if (capacity() < __min_cap - 1)
4069 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004070 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004072 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073 return false;
4074 return true;
4075}
4076
Vedant Kumar55e007e2018-03-08 21:15:26 +00004077// __clear_and_shrink
4078
4079template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004080inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne173f29e2019-05-29 16:01:36 +00004081void
Marshall Clowe60a7182018-05-29 17:04:37 +00004082basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004083{
4084 clear();
4085 if(__is_long())
4086 {
4087 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
Nikolas Klauser1821ec32022-10-01 15:37:24 +02004088 __default_init();
Vedant Kumar55e007e2018-03-08 21:15:26 +00004089 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004090}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004091
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092// operator==
4093
4094template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004095inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096bool
4097operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004098 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099{
Mark de Weverb4830e62022-07-19 07:56:23 +02004100#if _LIBCPP_STD_VER > 17
4101 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4102#else
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004103 size_t __lhs_sz = __lhs.size();
4104 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4105 __rhs.data(),
4106 __lhs_sz) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004107#endif
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004108}
4109
4110template<class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004111inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004112bool
4113operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4114 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4115{
4116 size_t __lhs_sz = __lhs.size();
4117 if (__lhs_sz != __rhs.size())
4118 return false;
4119 const char* __lp = __lhs.data();
4120 const char* __rp = __rhs.data();
4121 if (__lhs.__is_long())
4122 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4123 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4124 if (*__lp != *__rp)
4125 return false;
4126 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127}
4128
Mark de Weverb4830e62022-07-19 07:56:23 +02004129#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004131inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004133operator==(const _CharT* __lhs,
4134 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004136 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4137 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4138 size_t __lhs_len = _Traits::length(__lhs);
4139 if (__lhs_len != __rhs.size()) return false;
4140 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141}
Mark de Weverb4830e62022-07-19 07:56:23 +02004142#endif // _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004145inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004146bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004147operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4148 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149{
Mark de Weverb4830e62022-07-19 07:56:23 +02004150#if _LIBCPP_STD_VER > 17
4151 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4152#else
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004153 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4154 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4155 size_t __rhs_len = _Traits::length(__rhs);
4156 if (__rhs_len != __lhs.size()) return false;
4157 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004158#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159}
4160
Mark de Weverb4830e62022-07-19 07:56:23 +02004161#if _LIBCPP_STD_VER > 17
4162
4163template <class _CharT, class _Traits, class _Allocator>
4164_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
4165 const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4166 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
4167 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4168}
4169
4170template <class _CharT, class _Traits, class _Allocator>
4171_LIBCPP_HIDE_FROM_ABI constexpr auto
4172operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
4173 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4174}
4175
4176#else // _LIBCPP_STD_VER > 17
4177
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004178template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004179inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180bool
4181operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004182 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183{
4184 return !(__lhs == __rhs);
4185}
4186
4187template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004188inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004189bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004190operator!=(const _CharT* __lhs,
4191 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192{
4193 return !(__lhs == __rhs);
4194}
4195
4196template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004197inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004199operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4200 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201{
4202 return !(__lhs == __rhs);
4203}
4204
4205// operator<
4206
4207template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004208inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209bool
4210operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004211 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004213 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214}
4215
4216template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004217inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004219operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4220 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004222 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223}
4224
4225template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004226inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004228operator< (const _CharT* __lhs,
4229 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230{
4231 return __rhs.compare(__lhs) > 0;
4232}
4233
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234// operator>
4235
4236template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004237inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238bool
4239operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004240 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241{
4242 return __rhs < __lhs;
4243}
4244
4245template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004246inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004248operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4249 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004250{
4251 return __rhs < __lhs;
4252}
4253
4254template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004255inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004257operator> (const _CharT* __lhs,
4258 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259{
4260 return __rhs < __lhs;
4261}
4262
4263// operator<=
4264
4265template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004266inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267bool
4268operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004269 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270{
4271 return !(__rhs < __lhs);
4272}
4273
4274template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004275inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004277operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4278 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279{
4280 return !(__rhs < __lhs);
4281}
4282
4283template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004284inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004286operator<=(const _CharT* __lhs,
4287 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288{
4289 return !(__rhs < __lhs);
4290}
4291
4292// operator>=
4293
4294template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004295inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296bool
4297operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004298 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299{
4300 return !(__lhs < __rhs);
4301}
4302
4303template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004304inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004306operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4307 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308{
4309 return !(__lhs < __rhs);
4310}
4311
4312template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004313inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004315operator>=(const _CharT* __lhs,
4316 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004317{
4318 return !(__lhs < __rhs);
4319}
Mark de Weverb4830e62022-07-19 07:56:23 +02004320#endif // _LIBCPP_STD_VER > 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321
4322// operator +
4323
4324template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004325_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326basic_string<_CharT, _Traits, _Allocator>
4327operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4328 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4329{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004330 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004331 auto __lhs_sz = __lhs.size();
4332 auto __rhs_sz = __rhs.size();
4333 _String __r(__uninitialized_size_tag(),
4334 __lhs_sz + __rhs_sz,
4335 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4336 auto __ptr = std::__to_address(__r.__get_pointer());
4337 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4338 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4339 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340 return __r;
4341}
4342
4343template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004344_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345basic_string<_CharT, _Traits, _Allocator>
4346operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4347{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004348 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004349 auto __lhs_sz = _Traits::length(__lhs);
4350 auto __rhs_sz = __rhs.size();
4351 _String __r(__uninitialized_size_tag(),
4352 __lhs_sz + __rhs_sz,
4353 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4354 auto __ptr = std::__to_address(__r.__get_pointer());
4355 _Traits::copy(__ptr, __lhs, __lhs_sz);
4356 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4357 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358 return __r;
4359}
4360
4361template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004362_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363basic_string<_CharT, _Traits, _Allocator>
4364operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4365{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004366 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004367 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004368 _String __r(__uninitialized_size_tag(),
4369 __rhs_sz + 1,
4370 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4371 auto __ptr = std::__to_address(__r.__get_pointer());
4372 _Traits::assign(__ptr, 1, __lhs);
4373 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4374 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004375 return __r;
4376}
4377
4378template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004379inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380basic_string<_CharT, _Traits, _Allocator>
4381operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4382{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004383 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004384 typename _String::size_type __lhs_sz = __lhs.size();
4385 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004386 _String __r(__uninitialized_size_tag(),
4387 __lhs_sz + __rhs_sz,
4388 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4389 auto __ptr = std::__to_address(__r.__get_pointer());
4390 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4391 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4392 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004393 return __r;
4394}
4395
4396template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004397_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004398basic_string<_CharT, _Traits, _Allocator>
4399operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4400{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004401 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004402 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004403 _String __r(__uninitialized_size_tag(),
4404 __lhs_sz + 1,
4405 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4406 auto __ptr = std::__to_address(__r.__get_pointer());
4407 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4408 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4409 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004410 return __r;
4411}
4412
Eric Fiselierfc92be82017-04-19 00:28:44 +00004413#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004414
4415template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004416inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417basic_string<_CharT, _Traits, _Allocator>
4418operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4419{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004420 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004421}
4422
4423template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004424inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004425basic_string<_CharT, _Traits, _Allocator>
4426operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4427{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004428 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004429}
4430
4431template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004432inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004433basic_string<_CharT, _Traits, _Allocator>
4434operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4435{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004436 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004437}
4438
4439template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004440inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441basic_string<_CharT, _Traits, _Allocator>
4442operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4443{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004444 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004445}
4446
4447template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004448inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004449basic_string<_CharT, _Traits, _Allocator>
4450operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4451{
4452 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004453 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004454}
4455
4456template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004457inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004458basic_string<_CharT, _Traits, _Allocator>
4459operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4460{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004461 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004462}
4463
4464template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004465inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466basic_string<_CharT, _Traits, _Allocator>
4467operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4468{
4469 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004470 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004471}
4472
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004473#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004474
4475// swap
4476
4477template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004478inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004480swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004481 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4482 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004483{
4484 __lhs.swap(__rhs);
4485}
4486
Bruce Mitchener170d8972020-11-24 12:53:53 -05004487_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4488_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4489_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4490_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4491_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004492
Bruce Mitchener170d8972020-11-24 12:53:53 -05004493_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4494_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4495_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004496
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004497_LIBCPP_FUNC_VIS string to_string(int __val);
4498_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4499_LIBCPP_FUNC_VIS string to_string(long __val);
4500_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4501_LIBCPP_FUNC_VIS string to_string(long long __val);
4502_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4503_LIBCPP_FUNC_VIS string to_string(float __val);
4504_LIBCPP_FUNC_VIS string to_string(double __val);
4505_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004506
Louis Dionne89258142021-08-23 15:32:36 -04004507#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004508_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4509_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4510_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4511_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4512_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004513
Bruce Mitchener170d8972020-11-24 12:53:53 -05004514_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4515_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4516_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004517
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004518_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4519_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4520_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4521_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4522_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4523_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4524_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4525_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4526_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004527#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004528
Howard Hinnantc51e1022010-05-11 19:42:16 +00004529template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004530_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004531const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4532 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533
Marshall Clow851b9ec2019-05-20 21:56:51 +00004534template <class _CharT, class _Allocator>
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004535struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536{
4537 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004538 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4539 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004540};
4541
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004542template <class _Allocator>
4543struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
4544
4545#ifndef _LIBCPP_HAS_NO_CHAR8_T
4546template <class _Allocator>
4547struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
4548#endif
4549
4550template <class _Allocator>
4551struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
4552
4553template <class _Allocator>
4554struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
4555
4556#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4557template <class _Allocator>
4558struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
4559#endif
4560
Howard Hinnantdc095972011-07-18 15:51:59 +00004561template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004562_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004563operator<<(basic_ostream<_CharT, _Traits>& __os,
4564 const basic_string<_CharT, _Traits, _Allocator>& __str);
4565
4566template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004567_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004568operator>>(basic_istream<_CharT, _Traits>& __is,
4569 basic_string<_CharT, _Traits, _Allocator>& __str);
4570
4571template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004572_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004573getline(basic_istream<_CharT, _Traits>& __is,
4574 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4575
4576template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004577inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004578basic_istream<_CharT, _Traits>&
4579getline(basic_istream<_CharT, _Traits>& __is,
4580 basic_string<_CharT, _Traits, _Allocator>& __str);
4581
Howard Hinnantdc095972011-07-18 15:51:59 +00004582template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004583inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004584basic_istream<_CharT, _Traits>&
4585getline(basic_istream<_CharT, _Traits>&& __is,
4586 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4587
4588template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004589inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004590basic_istream<_CharT, _Traits>&
4591getline(basic_istream<_CharT, _Traits>&& __is,
4592 basic_string<_CharT, _Traits, _Allocator>& __str);
4593
Marshall Clow29b53f22018-12-14 18:49:35 +00004594#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004595template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004596inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004597 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4598 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4599 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004600 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004601 return __old_size - __str.size();
4602}
Marshall Clow29b53f22018-12-14 18:49:35 +00004603
Marek Kurdeja98b1412020-05-02 13:58:03 +02004604template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004605inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004606 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4607 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4608 _Predicate __pred) {
4609 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004610 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004611 __str.end());
4612 return __old_size - __str.size();
4613}
Marshall Clow29b53f22018-12-14 18:49:35 +00004614#endif
4615
Louis Dionne510450b2022-04-01 16:38:30 -04004616#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004617
4618template<class _CharT, class _Traits, class _Allocator>
4619bool
4620basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4621{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004622 return data() <= std::__to_address(__i->base()) &&
4623 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004624}
4625
4626template<class _CharT, class _Traits, class _Allocator>
4627bool
4628basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4629{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004630 return data() < std::__to_address(__i->base()) &&
4631 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004632}
4633
4634template<class _CharT, class _Traits, class _Allocator>
4635bool
4636basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4637{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004638 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004639 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004640}
4641
4642template<class _CharT, class _Traits, class _Allocator>
4643bool
4644basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4645{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004646 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004647 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004648}
4649
Louis Dionne510450b2022-04-01 16:38:30 -04004650#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004651
Louis Dionne173f29e2019-05-29 16:01:36 +00004652#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004653// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004654inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004655{
4656 inline namespace string_literals
4657 {
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004658 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004659 basic_string<char> operator "" s( const char *__str, size_t __len )
4660 {
4661 return basic_string<char> (__str, __len);
4662 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004663
Louis Dionne89258142021-08-23 15:32:36 -04004664#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004665 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004666 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4667 {
4668 return basic_string<wchar_t> (__str, __len);
4669 }
Louis Dionne89258142021-08-23 15:32:36 -04004670#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004671
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004672#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004673 inline _LIBCPP_HIDE_FROM_ABI constexpr
Nikolas Klauser5da956d2022-09-02 16:20:28 +02004674 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len)
Marshall Clow8732fed2018-12-11 04:35:44 +00004675 {
4676 return basic_string<char8_t> (__str, __len);
4677 }
4678#endif
4679
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004680 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004681 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4682 {
4683 return basic_string<char16_t> (__str, __len);
4684 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004685
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004686 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004687 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4688 {
4689 return basic_string<char32_t> (__str, __len);
4690 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004691 } // namespace string_literals
4692} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004693
4694#if _LIBCPP_STD_VER > 17
4695template <>
4696inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4697#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4698template <>
4699inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4700#endif
4701#endif
4702
Marshall Clowcba751f2013-07-23 17:05:24 +00004703#endif
4704
Howard Hinnantc51e1022010-05-11 19:42:16 +00004705_LIBCPP_END_NAMESPACE_STD
4706
Eric Fiselierf4433a32017-05-31 22:07:49 +00004707_LIBCPP_POP_MACROS
4708
Mark de Weveree5fe272022-09-02 17:53:28 +02004709#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
4710# include <algorithm>
4711# include <functional>
4712# include <iterator>
4713# include <new>
4714# include <typeinfo>
4715# include <utility>
4716# include <vector>
4717#endif
4718
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004719#endif // _LIBCPP_STRING