blob: ab8d2ac446d1c591d93da786ac3f9e527124e532 [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
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
Nikolas Klauser80b3cf32022-04-27 10:11:44 +020098 noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20
99 explicit basic_string(const allocator_type& a); // constexpr since C++20
100 basic_string(const basic_string& str); // constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200102 noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200104 const allocator_type& a = allocator_type()); // constexpr since C++20
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200106 const Allocator& a = Allocator()); // constexpr since C++20
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200108 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 +0000109 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17, constexpr since C++20
111 basic_string(const value_type* s, const allocator_type& a = allocator_type()); // constexpr since C++20
112 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 +0200113 basic_string(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200114 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 +0000115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200117 const allocator_type& a = allocator_type()); // constexpr since C++20
118 basic_string(initializer_list<value_type>, const Allocator& = Allocator()); // constexpr since C++20
119 basic_string(const basic_string&, const Allocator&); // constexpr since C++20
120 basic_string(basic_string&&, const Allocator&); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200122 ~basic_string(); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200124 operator basic_string_view<charT, traits>() const noexcept; // constexpr since C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000125
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200126 basic_string& operator=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200128 basic_string& operator=(const T& t); // C++17, constexpr since C++20
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200132 allocator_type::is_always_equal::value ); // C++17, constexpr since C++20
133 basic_string& operator=(const value_type* s); // constexpr since C++20
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200135 basic_string& operator=(value_type c); // constexpr since C++20
136 basic_string& operator=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200138 iterator begin() noexcept; // constexpr since C++20
139 const_iterator begin() const noexcept; // constexpr since C++20
140 iterator end() noexcept; // constexpr since C++20
141 const_iterator end() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200143 reverse_iterator rbegin() noexcept; // constexpr since C++20
144 const_reverse_iterator rbegin() const noexcept; // constexpr since C++20
145 reverse_iterator rend() noexcept; // constexpr since C++20
146 const_reverse_iterator rend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200148 const_iterator cbegin() const noexcept; // constexpr since C++20
149 const_iterator cend() const noexcept; // constexpr since C++20
150 const_reverse_iterator crbegin() const noexcept; // constexpr since C++20
151 const_reverse_iterator crend() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200153 size_type size() const noexcept; // constexpr since C++20
154 size_type length() const noexcept; // constexpr since C++20
155 size_type max_size() const noexcept; // constexpr since C++20
156 size_type capacity() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200158 void resize(size_type n, value_type c); // constexpr since C++20
159 void resize(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200164 void reserve(size_type res_arg); // constexpr since C++20
Marek Kurdejc9848142020-11-26 10:07:16 +0100165 void reserve(); // deprecated in C++20
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200166 void shrink_to_fit(); // constexpr since C++20
167 void clear() noexcept; // constexpr since C++20
168 bool empty() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200170 const_reference operator[](size_type pos) const; // constexpr since C++20
171 reference operator[](size_type pos); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200173 const_reference at(size_type n) const; // constexpr since C++20
174 reference at(size_type n); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200176 basic_string& operator+=(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200178 basic_string& operator+=(const T& t); // C++17, constexpr since C++20
179 basic_string& operator+=(const value_type* s); // constexpr since C++20
180 basic_string& operator+=(value_type c); // constexpr since C++20
181 basic_string& operator+=(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200183 basic_string& append(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200185 basic_string& append(const T& t); // C++17, constexpr since C++20
186 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 +0000187 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
189 basic_string& append(const value_type* s, size_type n); // constexpr since C++20
190 basic_string& append(const value_type* s); // constexpr since C++20
191 basic_string& append(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200193 basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
194 basic_string& append(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200196 void push_back(value_type c); // constexpr since C++20
197 void pop_back(); // constexpr since C++20
198 reference front(); // constexpr since C++20
199 const_reference front() const; // constexpr since C++20
200 reference back(); // constexpr since C++20
201 const_reference back() const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200203 basic_string& assign(const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200205 basic_string& assign(const T& t); // C++17, constexpr since C++20
206 basic_string& assign(basic_string&& str); // constexpr since C++20
207 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 +0000208 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
210 basic_string& assign(const value_type* s, size_type n); // constexpr since C++20
211 basic_string& assign(const value_type* s); // constexpr since C++20
212 basic_string& assign(size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200214 basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
215 basic_string& assign(initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200217 basic_string& insert(size_type pos1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200219 basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200221 size_type pos2, size_type n); // constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
225 basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
226 basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
227 iterator insert(const_iterator p, value_type c); // constexpr since C++20
228 iterator insert(const_iterator p, size_type n, value_type c); // constexpr since C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200230 iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20
231 iterator insert(const_iterator p, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200233 basic_string& erase(size_type pos = 0, size_type n = npos); // constexpr since C++20
234 iterator erase(const_iterator position); // constexpr since C++20
235 iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200239 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 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200241 size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200244 size_type pos2, size_type n); // C++17, constexpr since C++20
245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
246 basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17, constexpr since C++20
251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s); // constexpr since C++20
253 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 +0000254 template<class InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200258 size_type copy(value_type* s, size_type n, size_type pos = 0) const; // constexpr since C++20
259 basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200263 allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200265 const value_type* c_str() const noexcept; // constexpr since C++20
266 const value_type* data() const noexcept; // constexpr since C++20
267 value_type* data() noexcept; // C++17, constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200269 allocator_type get_allocator() const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200271 size_type find(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
275 size_type find(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
276 size_type find(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
283 size_type rfind(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200285 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 +0000286 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
290 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 +0000291
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200292 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 +0000293 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200294 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
295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
297 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 +0000298
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200299 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 +0000300 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200301 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
302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
304 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 +0000305
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200306 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 +0000307 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200308 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
309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
311 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 +0000312
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200313 int compare(const basic_string& str) const noexcept; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
316 int compare(size_type pos1, size_type n1, const basic_string& str) const; // constexpr since C++20
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200318 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 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200320 size_type pos2, size_type n2=npos) const; // C++14, constexpr since C++20
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200323 size_type pos2, size_type n2=npos) const; // C++17, constexpr since C++20
324 int compare(const value_type* s) const noexcept; // constexpr since C++20
325 int compare(size_type pos1, size_type n1, const value_type* s) const; // constexpr since C++20
326 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 +0000327
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200328 constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 constexpr bool starts_with(charT c) const noexcept; // C++20
330 constexpr bool starts_with(const charT* s) const; // C++20
331 constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 constexpr bool ends_with(charT c) const noexcept; // C++20
333 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338};
339
Marshall Clowa0563332018-02-08 06:34:03 +0000340template<class InputIterator,
341 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
342basic_string(InputIterator, InputIterator, Allocator = Allocator())
343 -> basic_string<typename iterator_traits<InputIterator>::value_type,
344 char_traits<typename iterator_traits<InputIterator>::value_type>,
345 Allocator>; // C++17
346
Howard Hinnantc51e1022010-05-11 19:42:16 +0000347template<class charT, class traits, class Allocator>
348basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000349operator+(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200350 const basic_string<charT, traits, Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351
352template<class charT, class traits, class Allocator>
353basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200354operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000355
356template<class charT, class traits, class Allocator>
357basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200358operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000359
360template<class charT, class traits, class Allocator>
361basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200362operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
364template<class charT, class traits, class Allocator>
365basic_string<charT, traits, Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200366operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367
368template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000369bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200370 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200373bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374
375template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200376bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000378template<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>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200383bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since 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
388template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000389bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200390 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200393bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394
395template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200396bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since 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,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200400 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200403bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200406bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since 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,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200410 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
412template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200413bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414
415template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200416bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since 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,
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200420 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200423bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424
425template<class charT, class traits, class Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200426bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427
428template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000429void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000430 basic_string<charT, traits, Allocator>& rhs)
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200431 noexcept(noexcept(lhs.swap(rhs))); // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432
433template<class charT, class traits, class Allocator>
434basic_istream<charT, traits>&
435operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
436
437template<class charT, class traits, class Allocator>
438basic_ostream<charT, traits>&
439operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
440
441template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000442basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000443getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
444 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445
446template<class charT, class traits, class Allocator>
447basic_istream<charT, traits>&
448getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
449
Marshall Clow29b53f22018-12-14 18:49:35 +0000450template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200451typename basic_string<charT, traits, Allocator>::size_type
452erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000453template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200454typename basic_string<charT, traits, Allocator>::size_type
455erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000456
Howard Hinnantc51e1022010-05-11 19:42:16 +0000457typedef basic_string<char> string;
458typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100459typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000460typedef basic_string<char16_t> u16string;
461typedef basic_string<char32_t> u32string;
462
Bruce Mitchener170d8972020-11-24 12:53:53 -0500463int stoi (const string& str, size_t* idx = nullptr, int base = 10);
464long stol (const string& str, size_t* idx = nullptr, int base = 10);
465unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
466long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000468
Bruce Mitchener170d8972020-11-24 12:53:53 -0500469float stof (const string& str, size_t* idx = nullptr);
470double stod (const string& str, size_t* idx = nullptr);
471long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000472
473string to_string(int val);
474string to_string(unsigned val);
475string to_string(long val);
476string to_string(unsigned long val);
477string to_string(long long val);
478string to_string(unsigned long long val);
479string to_string(float val);
480string to_string(double val);
481string to_string(long double val);
482
Bruce Mitchener170d8972020-11-24 12:53:53 -0500483int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
484long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
485unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
486long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000488
Bruce Mitchener170d8972020-11-24 12:53:53 -0500489float stof (const wstring& str, size_t* idx = nullptr);
490double stod (const wstring& str, size_t* idx = nullptr);
491long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000492
493wstring to_wstring(int val);
494wstring to_wstring(unsigned val);
495wstring to_wstring(long val);
496wstring to_wstring(unsigned long val);
497wstring to_wstring(long long val);
498wstring to_wstring(unsigned long long val);
499wstring to_wstring(float val);
500wstring to_wstring(double val);
501wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502
503template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100504template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000505template <> struct hash<u16string>;
506template <> struct hash<u32string>;
507template <> struct hash<wstring>;
508
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200509basic_string<char> operator "" s( const char *str, size_t len ); // C++14, constexpr since C++20
510basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14, constexpr since C++20
511constexpr basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
512basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14, constexpr since C++20
513basic_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 +0000514
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515} // std
516
517*/
518
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100519#include <__algorithm/max.h>
520#include <__algorithm/min.h>
521#include <__algorithm/remove.h>
522#include <__algorithm/remove_if.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400523#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400525#include <__debug>
Mark de Weverdf3e0d42021-09-26 15:47:42 +0200526#include <__format/enable_insertable.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200527#include <__functional/hash.h>
Nikolas Klauser24540d32022-05-21 00:45:51 +0200528#include <__functional/unary_function.h>
Nikolas Klauser80840272022-02-04 13:04:33 +0100529#include <__ios/fpos.h>
Nikolas Klauserfb1b0672022-06-10 19:53:10 +0200530#include <__iterator/distance.h>
531#include <__iterator/iterator_traits.h>
532#include <__iterator/reverse_iterator.h>
Louis Dionne77249522021-06-11 09:55:11 -0400533#include <__iterator/wrap_iter.h>
Nikolas Klauserc513eba2022-04-09 09:41:19 +0200534#include <__memory/allocate_at_least.h>
Nikolas Klauser11a0ff32022-06-06 23:35:24 +0200535#include <__string/char_traits.h>
536#include <__string/extern_template_lists.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100537#include <__utility/auto_cast.h>
538#include <__utility/move.h>
539#include <__utility/swap.h>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200540#include <__utility/unreachable.h>
541#include <climits>
Louis Dionne44962c32022-06-13 11:21:16 -0400542#include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400543#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400544#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400545#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400546#include <iosfwd>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200547#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548#include <memory>
549#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400550#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000552#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000553
Louis Dionne89258142021-08-23 15:32:36 -0400554#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100555# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400556#endif
557
Nikolas Klausera0e0edb2022-06-16 22:43:46 +0200558// standard-mandated includes
559
560// [iterator.range]
561#include <__iterator/access.h>
562#include <__iterator/data.h>
563#include <__iterator/empty.h>
564#include <__iterator/reverse_access.h>
565#include <__iterator/size.h>
566
567// [string.syn]
568#include <compare>
569#include <initializer_list>
570
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000571#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500572# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000573#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574
Eric Fiselierf4433a32017-05-31 22:07:49 +0000575_LIBCPP_PUSH_MACROS
576#include <__undef_macros>
577
578
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579_LIBCPP_BEGIN_NAMESPACE_STD
580
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581// basic_string
582
583template<class _CharT, class _Traits, class _Allocator>
584basic_string<_CharT, _Traits, _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200585_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant944510a2011-06-14 19:58:17 +0000586operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
587 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588
589template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200590_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000592operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593
594template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200595_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000597operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598
599template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200600inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000602operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603
604template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200605_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000607operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608
Louis Dionnedc496ec2021-06-08 17:25:08 -0400609extern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Shoaib Meenaiea363712017-07-29 02:54:41 +0000610
Marshall Clow039b2f02016-01-13 21:54:34 +0000611template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400612struct __string_is_trivial_iterator : public false_type {};
613
614template <class _Tp>
615struct __string_is_trivial_iterator<_Tp*>
616 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000617
Louis Dionne173f29e2019-05-29 16:01:36 +0000618template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400619struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
620 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000621
Marshall Clow82513342016-09-24 22:45:42 +0000622template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500623struct __can_be_converted_to_string_view : public _BoolConstant<
624 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
625 !is_convertible<const _Tp&, const _CharT*>::value
626 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000627
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400628#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800629typedef basic_string<char8_t> u8string;
630#endif
Richard Smith256954d2020-11-11 17:12:18 -0800631typedef basic_string<char16_t> u16string;
632typedef basic_string<char32_t> u32string;
Richard Smith256954d2020-11-11 17:12:18 -0800633
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200634struct __uninitialized_size_tag {};
635
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000636template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800637class
638 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400639#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800640 _LIBCPP_PREFERRED_NAME(u8string)
641#endif
Richard Smith256954d2020-11-11 17:12:18 -0800642 _LIBCPP_PREFERRED_NAME(u16string)
643 _LIBCPP_PREFERRED_NAME(u32string)
Richard Smith256954d2020-11-11 17:12:18 -0800644 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645{
646public:
647 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000648 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000650 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000652 typedef allocator_traits<allocator_type> __alloc_traits;
653 typedef typename __alloc_traits::size_type size_type;
654 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000655 typedef value_type& reference;
656 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000657 typedef typename __alloc_traits::pointer pointer;
658 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659
Marshall Clow79f33542018-03-21 00:36:05 +0000660 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
661 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
662 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
663 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000664 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000665 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000666 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000667
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668 typedef __wrap_iter<pointer> iterator;
669 typedef __wrap_iter<const_pointer> const_iterator;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200670 typedef std::reverse_iterator<iterator> reverse_iterator;
671 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672
673private:
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200674 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
Howard Hinnant68bf1812013-04-30 21:44:48 +0000675
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000676#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000677
678 struct __long
679 {
680 pointer __data_;
681 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200682 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
683 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000684 };
685
Howard Hinnant68bf1812013-04-30 21:44:48 +0000686 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
687 (sizeof(__long) - 1)/sizeof(value_type) : 2};
688
689 struct __short
690 {
691 value_type __data_[__min_cap];
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200692 unsigned char __padding_[sizeof(value_type) - 1];
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200693 unsigned char __size_ : 7;
694 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000695 };
696
Nikolas Klauser62060be2022-04-20 10:52:04 +0200697// The __endian_factor is required because the field we use to store the size
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200698// has one fewer bit than it would if it were not a bitfield.
Nikolas Klauser62060be2022-04-20 10:52:04 +0200699//
700// If the LSB is used to store the short-flag in the short string representation,
701// we have to multiply the size by two when it is stored and divide it by two when
702// it is loaded to make sure that we always store an even number. In the long string
703// representation, we can ignore this because we can assume that we always allocate
704// an even amount of value_types.
705//
706// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
707// This does not impact the short string representation, since we never need the MSB
708// for representing the size of a short string anyway.
709
710#ifdef _LIBCPP_BIG_ENDIAN
711 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000712#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200713 static const size_type __endian_factor = 1;
714#endif
715
716#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
717
718#ifdef _LIBCPP_BIG_ENDIAN
719 static const size_type __endian_factor = 1;
720#else
721 static const size_type __endian_factor = 2;
722#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000723
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724 struct __long
725 {
Nikolas Klauser62060be2022-04-20 10:52:04 +0200726 size_type __is_long_ : 1;
727 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728 size_type __size_;
729 pointer __data_;
730 };
731
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
733 (sizeof(__long) - 1)/sizeof(value_type) : 2};
734
735 struct __short
736 {
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200737 unsigned char __is_long_ : 1;
738 unsigned char __size_ : 7;
Nikolas Klauser1aa21112022-05-14 12:38:00 +0200739 char __padding_[sizeof(value_type) - 1];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 value_type __data_[__min_cap];
741 };
742
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400743#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000744
Nikolas Klauser95cfe622022-06-11 23:43:00 +0200745 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
746
Howard Hinnant8ea98242013-08-23 17:37:05 +0000747 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748
Howard Hinnant8ea98242013-08-23 17:37:05 +0000749 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750
751 struct __raw
752 {
753 size_type __words[__n_words];
754 };
755
756 struct __rep
757 {
758 union
759 {
760 __long __l;
761 __short __s;
762 __raw __r;
763 };
764 };
765
766 __compressed_pair<__rep, allocator_type> __r_;
767
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200768 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
769 // don't initialize the characters. The contents of the string, including the null terminator, must be
770 // initialized separately.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200771 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
772 explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200773 : __r_(__default_init_tag(), __a) {
774 if (__size > max_size())
775 __throw_length_error();
776 if (__fits_in_sso(__size)) {
777 __zero();
778 __set_short_size(__size);
779 } else {
780 auto __capacity = __recommend(__size) + 1;
781 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200782 __begin_lifetime(__allocation, __capacity);
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200783 __set_long_cap(__capacity);
784 __set_long_pointer(__allocation);
785 __set_long_size(__size);
786 }
787 std::__debug_db_insert_c(this);
788 }
789
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200791 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 static const size_type npos = -1;
793
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200794 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string()
Howard Hinnant3e276872011-06-03 18:40:47 +0000795 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000796
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200797 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit basic_string(const allocator_type& __a)
Marshall Clowa80abc72015-06-03 19:56:43 +0000798#if _LIBCPP_STD_VER <= 14
799 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
800#else
801 _NOEXCEPT;
802#endif
803
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200804 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string(const basic_string& __str);
805 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000806
Eric Fiselierfc92be82017-04-19 00:28:44 +0000807#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200808 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +0000809 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000810#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000811 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000812#else
813 _NOEXCEPT;
814#endif
815
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200816 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400818#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000819
Louis Dionne9ce598d2021-09-08 09:14:43 -0400820 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200821 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500822 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000823 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
824 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200825 std::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000826 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000827
Louis Dionne9ce598d2021-09-08 09:14:43 -0400828 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200829 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000830 basic_string(const _CharT* __s, const _Allocator& __a);
831
Marek Kurdej90d79712021-07-27 16:16:21 +0200832#if _LIBCPP_STD_VER > 20
833 basic_string(nullptr_t) = delete;
834#endif
835
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200836 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000837 basic_string(const _CharT* __s, size_type __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200838 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000839 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200840 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000841 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000842
Louis Dionne9ce598d2021-09-08 09:14:43 -0400843 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200844 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000845 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
846
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200847 _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +0000848 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000849 const _Allocator& __a = _Allocator());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200850 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +0000851 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000852 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000853
Louis Dionne9ce598d2021-09-08 09:14:43 -0400854 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200855 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000856 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500857 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000858
Louis Dionne9ce598d2021-09-08 09:14:43 -0400859 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500860 !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200861 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000862 explicit basic_string(const _Tp& __t);
863
Louis Dionne9ce598d2021-09-08 09:14:43 -0400864 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200865 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +0000866 explicit basic_string(const _Tp& __t, const allocator_type& __a);
867
Louis Dionne9ce598d2021-09-08 09:14:43 -0400868 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200869 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400871 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200872 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000874#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200875 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000876 basic_string(initializer_list<_CharT> __il);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200877 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +0000878 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400879#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200881 inline _LIBCPP_CONSTEXPR_AFTER_CXX17 ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200883 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000884 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
885
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200886 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000887
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200888 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
889 !__is_same_uncvref<_Tp, basic_string>::value> >
890 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(const _Tp& __t) {
891 __self_view __sv = __t;
892 return assign(__sv);
893 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000894
Eric Fiselierfc92be82017-04-19 00:28:44 +0000895#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200896 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +0000897 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000898 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200899 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierfc92be82017-04-19 00:28:44 +0000900 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200902 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200903 basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200904#if _LIBCPP_STD_VER > 20
905 basic_string& operator=(nullptr_t) = delete;
906#endif
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200907 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200909 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000910 iterator begin() _NOEXCEPT
911 {return iterator(this, __get_pointer());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200912 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000913 const_iterator begin() const _NOEXCEPT
914 {return const_iterator(this, __get_pointer());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200915 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000916 iterator end() _NOEXCEPT
917 {return iterator(this, __get_pointer() + size());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200918 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant8ea98242013-08-23 17:37:05 +0000919 const_iterator end() const _NOEXCEPT
920 {return const_iterator(this, __get_pointer() + size());}
Louis Dionnee554e102022-06-03 15:17:03 -0400921
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200922 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000923 reverse_iterator rbegin() _NOEXCEPT
924 {return reverse_iterator(end());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200925 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000926 const_reverse_iterator rbegin() const _NOEXCEPT
927 {return const_reverse_iterator(end());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200928 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000929 reverse_iterator rend() _NOEXCEPT
930 {return reverse_iterator(begin());}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200931 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000932 const_reverse_iterator rend() const _NOEXCEPT
933 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200935 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000936 const_iterator cbegin() const _NOEXCEPT
937 {return begin();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200938 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000939 const_iterator cend() const _NOEXCEPT
940 {return end();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200941 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000942 const_reverse_iterator crbegin() const _NOEXCEPT
943 {return rbegin();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200944 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000945 const_reverse_iterator crend() const _NOEXCEPT
946 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200948 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949 {return __is_long() ? __get_long_size() : __get_short_size();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200950 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type length() const _NOEXCEPT {return size();}
951 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type max_size() const _NOEXCEPT;
952 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type capacity() const _NOEXCEPT {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200953 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
954 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200956 _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200957 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n) { resize(__n, value_type()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200959 _LIBCPP_CONSTEXPR_AFTER_CXX17 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100960
961#if _LIBCPP_STD_VER > 20
962 template <class _Op>
963 _LIBCPP_HIDE_FROM_ABI constexpr
964 void resize_and_overwrite(size_type __n, _Op __op) {
965 __resize_default_init(__n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200966 __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100967 }
968#endif
969
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200970 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __resize_default_init(size_type __n);
Eric Fiselier451d5582018-11-26 20:15:38 +0000971
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200972 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
973 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void shrink_to_fit() _NOEXCEPT;
974 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void clear() _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200975
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200976 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowb7db4972017-11-15 20:02:27 +0000977 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200979 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200980 const_reference operator[](size_type __pos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200981 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200983 _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference at(size_type __n) const;
984 _LIBCPP_CONSTEXPR_AFTER_CXX17 reference at(size_type __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
Nikolas Klauser1d5108d2022-04-29 11:17:58 +0200986 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(const basic_string& __str) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200987 return append(__str);
988 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000989
990 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200991 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -0400992 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000993 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500994 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
995 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000996 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500997 >
Nikolas Klauser80b3cf32022-04-27 10:11:44 +0200998 operator+=(const _Tp& __t) {
999 __self_view __sv = __t; return append(__sv);
1000 }
1001
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001002 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(const value_type* __s) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001003 return append(__s);
1004 }
1005
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001006 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(value_type __c) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001007 push_back(__c);
1008 return *this;
1009 }
1010
Eric Fiselierfc92be82017-04-19 00:28:44 +00001011#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001012 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001013 basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001014#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001016 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001018
1019 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001020 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001021 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001022 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1023 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001024 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001025 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001026 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001027 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001028
Marshall Clow62953962016-10-03 23:40:48 +00001029 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001030 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001031 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001032 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001033 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1034 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001035 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001036 >
Marshall Clow82513342016-09-24 22:45:42 +00001037 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001038 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s, size_type __n);
1039 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s);
1040 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001041
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001042 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier451d5582018-11-26 20:15:38 +00001043 void __append_default_init(size_type __n);
1044
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001046 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001047 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001049 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001051 >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001052 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001053 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001054 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001055 append(__temp.data(), __temp.size());
1056 return *this;
1057 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001059 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001060 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001062 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001064 >
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001065 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001066 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001067
Eric Fiselierfc92be82017-04-19 00:28:44 +00001068#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001069 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001071#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001073 _LIBCPP_CONSTEXPR_AFTER_CXX17 void push_back(value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001074 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void pop_back();
1075 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference front() _NOEXCEPT;
1076 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference front() const _NOEXCEPT;
1077 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference back() _NOEXCEPT;
1078 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079
Marshall Clowe46031a2018-07-02 18:41:15 +00001080 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001081 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001082 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001083 <
1084 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1085 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001086 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001087 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001088 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001089 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001090#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001091 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001092 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001093 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001094 {*this = std::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001095#endif
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001096 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001097 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001098 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001099 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001100 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001101 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1102 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001103 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001104 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001105 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001106 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s, size_type __n);
1107 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s);
1108 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001110 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001111 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001113 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001115 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116 assign(_InputIterator __first, _InputIterator __last);
1117 template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001118 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001119 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001121 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001123 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001125#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001126 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001128#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001130 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001132
1133 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001134 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001135 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001136 <
1137 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1138 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001139 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001140 insert(size_type __pos1, const _Tp& __t)
1141 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1142
Marshall Clow82513342016-09-24 22:45:42 +00001143 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001144 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001145 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001146 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001147 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001148 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001149 >
Marshall Clow82513342016-09-24 22:45:42 +00001150 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001151 _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow8db7fb02014-03-04 19:17:19 +00001152 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001153 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1154 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s);
1155 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1156 _LIBCPP_CONSTEXPR_AFTER_CXX17 iterator insert(const_iterator __pos, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001157 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1159 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001160 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001161 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001163 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001165 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1167 template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001168 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001169 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001171 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001173 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001175#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001176 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1178 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001179#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001181 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001182 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 iterator erase(const_iterator __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001184 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 iterator erase(const_iterator __first, const_iterator __last);
1186
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001187 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001189
1190 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001191 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001192 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001193 <
1194 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1195 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001196 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001197 replace(size_type __pos1, size_type __n1, const _Tp& __t) { __self_view __sv = __t; return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001198 _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow8db7fb02014-03-04 19:17:19 +00001199 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 +00001200 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001201 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001202 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001203 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001204 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001205 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001206 >
Marshall Clow82513342016-09-24 22:45:42 +00001207 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001208 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001209 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001210 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1211 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001212 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant990d6e82010-11-17 21:11:40 +00001213 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001214
1215 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001216 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001217 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001218 <
1219 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1220 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001221 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001222 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1223
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001224 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001225 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001226 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001227 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001228 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant990d6e82010-11-17 21:11:40 +00001229 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230 template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001231 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001232 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001234 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001236 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001237 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001238#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001239 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant990d6e82010-11-17 21:11:40 +00001240 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001242#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001244 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001245 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1247
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001248 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001249 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001250#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001251 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001252#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001253 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001254 __is_nothrow_swappable<allocator_type>::value);
1255#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001257 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001258 const value_type* c_str() const _NOEXCEPT {return data();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001259 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1260 const value_type* data() const _NOEXCEPT {return std::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001261#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001262 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1263 value_type* data() _NOEXCEPT {return std::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001264#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001266 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001267 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001269 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001270 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001271
1272 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001273 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001274 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001275 <
1276 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1277 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001278 >
zoecarver1997e0a2021-02-05 11:54:47 -08001279 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001280 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001281 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001282 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001283 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001284 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001286 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001287 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001288
1289 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001290 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
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 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001295 >
zoecarver1997e0a2021-02-05 11:54:47 -08001296 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001297 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001298 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001299 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001301 _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001303 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001304 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001305
1306 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001307 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001308 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001309 <
1310 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1311 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001312 >
zoecarver1997e0a2021-02-05 11:54:47 -08001313 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001314 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001315 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001316 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001317 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001319 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001321 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001322 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001323
1324 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001325 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001326 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001327 <
1328 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1329 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001330 >
zoecarver1997e0a2021-02-05 11:54:47 -08001331 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001332 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001334 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001335 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001336 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001337 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001339 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001340 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001341
1342 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001343 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001344 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001345 <
1346 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1347 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001348 >
zoecarver1997e0a2021-02-05 11:54:47 -08001349 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001350 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001351 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001352 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001353 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001354 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001355 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1356
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001357 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001358 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001359
1360 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001361 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001362 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001363 <
1364 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1365 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001366 >
zoecarver1997e0a2021-02-05 11:54:47 -08001367 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001368 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001369 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001370 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001371 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001372 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001373 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1374
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001375 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001376 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001377
1378 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001379 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001380 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001381 <
1382 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1383 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001384 >
zoecarver1997e0a2021-02-05 11:54:47 -08001385 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001386
1387 template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001388 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001389 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001390 <
1391 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1392 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001393 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001394 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1395
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001396 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001398 _LIBCPP_CONSTEXPR_AFTER_CXX17
1399 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1400 size_type __n2 = npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001401
Marshall Clow82513342016-09-24 22:45:42 +00001402 template <class _Tp>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001403 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001404 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001405 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001406 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001407 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001408 >
Marshall Clow82513342016-09-24 22:45:42 +00001409 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001410 _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(const value_type* __s) const _NOEXCEPT;
1411 _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1412 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001413 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414
Marshall Clow18c293b2017-12-04 20:11:38 +00001415#if _LIBCPP_STD_VER > 17
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001416 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001417 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001418 { return __self_view(data(), size()).starts_with(__sv); }
1419
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001420 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001421 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001422 { return !empty() && _Traits::eq(front(), __c); }
1423
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001424 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001425 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001426 { return starts_with(__self_view(__s)); }
1427
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001428 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001429 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001430 { return __self_view(data(), size()).ends_with( __sv); }
1431
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001432 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001433 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001434 { return !empty() && _Traits::eq(back(), __c); }
1435
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001436 constexpr _LIBCPP_HIDE_FROM_ABI
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001437 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001438 { return ends_with(__self_view(__s)); }
1439#endif
1440
Wim Leflere023c3542021-01-19 14:33:30 -05001441#if _LIBCPP_STD_VER > 20
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001442 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001443 bool contains(__self_view __sv) const noexcept
1444 { return __self_view(data(), size()).contains(__sv); }
1445
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001446 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001447 bool contains(value_type __c) const noexcept
1448 { return __self_view(data(), size()).contains(__c); }
1449
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001450 constexpr _LIBCPP_HIDE_FROM_ABI
Wim Leflere023c3542021-01-19 14:33:30 -05001451 bool contains(const value_type* __s) const
1452 { return __self_view(data(), size()).contains(__s); }
1453#endif
1454
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001455 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001456
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001457 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __clear_and_shrink() _NOEXCEPT;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001458
Louis Dionne510450b2022-04-01 16:38:30 -04001459#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001460
1461 bool __dereferenceable(const const_iterator* __i) const;
1462 bool __decrementable(const const_iterator* __i) const;
1463 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1464 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1465
Louis Dionne510450b2022-04-01 16:38:30 -04001466#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00001467
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468private:
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001469 template<class _Alloc>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001470 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001471 bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1472 const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1473
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001474 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __shrink_or_extend(size_type __target_capacity);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001475
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001476 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001477 bool __is_long() const _NOEXCEPT {
1478 if (__libcpp_is_constant_evaluated())
1479 return true;
1480 return __r_.first().__s.__is_long_;
1481 }
1482
1483 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __begin_lifetime(pointer __begin, size_type __n) {
1484#if _LIBCPP_STD_VER > 17
1485 if (__libcpp_is_constant_evaluated()) {
1486 for (size_type __i = 0; __i != __n; ++__i)
1487 std::construct_at(std::addressof(__begin[__i]));
1488 }
1489#else
1490 (void)__begin;
1491 (void)__n;
1492#endif // _LIBCPP_STD_VER > 17
1493 }
1494
1495 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __default_init() {
1496 __zero();
1497 if (__libcpp_is_constant_evaluated()) {
1498 size_type __sz = __recommend(0) + 1;
1499 pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1500 __begin_lifetime(__ptr, __sz);
1501 __set_long_pointer(__ptr);
1502 __set_long_cap(__sz);
1503 __set_long_size(0);
1504 }
1505 }
1506
1507 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __deallocate_constexpr() {
1508 if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1509 __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1510 }
1511
Nikolas Klauser93826d12022-01-04 17:24:03 +01001512 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1513 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1514 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1515 }
1516
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001517 template <class _ForwardIterator>
1518 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1519 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1520 size_type __sz = size();
1521 size_type __cap = capacity();
1522 value_type* __p;
1523 if (__cap - __sz >= __n)
1524 {
1525 __p = std::__to_address(__get_pointer());
1526 size_type __n_move = __sz - __ip;
1527 if (__n_move != 0)
1528 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1529 }
1530 else
1531 {
1532 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1533 __p = std::__to_address(__get_long_pointer());
1534 }
1535 __sz += __n;
1536 __set_size(__sz);
1537 traits_type::assign(__p[__sz], value_type());
1538 for (__p += __ip; __first != __last; ++__p, ++__first)
1539 traits_type::assign(*__p, *__first);
1540
1541 return begin() + __ip;
1542 }
1543
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001544 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1545 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001547 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001548 void __set_short_size(size_type __s) _NOEXCEPT {
1549 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1550 __r_.first().__s.__size_ = __s;
1551 __r_.first().__s.__is_long_ = false;
1552 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001553
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001554 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001555 size_type __get_short_size() const _NOEXCEPT {
1556 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1557 return __r_.first().__s.__size_;
1558 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001559
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001560 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001561 void __set_long_size(size_type __s) _NOEXCEPT
1562 {__r_.first().__l.__size_ = __s;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001563 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001564 size_type __get_long_size() const _NOEXCEPT
1565 {return __r_.first().__l.__size_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001566 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001567 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1569
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001570 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001571 void __set_long_cap(size_type __s) _NOEXCEPT {
1572 __r_.first().__l.__cap_ = __s / __endian_factor;
1573 __r_.first().__l.__is_long_ = true;
1574 }
1575
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001576 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser62060be2022-04-20 10:52:04 +02001577 size_type __get_long_cap() const _NOEXCEPT {
1578 return __r_.first().__l.__cap_ * __endian_factor;
1579 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001581 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001582 void __set_long_pointer(pointer __p) _NOEXCEPT
1583 {__r_.first().__l.__data_ = __p;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001584 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001585 pointer __get_long_pointer() _NOEXCEPT
1586 {return __r_.first().__l.__data_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001587 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001588 const_pointer __get_long_pointer() const _NOEXCEPT
1589 {return __r_.first().__l.__data_;}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001590 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001591 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001592 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001593 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001594 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001595 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001596 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001597 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001599 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001600 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1602
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001603 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001604 void __zero() _NOEXCEPT {
1605 __r_.first() = __rep();
1606 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607
1608 template <size_type __a> static
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001609 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea382952013-08-14 18:00:20 +00001610 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001611 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 enum {__alignment = 16};
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001613 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001614 size_type __recommend(size_type __s) _NOEXCEPT
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001615 {
1616 if (__s < __min_cap) {
1617 if (__libcpp_is_constant_evaluated())
1618 return static_cast<size_type>(__min_cap);
1619 else
1620 return static_cast<size_type>(__min_cap) - 1;
1621 }
Marshall Clow80584522018-02-07 21:30:17 +00001622 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1623 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1624 if (__guess == __min_cap) ++__guess;
1625 return __guess;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001626 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001628 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001629 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001630 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantd17880b2013-06-28 16:59:19 +00001631 void __init(const value_type* __s, size_type __sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001632 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001634
Martijn Vels5e7c9752020-03-04 17:52:46 -05001635 // Slow path for the (inlined) copy constructor for 'long' strings.
1636 // Always externally instantiated and not inlined.
1637 // Requires that __s is zero terminated.
1638 // The main reason for this function to exist is because for unstable, we
1639 // want to allow inlining of the copy constructor. However, we don't want
1640 // to call the __init() functions as those are marked as inline which may
1641 // result in over-aggressive inlining by the compiler, where our aim is
1642 // to only inline the fast path code directly in the ctor.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001643 _LIBCPP_CONSTEXPR_AFTER_CXX17 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001644
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645 template <class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001646 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001647 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001649 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1650 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651 __init(_InputIterator __first, _InputIterator __last);
1652
1653 template <class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001654 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04001655 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001657 __is_cpp17_forward_iterator<_ForwardIterator>::value
1658 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659 __init(_ForwardIterator __first, _ForwardIterator __last);
1660
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001661 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001663 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001664 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1666 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001667 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668
Martijn Vels596e3de2020-02-26 15:55:49 -05001669 // __assign_no_alias is invoked for assignment operations where we
1670 // have proof that the input does not alias the current instance.
1671 // For example, operator=(basic_string) performs a 'self' check.
1672 template <bool __is_short>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001673 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001674
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001675 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001676 void __erase_to_end(size_type __pos);
1677
Martijn Velsa81fc792020-02-26 13:25:43 -05001678 // __erase_external_with_move is invoked for erase() invocations where
1679 // `n ~= npos`, likely requiring memory moves on the string data.
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001680 _LIBCPP_CONSTEXPR_AFTER_CXX17 void __erase_external_with_move(size_type __pos, size_type __n);
Martijn Velsa81fc792020-02-26 13:25:43 -05001681
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001682 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001683 void __copy_assign_alloc(const basic_string& __str)
1684 {__copy_assign_alloc(__str, integral_constant<bool,
1685 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1686
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001687 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001688 void __copy_assign_alloc(const basic_string& __str, true_type)
1689 {
Marshall Clowf258c202017-01-31 03:40:52 +00001690 if (__alloc() == __str.__alloc())
1691 __alloc() = __str.__alloc();
1692 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001693 {
Marshall Clowf258c202017-01-31 03:40:52 +00001694 if (!__str.__is_long())
1695 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001696 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001697 __alloc() = __str.__alloc();
1698 }
1699 else
1700 {
1701 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001702 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001703 __begin_lifetime(__allocation.ptr, __allocation.count);
Vedant Kumar55e007e2018-03-08 21:15:26 +00001704 __clear_and_shrink();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001705 __alloc() = std::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001706 __set_long_pointer(__allocation.ptr);
1707 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001708 __set_long_size(__str.size());
1709 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001710 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001711 }
1712
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001713 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant28b24882011-12-01 20:21:04 +00001714 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001715 {}
1716
Eric Fiselierfc92be82017-04-19 00:28:44 +00001717#ifndef _LIBCPP_CXX03_LANG
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001718 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001719 void __move_assign(basic_string& __str, false_type)
1720 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001721 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001722 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001723#if _LIBCPP_STD_VER > 14
1724 _NOEXCEPT;
1725#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001726 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001727#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001728#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001729
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001730 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001731 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001732 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001733 _NOEXCEPT_(
1734 !__alloc_traits::propagate_on_container_move_assignment::value ||
1735 is_nothrow_move_assignable<allocator_type>::value)
1736 {__move_assign_alloc(__str, integral_constant<bool,
1737 __alloc_traits::propagate_on_container_move_assignment::value>());}
1738
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001739 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc2734962011-09-02 20:42:31 +00001740 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001741 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1742 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001743 __alloc() = std::move(__c.__alloc());
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001744 }
1745
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001746 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant28b24882011-12-01 20:21:04 +00001747 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001748 _NOEXCEPT
1749 {}
1750
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001751 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s);
1752 _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s, size_type __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001753
1754 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1755 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1756 pointer __p = __is_long()
1757 ? (__set_long_size(__n), __get_long_pointer())
1758 : (__set_short_size(__n), __get_short_pointer());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001759 traits_type::move(std::__to_address(__p), __s, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04001760 traits_type::assign(__p[__n], value_type());
1761 return *this;
1762 }
1763
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001764 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1765 basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001766 __set_size(__newsz);
1767 __invalidate_iterators_past(__newsz);
1768 traits_type::assign(__p[__newsz], value_type());
1769 return *this;
1770 }
1771
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001772 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001774 template<class _Tp>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001775 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001776 bool __addr_in_range(_Tp&& __t) const {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001777 // assume that the ranges overlap, because we can't check during constant evaluation
1778 if (__libcpp_is_constant_evaluated())
1779 return true;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001780 const volatile void *__p = std::addressof(__t);
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001781 return data() <= __p && __p <= data() + size();
1782 }
1783
Louis Dionned24191c2021-08-19 12:39:16 -04001784 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1785 void __throw_length_error() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001786 std::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001787 }
1788
1789 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1790 void __throw_out_of_range() const {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001791 std::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001792 }
1793
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001794 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const basic_string&);
1795 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const value_type*, const basic_string&);
1796 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(value_type, const basic_string&);
1797 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const value_type*);
1798 friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, value_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799};
1800
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001801// These declarations must appear before any functions are implicitly used
1802// so that they have the correct visibility specifier.
Louis Dionnedc496ec2021-06-08 17:25:08 -04001803#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001804#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnedc496ec2021-06-08 17:25:08 -04001805 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001806# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001807 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001808# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001809#else
Louis Dionnedc496ec2021-06-08 17:25:08 -04001810 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
Louis Dionne89258142021-08-23 15:32:36 -04001811# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc496ec2021-06-08 17:25:08 -04001812 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
Louis Dionne89258142021-08-23 15:32:36 -04001813# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001814#endif
Louis Dionnedc496ec2021-06-08 17:25:08 -04001815#undef _LIBCPP_DECLARE
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001816
1817
Louis Dionned59f8a52021-08-17 11:59:07 -04001818#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001819template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001820 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001821 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001822 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1823 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001824 >
1825basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1826 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001827
1828template<class _CharT,
1829 class _Traits,
1830 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001831 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001832 >
1833explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1834 -> basic_string<_CharT, _Traits, _Allocator>;
1835
1836template<class _CharT,
1837 class _Traits,
1838 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001839 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001840 class _Sz = typename allocator_traits<_Allocator>::size_type
1841 >
1842basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1843 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001844#endif
1845
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001847inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001849basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850{
Louis Dionne510450b2022-04-01 16:38:30 -04001851#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001852 if (!__libcpp_is_constant_evaluated()) {
1853 __c_node* __c = __get_db()->__find_c_and_lock(this);
1854 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001856 const_pointer __new_last = __get_pointer() + __pos;
1857 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001859 --__p;
1860 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1861 if (__i->base() > __new_last)
1862 {
1863 (*__p)->__c_ = nullptr;
1864 if (--__c->end_ != __p)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001865 std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001866 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001868 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869 }
1870 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001871#else
1872 (void)__pos;
Louis Dionne510450b2022-04-01 16:38:30 -04001873#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874}
1875
1876template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001877inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00001878basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001879 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001880 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001881{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001882 std::__debug_db_insert_c(this);
1883 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884}
1885
1886template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001887inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001889#if _LIBCPP_STD_VER <= 14
1890 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1891#else
1892 _NOEXCEPT
1893#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001894: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001895{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001896 std::__debug_db_insert_c(this);
1897 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898}
1899
1900template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001901_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier815ed732016-09-16 00:00:48 +00001902void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1903 size_type __sz,
1904 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001906 if (__libcpp_is_constant_evaluated())
1907 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001909 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001911 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912 {
1913 __set_short_size(__sz);
1914 __p = __get_short_pointer();
1915 }
1916 else
1917 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001918 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1919 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001920 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001922 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923 __set_long_size(__sz);
1924 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001925 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926 traits_type::assign(__p[__sz], value_type());
1927}
1928
1929template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001930_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001932basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001934 if (__libcpp_is_constant_evaluated())
1935 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001937 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001939 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 {
1941 __set_short_size(__sz);
1942 __p = __get_short_pointer();
1943 }
1944 else
1945 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001946 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1947 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001948 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001950 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951 __set_long_size(__sz);
1952 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001953 traits_type::copy(std::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954 traits_type::assign(__p[__sz], value_type());
1955}
1956
1957template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001958template <class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001959_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001960basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001961 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001963 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964 __init(__s, traits_type::length(__s));
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001965 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966}
1967
1968template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001969inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001970basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001971 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001973 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1974 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001975 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976}
1977
1978template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001979inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00001980basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001981 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001983 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984 __init(__s, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001985 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986}
1987
1988template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02001989_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001991 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992{
1993 if (!__str.__is_long())
1994 __r_.first().__r = __str.__r_.first().__r;
1995 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001996 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05001997 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02001998 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999}
2000
2001template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002002_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002003basic_string<_CharT, _Traits, _Allocator>::basic_string(
2004 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002005 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006{
2007 if (!__str.__is_long())
2008 __r_.first().__r = __str.__r_.first().__r;
2009 else
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002010 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
Martijn Vels5e7c9752020-03-04 17:52:46 -05002011 __str.__get_long_size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002012 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013}
2014
Martijn Vels5e7c9752020-03-04 17:52:46 -05002015template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002016_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Vels5e7c9752020-03-04 17:52:46 -05002017void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2018 const value_type* __s, size_type __sz) {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002019 if (__libcpp_is_constant_evaluated())
2020 __zero();
Martijn Vels5e7c9752020-03-04 17:52:46 -05002021 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002022 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05002023 __p = __get_short_pointer();
2024 __set_short_size(__sz);
2025 } else {
2026 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002027 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002028 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2029 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002030 __begin_lifetime(__p, __allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002031 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002032 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002033 __set_long_size(__sz);
2034 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002035 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
Martijn Vels5e7c9752020-03-04 17:52:46 -05002036}
2037
Eric Fiselierfc92be82017-04-19 00:28:44 +00002038#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039
2040template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002041inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e276872011-06-03 18:40:47 +00002042basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00002043#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00002044 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00002045#else
2046 _NOEXCEPT
2047#endif
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002048 : __r_(std::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002050 __str.__default_init();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002051 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002052 if (__is_long())
2053 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054}
2055
2056template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002057inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002059 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060{
Marshall Clowd2d24692014-07-17 15:32:20 +00002061 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002062 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002063 else
2064 {
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002065 if (__libcpp_is_constant_evaluated()) {
2066 __zero();
2067 __r_.first().__l = __str.__r_.first().__l;
2068 } else {
2069 __r_.first().__r = __str.__r_.first().__r;
2070 }
2071 __str.__default_init();
Marshall Clowd2d24692014-07-17 15:32:20 +00002072 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002073 std::__debug_db_insert_c(this);
Nikolas Klauser98833542022-05-07 22:20:23 +02002074 if (__is_long())
2075 std::__debug_db_swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076}
2077
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002078#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079
2080template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002081_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082void
2083basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2084{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002085 if (__libcpp_is_constant_evaluated())
2086 __zero();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002088 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002090 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091 {
2092 __set_short_size(__n);
2093 __p = __get_short_pointer();
2094 }
2095 else
2096 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002097 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2098 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002099 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002101 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102 __set_long_size(__n);
2103 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002104 traits_type::assign(std::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 traits_type::assign(__p[__n], value_type());
2106}
2107
2108template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002109inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002110basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002111 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112{
2113 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002114 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115}
2116
2117template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002118template <class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002119_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002120basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002121 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122{
2123 __init(__n, __c);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002124 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125}
2126
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002128_LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002129basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2130 size_type __pos, size_type __n,
2131 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002132 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133{
2134 size_type __str_sz = __str.size();
2135 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002136 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002137 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2138 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139}
2140
2141template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002142inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow83445802016-04-07 18:13:41 +00002143basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002144 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002145 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002146{
2147 size_type __str_sz = __str.size();
2148 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002149 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002150 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002151 std::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002152}
2153
2154template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002155template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002156_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow78dbe462016-11-14 18:22:19 +00002157basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002158 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002159 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002160{
Marshall Clowe46031a2018-07-02 18:41:15 +00002161 __self_view __sv0 = __t;
2162 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002163 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002164 std::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002165}
2166
2167template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002168template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002169_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +00002170basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002171 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002172{
Marshall Clowe46031a2018-07-02 18:41:15 +00002173 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002174 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002175 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002176}
2177
2178template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002179template <class _Tp, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002180_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowe46031a2018-07-02 18:41:15 +00002181basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002182 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002183{
Marshall Clowe46031a2018-07-02 18:41:15 +00002184 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002185 __init(__sv.data(), __sv.size());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002186 std::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002187}
2188
2189template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190template <class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002191_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002192__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002194 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2195>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2197{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002198 __default_init();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199#ifndef _LIBCPP_NO_EXCEPTIONS
2200 try
2201 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002202#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203 for (; __first != __last; ++__first)
2204 push_back(*__first);
2205#ifndef _LIBCPP_NO_EXCEPTIONS
2206 }
2207 catch (...)
2208 {
2209 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002210 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 throw;
2212 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002213#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214}
2215
2216template <class _CharT, class _Traits, class _Allocator>
2217template <class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002218_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002219__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002221 __is_cpp17_forward_iterator<_ForwardIterator>::value
2222>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2224{
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002225 if (__libcpp_is_constant_evaluated())
2226 __zero();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002227 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002229 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002231 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 {
2233 __set_short_size(__sz);
2234 __p = __get_short_pointer();
2235 }
2236 else
2237 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002238 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2239 __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002240 __begin_lifetime(__p, __allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002242 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 __set_long_size(__sz);
2244 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002245
2246#ifndef _LIBCPP_NO_EXCEPTIONS
2247 try
2248 {
2249#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002250 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 traits_type::assign(*__p, *__first);
2252 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002253#ifndef _LIBCPP_NO_EXCEPTIONS
2254 }
2255 catch (...)
2256 {
2257 if (__is_long())
2258 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2259 throw;
2260 }
2261#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262}
2263
2264template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002265template<class _InputIterator, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002266inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002268 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269{
2270 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002271 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272}
2273
2274template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002275template<class _InputIterator, class>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002276inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2278 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002279 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280{
2281 __init(__first, __last);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002282 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283}
2284
Eric Fiselierfc92be82017-04-19 00:28:44 +00002285#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002286
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002288inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002289basic_string<_CharT, _Traits, _Allocator>::basic_string(
2290 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002291 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292{
2293 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002294 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295}
2296
2297template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002298inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier812882b2017-02-17 01:17:10 +00002299basic_string<_CharT, _Traits, _Allocator>::basic_string(
2300 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002301 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302{
2303 __init(__il.begin(), __il.end());
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002304 std::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305}
2306
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002307#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002308
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002310_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2312{
Nikolas Klauser98833542022-05-07 22:20:23 +02002313 std::__debug_db_erase_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002315 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316}
2317
2318template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002319_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320void
2321basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2322 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002323 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 +00002324{
2325 size_type __ms = max_size();
2326 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002327 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328 pointer __old_p = __get_pointer();
2329 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002330 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002332 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2333 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002334 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002335 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002337 traits_type::copy(std::__to_address(__p),
2338 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339 if (__n_add != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002340 traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2342 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002343 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2344 std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002345 if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002346 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002348 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2350 __set_long_size(__old_sz);
2351 traits_type::assign(__p[__old_sz], value_type());
2352}
2353
2354template <class _CharT, class _Traits, class _Allocator>
2355void
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002356_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002357basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2358 size_type __n_copy, size_type __n_del, size_type __n_add)
2359{
2360 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002361 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002362 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363 pointer __old_p = __get_pointer();
2364 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002365 __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002367 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2368 pointer __p = __allocation.ptr;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002369 __begin_lifetime(__p, __allocation.count);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02002370 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371 if (__n_copy != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002372 traits_type::copy(std::__to_address(__p),
2373 std::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2375 if (__sec_cp_sz != 0)
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002376 traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2377 std::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002378 __sec_cp_sz);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002379 if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2380 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002382 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383}
2384
2385// assign
2386
2387template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002388template <bool __is_short>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002389_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsb6a08b62020-04-10 18:36:31 -04002390basic_string<_CharT, _Traits, _Allocator>&
2391basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002392 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002393 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002394 if (__n < __cap) {
2395 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2396 __is_short ? __set_short_size(__n) : __set_long_size(__n);
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002397 traits_type::copy(std::__to_address(__p), __s, __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05002398 traits_type::assign(__p[__n], value_type());
2399 __invalidate_iterators_past(__n);
2400 } else {
2401 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2402 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2403 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002404 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002405}
2406
2407template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002408_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002410basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2411 const value_type* __s, size_type __n) {
2412 size_type __cap = capacity();
2413 if (__cap >= __n) {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002414 value_type* __p = std::__to_address(__get_pointer());
Martijn Velsda7d94f2020-06-19 14:24:03 -04002415 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002416 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002417 } else {
2418 size_type __sz = size();
2419 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002420 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002421 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002422}
2423
2424template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002425_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsda7d94f2020-06-19 14:24:03 -04002426basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002427basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002428{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002429 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002430 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002431 ? __assign_short(__s, __n)
2432 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433}
2434
2435template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002436_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437basic_string<_CharT, _Traits, _Allocator>&
2438basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2439{
2440 size_type __cap = capacity();
2441 if (__cap < __n)
2442 {
2443 size_type __sz = size();
2444 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2445 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002446 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002448 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449}
2450
2451template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002452_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453basic_string<_CharT, _Traits, _Allocator>&
2454basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2455{
2456 pointer __p;
2457 if (__is_long())
2458 {
2459 __p = __get_long_pointer();
2460 __set_long_size(1);
2461 }
2462 else
2463 {
2464 __p = __get_short_pointer();
2465 __set_short_size(1);
2466 }
2467 traits_type::assign(*__p, __c);
2468 traits_type::assign(*++__p, value_type());
2469 __invalidate_iterators_past(1);
2470 return *this;
2471}
2472
2473template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002474_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002475basic_string<_CharT, _Traits, _Allocator>&
2476basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2477{
Martijn Vels596e3de2020-02-26 15:55:49 -05002478 if (this != &__str) {
2479 __copy_assign_alloc(__str);
2480 if (!__is_long()) {
2481 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002482 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002483 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002484 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002485 }
2486 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002487 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002488 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002489 }
2490 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002491}
2492
Eric Fiselierfc92be82017-04-19 00:28:44 +00002493#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002494
2495template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002496inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002497void
2498basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002499 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002500{
2501 if (__alloc() != __str.__alloc())
2502 assign(__str);
2503 else
2504 __move_assign(__str, true_type());
2505}
2506
2507template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002508inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002509void
2510basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002511#if _LIBCPP_STD_VER > 14
2512 _NOEXCEPT
2513#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002514 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002515#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002516{
marshall2ed41622019-11-27 07:13:00 -08002517 if (__is_long()) {
2518 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2519 __get_long_cap());
2520#if _LIBCPP_STD_VER <= 14
2521 if (!is_nothrow_move_assignable<allocator_type>::value) {
2522 __set_short_size(0);
2523 traits_type::assign(__get_short_pointer()[0], value_type());
2524 }
2525#endif
2526 }
2527 __move_assign_alloc(__str);
2528 __r_.first() = __str.__r_.first();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002529 if (__libcpp_is_constant_evaluated()) {
2530 __str.__default_init();
2531 } else {
2532 __str.__set_short_size(0);
2533 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2534 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002535}
2536
2537template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002538inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002539basic_string<_CharT, _Traits, _Allocator>&
2540basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002541 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002542{
2543 __move_assign(__str, integral_constant<bool,
2544 __alloc_traits::propagate_on_container_move_assignment::value>());
2545 return *this;
2546}
2547
2548#endif
2549
2550template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002552_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002553__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002555 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002557>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2559{
Marshall Clow958362f2016-09-05 01:54:30 +00002560 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002561 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002562 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563}
2564
2565template <class _CharT, class _Traits, class _Allocator>
2566template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002567_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002568__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002570 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002572>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2574{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002576 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002577 static_cast<size_type>(std::distance(__first, __last)) : 0;
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002578
2579 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2580 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002582 if (__cap < __n)
2583 {
2584 size_type __sz = size();
2585 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2586 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002587 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002588 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002589 traits_type::assign(*__p, *__first);
2590 traits_type::assign(*__p, value_type());
2591 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002592 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 }
2594 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002595 {
2596 const basic_string __temp(__first, __last, __alloc());
2597 assign(__temp.data(), __temp.size());
2598 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599 return *this;
2600}
2601
2602template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002603_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604basic_string<_CharT, _Traits, _Allocator>&
2605basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2606{
2607 size_type __sz = __str.size();
2608 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002609 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002610 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611}
2612
2613template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002614template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002615_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002616__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002617<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002618 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2619 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002620 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002621>
Marshall Clow82513342016-09-24 22:45:42 +00002622basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002623{
Marshall Clow82513342016-09-24 22:45:42 +00002624 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002625 size_type __sz = __sv.size();
2626 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002627 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002628 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002629}
2630
2631
2632template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002633_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002635basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2636 return __assign_external(__s, traits_type::length(__s));
2637}
2638
2639template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002640_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsda7d94f2020-06-19 14:24:03 -04002641basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002642basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002644 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002645 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002646 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002647 ? __assign_short(__s, traits_type::length(__s))
2648 : __assign_external(__s, traits_type::length(__s)))
2649 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651// append
2652
2653template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002654_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002656basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002658 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 size_type __cap = capacity();
2660 size_type __sz = size();
2661 if (__cap - __sz >= __n)
2662 {
2663 if (__n)
2664 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002665 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666 traits_type::copy(__p + __sz, __s, __n);
2667 __sz += __n;
2668 __set_size(__sz);
2669 traits_type::assign(__p[__sz], value_type());
2670 }
2671 }
2672 else
2673 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2674 return *this;
2675}
2676
2677template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002678_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679basic_string<_CharT, _Traits, _Allocator>&
2680basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2681{
2682 if (__n)
2683 {
2684 size_type __cap = capacity();
2685 size_type __sz = size();
2686 if (__cap - __sz < __n)
2687 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2688 pointer __p = __get_pointer();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002689 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690 __sz += __n;
2691 __set_size(__sz);
2692 traits_type::assign(__p[__sz], value_type());
2693 }
2694 return *this;
2695}
2696
2697template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002698_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00002699basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2700{
2701 if (__n)
2702 {
2703 size_type __cap = capacity();
2704 size_type __sz = size();
2705 if (__cap - __sz < __n)
2706 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2707 pointer __p = __get_pointer();
2708 __sz += __n;
2709 __set_size(__sz);
2710 traits_type::assign(__p[__sz], value_type());
2711 }
2712}
2713
2714template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002715_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716void
2717basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2718{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002719 bool __is_short = !__is_long();
2720 size_type __cap;
2721 size_type __sz;
2722 if (__is_short)
2723 {
2724 __cap = __min_cap - 1;
2725 __sz = __get_short_size();
2726 }
2727 else
2728 {
2729 __cap = __get_long_cap() - 1;
2730 __sz = __get_long_size();
2731 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002733 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002735 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002736 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002737 pointer __p = __get_pointer();
Howard Hinnant68bf1812013-04-30 21:44:48 +00002738 if (__is_short)
2739 {
2740 __p = __get_short_pointer() + __sz;
2741 __set_short_size(__sz+1);
2742 }
2743 else
2744 {
2745 __p = __get_long_pointer() + __sz;
2746 __set_long_size(__sz+1);
2747 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748 traits_type::assign(*__p, __c);
2749 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750}
2751
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752template <class _CharT, class _Traits, class _Allocator>
2753template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002754_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002755__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002756<
2757 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2758 basic_string<_CharT, _Traits, _Allocator>&
2759>
2760basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002761 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762{
2763 size_type __sz = size();
2764 size_type __cap = capacity();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002765 size_type __n = static_cast<size_type>(std::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766 if (__n)
2767 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002768 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2769 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002770 {
2771 if (__cap - __sz < __n)
2772 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2773 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002774 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002775 traits_type::assign(*__p, *__first);
2776 traits_type::assign(*__p, value_type());
2777 __set_size(__sz + __n);
2778 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002779 else
2780 {
2781 const basic_string __temp(__first, __last, __alloc());
2782 append(__temp.data(), __temp.size());
2783 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784 }
2785 return *this;
2786}
2787
2788template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002789inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790basic_string<_CharT, _Traits, _Allocator>&
2791basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2792{
2793 return append(__str.data(), __str.size());
2794}
2795
2796template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002797_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798basic_string<_CharT, _Traits, _Allocator>&
2799basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2800{
2801 size_type __sz = __str.size();
2802 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002803 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002804 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805}
2806
2807template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002808template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002809_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002810 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002811 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002812 __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 +00002813 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002814 >
Marshall Clow82513342016-09-24 22:45:42 +00002815basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002816{
Marshall Clow82513342016-09-24 22:45:42 +00002817 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002818 size_type __sz = __sv.size();
2819 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002820 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002821 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002822}
2823
2824template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002825_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002827basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002829 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 return append(__s, traits_type::length(__s));
2831}
2832
2833// insert
2834
2835template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002836_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002838basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002840 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841 size_type __sz = size();
2842 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002843 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844 size_type __cap = capacity();
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002845 if (__libcpp_is_constant_evaluated()) {
2846 if (__cap - __sz >= __n)
2847 __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2848 else
2849 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2850 return *this;
2851 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852 if (__cap - __sz >= __n)
2853 {
2854 if (__n)
2855 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002856 value_type* __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 size_type __n_move = __sz - __pos;
2858 if (__n_move != 0)
2859 {
2860 if (__p + __pos <= __s && __s < __p + __sz)
2861 __s += __n;
2862 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2863 }
2864 traits_type::move(__p + __pos, __s, __n);
2865 __sz += __n;
2866 __set_size(__sz);
2867 traits_type::assign(__p[__sz], value_type());
2868 }
2869 }
2870 else
2871 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2872 return *this;
2873}
2874
2875template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002876_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877basic_string<_CharT, _Traits, _Allocator>&
2878basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2879{
2880 size_type __sz = size();
2881 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002882 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883 if (__n)
2884 {
2885 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002886 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887 if (__cap - __sz >= __n)
2888 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002889 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890 size_type __n_move = __sz - __pos;
2891 if (__n_move != 0)
2892 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2893 }
2894 else
2895 {
2896 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002897 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898 }
2899 traits_type::assign(__p + __pos, __n, __c);
2900 __sz += __n;
2901 __set_size(__sz);
2902 traits_type::assign(__p[__sz], value_type());
2903 }
2904 return *this;
2905}
2906
2907template <class _CharT, class _Traits, class _Allocator>
2908template<class _InputIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002909_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002910__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002912 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002913 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002914>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2916{
Nikolas Klausereed25832021-12-15 01:32:30 +01002917 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2918 "string::insert(iterator, range) called with an iterator not"
2919 " referring to this string");
2920 const basic_string __temp(__first, __last, __alloc());
2921 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922}
2923
2924template <class _CharT, class _Traits, class _Allocator>
2925template<class _ForwardIterator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002926_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002927__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002929 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002931>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2933{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002934 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2935 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002936
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002938 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2939 if (__n == 0)
2940 return begin() + __ip;
2941
2942 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002944 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002946 else
2947 {
2948 const basic_string __temp(__first, __last, __alloc());
2949 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2950 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951}
2952
2953template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002954inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955basic_string<_CharT, _Traits, _Allocator>&
2956basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2957{
2958 return insert(__pos1, __str.data(), __str.size());
2959}
2960
2961template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002962_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963basic_string<_CharT, _Traits, _Allocator>&
2964basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2965 size_type __pos2, size_type __n)
2966{
2967 size_type __str_sz = __str.size();
2968 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002969 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002970 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971}
2972
2973template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002974template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002975_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04002976__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002977<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002978 __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 +00002979 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002980>
Marshall Clow82513342016-09-24 22:45:42 +00002981basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002982 size_type __pos2, size_type __n)
2983{
Marshall Clow82513342016-09-24 22:45:42 +00002984 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002985 size_type __str_sz = __sv.size();
2986 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002987 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02002988 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002989}
2990
2991template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02002992_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002994basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002996 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997 return insert(__pos, __s, traits_type::length(__s));
2998}
2999
3000template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003001_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002typename basic_string<_CharT, _Traits, _Allocator>::iterator
3003basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
3004{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05003005 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3006 "string::insert(iterator, character) called with an iterator not"
3007 " referring to this string");
3008
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009 size_type __ip = static_cast<size_type>(__pos - begin());
3010 size_type __sz = size();
3011 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003012 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013 if (__cap == __sz)
3014 {
3015 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003016 __p = std::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003017 }
3018 else
3019 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003020 __p = std::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021 size_type __n_move = __sz - __ip;
3022 if (__n_move != 0)
3023 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3024 }
3025 traits_type::assign(__p[__ip], __c);
3026 traits_type::assign(__p[++__sz], value_type());
3027 __set_size(__sz);
3028 return begin() + static_cast<difference_type>(__ip);
3029}
3030
3031template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003032inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033typename basic_string<_CharT, _Traits, _Allocator>::iterator
3034basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3035{
Nikolas Klausereed25832021-12-15 01:32:30 +01003036 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3037 "string::insert(iterator, n, value) called with an iterator not"
3038 " referring to this string");
3039 difference_type __p = __pos - begin();
3040 insert(static_cast<size_type>(__p), __n, __c);
3041 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042}
3043
3044// replace
3045
3046template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003047_LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02003101_LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02003132_LIBCPP_CONSTEXPR_AFTER_CXX17
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 Klauser80b3cf32022-04-27 10:11:44 +02003146inline _LIBCPP_CONSTEXPR_AFTER_CXX17
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{
3150 return replace(__pos1, __n1, __str.data(), __str.size());
3151}
3152
3153template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003154_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155basic_string<_CharT, _Traits, _Allocator>&
3156basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3157 size_type __pos2, size_type __n2)
3158{
3159 size_type __str_sz = __str.size();
3160 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003161 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003162 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163}
3164
3165template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003166template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003167_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003168__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003169<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003170 __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 +00003171 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003172>
Marshall Clow82513342016-09-24 22:45:42 +00003173basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003174 size_type __pos2, size_type __n2)
3175{
Marshall Clow82513342016-09-24 22:45:42 +00003176 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003177 size_type __str_sz = __sv.size();
3178 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003179 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003180 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003181}
3182
3183template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003184_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003185basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003186basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003188 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189 return replace(__pos, __n1, __s, traits_type::length(__s));
3190}
3191
3192template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003193inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003195basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196{
3197 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3198 __str.data(), __str.size());
3199}
3200
3201template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003202inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003204basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205{
3206 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3207}
3208
3209template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003210inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003212basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213{
3214 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3215}
3216
3217template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003218inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003220basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221{
3222 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3223}
3224
3225// erase
3226
Martijn Velsa81fc792020-02-26 13:25:43 -05003227// 'externally instantiated' erase() implementation, called when __n != npos.
3228// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003230_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsa81fc792020-02-26 13:25:43 -05003231void
3232basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3233 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235 if (__n)
3236 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003237 size_type __sz = size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003238 value_type* __p = std::__to_address(__get_pointer());
3239 __n = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240 size_type __n_move = __sz - __pos - __n;
3241 if (__n_move != 0)
3242 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003243 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003244 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003245}
3246
3247template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003248_LIBCPP_CONSTEXPR_AFTER_CXX17
Martijn Velsa81fc792020-02-26 13:25:43 -05003249basic_string<_CharT, _Traits, _Allocator>&
3250basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3251 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003252 if (__pos > size())
3253 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003254 if (__n == npos) {
3255 __erase_to_end(__pos);
3256 } else {
3257 __erase_external_with_move(__pos, __n);
3258 }
3259 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260}
3261
3262template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003263inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264typename basic_string<_CharT, _Traits, _Allocator>::iterator
3265basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3266{
Nikolas Klausereed25832021-12-15 01:32:30 +01003267 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3268 "string::erase(iterator) called with an iterator not"
3269 " referring to this string");
3270
3271 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3272 iterator __b = begin();
3273 size_type __r = static_cast<size_type>(__pos - __b);
3274 erase(__r, 1);
3275 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276}
3277
3278template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003279inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280typename basic_string<_CharT, _Traits, _Allocator>::iterator
3281basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3282{
Nikolas Klausereed25832021-12-15 01:32:30 +01003283 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3284 "string::erase(iterator, iterator) called with an iterator not"
3285 " referring to this string");
3286
3287 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3288 iterator __b = begin();
3289 size_type __r = static_cast<size_type>(__first - __b);
3290 erase(__r, static_cast<size_type>(__last - __first));
3291 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003292}
3293
3294template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003295inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296void
3297basic_string<_CharT, _Traits, _Allocator>::pop_back()
3298{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003299 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003300 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003301}
3302
3303template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003304inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003306basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307{
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003308 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309 if (__is_long())
3310 {
3311 traits_type::assign(*__get_long_pointer(), value_type());
3312 __set_long_size(0);
3313 }
3314 else
3315 {
3316 traits_type::assign(*__get_short_pointer(), value_type());
3317 __set_short_size(0);
3318 }
3319}
3320
3321template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003322inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323void
3324basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3325{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003326 __null_terminate_at(std::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327}
3328
3329template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003330_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331void
3332basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3333{
3334 size_type __sz = size();
3335 if (__n > __sz)
3336 append(__n - __sz, __c);
3337 else
3338 __erase_to_end(__n);
3339}
3340
3341template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003342_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
Eric Fiselier451d5582018-11-26 20:15:38 +00003343basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3344{
3345 size_type __sz = size();
3346 if (__n > __sz) {
3347 __append_default_init(__n - __sz);
3348 } else
3349 __erase_to_end(__n);
3350}
3351
3352template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003353inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003355basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003357 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003358 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3359 return __m - __alignment;
3360 } else {
3361 bool __uses_lsb = __endian_factor == 2;
3362 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3363 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364}
3365
3366template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003367_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368void
Marek Kurdejc9848142020-11-26 10:07:16 +01003369basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370{
Marek Kurdejc9848142020-11-26 10:07:16 +01003371 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003372 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003373
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003374 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3375 // and later (since P0966R1), however we provide consistent behavior in all Standard
3376 // modes because this function is instantiated in the shared library.
3377 if (__requested_capacity <= capacity())
3378 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003379
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003380 size_type __target_capacity = std::max(__requested_capacity, size());
Marek Kurdejc9848142020-11-26 10:07:16 +01003381 __target_capacity = __recommend(__target_capacity);
3382 if (__target_capacity == capacity()) return;
3383
3384 __shrink_or_extend(__target_capacity);
3385}
3386
3387template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003388inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marek Kurdejc9848142020-11-26 10:07:16 +01003389void
3390basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3391{
3392 size_type __target_capacity = __recommend(size());
3393 if (__target_capacity == capacity()) return;
3394
3395 __shrink_or_extend(__target_capacity);
3396}
3397
3398template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003399inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marek Kurdejc9848142020-11-26 10:07:16 +01003400void
3401basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3402{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003403 size_type __cap = capacity();
3404 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003405
3406 pointer __new_data, __p;
3407 bool __was_long, __now_long;
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003408 if (__fits_in_sso(__target_capacity))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003410 __was_long = true;
3411 __now_long = false;
3412 __new_data = __get_short_pointer();
3413 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003415 else
3416 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003417 if (__target_capacity > __cap) {
3418 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3419 __new_data = __allocation.ptr;
3420 __target_capacity = __allocation.count - 1;
3421 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003422 else
3423 {
3424 #ifndef _LIBCPP_NO_EXCEPTIONS
3425 try
3426 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003427 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003428 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3429 __new_data = __allocation.ptr;
3430 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003431 #ifndef _LIBCPP_NO_EXCEPTIONS
3432 }
3433 catch (...)
3434 {
3435 return;
3436 }
3437 #else // _LIBCPP_NO_EXCEPTIONS
3438 if (__new_data == nullptr)
3439 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003440 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003441 }
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003442 __begin_lifetime(__new_data, __target_capacity + 1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003443 __now_long = true;
3444 __was_long = __is_long();
3445 __p = __get_pointer();
3446 }
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003447 traits_type::copy(std::__to_address(__new_data),
3448 std::__to_address(__p), size()+1);
Marek Kurdejc9848142020-11-26 10:07:16 +01003449 if (__was_long)
3450 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3451 if (__now_long)
3452 {
3453 __set_long_cap(__target_capacity+1);
3454 __set_long_size(__sz);
3455 __set_long_pointer(__new_data);
3456 }
3457 else
3458 __set_short_size(__sz);
Nikolas Klauserf1d286b2022-05-08 16:40:04 +02003459 std::__debug_db_invalidate_all(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460}
3461
3462template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003463inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003465basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003467 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468 return *(data() + __pos);
3469}
3470
3471template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003472inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003474basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003476 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477 return *(__get_pointer() + __pos);
3478}
3479
3480template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003481_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3483basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3484{
3485 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003486 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487 return (*this)[__n];
3488}
3489
3490template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003491_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492typename basic_string<_CharT, _Traits, _Allocator>::reference
3493basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3494{
3495 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003496 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497 return (*this)[__n];
3498}
3499
3500template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003501inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003503basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003505 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506 return *__get_pointer();
3507}
3508
3509template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003510inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003512basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003514 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515 return *data();
3516}
3517
3518template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003519inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003521basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003523 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524 return *(__get_pointer() + size() - 1);
3525}
3526
3527template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003528inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003530basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003532 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533 return *(data() + size() - 1);
3534}
3535
3536template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003537_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003539basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540{
3541 size_type __sz = size();
3542 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003543 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003544 size_type __rlen = std::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545 traits_type::copy(__s, data() + __pos, __rlen);
3546 return __rlen;
3547}
3548
3549template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003550inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551basic_string<_CharT, _Traits, _Allocator>
3552basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3553{
3554 return basic_string(*this, __pos, __n, __alloc());
3555}
3556
3557template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003558inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559void
3560basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003561#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003562 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003563#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003564 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003565 __is_nothrow_swappable<allocator_type>::value)
3566#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567{
Nikolas Klauser98833542022-05-07 22:20:23 +02003568 if (!__is_long())
3569 std::__debug_db_invalidate_all(this);
3570 if (!__str.__is_long())
3571 std::__debug_db_invalidate_all(&__str);
3572 std::__debug_db_swap(this, &__str);
3573
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003574 _LIBCPP_ASSERT(
3575 __alloc_traits::propagate_on_container_swap::value ||
3576 __alloc_traits::is_always_equal::value ||
3577 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003578 std::swap(__r_.first(), __str.__r_.first());
3579 std::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580}
3581
3582// find
3583
3584template <class _Traits>
3585struct _LIBCPP_HIDDEN __traits_eq
3586{
3587 typedef typename _Traits::char_type char_type;
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003588 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003589 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3590 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591};
3592
3593template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003594_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003596basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003597 size_type __pos,
3598 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003600 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003601 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003602 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603}
3604
3605template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003606inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003608basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3609 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003611 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003612 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613}
3614
3615template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003616template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003617_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003618__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003619<
3620 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3621 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003622>
Marshall Clowe46031a2018-07-02 18:41:15 +00003623basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003624 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003625{
Marshall Clowe46031a2018-07-02 18:41:15 +00003626 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003627 return __str_find<value_type, size_type, traits_type, npos>
3628 (data(), size(), __sv.data(), __pos, __sv.size());
3629}
3630
3631template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003632inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003633typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003634basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003635 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003637 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003638 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003639 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003640}
3641
3642template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003643_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003645basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3646 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003648 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003649 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650}
3651
3652// rfind
3653
3654template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003655_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003657basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003658 size_type __pos,
3659 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003661 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003662 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003663 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664}
3665
3666template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003667inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003669basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3670 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003672 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003673 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674}
3675
3676template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003677template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003678_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003679__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003680<
3681 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3682 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003683>
Marshall Clowe46031a2018-07-02 18:41:15 +00003684basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003685 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003686{
Marshall Clowe46031a2018-07-02 18:41:15 +00003687 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003688 return __str_rfind<value_type, size_type, traits_type, npos>
3689 (data(), size(), __sv.data(), __pos, __sv.size());
3690}
3691
3692template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003693inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003695basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003696 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003698 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003699 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003700 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701}
3702
3703template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003704_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003706basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3707 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003709 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003710 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711}
3712
3713// find_first_of
3714
3715template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003716_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003718basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003719 size_type __pos,
3720 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003722 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003723 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003724 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725}
3726
3727template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003728inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003730basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3731 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003733 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003734 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735}
3736
3737template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003738template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003739_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003740__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003741<
3742 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3743 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003744>
Marshall Clowe46031a2018-07-02 18:41:15 +00003745basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003746 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003747{
Marshall Clowe46031a2018-07-02 18:41:15 +00003748 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003749 return __str_find_first_of<value_type, size_type, traits_type, npos>
3750 (data(), size(), __sv.data(), __pos, __sv.size());
3751}
3752
3753template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003754inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003755typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003756basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003757 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003759 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003760 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003761 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762}
3763
3764template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003765inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003767basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3768 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769{
3770 return find(__c, __pos);
3771}
3772
3773// find_last_of
3774
3775template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003776inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003778basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003779 size_type __pos,
3780 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003782 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003783 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003784 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785}
3786
3787template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003788inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003790basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3791 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003793 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003794 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795}
3796
3797template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003798template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003799_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003800__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003801<
3802 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3803 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003804>
Marshall Clowe46031a2018-07-02 18:41:15 +00003805basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003806 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003807{
Marshall Clowe46031a2018-07-02 18:41:15 +00003808 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003809 return __str_find_last_of<value_type, size_type, traits_type, npos>
3810 (data(), size(), __sv.data(), __pos, __sv.size());
3811}
3812
3813template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003814inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003815typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003816basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003817 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003819 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003820 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003821 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822}
3823
3824template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003825inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003826typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003827basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3828 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003829{
3830 return rfind(__c, __pos);
3831}
3832
3833// find_first_not_of
3834
3835template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003836_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003838basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003839 size_type __pos,
3840 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003841{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003842 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003843 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003844 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845}
3846
3847template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003848inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003850basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3851 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003852{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003853 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003854 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855}
3856
3857template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003858template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003859_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003860__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003861<
3862 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3863 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003864>
Marshall Clowe46031a2018-07-02 18:41:15 +00003865basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003866 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003867{
Marshall Clowe46031a2018-07-02 18:41:15 +00003868 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003869 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3870 (data(), size(), __sv.data(), __pos, __sv.size());
3871}
3872
3873template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003874inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003875typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003876basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003877 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003879 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003880 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003881 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882}
3883
3884template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003885inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003887basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3888 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003890 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003891 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892}
3893
3894// find_last_not_of
3895
3896template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003897_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003899basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003900 size_type __pos,
3901 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003903 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003904 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003905 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003906}
3907
3908template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003909inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003910typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003911basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3912 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003914 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003915 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003916}
3917
3918template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003919template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003920_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003921__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003922<
3923 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3924 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003925>
Marshall Clowe46031a2018-07-02 18:41:15 +00003926basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003927 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003928{
Marshall Clowe46031a2018-07-02 18:41:15 +00003929 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003930 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3931 (data(), size(), __sv.data(), __pos, __sv.size());
3932}
3933
3934template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003935inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003936typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003937basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003938 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003940 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003941 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003942 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003943}
3944
3945template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003946inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003948basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3949 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003951 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003952 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003953}
3954
3955// compare
3956
3957template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003958template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003959_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04003960__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003961<
3962 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3963 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003964>
zoecarver1997e0a2021-02-05 11:54:47 -08003965basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966{
Marshall Clowe46031a2018-07-02 18:41:15 +00003967 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003968 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003969 size_t __rhs_sz = __sv.size();
3970 int __result = traits_type::compare(data(), __sv.data(),
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02003971 std::min(__lhs_sz, __rhs_sz));
Howard Hinnantb0485532011-07-24 21:45:06 +00003972 if (__result != 0)
3973 return __result;
3974 if (__lhs_sz < __rhs_sz)
3975 return -1;
3976 if (__lhs_sz > __rhs_sz)
3977 return 1;
3978 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979}
3980
3981template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003982inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003984basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003986 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987}
3988
3989template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02003990inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003992basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3993 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003994 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003995 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003997 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998 size_type __sz = size();
3999 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004000 __throw_out_of_range();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004001 size_type __rlen = std::min(__n1, __sz - __pos1);
4002 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003 if (__r == 0)
4004 {
4005 if (__rlen < __n2)
4006 __r = -1;
4007 else if (__rlen > __n2)
4008 __r = 1;
4009 }
4010 return __r;
4011}
4012
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004013template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00004014template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004015_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04004016__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00004017<
4018 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
4019 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004020>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004021basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4022 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00004023 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004024{
Marshall Clowe46031a2018-07-02 18:41:15 +00004025 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004026 return compare(__pos1, __n1, __sv.data(), __sv.size());
4027}
4028
4029template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004030inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004031int
4032basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4033 size_type __n1,
4034 const basic_string& __str) const
4035{
4036 return compare(__pos1, __n1, __str.data(), __str.size());
4037}
4038
4039template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00004040template <class _Tp>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004041_LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne9ce598d2021-09-08 09:14:43 -04004042__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00004043<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004044 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
4045 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00004046 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05004047>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004048basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4049 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00004050 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004051 size_type __pos2,
4052 size_type __n2) const
4053{
Marshall Clow82513342016-09-24 22:45:42 +00004054 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004055 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
4056}
4057
4058template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004059_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004060int
4061basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4062 size_type __n1,
4063 const basic_string& __str,
4064 size_type __pos2,
4065 size_type __n2) const
4066{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004067 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004068}
4069
4070template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004071_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004072int
4073basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4074{
4075 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4076 return compare(0, npos, __s, traits_type::length(__s));
4077}
4078
4079template <class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004080_LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +00004081int
4082basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4083 size_type __n1,
4084 const value_type* __s) const
4085{
4086 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4087 return compare(__pos1, __n1, __s, traits_type::length(__s));
4088}
4089
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090// __invariants
4091
4092template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004093inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004094bool
4095basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4096{
4097 if (size() > capacity())
4098 return false;
4099 if (capacity() < __min_cap - 1)
4100 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004101 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004103 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104 return false;
4105 return true;
4106}
4107
Vedant Kumar55e007e2018-03-08 21:15:26 +00004108// __clear_and_shrink
4109
4110template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004111inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne173f29e2019-05-29 16:01:36 +00004112void
Marshall Clowe60a7182018-05-29 17:04:37 +00004113basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004114{
4115 clear();
4116 if(__is_long())
4117 {
4118 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4119 __set_long_cap(0);
4120 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004121 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004122 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004123}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004124
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125// operator==
4126
4127template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004128inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129bool
4130operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004131 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004133 size_t __lhs_sz = __lhs.size();
4134 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4135 __rhs.data(),
4136 __lhs_sz) == 0;
4137}
4138
4139template<class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004140inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004141bool
4142operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4143 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4144{
4145 size_t __lhs_sz = __lhs.size();
4146 if (__lhs_sz != __rhs.size())
4147 return false;
4148 const char* __lp = __lhs.data();
4149 const char* __rp = __rhs.data();
4150 if (__lhs.__is_long())
4151 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4152 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4153 if (*__lp != *__rp)
4154 return false;
4155 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156}
4157
4158template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004159inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004161operator==(const _CharT* __lhs,
4162 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004163{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004164 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4165 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4166 size_t __lhs_len = _Traits::length(__lhs);
4167 if (__lhs_len != __rhs.size()) return false;
4168 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169}
4170
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004172inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004174operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4175 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004177 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4178 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4179 size_t __rhs_len = _Traits::length(__rhs);
4180 if (__rhs_len != __lhs.size()) return false;
4181 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182}
4183
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004184template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004185inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186bool
4187operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004188 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004189{
4190 return !(__lhs == __rhs);
4191}
4192
4193template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004194inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004196operator!=(const _CharT* __lhs,
4197 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198{
4199 return !(__lhs == __rhs);
4200}
4201
4202template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004203inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004205operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4206 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207{
4208 return !(__lhs == __rhs);
4209}
4210
4211// operator<
4212
4213template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004214inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215bool
4216operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004217 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004219 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220}
4221
4222template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004223inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004225operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4226 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004228 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229}
4230
4231template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004232inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004234operator< (const _CharT* __lhs,
4235 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004236{
4237 return __rhs.compare(__lhs) > 0;
4238}
4239
Howard Hinnantc51e1022010-05-11 19:42:16 +00004240// operator>
4241
4242template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004243inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244bool
4245operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004246 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247{
4248 return __rhs < __lhs;
4249}
4250
4251template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004252inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004254operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4255 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256{
4257 return __rhs < __lhs;
4258}
4259
4260template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004261inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004262bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004263operator> (const _CharT* __lhs,
4264 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265{
4266 return __rhs < __lhs;
4267}
4268
4269// operator<=
4270
4271template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004272inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273bool
4274operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004275 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276{
4277 return !(__rhs < __lhs);
4278}
4279
4280template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004281inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004283operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4284 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285{
4286 return !(__rhs < __lhs);
4287}
4288
4289template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004290inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004291bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004292operator<=(const _CharT* __lhs,
4293 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294{
4295 return !(__rhs < __lhs);
4296}
4297
4298// operator>=
4299
4300template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004301inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302bool
4303operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004304 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305{
4306 return !(__lhs < __rhs);
4307}
4308
4309template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004310inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004312operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4313 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314{
4315 return !(__lhs < __rhs);
4316}
4317
4318template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004319inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004320bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004321operator>=(const _CharT* __lhs,
4322 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323{
4324 return !(__lhs < __rhs);
4325}
4326
4327// operator +
4328
4329template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004330_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331basic_string<_CharT, _Traits, _Allocator>
4332operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4333 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4334{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004335 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004336 auto __lhs_sz = __lhs.size();
4337 auto __rhs_sz = __rhs.size();
4338 _String __r(__uninitialized_size_tag(),
4339 __lhs_sz + __rhs_sz,
4340 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4341 auto __ptr = std::__to_address(__r.__get_pointer());
4342 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4343 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4344 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345 return __r;
4346}
4347
4348template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004349_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350basic_string<_CharT, _Traits, _Allocator>
4351operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4352{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004353 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004354 auto __lhs_sz = _Traits::length(__lhs);
4355 auto __rhs_sz = __rhs.size();
4356 _String __r(__uninitialized_size_tag(),
4357 __lhs_sz + __rhs_sz,
4358 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4359 auto __ptr = std::__to_address(__r.__get_pointer());
4360 _Traits::copy(__ptr, __lhs, __lhs_sz);
4361 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4362 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363 return __r;
4364}
4365
4366template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004367_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368basic_string<_CharT, _Traits, _Allocator>
4369operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4370{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004371 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004372 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004373 _String __r(__uninitialized_size_tag(),
4374 __rhs_sz + 1,
4375 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4376 auto __ptr = std::__to_address(__r.__get_pointer());
4377 _Traits::assign(__ptr, 1, __lhs);
4378 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4379 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380 return __r;
4381}
4382
4383template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004384inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385basic_string<_CharT, _Traits, _Allocator>
4386operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4387{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004388 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004389 typename _String::size_type __lhs_sz = __lhs.size();
4390 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004391 _String __r(__uninitialized_size_tag(),
4392 __lhs_sz + __rhs_sz,
4393 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4394 auto __ptr = std::__to_address(__r.__get_pointer());
4395 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4396 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4397 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004398 return __r;
4399}
4400
4401template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser80b3cf32022-04-27 10:11:44 +02004402_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403basic_string<_CharT, _Traits, _Allocator>
4404operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4405{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004406 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004407 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004408 _String __r(__uninitialized_size_tag(),
4409 __lhs_sz + 1,
4410 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4411 auto __ptr = std::__to_address(__r.__get_pointer());
4412 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4413 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4414 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415 return __r;
4416}
4417
Eric Fiselierfc92be82017-04-19 00:28:44 +00004418#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004419
4420template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004421inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422basic_string<_CharT, _Traits, _Allocator>
4423operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4424{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004425 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004426}
4427
4428template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004429inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004430basic_string<_CharT, _Traits, _Allocator>
4431operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4432{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004433 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434}
4435
4436template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004437inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438basic_string<_CharT, _Traits, _Allocator>
4439operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4440{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004441 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442}
4443
4444template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004445inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446basic_string<_CharT, _Traits, _Allocator>
4447operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4448{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004449 return std::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004450}
4451
4452template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004453inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004454basic_string<_CharT, _Traits, _Allocator>
4455operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4456{
4457 __rhs.insert(__rhs.begin(), __lhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004458 return std::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459}
4460
4461template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004462inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004463basic_string<_CharT, _Traits, _Allocator>
4464operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4465{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004466 return std::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004467}
4468
4469template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004470inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004471basic_string<_CharT, _Traits, _Allocator>
4472operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4473{
4474 __lhs.push_back(__rhs);
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004475 return std::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004476}
4477
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004478#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479
4480// swap
4481
4482template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004483inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004485swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004486 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4487 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004488{
4489 __lhs.swap(__rhs);
4490}
4491
Bruce Mitchener170d8972020-11-24 12:53:53 -05004492_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4493_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4494_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4495_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4496_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004497
Bruce Mitchener170d8972020-11-24 12:53:53 -05004498_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4499_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4500_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004501
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004502_LIBCPP_FUNC_VIS string to_string(int __val);
4503_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4504_LIBCPP_FUNC_VIS string to_string(long __val);
4505_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4506_LIBCPP_FUNC_VIS string to_string(long long __val);
4507_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4508_LIBCPP_FUNC_VIS string to_string(float __val);
4509_LIBCPP_FUNC_VIS string to_string(double __val);
4510_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004511
Louis Dionne89258142021-08-23 15:32:36 -04004512#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004513_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4514_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4515_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4516_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4517_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004518
Bruce Mitchener170d8972020-11-24 12:53:53 -05004519_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4520_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4521_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004522
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004523_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4524_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4525_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4526_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4527_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4528_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4529_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4530_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4531_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004532#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004533
Howard Hinnantc51e1022010-05-11 19:42:16 +00004534template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004535_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004536const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4537 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538
Marshall Clow851b9ec2019-05-20 21:56:51 +00004539template <class _CharT, class _Allocator>
4540struct _LIBCPP_TEMPLATE_VIS
4541 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
Nikolas Klauser672ee852022-06-22 10:11:14 +02004542 : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004543{
4544 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004545 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4546 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004547};
4548
Howard Hinnantdc095972011-07-18 15:51:59 +00004549template<class _CharT, class _Traits, class _Allocator>
4550basic_ostream<_CharT, _Traits>&
4551operator<<(basic_ostream<_CharT, _Traits>& __os,
4552 const basic_string<_CharT, _Traits, _Allocator>& __str);
4553
4554template<class _CharT, class _Traits, class _Allocator>
4555basic_istream<_CharT, _Traits>&
4556operator>>(basic_istream<_CharT, _Traits>& __is,
4557 basic_string<_CharT, _Traits, _Allocator>& __str);
4558
4559template<class _CharT, class _Traits, class _Allocator>
4560basic_istream<_CharT, _Traits>&
4561getline(basic_istream<_CharT, _Traits>& __is,
4562 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4563
4564template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004565inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004566basic_istream<_CharT, _Traits>&
4567getline(basic_istream<_CharT, _Traits>& __is,
4568 basic_string<_CharT, _Traits, _Allocator>& __str);
4569
Howard Hinnantdc095972011-07-18 15:51:59 +00004570template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004571inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004572basic_istream<_CharT, _Traits>&
4573getline(basic_istream<_CharT, _Traits>&& __is,
4574 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4575
4576template<class _CharT, class _Traits, class _Allocator>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004577inline _LIBCPP_HIDE_FROM_ABI
Howard Hinnantdc095972011-07-18 15:51:59 +00004578basic_istream<_CharT, _Traits>&
4579getline(basic_istream<_CharT, _Traits>&& __is,
4580 basic_string<_CharT, _Traits, _Allocator>& __str);
4581
Marshall Clow29b53f22018-12-14 18:49:35 +00004582#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004583template <class _CharT, class _Traits, class _Allocator, class _Up>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004584inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004585 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4586 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4587 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004588 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
Marek Kurdeja98b1412020-05-02 13:58:03 +02004589 return __old_size - __str.size();
4590}
Marshall Clow29b53f22018-12-14 18:49:35 +00004591
Marek Kurdeja98b1412020-05-02 13:58:03 +02004592template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004593inline _LIBCPP_HIDE_FROM_ABI
Marek Kurdeja98b1412020-05-02 13:58:03 +02004594 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4595 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4596 _Predicate __pred) {
4597 auto __old_size = __str.size();
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004598 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
Marek Kurdeja98b1412020-05-02 13:58:03 +02004599 __str.end());
4600 return __old_size - __str.size();
4601}
Marshall Clow29b53f22018-12-14 18:49:35 +00004602#endif
4603
Louis Dionne510450b2022-04-01 16:38:30 -04004604#ifdef _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004605
4606template<class _CharT, class _Traits, class _Allocator>
4607bool
4608basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4609{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004610 return data() <= std::__to_address(__i->base()) &&
4611 std::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004612}
4613
4614template<class _CharT, class _Traits, class _Allocator>
4615bool
4616basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4617{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004618 return data() < std::__to_address(__i->base()) &&
4619 std::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004620}
4621
4622template<class _CharT, class _Traits, class _Allocator>
4623bool
4624basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4625{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004626 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004627 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004628}
4629
4630template<class _CharT, class _Traits, class _Allocator>
4631bool
4632basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4633{
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004634 const value_type* __p = std::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004635 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004636}
4637
Louis Dionne510450b2022-04-01 16:38:30 -04004638#endif // _LIBCPP_ENABLE_DEBUG_MODE
Howard Hinnant8ea98242013-08-23 17:37:05 +00004639
Louis Dionne173f29e2019-05-29 16:01:36 +00004640#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004641// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004642inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004643{
4644 inline namespace string_literals
4645 {
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004646 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004647 basic_string<char> operator "" s( const char *__str, size_t __len )
4648 {
4649 return basic_string<char> (__str, __len);
4650 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004651
Louis Dionne89258142021-08-23 15:32:36 -04004652#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004653 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004654 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4655 {
4656 return basic_string<wchar_t> (__str, __len);
4657 }
Louis Dionne89258142021-08-23 15:32:36 -04004658#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004659
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004660#ifndef _LIBCPP_HAS_NO_CHAR8_T
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004661 inline _LIBCPP_HIDE_FROM_ABI constexpr
Marshall Clow8732fed2018-12-11 04:35:44 +00004662 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4663 {
4664 return basic_string<char8_t> (__str, __len);
4665 }
4666#endif
4667
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004668 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004669 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4670 {
4671 return basic_string<char16_t> (__str, __len);
4672 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004673
Nikolas Klauser1d5108d2022-04-29 11:17:58 +02004674 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant5c167562013-08-07 19:39:48 +00004675 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4676 {
4677 return basic_string<char32_t> (__str, __len);
4678 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004679 } // namespace string_literals
4680} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004681
4682#if _LIBCPP_STD_VER > 17
4683template <>
4684inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4685#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4686template <>
4687inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4688#endif
4689#endif
4690
Marshall Clowcba751f2013-07-23 17:05:24 +00004691#endif
4692
Howard Hinnantc51e1022010-05-11 19:42:16 +00004693_LIBCPP_END_NAMESPACE_STD
4694
Eric Fiselierf4433a32017-05-31 22:07:49 +00004695_LIBCPP_POP_MACROS
4696
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004697#endif // _LIBCPP_STRING