blob: 03bb5ea8ab72d0d695558d28d856e272e0f2e142 [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
Louis Dionnedd059fa2022-11-14 10:33:03 -1000685 // TODO: Implement iterator bounds checking without requiring the global database.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686 typedef __wrap_iter<pointer> iterator;
687 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200688 typedef std::reverse_iterator<iterator> reverse_iterator;
689 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690
691private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200692 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000693
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000694#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000695
696 struct __long
697 {
698 pointer __data_;
699 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200700 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
701 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000702 };
703
Howard Hinnant68bf1812013-04-30 21:44:48 +0000704 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
705 (sizeof(__long) - 1)/sizeof(value_type) : 2};
706
707 struct __short
708 {
709 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200710 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200711 unsigned char __size_ : 7;
712 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000713 };
714
Nikolas Klauser62060be2022-04-20 10:52:04 +0200715// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200716// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200717//
718// If the LSB is used to store the short-flag in the short string representation,
719// we have to multiply the size by two when it is stored and divide it by two when
720// it is loaded to make sure that we always store an even number. In the long string
721// representation, we can ignore this because we can assume that we always allocate
722// an even amount of value_types.
723//
724// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
725// This does not impact the short string representation, since we never need the MSB
726// for representing the size of a short string anyway.
727
728#ifdef _LIBCPP_BIG_ENDIAN
729 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000730#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200731 static const size_type __endian_factor = 1;
732#endif
733
734#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
735
736#ifdef _LIBCPP_BIG_ENDIAN
737 static const size_type __endian_factor = 1;
738#else
739 static const size_type __endian_factor = 2;
740#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000741
Xing Xue38b9fc42022-06-24 17:25:15 -0400742 // Attribute 'packed' is used to keep the layout compatible with the
743 // previous definition that did not use bit fields. This is because on
744 // some platforms bit fields have a default size rather than the actual
745 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 struct __long
747 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400748 struct _LIBCPP_PACKED {
749 size_type __is_long_ : 1;
750 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
751 };
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 size_type __size_;
753 pointer __data_;
754 };
755
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
757 (sizeof(__long) - 1)/sizeof(value_type) : 2};
758
759 struct __short
760 {
Xing Xue38b9fc42022-06-24 17:25:15 -0400761 struct _LIBCPP_PACKED {
762 unsigned char __is_long_ : 1;
763 unsigned char __size_ : 7;
764 };
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200765 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766 value_type __data_[__min_cap];
767 };
768
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400769#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000770
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200771 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
772
Howard Hinnant8ea98242013-08-23 17:37:05 +0000773 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774
Howard Hinnant8ea98242013-08-23 17:37:05 +0000775 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776
777 struct __raw
778 {
779 size_type __words[__n_words];
780 };
781
782 struct __rep
783 {
784 union
785 {
786 __long __l;
787 __short __s;
788 __raw __r;
789 };
790 };
791
792 __compressed_pair<__rep, allocator_type> __r_;
793
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200794 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
795 // don't initialize the characters. The contents of the string, including the null terminator, must be
796 // initialized separately.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200797 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200798 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200799 : __r_(__default_init_tag(), __a) {
800 if (__size > max_size())
801 __throw_length_error();
802 if (__fits_in_sso(__size)) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +0200803 __r_.first() = __rep();
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200804 __set_short_size(__size);
805 } else {
806 auto __capacity = __recommend(__size) + 1;
807 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200808 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200809 __set_long_cap(__capacity);
810 __set_long_pointer(__allocation);
811 __set_long_size(__size);
812 }
813 std::__debug_db_insert_c(this);
814 }
815
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816public:
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200817 _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200819 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
820 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
821 : __r_(__default_init_tag(), __default_init_tag()) {
822 std::__debug_db_insert_c(this);
823 __default_init();
824 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000825
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200826 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000827#if _LIBCPP_STD_VER <= 14
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200828 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +0000829#else
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200830 _NOEXCEPT
Marshall Clowa80abc72015-06-03 19:56:43 +0000831#endif
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200832 : __r_(__default_init_tag(), __a) {
833 std::__debug_db_insert_c(this);
834 __default_init();
835 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000836
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200837 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
838 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000839
Eric Fiselierfc92be82017-04-19 00:28:44 +0000840#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200841 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)
842# if _LIBCPP_STD_VER <= 14
843 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
844# else
845 _NOEXCEPT
846# endif
847 : __r_(std::move(__str.__r_)) {
848 __str.__default_init();
849 std::__debug_db_insert_c(this);
850 if (__is_long())
851 std::__debug_db_swap(this, &__str);
852 }
Marshall Clowa80abc72015-06-03 19:56:43 +0000853
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200854 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)
855 : __r_(__default_init_tag(), __a) {
856 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
857 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
858 else {
859 if (__libcpp_is_constant_evaluated())
860 __r_.first() = __rep();
861 __r_.first() = __str.__r_.first();
862 __str.__default_init();
863 }
864 std::__debug_db_insert_c(this);
865 if (__is_long())
866 std::__debug_db_swap(this, &__str);
867 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400868#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000869
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200870 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
871 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s)
872 : __r_(__default_init_tag(), __default_init_tag()) {
873 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
874 __init(__s, traits_type::length(__s));
875 std::__debug_db_insert_c(this);
876 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000877
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200878 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
879 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, const _Allocator& __a);
Marshall Clowe46031a2018-07-02 18:41:15 +0000880
Marek Kurdej90d79712021-07-27 16:16:21 +0200881#if _LIBCPP_STD_VER > 20
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200882 basic_string(nullptr_t) = delete;
Marek Kurdej90d79712021-07-27 16:16:21 +0200883#endif
884
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200885 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n)
886 : __r_(__default_init_tag(), __default_init_tag()) {
887 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
888 __init(__s, __n);
889 std::__debug_db_insert_c(this);
890 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000891
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200892 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
893 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
894 : __r_(__default_init_tag(), __a) {
895 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
896 __init(__s, __n);
897 std::__debug_db_insert_c(this);
898 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000899
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200900 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c)
901 : __r_(__default_init_tag(), __default_init_tag()) {
902 __init(__n, __c);
903 std::__debug_db_insert_c(this);
904 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000905
Nikolas Klauser1b531cf2022-08-09 13:17:30 +0200906#if _LIBCPP_STD_VER > 20
907 _LIBCPP_HIDE_FROM_ABI constexpr
908 basic_string(basic_string&& __str, size_type __pos, const _Allocator& __alloc = _Allocator())
909 : basic_string(std::move(__str), __pos, npos, __alloc) {}
910
911 _LIBCPP_HIDE_FROM_ABI constexpr
912 basic_string(basic_string&& __str, size_type __pos, size_type __n, const _Allocator& __alloc = _Allocator())
913 : __r_(__default_init_tag(), __alloc) {
914 if (__pos > __str.size())
915 __throw_out_of_range();
916
917 auto __len = std::min<size_type>(__n, __str.size() - __pos);
918 if (__alloc_traits::is_always_equal::value || __alloc == __str.__alloc()) {
919 __r_.first() = __str.__r_.first();
920 __str.__default_init();
921
922 _Traits::move(data(), data() + __pos, __len);
923 __set_size(__len);
924 _Traits::assign(data()[__len], value_type());
925 } else {
926 // Perform a copy because the allocators are not compatible.
927 __init(__str.data() + __pos, __len);
928 }
929
930 std::__debug_db_insert_c(this);
931 if (__is_long())
932 std::__debug_db_swap(this, &__str);
933 }
934#endif
935
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200936 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
937 _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 +0000938
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200939 _LIBCPP_CONSTEXPR_SINCE_CXX20
940 basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000941
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200942 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
943 basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())
944 : __r_(__default_init_tag(), __a) {
945 size_type __str_sz = __str.size();
946 if (__pos > __str_sz)
947 __throw_out_of_range();
948 __init(__str.data() + __pos, __str_sz - __pos);
949 std::__debug_db_insert_c(this);
950 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000951
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200952 template <class _Tp,
953 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
954 !__is_same_uncvref<_Tp, basic_string>::value> >
955 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
956 basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type());
957
958 template <class _Tp,
959 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
960 !__is_same_uncvref<_Tp, basic_string>::value> >
961 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
962 const _Tp& __t);
963
964 template <class _Tp,
965 class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
966 !__is_same_uncvref<_Tp, basic_string>::value> >
967 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
968 const _Tp& __t, const allocator_type& __a);
969
970 template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
971 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last)
972 : __r_(__default_init_tag(), __default_init_tag()) {
973 __init(__first, __last);
974 std::__debug_db_insert_c(this);
975 }
976
977 template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
978 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
979 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
980 : __r_(__default_init_tag(), __a) {
981 __init(__first, __last);
982 std::__debug_db_insert_c(this);
983 }
984
Eric Fiselierfc92be82017-04-19 00:28:44 +0000985#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauserd128f2b2022-08-26 18:35:25 +0200986 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il)
987 : __r_(__default_init_tag(), __default_init_tag()) {
988 __init(__il.begin(), __il.end());
989 std::__debug_db_insert_c(this);
990 }
991
992 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)
993 : __r_(__default_init_tag(), __a) {
994 __init(__il.begin(), __il.end());
995 std::__debug_db_insert_c(this);
996 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400997#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998
Nikolas Klauser8b1c5062022-08-19 13:08:01 +0200999 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001001 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001002 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
1003
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001004 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +00001005
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001006 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1007 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001008 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001009 __self_view __sv = __t;
1010 return assign(__sv);
1011 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001012
Eric Fiselierfc92be82017-04-19 00:28:44 +00001013#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser02327072022-10-29 21:04:35 +02001014 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(basic_string&& __str)
1015 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)) {
1016 __move_assign(__str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
1017 return *this;
1018 }
1019
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001020 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierfc92be82017-04-19 00:28:44 +00001021 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001023 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001024 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +02001025#if _LIBCPP_STD_VER > 20
1026 basic_string& operator=(nullptr_t) = delete;
1027#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001028 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001030 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001031 iterator begin() _NOEXCEPT
1032 {return iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001033 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001034 const_iterator begin() const _NOEXCEPT
1035 {return const_iterator(this, __get_pointer());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001036 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001037 iterator end() _NOEXCEPT
1038 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001039 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant8ea98242013-08-23 17:37:05 +00001040 const_iterator end() const _NOEXCEPT
1041 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -04001042
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001043 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001044 reverse_iterator rbegin() _NOEXCEPT
1045 {return reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001046 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001047 const_reverse_iterator rbegin() const _NOEXCEPT
1048 {return const_reverse_iterator(end());}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001049 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001050 reverse_iterator rend() _NOEXCEPT
1051 {return reverse_iterator(begin());}
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_reverse_iterator rend() const _NOEXCEPT
1054 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001056 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001057 const_iterator cbegin() const _NOEXCEPT
1058 {return begin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001059 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001060 const_iterator cend() const _NOEXCEPT
1061 {return end();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001062 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001063 const_reverse_iterator crbegin() const _NOEXCEPT
1064 {return rbegin();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001065 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001066 const_reverse_iterator crend() const _NOEXCEPT
1067 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001069 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001071 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {return size();}
Nikolas Klauser02327072022-10-29 21:04:35 +02001072
1073 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT {
1074 size_type __m = __alloc_traits::max_size(__alloc());
1075 if (__m <= std::numeric_limits<size_type>::max() / 2) {
1076 return __m - __alignment;
1077 } else {
1078 bool __uses_lsb = __endian_factor == 2;
1079 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
1080 }
1081 }
1082
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001083 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001084 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
1085 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001087 _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
1088 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001090 _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +01001091
1092#if _LIBCPP_STD_VER > 20
1093 template <class _Op>
1094 _LIBCPP_HIDE_FROM_ABI constexpr
1095 void resize_and_overwrite(size_type __n, _Op __op) {
1096 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001097 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +01001098 }
1099#endif
1100
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001101 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +00001102
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001103 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001104 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
1105 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001106
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001107 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowb7db4972017-11-15 20:02:27 +00001108 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109
Nikolas Klauser02327072022-10-29 21:04:35 +02001110 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator[](size_type __pos) const _NOEXCEPT {
1111 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
1112 return *(data() + __pos);
1113 }
1114
1115 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT {
1116 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
1117 return *(__get_pointer() + __pos);
1118 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001120 _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
1121 _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001123 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001124 return append(__str);
1125 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001126
1127 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001128 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001129 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001130 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001131 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1132 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001133 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001134 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001135 operator+=(const _Tp& __t) {
1136 __self_view __sv = __t; return append(__sv);
1137 }
1138
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001139 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001140 return append(__s);
1141 }
1142
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001143 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001144 push_back(__c);
1145 return *this;
1146 }
1147
Eric Fiselierfc92be82017-04-19 00:28:44 +00001148#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001149 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001150 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001151#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152
Nikolas Klauser02327072022-10-29 21:04:35 +02001153 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str) {
1154 return append(__str.data(), __str.size());
1155 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001156
1157 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001158 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001159 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001160 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1161 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001162 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001163 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001164 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001165 _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 +00001166
Marshall Clow62953962016-10-03 23:40:48 +00001167 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001168 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001169 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001170 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001171 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1172 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001173 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001174 >
Marshall Clow82513342016-09-24 22:45:42 +00001175 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001176 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n);
1177 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s);
1178 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001179
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001180 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier451d5582018-11-26 20:15:38 +00001181 void __append_default_init(size_type __n);
1182
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001184 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001185 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001187 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001189 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001190 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001191 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001192 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001193 append(__temp.data(), __temp.size());
1194 return *this;
1195 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001197 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001198 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001200 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001202 >
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001203 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001204 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001205
Eric Fiselierfc92be82017-04-19 00:28:44 +00001206#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001207 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001209#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001211 _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
1212 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
Nikolas Klauser02327072022-10-29 21:04:35 +02001213
1214 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT {
1215 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
1216 return *__get_pointer();
1217 }
1218
1219 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT {
1220 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
1221 return *data();
1222 }
1223
1224 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT {
1225 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
1226 return *(__get_pointer() + size() - 1);
1227 }
1228
1229 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT {
1230 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
1231 return *(data() + size() - 1);
1232 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233
Marshall Clowe46031a2018-07-02 18:41:15 +00001234 template <class _Tp>
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
Marshall Clowe46031a2018-07-02 18:41:15 +00001237 <
1238 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1239 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001240 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001241 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001242 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001243 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001244#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001245 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001246 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001247 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001248 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001249#endif
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001250 _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 +00001251 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001252 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001253 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001254 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001255 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1256 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001257 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001258 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001259 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001260 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n);
1261 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s);
1262 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001264 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001265 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001266 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001267 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001269 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270 assign(_InputIterator __first, _InputIterator __last);
1271 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001272 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001273 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001275 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001277 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001279#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001280 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001282#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283
Nikolas Klauser02327072022-10-29 21:04:35 +02001284 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1285 insert(size_type __pos1, const basic_string& __str) {
1286 return insert(__pos1, __str.data(), __str.size());
1287 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001288
1289 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001290 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001291 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001292 <
1293 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1294 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001295 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001296 insert(size_type __pos1, const _Tp& __t)
1297 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1298
Marshall Clow82513342016-09-24 22:45:42 +00001299 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001300 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001301 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001302 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001303 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001304 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001305 >
Marshall Clow82513342016-09-24 22:45:42 +00001306 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001307 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001308 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001309 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1310 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s);
1311 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1312 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
Nikolas Klauser02327072022-10-29 21:04:35 +02001313
1314 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1315 insert(const_iterator __pos, size_type __n, value_type __c) {
1316 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
1317 "string::insert(iterator, n, value) called with an iterator not referring to this string");
1318 difference_type __p = __pos - begin();
1319 insert(static_cast<size_type>(__p), __n, __c);
1320 return begin() + __p;
1321 }
1322
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001324 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001325 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001327 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001329 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1331 template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001332 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001333 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001335 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001337 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001339#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001340 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1342 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001343#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001345 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1346 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 iterator erase(const_iterator __pos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001348 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349 iterator erase(const_iterator __first, const_iterator __last);
1350
Nikolas Klauser02327072022-10-29 21:04:35 +02001351 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1352 replace(size_type __pos1, size_type __n1, const basic_string& __str) {
1353 return replace(__pos1, __n1, __str.data(), __str.size());
1354 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001355
1356 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001357 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001358 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001359 <
1360 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1361 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001362 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001363 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 +02001364 _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow8db7fb02014-03-04 19:17:19 +00001365 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 +00001366 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001367 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001368 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001369 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001370 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001371 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001372 >
Marshall Clow82513342016-09-24 22:45:42 +00001373 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001374 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001375 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001376 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1377 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Nikolas Klauser02327072022-10-29 21:04:35 +02001378
1379 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1380 replace(const_iterator __i1, const_iterator __i2, const basic_string& __str) {
1381 return replace(
1382 static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __str.data(), __str.size());
1383 }
Marshall Clowe46031a2018-07-02 18:41:15 +00001384
1385 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001386 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001387 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001388 <
1389 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1390 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001391 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001392 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1393
Nikolas Klauser02327072022-10-29 21:04:35 +02001394 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1395 replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n) {
1396 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
1397 }
1398
1399 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1400 replace(const_iterator __i1, const_iterator __i2, const value_type* __s) {
1401 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
1402 }
1403
1404 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1405 replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c) {
1406 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
1407 }
1408
Howard Hinnantc51e1022010-05-11 19:42:16 +00001409 template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001410 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001411 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001413 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001415 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001416 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001417#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001418 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant990d6e82010-11-17 21:11:40 +00001419 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001421#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001423 _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 +02001424
1425 // TODO: Maybe don't pass in the allocator. See https://llvm.org/PR57190
1426#if _LIBCPP_STD_VER <= 20
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001427 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1b531cf2022-08-09 13:17:30 +02001428 basic_string substr(size_type __pos = 0, size_type __n = npos) const {
1429 return basic_string(*this, __pos, __n, __alloc());
1430 }
1431#else
1432 _LIBCPP_HIDE_FROM_ABI constexpr
1433 basic_string substr(size_type __pos = 0, size_type __n = npos) const& {
1434 return basic_string(*this, __pos, __n, __alloc());
1435 }
1436
1437 _LIBCPP_HIDE_FROM_ABI constexpr
1438 basic_string substr(size_type __pos = 0, size_type __n = npos) && {
1439 return basic_string(std::move(*this), __pos, __n, __alloc());
1440 }
1441#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001443 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001444 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001445#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001446 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001447#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001448 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001449 __is_nothrow_swappable<allocator_type>::value);
1450#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001452 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001453 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001454 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001455 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001456#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001457 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001458 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001459#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001461 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001462 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001464 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001465 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001466
1467 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001468 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001469 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001470 <
1471 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1472 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001473 >
zoecarver1997e0a2021-02-05 11:54:47 -08001474 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001475 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001476 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001477 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001478 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001479 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001481 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001482 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001483
1484 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001485 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001486 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001487 <
1488 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1489 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001490 >
zoecarver1997e0a2021-02-05 11:54:47 -08001491 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001492 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001493 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001494 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001495 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001496 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001498 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001499 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001500
1501 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001502 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001503 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001504 <
1505 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1506 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001507 >
zoecarver1997e0a2021-02-05 11:54:47 -08001508 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001509 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001510 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001511 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001512 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001513 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001514 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001516 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001517 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001518
1519 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001520 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001521 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001522 <
1523 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1524 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001525 >
zoecarver1997e0a2021-02-05 11:54:47 -08001526 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001527 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001528 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001529 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001530 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001531 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001534 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001535 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001536
1537 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001538 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001539 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001540 <
1541 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1542 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001543 >
zoecarver1997e0a2021-02-05 11:54:47 -08001544 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001545 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001546 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 +02001547 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001548 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001549 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001550 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1551
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001552 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001553 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001554
1555 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001556 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001557 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001558 <
1559 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1560 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001561 >
zoecarver1997e0a2021-02-05 11:54:47 -08001562 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001563 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001564 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 +02001565 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001566 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001567 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001568 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1569
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001570 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001571 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001572
1573 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001574 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001575 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001576 <
1577 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1578 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001579 >
zoecarver1997e0a2021-02-05 11:54:47 -08001580 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001581
1582 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001583 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001584 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001585 <
1586 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1587 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001588 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001589 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1590
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001591 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001593 _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001594 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1595 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001596
Marshall Clow82513342016-09-24 22:45:42 +00001597 template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001598 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001599 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001600 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001601 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001602 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001603 >
Marshall Clow82513342016-09-24 22:45:42 +00001604 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 +02001605 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const value_type* __s) const _NOEXCEPT;
1606 _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1607 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001608 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609
Marshall Clow18c293b2017-12-04 20:11:38 +00001610#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001611 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001612 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001613 { return __self_view(data(), size()).starts_with(__sv); }
1614
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001615 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001616 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001617 { return !empty() && _Traits::eq(front(), __c); }
1618
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001619 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001620 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001621 { return starts_with(__self_view(__s)); }
1622
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001623 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001624 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001625 { return __self_view(data(), size()).ends_with( __sv); }
1626
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001627 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001628 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001629 { return !empty() && _Traits::eq(back(), __c); }
1630
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001631 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001632 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001633 { return ends_with(__self_view(__s)); }
1634#endif
1635
Wim Leflere023c3542021-01-19 14:33:30 -05001636#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001637 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001638 bool contains(__self_view __sv) const noexcept
1639 { return __self_view(data(), size()).contains(__sv); }
1640
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001641 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001642 bool contains(value_type __c) const noexcept
1643 { return __self_view(data(), size()).contains(__c); }
1644
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001645 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001646 bool contains(const value_type* __s) const
1647 { return __self_view(data(), size()).contains(__s); }
1648#endif
1649
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001650 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001651
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001652 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001653
Louis Dionne510450b2022-04-01 16:38:30 -04001654#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001655
1656 bool __dereferenceable(const const_iterator* __i) const;
1657 bool __decrementable(const const_iterator* __i) const;
1658 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1659 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1660
Louis Dionne510450b2022-04-01 16:38:30 -04001661#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001662
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001664 template<class _Alloc>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001665 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001666 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1667 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1668
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001669 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001670
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001671 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001672 bool __is_long() const _NOEXCEPT {
1673 if (__libcpp_is_constant_evaluated())
1674 return true;
1675 return __r_.first().__s.__is_long_;
1676 }
1677
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001678 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __begin_lifetime(pointer __begin, size_type __n) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001679#if _LIBCPP_STD_VER > 17
1680 if (__libcpp_is_constant_evaluated()) {
1681 for (size_type __i = 0; __i != __n; ++__i)
1682 std::construct_at(std::addressof(__begin[__i]));
1683 }
1684#else
1685 (void)__begin;
1686 (void)__n;
1687#endif // _LIBCPP_STD_VER > 17
1688 }
1689
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001690 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __default_init() {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02001691 __r_.first() = __rep();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001692 if (__libcpp_is_constant_evaluated()) {
1693 size_type __sz = __recommend(0) + 1;
1694 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1695 __begin_lifetime(__ptr, __sz);
1696 __set_long_pointer(__ptr);
1697 __set_long_cap(__sz);
1698 __set_long_size(0);
1699 }
1700 }
1701
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001702 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __deallocate_constexpr() {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001703 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1704 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1705 }
1706
Nikolas Klauser93826d12022-01-04 17:24:03 +01001707 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1708 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1709 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1710 }
1711
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001712 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001713 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001714 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1715 size_type __sz = size();
1716 size_type __cap = capacity();
1717 value_type* __p;
1718 if (__cap - __sz >= __n)
1719 {
1720 __p = std::__to_address(__get_pointer());
1721 size_type __n_move = __sz - __ip;
1722 if (__n_move != 0)
1723 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1724 }
1725 else
1726 {
1727 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1728 __p = std::__to_address(__get_long_pointer());
1729 }
1730 __sz += __n;
1731 __set_size(__sz);
1732 traits_type::assign(__p[__sz], value_type());
1733 for (__p += __ip; __first != __last; ++__p, ++__first)
1734 traits_type::assign(*__p, *__first);
1735
1736 return begin() + __ip;
1737 }
1738
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001739 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001740 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001742 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001743 void __set_short_size(size_type __s) _NOEXCEPT {
1744 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1745 __r_.first().__s.__size_ = __s;
1746 __r_.first().__s.__is_long_ = false;
1747 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001748
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001749 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001750 size_type __get_short_size() const _NOEXCEPT {
1751 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1752 return __r_.first().__s.__size_;
1753 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001754
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001755 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001756 void __set_long_size(size_type __s) _NOEXCEPT
1757 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001758 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001759 size_type __get_long_size() const _NOEXCEPT
1760 {return __r_.first().__l.__size_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001761 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001762 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1764
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001765 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001766 void __set_long_cap(size_type __s) _NOEXCEPT {
1767 __r_.first().__l.__cap_ = __s / __endian_factor;
1768 __r_.first().__l.__is_long_ = true;
1769 }
1770
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001771 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser62060be2022-04-20 10:52:04 +02001772 size_type __get_long_cap() const _NOEXCEPT {
1773 return __r_.first().__l.__cap_ * __endian_factor;
1774 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001776 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001777 void __set_long_pointer(pointer __p) _NOEXCEPT
1778 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001779 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001780 pointer __get_long_pointer() _NOEXCEPT
1781 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001782 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001783 const_pointer __get_long_pointer() const _NOEXCEPT
1784 {return __r_.first().__l.__data_;}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001785 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001786 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001787 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001788 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001789 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001790 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001791 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001792 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001794 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001795 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1797
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798 template <size_type __a> static
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001799 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea382952013-08-14 18:00:20 +00001800 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001801 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 enum {__alignment = 16};
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001803 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001804 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001805 {
1806 if (__s < __min_cap) {
1807 if (__libcpp_is_constant_evaluated())
1808 return static_cast<size_type>(__min_cap);
1809 else
1810 return static_cast<size_type>(__min_cap) - 1;
1811 }
Marshall Clow80584522018-02-07 21:30:17 +00001812 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1813 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1814 if (__guess == __min_cap) ++__guess;
1815 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001816 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001818 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001819 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001820 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantd17880b2013-06-28 16:59:19 +00001821 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001822 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001824
Martijn Vels5e7c9752020-03-04 17:52:46 -05001825 // Slow path for the (inlined) copy constructor for 'long' strings.
1826 // Always externally instantiated and not inlined.
1827 // Requires that __s is zero terminated.
1828 // The main reason for this function to exist is because for unstable, we
1829 // want to allow inlining of the copy constructor. However, we don't want
1830 // to call the __init() functions as those are marked as inline which may
1831 // result in over-aggressive inlining by the compiler, where our aim is
1832 // to only inline the fast path code directly in the ctor.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001833 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001834
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835 template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001836 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001837 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001839 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1840 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841 __init(_InputIterator __first, _InputIterator __last);
1842
1843 template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001844 inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04001845 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001847 __is_cpp17_forward_iterator<_ForwardIterator>::value
1848 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849 __init(_ForwardIterator __first, _ForwardIterator __last);
1850
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001851 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001853 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001854 _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1856 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001857 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858
Martijn Vels596e3de2020-02-26 15:55:49 -05001859 // __assign_no_alias is invoked for assignment operations where we
1860 // have proof that the input does not alias the current instance.
1861 // For example, operator=(basic_string) performs a 'self' check.
1862 template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001863 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001864
Nikolas Klauser02327072022-10-29 21:04:35 +02001865 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_to_end(size_type __pos) {
1866 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
1867 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868
Martijn Velsa81fc792020-02-26 13:25:43 -05001869 // __erase_external_with_move is invoked for erase() invocations where
1870 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001871 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001872
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001873 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001874 void __copy_assign_alloc(const basic_string& __str)
1875 {__copy_assign_alloc(__str, integral_constant<bool,
1876 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1877
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001878 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001879 void __copy_assign_alloc(const basic_string& __str, true_type)
1880 {
Marshall Clowf258c202017-01-31 03:40:52 +00001881 if (__alloc() == __str.__alloc())
1882 __alloc() = __str.__alloc();
1883 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001884 {
Marshall Clowf258c202017-01-31 03:40:52 +00001885 if (!__str.__is_long())
1886 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001887 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001888 __alloc() = __str.__alloc();
1889 }
1890 else
1891 {
1892 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001893 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001894 __begin_lifetime(__allocation.ptr, __allocation.count);
Nikolas Klauser1821ec32022-10-01 15:37:24 +02001895 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001896 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001897 __set_long_pointer(__allocation.ptr);
1898 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001899 __set_long_size(__str.size());
1900 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001901 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001902 }
1903
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001904 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001905 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001906 {}
1907
Eric Fiselierfc92be82017-04-19 00:28:44 +00001908#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001909 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001910 void __move_assign(basic_string& __str, false_type)
1911 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001912 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant3e276872011-06-03 18:40:47 +00001913 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001914#if _LIBCPP_STD_VER > 14
1915 _NOEXCEPT;
1916#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001917 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001918#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001919#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001920
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001921 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001922 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001923 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001924 _NOEXCEPT_(
1925 !__alloc_traits::propagate_on_container_move_assignment::value ||
1926 is_nothrow_move_assignable<allocator_type>::value)
1927 {__move_assign_alloc(__str, integral_constant<bool,
1928 __alloc_traits::propagate_on_container_move_assignment::value>());}
1929
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001930 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc2734962011-09-02 20:42:31 +00001931 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001932 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1933 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001934 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001935 }
1936
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001937 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant28b24882011-12-01 20:21:04 +00001938 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001939 _NOEXCEPT
1940 {}
1941
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001942 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s);
1943 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001944
1945 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1946 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1947 pointer __p = __is_long()
1948 ? (__set_long_size(__n), __get_long_pointer())
1949 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001950 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001951 traits_type::assign(__p[__n], value_type());
1952 return *this;
1953 }
1954
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001955 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001956 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001957 __set_size(__newsz);
1958 __invalidate_iterators_past(__newsz);
1959 traits_type::assign(__p[__newsz], value_type());
1960 return *this;
1961 }
1962
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001963 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001965 template<class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001966 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001967 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001968 // assume that the ranges overlap, because we can't check during constant evaluation
1969 if (__libcpp_is_constant_evaluated())
1970 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001971 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001972 return data() <= __p && __p <= data() + size();
1973 }
1974
Louis Dionned24191c2021-08-19 12:39:16 -04001975 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1976 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001977 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001978 }
1979
1980 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1981 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001982 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001983 }
1984
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02001985 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const basic_string&);
1986 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const value_type*, const basic_string&);
1987 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(value_type, const basic_string&);
1988 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, const value_type*);
1989 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990};
1991
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001992// These declarations must appear before any functions are implicitly used
1993// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001994#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001995#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001996 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001997# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001998 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001999# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05002000#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04002001 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04002002# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04002003 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04002004# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05002005#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04002006#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05002007
2008
Louis Dionned59f8a52021-08-17 11:59:07 -04002009#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00002010template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05002011 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00002012 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04002013 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
2014 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00002015 >
2016basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
2017 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00002018
2019template<class _CharT,
2020 class _Traits,
2021 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04002022 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00002023 >
2024explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
2025 -> basic_string<_CharT, _Traits, _Allocator>;
2026
2027template<class _CharT,
2028 class _Traits,
2029 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04002030 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00002031 class _Sz = typename allocator_traits<_Allocator>::size_type
2032 >
2033basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
2034 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00002035#endif
2036
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002038inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002040basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041{
Louis Dionne510450b2022-04-01 16:38:30 -04002042#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002043 if (!__libcpp_is_constant_evaluated()) {
2044 __c_node* __c = __get_db()->__find_c_and_lock(this);
2045 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002047 const_pointer __new_last = __get_pointer() + __pos;
2048 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002050 --__p;
2051 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
2052 if (__i->base() > __new_last)
2053 {
2054 (*__p)->__c_ = nullptr;
2055 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002056 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002057 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002059 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060 }
2061 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002062#else
2063 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04002064#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065}
2066
2067template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002068_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier815ed732016-09-16 00:00:48 +00002069void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
2070 size_type __sz,
2071 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002073 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002074 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002076 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002077 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002078 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079 {
2080 __set_short_size(__sz);
2081 __p = __get_short_pointer();
2082 }
2083 else
2084 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002085 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
2086 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002087 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002088 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002089 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090 __set_long_size(__sz);
2091 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002092 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093 traits_type::assign(__p[__sz], value_type());
2094}
2095
2096template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002097_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098void
Howard Hinnantd17880b2013-06-28 16:59:19 +00002099basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002101 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002102 __r_.first() = __rep();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002104 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002106 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107 {
2108 __set_short_size(__sz);
2109 __p = __get_short_pointer();
2110 }
2111 else
2112 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002113 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2114 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002115 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002117 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118 __set_long_size(__sz);
2119 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002120 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 traits_type::assign(__p[__sz], value_type());
2122}
2123
2124template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002125template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002126_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002127basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002128 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002130 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002132 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133}
2134
2135template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002136_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002138 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139{
2140 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002141 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002143 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002144 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002145 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146}
2147
2148template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002149_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002150basic_string<_CharT, _Traits, _Allocator>::basic_string(
2151 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002152 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153{
2154 if (!__str.__is_long())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002155 __r_.first() = __str.__r_.first();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002157 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002158 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002159 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160}
2161
Martijn Vels5e7c9752020-03-04 17:52:46 -05002162template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002163_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Vels5e7c9752020-03-04 17:52:46 -05002164void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2165 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002166 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002167 __r_.first() = __rep();
2168
Martijn Vels5e7c9752020-03-04 17:52:46 -05002169 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002170 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002171 __p = __get_short_pointer();
2172 __set_short_size(__sz);
2173 } else {
2174 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002175 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002176 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2177 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002178 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002179 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002180 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002181 __set_long_size(__sz);
2182 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002183 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002184}
2185
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002187_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188void
2189basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2190{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002191 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002192 __r_.first() = __rep();
2193
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002195 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002197 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198 {
2199 __set_short_size(__n);
2200 __p = __get_short_pointer();
2201 }
2202 else
2203 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002204 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2205 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002206 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002208 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209 __set_long_size(__n);
2210 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002211 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 traits_type::assign(__p[__n], value_type());
2213}
2214
2215template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002216template <class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002217_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002218basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002219 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220{
2221 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002222 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223}
2224
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002226_LIBCPP_CONSTEXPR_SINCE_CXX20
Eric Fiselier812882b2017-02-17 01:17:10 +00002227basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2228 size_type __pos, size_type __n,
2229 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002230 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231{
2232 size_type __str_sz = __str.size();
2233 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002234 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002235 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2236 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237}
2238
2239template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002240template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002241_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clow78dbe462016-11-14 18:22:19 +00002242basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002243 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002244 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002245{
Marshall Clowe46031a2018-07-02 18:41:15 +00002246 __self_view __sv0 = __t;
2247 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002248 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002249 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002250}
2251
2252template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002253template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002254_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002255basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002256 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002257{
Marshall Clowe46031a2018-07-02 18:41:15 +00002258 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002259 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002260 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002261}
2262
2263template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002264template <class _Tp, class>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002265_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowe46031a2018-07-02 18:41:15 +00002266basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002267 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002268{
Marshall Clowe46031a2018-07-02 18:41:15 +00002269 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002270 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002271 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002272}
2273
2274template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275template <class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002276_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002277__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002279 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2280>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2282{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002283 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284#ifndef _LIBCPP_NO_EXCEPTIONS
2285 try
2286 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002287#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 for (; __first != __last; ++__first)
2289 push_back(*__first);
2290#ifndef _LIBCPP_NO_EXCEPTIONS
2291 }
2292 catch (...)
2293 {
2294 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002295 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 throw;
2297 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002298#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299}
2300
2301template <class _CharT, class _Traits, class _Allocator>
2302template <class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002303_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002304__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002306 __is_cpp17_forward_iterator<_ForwardIterator>::value
2307>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2309{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002310 if (__libcpp_is_constant_evaluated())
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002311 __r_.first() = __rep();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002312 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002314 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002316 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317 {
2318 __set_short_size(__sz);
2319 __p = __get_short_pointer();
2320 }
2321 else
2322 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002323 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2324 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002325 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002327 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328 __set_long_size(__sz);
2329 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002330
2331#ifndef _LIBCPP_NO_EXCEPTIONS
2332 try
2333 {
2334#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002335 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 traits_type::assign(*__p, *__first);
2337 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002338#ifndef _LIBCPP_NO_EXCEPTIONS
2339 }
2340 catch (...)
2341 {
2342 if (__is_long())
2343 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2344 throw;
2345 }
2346#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347}
2348
2349template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002350_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2352{
Nikolas Klauser98833542022-05-07 22:20:23 +02002353 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002355 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356}
2357
2358template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002359_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360void
2361basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2362 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002363 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 +00002364{
2365 size_type __ms = max_size();
2366 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002367 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368 pointer __old_p = __get_pointer();
2369 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002370 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002372 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2373 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002374 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002375 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002376 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002377 traits_type::copy(std::__to_address(__p),
2378 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002380 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2382 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002383 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2384 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002385 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002386 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002388 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2390 __set_long_size(__old_sz);
2391 traits_type::assign(__p[__old_sz], value_type());
2392}
2393
2394template <class _CharT, class _Traits, class _Allocator>
2395void
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002396_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2398 size_type __n_copy, size_type __n_del, size_type __n_add)
2399{
2400 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002401 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002402 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403 pointer __old_p = __get_pointer();
2404 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002405 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002406 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002407 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2408 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002409 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002410 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002412 traits_type::copy(std::__to_address(__p),
2413 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2415 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002416 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2417 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002418 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002419 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2420 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002422 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423}
2424
2425// assign
2426
2427template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002428template <bool __is_short>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002429_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsb6a08b62020-04-10 18:36:31 -04002430basic_string<_CharT, _Traits, _Allocator>&
2431basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002432 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002433 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002434 if (__n < __cap) {
2435 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2436 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002437 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002438 traits_type::assign(__p[__n], value_type());
2439 __invalidate_iterators_past(__n);
2440 } else {
2441 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2442 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2443 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002444 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002445}
2446
2447template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002448_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002450basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2451 const value_type* __s, size_type __n) {
2452 size_type __cap = capacity();
2453 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002454 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002455 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002456 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002457 } else {
2458 size_type __sz = size();
2459 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002460 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002461 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002462}
2463
2464template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002465_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002466basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002467basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002469 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002470 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002471 ? __assign_short(__s, __n)
2472 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473}
2474
2475template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002476_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477basic_string<_CharT, _Traits, _Allocator>&
2478basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2479{
2480 size_type __cap = capacity();
2481 if (__cap < __n)
2482 {
2483 size_type __sz = size();
2484 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2485 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002486 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002488 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489}
2490
2491template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002492_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493basic_string<_CharT, _Traits, _Allocator>&
2494basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2495{
2496 pointer __p;
2497 if (__is_long())
2498 {
2499 __p = __get_long_pointer();
2500 __set_long_size(1);
2501 }
2502 else
2503 {
2504 __p = __get_short_pointer();
2505 __set_short_size(1);
2506 }
2507 traits_type::assign(*__p, __c);
2508 traits_type::assign(*++__p, value_type());
2509 __invalidate_iterators_past(1);
2510 return *this;
2511}
2512
2513template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002514_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002515basic_string<_CharT, _Traits, _Allocator>&
2516basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2517{
Martijn Vels596e3de2020-02-26 15:55:49 -05002518 if (this != &__str) {
2519 __copy_assign_alloc(__str);
2520 if (!__is_long()) {
2521 if (!__str.__is_long()) {
Nikolas Klauser8577eec2022-08-30 17:43:14 +02002522 __r_.first() = __str.__r_.first();
Martijn Vels596e3de2020-02-26 15:55:49 -05002523 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002524 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002525 }
2526 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002527 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002528 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002529 }
2530 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002531}
2532
Eric Fiselierfc92be82017-04-19 00:28:44 +00002533#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002534
2535template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002536inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002537void
2538basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002539 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002540{
2541 if (__alloc() != __str.__alloc())
2542 assign(__str);
2543 else
2544 __move_assign(__str, true_type());
2545}
2546
2547template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002548inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002549void
2550basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002551#if _LIBCPP_STD_VER > 14
2552 _NOEXCEPT
2553#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002554 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002555#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002556{
marshall2ed41622019-11-27 07:13:00 -08002557 if (__is_long()) {
2558 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2559 __get_long_cap());
2560#if _LIBCPP_STD_VER <= 14
2561 if (!is_nothrow_move_assignable<allocator_type>::value) {
2562 __set_short_size(0);
2563 traits_type::assign(__get_short_pointer()[0], value_type());
2564 }
2565#endif
2566 }
2567 __move_assign_alloc(__str);
2568 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002569 if (__libcpp_is_constant_evaluated()) {
2570 __str.__default_init();
2571 } else {
2572 __str.__set_short_size(0);
2573 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2574 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002575}
2576
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002577#endif
2578
2579template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002581_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002582__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002584 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002586>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2588{
Marshall Clow958362f2016-09-05 01:54:30 +00002589 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002590 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002591 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592}
2593
2594template <class _CharT, class _Traits, class _Allocator>
2595template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002596_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002597__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002599 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002601>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2603{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002605 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002606 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002607
2608 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2609 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002611 if (__cap < __n)
2612 {
2613 size_type __sz = size();
2614 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2615 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002616 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002617 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002618 traits_type::assign(*__p, *__first);
2619 traits_type::assign(*__p, value_type());
2620 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002621 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 }
2623 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002624 {
2625 const basic_string __temp(__first, __last, __alloc());
2626 assign(__temp.data(), __temp.size());
2627 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 return *this;
2629}
2630
2631template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002632_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633basic_string<_CharT, _Traits, _Allocator>&
2634basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2635{
2636 size_type __sz = __str.size();
2637 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002638 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002639 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640}
2641
2642template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002643template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002644_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002645__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002646<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002647 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2648 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002649 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002650>
Marshall Clow82513342016-09-24 22:45:42 +00002651basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002652{
Marshall Clow82513342016-09-24 22:45:42 +00002653 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002654 size_type __sz = __sv.size();
2655 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002656 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002657 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002658}
2659
2660
2661template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002662_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002664basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2665 return __assign_external(__s, traits_type::length(__s));
2666}
2667
2668template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002669_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsda7d94f2020-06-19 14:24:03 -04002670basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002671basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002673 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002674 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002675 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002676 ? __assign_short(__s, traits_type::length(__s))
2677 : __assign_external(__s, traits_type::length(__s)))
2678 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680// append
2681
2682template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002683_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002685basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002687 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688 size_type __cap = capacity();
2689 size_type __sz = size();
2690 if (__cap - __sz >= __n)
2691 {
2692 if (__n)
2693 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002694 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 traits_type::copy(__p + __sz, __s, __n);
2696 __sz += __n;
2697 __set_size(__sz);
2698 traits_type::assign(__p[__sz], value_type());
2699 }
2700 }
2701 else
2702 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2703 return *this;
2704}
2705
2706template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002707_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708basic_string<_CharT, _Traits, _Allocator>&
2709basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2710{
2711 if (__n)
2712 {
2713 size_type __cap = capacity();
2714 size_type __sz = size();
2715 if (__cap - __sz < __n)
2716 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2717 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002718 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719 __sz += __n;
2720 __set_size(__sz);
2721 traits_type::assign(__p[__sz], value_type());
2722 }
2723 return *this;
2724}
2725
2726template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002727_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002728basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2729{
2730 if (__n)
2731 {
2732 size_type __cap = capacity();
2733 size_type __sz = size();
2734 if (__cap - __sz < __n)
2735 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2736 pointer __p = __get_pointer();
2737 __sz += __n;
2738 __set_size(__sz);
2739 traits_type::assign(__p[__sz], value_type());
2740 }
2741}
2742
2743template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002744_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745void
2746basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2747{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002748 bool __is_short = !__is_long();
2749 size_type __cap;
2750 size_type __sz;
2751 if (__is_short)
2752 {
2753 __cap = __min_cap - 1;
2754 __sz = __get_short_size();
2755 }
2756 else
2757 {
2758 __cap = __get_long_cap() - 1;
2759 __sz = __get_long_size();
2760 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002762 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002764 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002765 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002766 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002767 if (__is_short)
2768 {
2769 __p = __get_short_pointer() + __sz;
2770 __set_short_size(__sz+1);
2771 }
2772 else
2773 {
2774 __p = __get_long_pointer() + __sz;
2775 __set_long_size(__sz+1);
2776 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777 traits_type::assign(*__p, __c);
2778 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779}
2780
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781template <class _CharT, class _Traits, class _Allocator>
2782template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002783_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002784__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002785<
2786 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2787 basic_string<_CharT, _Traits, _Allocator>&
2788>
2789basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002790 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791{
2792 size_type __sz = size();
2793 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002794 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795 if (__n)
2796 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002797 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2798 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002799 {
2800 if (__cap - __sz < __n)
2801 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2802 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002803 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002804 traits_type::assign(*__p, *__first);
2805 traits_type::assign(*__p, value_type());
2806 __set_size(__sz + __n);
2807 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002808 else
2809 {
2810 const basic_string __temp(__first, __last, __alloc());
2811 append(__temp.data(), __temp.size());
2812 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 }
2814 return *this;
2815}
2816
2817template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002818_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819basic_string<_CharT, _Traits, _Allocator>&
2820basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2821{
2822 size_type __sz = __str.size();
2823 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002824 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002825 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826}
2827
2828template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002829template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002830_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002831 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002832 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002833 __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 +00002834 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002835 >
Marshall Clow82513342016-09-24 22:45:42 +00002836basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002837{
Marshall Clow82513342016-09-24 22:45:42 +00002838 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002839 size_type __sz = __sv.size();
2840 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002841 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002842 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002843}
2844
2845template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002846_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002848basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002850 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851 return append(__s, traits_type::length(__s));
2852}
2853
2854// insert
2855
2856template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002857_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002859basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002861 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862 size_type __sz = size();
2863 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002864 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002866 if (__libcpp_is_constant_evaluated()) {
2867 if (__cap - __sz >= __n)
2868 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2869 else
2870 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2871 return *this;
2872 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 if (__cap - __sz >= __n)
2874 {
2875 if (__n)
2876 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002877 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 size_type __n_move = __sz - __pos;
2879 if (__n_move != 0)
2880 {
2881 if (__p + __pos <= __s && __s < __p + __sz)
2882 __s += __n;
2883 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2884 }
2885 traits_type::move(__p + __pos, __s, __n);
2886 __sz += __n;
2887 __set_size(__sz);
2888 traits_type::assign(__p[__sz], value_type());
2889 }
2890 }
2891 else
2892 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2893 return *this;
2894}
2895
2896template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002897_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898basic_string<_CharT, _Traits, _Allocator>&
2899basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2900{
2901 size_type __sz = size();
2902 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002903 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904 if (__n)
2905 {
2906 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002907 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 if (__cap - __sz >= __n)
2909 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002910 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 size_type __n_move = __sz - __pos;
2912 if (__n_move != 0)
2913 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2914 }
2915 else
2916 {
2917 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002918 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919 }
2920 traits_type::assign(__p + __pos, __n, __c);
2921 __sz += __n;
2922 __set_size(__sz);
2923 traits_type::assign(__p[__sz], value_type());
2924 }
2925 return *this;
2926}
2927
2928template <class _CharT, class _Traits, class _Allocator>
2929template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002930_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002931__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002933 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002934 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002935>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2937{
Nikolas Klausereed25832021-12-15 01:32:30 +01002938 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2939 "string::insert(iterator, range) called with an iterator not"
2940 " referring to this string");
2941 const basic_string __temp(__first, __last, __alloc());
2942 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943}
2944
2945template <class _CharT, class _Traits, class _Allocator>
2946template<class _ForwardIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002947_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002948__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002950 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002952>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2954{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002955 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2956 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002957
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002959 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2960 if (__n == 0)
2961 return begin() + __ip;
2962
2963 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002965 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002967 else
2968 {
2969 const basic_string __temp(__first, __last, __alloc());
2970 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2971 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972}
2973
2974template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002975_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976basic_string<_CharT, _Traits, _Allocator>&
2977basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2978 size_type __pos2, size_type __n)
2979{
2980 size_type __str_sz = __str.size();
2981 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002982 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002983 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984}
2985
2986template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002987template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02002988_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04002989__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002990<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002991 __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 +00002992 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002993>
Marshall Clow82513342016-09-24 22:45:42 +00002994basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002995 size_type __pos2, size_type __n)
2996{
Marshall Clow82513342016-09-24 22:45:42 +00002997 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002998 size_type __str_sz = __sv.size();
2999 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003000 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003001 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003002}
3003
3004template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003005_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003007basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003008{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003009 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010 return insert(__pos, __s, traits_type::length(__s));
3011}
3012
3013template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003014_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015typename basic_string<_CharT, _Traits, _Allocator>::iterator
3016basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
3017{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05003018 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3019 "string::insert(iterator, character) called with an iterator not"
3020 " referring to this string");
3021
Howard Hinnantc51e1022010-05-11 19:42:16 +00003022 size_type __ip = static_cast<size_type>(__pos - begin());
3023 size_type __sz = size();
3024 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003025 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003026 if (__cap == __sz)
3027 {
3028 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003029 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003030 }
3031 else
3032 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003033 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034 size_type __n_move = __sz - __ip;
3035 if (__n_move != 0)
3036 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3037 }
3038 traits_type::assign(__p[__ip], __c);
3039 traits_type::assign(__p[++__sz], value_type());
3040 __set_size(__sz);
3041 return begin() + static_cast<difference_type>(__ip);
3042}
3043
Howard Hinnantc51e1022010-05-11 19:42:16 +00003044// replace
3045
3046template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003047_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003049basic_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 +00003050 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003052 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003053 size_type __sz = size();
3054 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003055 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003056 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003057 size_type __cap = capacity();
3058 if (__cap - __sz + __n1 >= __n2)
3059 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003060 if (__libcpp_is_constant_evaluated()) {
3061 __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3062 return *this;
3063 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003064 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065 if (__n1 != __n2)
3066 {
3067 size_type __n_move = __sz - __pos - __n1;
3068 if (__n_move != 0)
3069 {
3070 if (__n1 > __n2)
3071 {
3072 traits_type::move(__p + __pos, __s, __n2);
3073 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003074 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075 }
3076 if (__p + __pos < __s && __s < __p + __sz)
3077 {
3078 if (__p + __pos + __n1 <= __s)
3079 __s += __n2 - __n1;
3080 else // __p + __pos < __s < __p + __pos + __n1
3081 {
3082 traits_type::move(__p + __pos, __s, __n1);
3083 __pos += __n1;
3084 __s += __n2;
3085 __n2 -= __n1;
3086 __n1 = 0;
3087 }
3088 }
3089 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3090 }
3091 }
3092 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003093 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094 }
3095 else
3096 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3097 return *this;
3098}
3099
3100template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003101_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102basic_string<_CharT, _Traits, _Allocator>&
3103basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3104{
3105 size_type __sz = size();
3106 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003107 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003108 __n1 = std::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003110 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111 if (__cap - __sz + __n1 >= __n2)
3112 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003113 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114 if (__n1 != __n2)
3115 {
3116 size_type __n_move = __sz - __pos - __n1;
3117 if (__n_move != 0)
3118 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3119 }
3120 }
3121 else
3122 {
3123 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003124 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125 }
3126 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003127 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128}
3129
3130template <class _CharT, class _Traits, class _Allocator>
3131template<class _InputIterator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003132_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003133__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003135 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003137>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003138basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139 _InputIterator __j1, _InputIterator __j2)
3140{
Marshall Clow958362f2016-09-05 01:54:30 +00003141 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003142 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143}
3144
3145template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003146_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147basic_string<_CharT, _Traits, _Allocator>&
3148basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3149 size_type __pos2, size_type __n2)
3150{
3151 size_type __str_sz = __str.size();
3152 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003153 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003154 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155}
3156
3157template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003158template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003159_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003160__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003161<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003162 __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 +00003163 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003164>
Marshall Clow82513342016-09-24 22:45:42 +00003165basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003166 size_type __pos2, size_type __n2)
3167{
Marshall Clow82513342016-09-24 22:45:42 +00003168 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003169 size_type __str_sz = __sv.size();
3170 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003171 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003172 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003173}
3174
3175template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003176_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003177basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003178basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003180 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003181 return replace(__pos, __n1, __s, traits_type::length(__s));
3182}
3183
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184// erase
3185
Martijn Velsa81fc792020-02-26 13:25:43 -05003186// 'externally instantiated' erase() implementation, called when __n != npos.
3187// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003189_LIBCPP_CONSTEXPR_SINCE_CXX20
Martijn Velsa81fc792020-02-26 13:25:43 -05003190void
3191basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3192 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194 if (__n)
3195 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003196 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003197 value_type* __p = std::__to_address(__get_pointer());
3198 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003199 size_type __n_move = __sz - __pos - __n;
3200 if (__n_move != 0)
3201 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003202 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003204}
3205
3206template <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 -05003208basic_string<_CharT, _Traits, _Allocator>&
3209basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3210 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003211 if (__pos > size())
3212 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003213 if (__n == npos) {
3214 __erase_to_end(__pos);
3215 } else {
3216 __erase_external_with_move(__pos, __n);
3217 }
3218 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219}
3220
3221template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003222inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223typename basic_string<_CharT, _Traits, _Allocator>::iterator
3224basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3225{
Nikolas Klausereed25832021-12-15 01:32:30 +01003226 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3227 "string::erase(iterator) called with an iterator not"
3228 " referring to this string");
3229
3230 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3231 iterator __b = begin();
3232 size_type __r = static_cast<size_type>(__pos - __b);
3233 erase(__r, 1);
3234 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235}
3236
3237template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003238inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239typename basic_string<_CharT, _Traits, _Allocator>::iterator
3240basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3241{
Nikolas Klausereed25832021-12-15 01:32:30 +01003242 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3243 "string::erase(iterator, iterator) called with an iterator not"
3244 " referring to this string");
3245
3246 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3247 iterator __b = begin();
3248 size_type __r = static_cast<size_type>(__first - __b);
3249 erase(__r, static_cast<size_type>(__last - __first));
3250 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251}
3252
3253template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003254inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255void
3256basic_string<_CharT, _Traits, _Allocator>::pop_back()
3257{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003258 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003259 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260}
3261
3262template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003263inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003265basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003267 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268 if (__is_long())
3269 {
3270 traits_type::assign(*__get_long_pointer(), value_type());
3271 __set_long_size(0);
3272 }
3273 else
3274 {
3275 traits_type::assign(*__get_short_pointer(), value_type());
3276 __set_short_size(0);
3277 }
3278}
3279
3280template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003281_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282void
3283basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3284{
3285 size_type __sz = size();
3286 if (__n > __sz)
3287 append(__n - __sz, __c);
3288 else
3289 __erase_to_end(__n);
3290}
3291
3292template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003293_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003294basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3295{
3296 size_type __sz = size();
3297 if (__n > __sz) {
3298 __append_default_init(__n - __sz);
3299 } else
3300 __erase_to_end(__n);
3301}
3302
3303template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003304_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305void
Marek Kurdejc9848142020-11-26 10:07:16 +01003306basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307{
Marek Kurdejc9848142020-11-26 10:07:16 +01003308 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003309 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003310
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003311 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3312 // and later (since P0966R1), however we provide consistent behavior in all Standard
3313 // modes because this function is instantiated in the shared library.
3314 if (__requested_capacity <= capacity())
3315 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003316
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003317 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003318 __target_capacity = __recommend(__target_capacity);
3319 if (__target_capacity == capacity()) return;
3320
3321 __shrink_or_extend(__target_capacity);
3322}
3323
3324template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003325inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003326void
3327basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3328{
3329 size_type __target_capacity = __recommend(size());
3330 if (__target_capacity == capacity()) return;
3331
3332 __shrink_or_extend(__target_capacity);
3333}
3334
3335template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003336inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marek Kurdejc9848142020-11-26 10:07:16 +01003337void
3338basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3339{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340 size_type __cap = capacity();
3341 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003342
3343 pointer __new_data, __p;
3344 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003345 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003347 __was_long = true;
3348 __now_long = false;
3349 __new_data = __get_short_pointer();
3350 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003352 else
3353 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003354 if (__target_capacity > __cap) {
3355 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3356 __new_data = __allocation.ptr;
3357 __target_capacity = __allocation.count - 1;
3358 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003359 else
3360 {
3361 #ifndef _LIBCPP_NO_EXCEPTIONS
3362 try
3363 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003364 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003365 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3366 __new_data = __allocation.ptr;
3367 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003368 #ifndef _LIBCPP_NO_EXCEPTIONS
3369 }
3370 catch (...)
3371 {
3372 return;
3373 }
3374 #else // _LIBCPP_NO_EXCEPTIONS
3375 if (__new_data == nullptr)
3376 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003377 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003378 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003379 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003380 __now_long = true;
3381 __was_long = __is_long();
3382 __p = __get_pointer();
3383 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003384 traits_type::copy(std::__to_address(__new_data),
3385 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003386 if (__was_long)
3387 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3388 if (__now_long)
3389 {
3390 __set_long_cap(__target_capacity+1);
3391 __set_long_size(__sz);
3392 __set_long_pointer(__new_data);
3393 }
3394 else
3395 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003396 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397}
3398
3399template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003400_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3402basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3403{
3404 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003405 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003406 return (*this)[__n];
3407}
3408
3409template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003410_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411typename basic_string<_CharT, _Traits, _Allocator>::reference
3412basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3413{
3414 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003415 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416 return (*this)[__n];
3417}
3418
3419template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003420_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003421typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003422basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423{
3424 size_type __sz = size();
3425 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003426 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003427 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428 traits_type::copy(__s, data() + __pos, __rlen);
3429 return __rlen;
3430}
3431
3432template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003433inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434void
3435basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003436#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003437 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003438#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003439 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003440 __is_nothrow_swappable<allocator_type>::value)
3441#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442{
Nikolas Klauser98833542022-05-07 22:20:23 +02003443 if (!__is_long())
3444 std::__debug_db_invalidate_all(this);
3445 if (!__str.__is_long())
3446 std::__debug_db_invalidate_all(&__str);
3447 std::__debug_db_swap(this, &__str);
3448
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003449 _LIBCPP_ASSERT(
3450 __alloc_traits::propagate_on_container_swap::value ||
3451 __alloc_traits::is_always_equal::value ||
3452 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003453 std::swap(__r_.first(), __str.__r_.first());
3454 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455}
3456
3457// find
3458
3459template <class _Traits>
3460struct _LIBCPP_HIDDEN __traits_eq
3461{
3462 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003463 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003464 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3465 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466};
3467
3468template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003469_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003470typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003471basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003472 size_type __pos,
3473 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003475 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003476 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003477 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003478}
3479
3480template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003481inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003483basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3484 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003486 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003487 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488}
3489
3490template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003491template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003492_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003493__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003494<
3495 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3496 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003497>
Marshall Clowe46031a2018-07-02 18:41:15 +00003498basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003499 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003500{
Marshall Clowe46031a2018-07-02 18:41:15 +00003501 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003502 return __str_find<value_type, size_type, traits_type, npos>
3503 (data(), size(), __sv.data(), __pos, __sv.size());
3504}
3505
3506template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003507inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003508typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003509basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003510 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003512 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003513 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003514 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515}
3516
3517template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003518_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003520basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3521 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003523 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003524 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525}
3526
3527// rfind
3528
3529template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003530_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003532basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003533 size_type __pos,
3534 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003536 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003537 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003538 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539}
3540
3541template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003542inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003544basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3545 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003547 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003548 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549}
3550
3551template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003552template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003553_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003554__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003555<
3556 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3557 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003558>
Marshall Clowe46031a2018-07-02 18:41:15 +00003559basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003560 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003561{
Marshall Clowe46031a2018-07-02 18:41:15 +00003562 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003563 return __str_rfind<value_type, size_type, traits_type, npos>
3564 (data(), size(), __sv.data(), __pos, __sv.size());
3565}
3566
3567template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003568inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003569typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003570basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003571 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003573 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003574 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003575 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576}
3577
3578template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003579_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003581basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3582 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003584 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003585 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586}
3587
3588// find_first_of
3589
3590template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003591_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003593basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003594 size_type __pos,
3595 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003596{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003597 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003598 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003599 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600}
3601
3602template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003603inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003605basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3606 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003608 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003609 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610}
3611
3612template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003613template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003614_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003615__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003616<
3617 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3618 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003619>
Marshall Clowe46031a2018-07-02 18:41:15 +00003620basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003621 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003622{
Marshall Clowe46031a2018-07-02 18:41:15 +00003623 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003624 return __str_find_first_of<value_type, size_type, traits_type, npos>
3625 (data(), size(), __sv.data(), __pos, __sv.size());
3626}
3627
3628template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003629inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003630typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003631basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003632 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003634 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003635 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003636 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637}
3638
3639template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003640inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003642basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3643 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644{
3645 return find(__c, __pos);
3646}
3647
3648// find_last_of
3649
3650template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003651inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003653basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003654 size_type __pos,
3655 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003657 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003658 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003659 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660}
3661
3662template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003663inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003665basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3666 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003668 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003669 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670}
3671
3672template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003673template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003674_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003675__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003676<
3677 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3678 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003679>
Marshall Clowe46031a2018-07-02 18:41:15 +00003680basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003681 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003682{
Marshall Clowe46031a2018-07-02 18:41:15 +00003683 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003684 return __str_find_last_of<value_type, size_type, traits_type, npos>
3685 (data(), size(), __sv.data(), __pos, __sv.size());
3686}
3687
3688template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003689inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003690typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003691basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003692 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003694 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003695 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003696 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697}
3698
3699template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003700inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003702basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3703 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704{
3705 return rfind(__c, __pos);
3706}
3707
3708// find_first_not_of
3709
3710template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003711_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003712typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003713basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003714 size_type __pos,
3715 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003717 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003718 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003719 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720}
3721
3722template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003723inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003725basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3726 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003728 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003729 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730}
3731
3732template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003733template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003734_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003735__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003736<
3737 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3738 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003739>
Marshall Clowe46031a2018-07-02 18:41:15 +00003740basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003741 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003742{
Marshall Clowe46031a2018-07-02 18:41:15 +00003743 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003744 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3745 (data(), size(), __sv.data(), __pos, __sv.size());
3746}
3747
3748template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003749inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003750typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003751basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003752 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003754 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003755 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003756 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003757}
3758
3759template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003760inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003762basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3763 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003765 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003766 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767}
3768
3769// find_last_not_of
3770
3771template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003772_LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003774basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003775 size_type __pos,
3776 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003778 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003779 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003780 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781}
3782
3783template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003784inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003786basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3787 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003789 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003790 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791}
3792
3793template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003794template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003795_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003796__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003797<
3798 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3799 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003800>
Marshall Clowe46031a2018-07-02 18:41:15 +00003801basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003802 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003803{
Marshall Clowe46031a2018-07-02 18:41:15 +00003804 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003805 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3806 (data(), size(), __sv.data(), __pos, __sv.size());
3807}
3808
3809template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003810inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003811typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003812basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003813 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003815 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003816 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003817 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818}
3819
3820template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003821inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003823basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3824 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003826 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003827 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828}
3829
3830// compare
3831
3832template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003833template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003834_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003835__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003836<
3837 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3838 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003839>
zoecarver1997e0a2021-02-05 11:54:47 -08003840basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003841{
Marshall Clowe46031a2018-07-02 18:41:15 +00003842 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003843 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003844 size_t __rhs_sz = __sv.size();
3845 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003846 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003847 if (__result != 0)
3848 return __result;
3849 if (__lhs_sz < __rhs_sz)
3850 return -1;
3851 if (__lhs_sz > __rhs_sz)
3852 return 1;
3853 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854}
3855
3856template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003857inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003861 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003862}
3863
3864template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003865inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003867basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3868 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003869 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003870 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003871{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003872 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873 size_type __sz = size();
3874 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003875 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003876 size_type __rlen = std::min(__n1, __sz - __pos1);
3877 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878 if (__r == 0)
3879 {
3880 if (__rlen < __n2)
3881 __r = -1;
3882 else if (__rlen > __n2)
3883 __r = 1;
3884 }
3885 return __r;
3886}
3887
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003888template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003889template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003890_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003891__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003892<
3893 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3894 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003895>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003896basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3897 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003898 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003899{
Marshall Clowe46031a2018-07-02 18:41:15 +00003900 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003901 return compare(__pos1, __n1, __sv.data(), __sv.size());
3902}
3903
3904template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003905inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003906int
3907basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3908 size_type __n1,
3909 const basic_string& __str) const
3910{
3911 return compare(__pos1, __n1, __str.data(), __str.size());
3912}
3913
3914template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003915template <class _Tp>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003916_LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne9ce598d2021-09-08 09:14:43 -04003917__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003918<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003919 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3920 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003921 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003922>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003923basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3924 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003925 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003926 size_type __pos2,
3927 size_type __n2) const
3928{
Marshall Clow82513342016-09-24 22:45:42 +00003929 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003930 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3931}
3932
3933template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003934_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003935int
3936basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3937 size_type __n1,
3938 const basic_string& __str,
3939 size_type __pos2,
3940 size_type __n2) const
3941{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003942 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003943}
3944
3945template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003946_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003947int
3948basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3949{
3950 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3951 return compare(0, npos, __s, traits_type::length(__s));
3952}
3953
3954template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003955_LIBCPP_CONSTEXPR_SINCE_CXX20
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003956int
3957basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3958 size_type __n1,
3959 const value_type* __s) const
3960{
3961 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3962 return compare(__pos1, __n1, __s, traits_type::length(__s));
3963}
3964
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965// __invariants
3966
3967template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003968inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969bool
3970basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3971{
3972 if (size() > capacity())
3973 return false;
3974 if (capacity() < __min_cap - 1)
3975 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003976 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003978 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979 return false;
3980 return true;
3981}
3982
Vedant Kumar55e007e2018-03-08 21:15:26 +00003983// __clear_and_shrink
3984
3985template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02003986inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Louis Dionne173f29e2019-05-29 16:01:36 +00003987void
Marshall Clowe60a7182018-05-29 17:04:37 +00003988basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003989{
3990 clear();
3991 if(__is_long())
3992 {
3993 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
Nikolas Klauser1821ec32022-10-01 15:37:24 +02003994 __default_init();
Vedant Kumar55e007e2018-03-08 21:15:26 +00003995 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003996}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003997
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998// operator==
3999
4000template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004001inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002bool
4003operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004004 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005{
Mark de Weverb4830e62022-07-19 07:56:23 +02004006#if _LIBCPP_STD_VER > 17
4007 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4008#else
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004009 size_t __lhs_sz = __lhs.size();
4010 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4011 __rhs.data(),
4012 __lhs_sz) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004013#endif
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004014}
4015
4016template<class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004017inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004018bool
4019operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4020 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4021{
4022 size_t __lhs_sz = __lhs.size();
4023 if (__lhs_sz != __rhs.size())
4024 return false;
4025 const char* __lp = __lhs.data();
4026 const char* __rp = __rhs.data();
4027 if (__lhs.__is_long())
4028 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4029 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4030 if (*__lp != *__rp)
4031 return false;
4032 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033}
4034
Mark de Weverb4830e62022-07-19 07:56:23 +02004035#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004037inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004039operator==(const _CharT* __lhs,
4040 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004042 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4043 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4044 size_t __lhs_len = _Traits::length(__lhs);
4045 if (__lhs_len != __rhs.size()) return false;
4046 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047}
Mark de Weverb4830e62022-07-19 07:56:23 +02004048#endif // _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004051inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004053operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4054 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055{
Mark de Weverb4830e62022-07-19 07:56:23 +02004056#if _LIBCPP_STD_VER > 17
4057 return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
4058#else
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004059 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4060 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4061 size_t __rhs_len = _Traits::length(__rhs);
4062 if (__rhs_len != __lhs.size()) return false;
4063 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Mark de Weverb4830e62022-07-19 07:56:23 +02004064#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065}
4066
Mark de Weverb4830e62022-07-19 07:56:23 +02004067#if _LIBCPP_STD_VER > 17
4068
4069template <class _CharT, class _Traits, class _Allocator>
4070_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
4071 const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4072 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
4073 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4074}
4075
4076template <class _CharT, class _Traits, class _Allocator>
4077_LIBCPP_HIDE_FROM_ABI constexpr auto
4078operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
4079 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
4080}
4081
4082#else // _LIBCPP_STD_VER > 17
4083
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004084template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004085inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086bool
4087operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004088 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089{
4090 return !(__lhs == __rhs);
4091}
4092
4093template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004094inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004096operator!=(const _CharT* __lhs,
4097 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098{
4099 return !(__lhs == __rhs);
4100}
4101
4102template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004103inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004105operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4106 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107{
4108 return !(__lhs == __rhs);
4109}
4110
4111// operator<
4112
4113template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004114inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115bool
4116operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004117 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004119 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120}
4121
4122template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004123inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004125operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4126 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004128 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129}
4130
4131template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004132inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004134operator< (const _CharT* __lhs,
4135 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136{
4137 return __rhs.compare(__lhs) > 0;
4138}
4139
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140// operator>
4141
4142template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004143inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144bool
4145operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004146 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147{
4148 return __rhs < __lhs;
4149}
4150
4151template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004152inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004154operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4155 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156{
4157 return __rhs < __lhs;
4158}
4159
4160template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004161inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004163operator> (const _CharT* __lhs,
4164 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165{
4166 return __rhs < __lhs;
4167}
4168
4169// operator<=
4170
4171template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004172inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173bool
4174operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004175 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176{
4177 return !(__rhs < __lhs);
4178}
4179
4180template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004181inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004183operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4184 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185{
4186 return !(__rhs < __lhs);
4187}
4188
4189template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004190inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004192operator<=(const _CharT* __lhs,
4193 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194{
4195 return !(__rhs < __lhs);
4196}
4197
4198// operator>=
4199
4200template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004201inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202bool
4203operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004204 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205{
4206 return !(__lhs < __rhs);
4207}
4208
4209template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004210inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004212operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4213 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214{
4215 return !(__lhs < __rhs);
4216}
4217
4218template<class _CharT, class _Traits, class _Allocator>
Mark de Weveraf1968a2022-08-14 14:14:09 +02004219inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004221operator>=(const _CharT* __lhs,
4222 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223{
4224 return !(__lhs < __rhs);
4225}
Mark de Weverb4830e62022-07-19 07:56:23 +02004226#endif // _LIBCPP_STD_VER > 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227
4228// operator +
4229
4230template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004231_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004232basic_string<_CharT, _Traits, _Allocator>
4233operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4234 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4235{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004236 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004237 auto __lhs_sz = __lhs.size();
4238 auto __rhs_sz = __rhs.size();
4239 _String __r(__uninitialized_size_tag(),
4240 __lhs_sz + __rhs_sz,
4241 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4242 auto __ptr = std::__to_address(__r.__get_pointer());
4243 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4244 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4245 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246 return __r;
4247}
4248
4249template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004250_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251basic_string<_CharT, _Traits, _Allocator>
4252operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4253{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004254 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004255 auto __lhs_sz = _Traits::length(__lhs);
4256 auto __rhs_sz = __rhs.size();
4257 _String __r(__uninitialized_size_tag(),
4258 __lhs_sz + __rhs_sz,
4259 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4260 auto __ptr = std::__to_address(__r.__get_pointer());
4261 _Traits::copy(__ptr, __lhs, __lhs_sz);
4262 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4263 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264 return __r;
4265}
4266
4267template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004268_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269basic_string<_CharT, _Traits, _Allocator>
4270operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4271{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004272 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004273 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004274 _String __r(__uninitialized_size_tag(),
4275 __rhs_sz + 1,
4276 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4277 auto __ptr = std::__to_address(__r.__get_pointer());
4278 _Traits::assign(__ptr, 1, __lhs);
4279 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4280 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281 return __r;
4282}
4283
4284template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004285inline _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286basic_string<_CharT, _Traits, _Allocator>
4287operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4288{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004289 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004290 typename _String::size_type __lhs_sz = __lhs.size();
4291 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004292 _String __r(__uninitialized_size_tag(),
4293 __lhs_sz + __rhs_sz,
4294 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4295 auto __ptr = std::__to_address(__r.__get_pointer());
4296 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4297 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4298 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299 return __r;
4300}
4301
4302template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004303_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304basic_string<_CharT, _Traits, _Allocator>
4305operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4306{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004307 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004308 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004309 _String __r(__uninitialized_size_tag(),
4310 __lhs_sz + 1,
4311 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4312 auto __ptr = std::__to_address(__r.__get_pointer());
4313 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4314 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4315 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004316 return __r;
4317}
4318
Eric Fiselierfc92be82017-04-19 00:28:44 +00004319#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004320
4321template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004322inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323basic_string<_CharT, _Traits, _Allocator>
4324operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4325{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004326 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327}
4328
4329template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004330inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331basic_string<_CharT, _Traits, _Allocator>
4332operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4333{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004334 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335}
4336
4337template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004338inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339basic_string<_CharT, _Traits, _Allocator>
4340operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4341{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004342 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343}
4344
4345template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004346inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347basic_string<_CharT, _Traits, _Allocator>
4348operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4349{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004350 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351}
4352
4353template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004354inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355basic_string<_CharT, _Traits, _Allocator>
4356operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4357{
4358 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004359 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360}
4361
4362template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004363inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364basic_string<_CharT, _Traits, _Allocator>
4365operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4366{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004367 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368}
4369
4370template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004371inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372basic_string<_CharT, _Traits, _Allocator>
4373operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4374{
4375 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004376 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377}
4378
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004379#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380
4381// swap
4382
4383template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004384inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004386swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004387 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4388 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004389{
4390 __lhs.swap(__rhs);
4391}
4392
Bruce Mitchener170d8972020-11-24 12:53:53 -05004393_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4394_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4395_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4396_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4397_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004398
Bruce Mitchener170d8972020-11-24 12:53:53 -05004399_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4400_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4401_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004402
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004403_LIBCPP_FUNC_VIS string to_string(int __val);
4404_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4405_LIBCPP_FUNC_VIS string to_string(long __val);
4406_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4407_LIBCPP_FUNC_VIS string to_string(long long __val);
4408_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4409_LIBCPP_FUNC_VIS string to_string(float __val);
4410_LIBCPP_FUNC_VIS string to_string(double __val);
4411_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004412
Louis Dionne89258142021-08-23 15:32:36 -04004413#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004414_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4415_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4416_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4417_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4418_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004419
Bruce Mitchener170d8972020-11-24 12:53:53 -05004420_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4421_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4422_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004423
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004424_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4425_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4426_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4427_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4428_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4429_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4430_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4431_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4432_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004433#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004434
Howard Hinnantc51e1022010-05-11 19:42:16 +00004435template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004436_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004437const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4438 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004439
Marshall Clow851b9ec2019-05-20 21:56:51 +00004440template <class _CharT, class _Allocator>
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004441struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442{
4443 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004444 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4445 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446};
4447
Nikolas Klauser3ab26e32022-08-22 02:59:09 +02004448template <class _Allocator>
4449struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
4450
4451#ifndef _LIBCPP_HAS_NO_CHAR8_T
4452template <class _Allocator>
4453struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
4454#endif
4455
4456template <class _Allocator>
4457struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
4458
4459template <class _Allocator>
4460struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
4461
4462#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4463template <class _Allocator>
4464struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
4465#endif
4466
Howard Hinnantdc095972011-07-18 15:51:59 +00004467template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004468_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004469operator<<(basic_ostream<_CharT, _Traits>& __os,
4470 const basic_string<_CharT, _Traits, _Allocator>& __str);
4471
4472template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004473_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004474operator>>(basic_istream<_CharT, _Traits>& __is,
4475 basic_string<_CharT, _Traits, _Allocator>& __str);
4476
4477template<class _CharT, class _Traits, class _Allocator>
Nikolas Klausera9ad6702022-08-13 13:23:16 +02004478_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
Howard Hinnantdc095972011-07-18 15:51:59 +00004479getline(basic_istream<_CharT, _Traits>& __is,
4480 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4481
4482template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004483inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004484basic_istream<_CharT, _Traits>&
4485getline(basic_istream<_CharT, _Traits>& __is,
4486 basic_string<_CharT, _Traits, _Allocator>& __str);
4487
Howard Hinnantdc095972011-07-18 15:51:59 +00004488template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004489inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004490basic_istream<_CharT, _Traits>&
4491getline(basic_istream<_CharT, _Traits>&& __is,
4492 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4493
4494template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004495inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004496basic_istream<_CharT, _Traits>&
4497getline(basic_istream<_CharT, _Traits>&& __is,
4498 basic_string<_CharT, _Traits, _Allocator>& __str);
4499
Marshall Clow29b53f22018-12-14 18:49:35 +00004500#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004501template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004502inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004503 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4504 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4505 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004506 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004507 return __old_size - __str.size();
4508}
Marshall Clow29b53f22018-12-14 18:49:35 +00004509
Marek Kurdeja98b1412020-05-02 13:58:03 +02004510template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004511inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004512 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4513 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4514 _Predicate __pred) {
4515 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004516 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004517 __str.end());
4518 return __old_size - __str.size();
4519}
Marshall Clow29b53f22018-12-14 18:49:35 +00004520#endif
4521
Louis Dionne510450b2022-04-01 16:38:30 -04004522#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004523
4524template<class _CharT, class _Traits, class _Allocator>
4525bool
4526basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4527{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004528 return data() <= std::__to_address(__i->base()) &&
4529 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004530}
4531
4532template<class _CharT, class _Traits, class _Allocator>
4533bool
4534basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4535{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004536 return data() < std::__to_address(__i->base()) &&
4537 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004538}
4539
4540template<class _CharT, class _Traits, class _Allocator>
4541bool
4542basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4543{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004544 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004545 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004546}
4547
4548template<class _CharT, class _Traits, class _Allocator>
4549bool
4550basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4551{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004552 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004553 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004554}
4555
Louis Dionne510450b2022-04-01 16:38:30 -04004556#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004557
Louis Dionne173f29e2019-05-29 16:01:36 +00004558#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004559// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004560inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004561{
4562 inline namespace string_literals
4563 {
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004564 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004565 basic_string<char> operator "" s( const char *__str, size_t __len )
4566 {
4567 return basic_string<char> (__str, __len);
4568 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004569
Louis Dionne89258142021-08-23 15:32:36 -04004570#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004571 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004572 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4573 {
4574 return basic_string<wchar_t> (__str, __len);
4575 }
Louis Dionne89258142021-08-23 15:32:36 -04004576#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004577
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004578#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004579 inline _LIBCPP_HIDE_FROM_ABI constexpr
Nikolas Klauser5da956d2022-09-02 16:20:28 +02004580 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len)
Marshall Clow8732fed2018-12-11 04:35:44 +00004581 {
4582 return basic_string<char8_t> (__str, __len);
4583 }
4584#endif
4585
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004586 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004587 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4588 {
4589 return basic_string<char16_t> (__str, __len);
4590 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004591
Nikolas Klauser8b1c5062022-08-19 13:08:01 +02004592 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Howard Hinnant5c167562013-08-07 19:39:48 +00004593 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4594 {
4595 return basic_string<char32_t> (__str, __len);
4596 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004597 } // namespace string_literals
4598} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004599
4600#if _LIBCPP_STD_VER > 17
4601template <>
4602inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4603#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4604template <>
4605inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4606#endif
4607#endif
4608
Marshall Clowcba751f2013-07-23 17:05:24 +00004609#endif
4610
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611_LIBCPP_END_NAMESPACE_STD
4612
Eric Fiselierf4433a32017-05-31 22:07:49 +00004613_LIBCPP_POP_MACROS
4614
Mark de Weveree5fe272022-09-02 17:53:28 +02004615#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
4616# include <algorithm>
Nikolas Klauser1e4ae5d2022-11-02 20:27:42 +01004617# include <concepts>
Mark de Weveree5fe272022-09-02 17:53:28 +02004618# include <functional>
4619# include <iterator>
4620# include <new>
4621# include <typeinfo>
4622# include <utility>
4623# include <vector>
4624#endif
4625
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004626#endif // _LIBCPP_STRING