4039 lines · plain
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// 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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP_STRING11#define _LIBCPP_STRING12 13// clang-format off14 15/*16 string synopsis17 18#include <compare>19#include <initializer_list>20 21namespace std22{23 24template <class stateT>25class fpos26{27private:28 stateT st;29public:30 fpos(streamoff = streamoff());31 32 operator streamoff() const;33 34 stateT state() const;35 void state(stateT);36 37 fpos& operator+=(streamoff);38 fpos operator+ (streamoff) const;39 fpos& operator-=(streamoff);40 fpos operator- (streamoff) const;41};42 43template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);44 45template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);46template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);47 48template <class charT>49struct char_traits50{51 using char_type = charT;52 using int_type = ...;53 using off_type = streamoff;54 using pos_type = streampos;55 using state_type = mbstate_t;56 using comparison_category = strong_ordering; // Since C++20 only for the specializations57 // char, wchar_t, char8_t, char16_t, and char32_t.58 59 static void assign(char_type& c1, const char_type& c2) noexcept;60 static constexpr bool eq(char_type c1, char_type c2) noexcept;61 static constexpr bool lt(char_type c1, char_type c2) noexcept;62 63 static int compare(const char_type* s1, const char_type* s2, size_t n);64 static size_t length(const char_type* s);65 static const char_type* find(const char_type* s, size_t n, const char_type& a);66 static char_type* move(char_type* s1, const char_type* s2, size_t n);67 static char_type* copy(char_type* s1, const char_type* s2, size_t n);68 static char_type* assign(char_type* s, size_t n, char_type a);69 70 static constexpr int_type not_eof(int_type c) noexcept;71 static constexpr char_type to_char_type(int_type c) noexcept;72 static constexpr int_type to_int_type(char_type c) noexcept;73 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;74 static constexpr int_type eof() noexcept;75};76 77template <> struct char_traits<char>;78template <> struct char_traits<wchar_t>;79template <> struct char_traits<char8_t>; // C++2080template <> struct char_traits<char16_t>;81template <> struct char_traits<char32_t>;82 83template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >84class basic_string85{86public:87// types:88 typedef traits traits_type;89 typedef typename traits_type::char_type value_type;90 typedef Allocator allocator_type;91 typedef typename allocator_type::size_type size_type;92 typedef typename allocator_type::difference_type difference_type;93 typedef typename allocator_type::reference reference;94 typedef typename allocator_type::const_reference const_reference;95 typedef typename allocator_type::pointer pointer;96 typedef typename allocator_type::const_pointer const_pointer;97 typedef implementation-defined iterator;98 typedef implementation-defined const_iterator;99 typedef std::reverse_iterator<iterator> reverse_iterator;100 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;101 102 static const size_type npos = -1;103 104 basic_string()105 noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20106 explicit basic_string(const allocator_type& a); // constexpr since C++20107 basic_string(const basic_string& str); // constexpr since C++20108 basic_string(basic_string&& str)109 noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20110 basic_string(const basic_string& str, size_type pos,111 const allocator_type& a = allocator_type()); // constexpr since C++20112 basic_string(const basic_string& str, size_type pos, size_type n,113 const Allocator& a = Allocator()); // constexpr since C++20114 constexpr basic_string(115 basic_string&& str, size_type pos, const Allocator& a = Allocator()); // since C++23116 constexpr basic_string(117 basic_string&& str, size_type pos, size_type n, const Allocator& a = Allocator()); // since C++23118 template<class T>119 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17, constexpr since C++20120 template <class T>121 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17, constexpr since C++20122 basic_string(const value_type* s, const allocator_type& a = allocator_type()); // constexpr since C++20123 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type()); // constexpr since C++20124 basic_string(nullptr_t) = delete; // C++23125 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type()); // constexpr since C++20126 template<class InputIterator>127 basic_string(InputIterator begin, InputIterator end,128 const allocator_type& a = allocator_type()); // constexpr since C++20129 template<container-compatible-range<charT> R>130 constexpr basic_string(from_range_t, R&& rg, const Allocator& a = Allocator()); // since C++23131 basic_string(initializer_list<value_type>, const Allocator& = Allocator()); // constexpr since C++20132 basic_string(const basic_string&, const Allocator&); // constexpr since C++20133 basic_string(basic_string&&, const Allocator&); // constexpr since C++20134 135 ~basic_string(); // constexpr since C++20136 137 operator basic_string_view<charT, traits>() const noexcept; // constexpr since C++20138 139 basic_string& operator=(const basic_string& str); // constexpr since C++20140 template <class T>141 basic_string& operator=(const T& t); // C++17, constexpr since C++20142 basic_string& operator=(basic_string&& str)143 noexcept(144 allocator_type::propagate_on_container_move_assignment::value ||145 allocator_type::is_always_equal::value ); // C++17, constexpr since C++20146 basic_string& operator=(const value_type* s); // constexpr since C++20147 basic_string& operator=(nullptr_t) = delete; // C++23148 basic_string& operator=(value_type c); // constexpr since C++20149 basic_string& operator=(initializer_list<value_type>); // constexpr since C++20150 151 iterator begin() noexcept; // constexpr since C++20152 const_iterator begin() const noexcept; // constexpr since C++20153 iterator end() noexcept; // constexpr since C++20154 const_iterator end() const noexcept; // constexpr since C++20155 156 reverse_iterator rbegin() noexcept; // constexpr since C++20157 const_reverse_iterator rbegin() const noexcept; // constexpr since C++20158 reverse_iterator rend() noexcept; // constexpr since C++20159 const_reverse_iterator rend() const noexcept; // constexpr since C++20160 161 const_iterator cbegin() const noexcept; // constexpr since C++20162 const_iterator cend() const noexcept; // constexpr since C++20163 const_reverse_iterator crbegin() const noexcept; // constexpr since C++20164 const_reverse_iterator crend() const noexcept; // constexpr since C++20165 166 size_type size() const noexcept; // constexpr since C++20167 size_type length() const noexcept; // constexpr since C++20168 size_type max_size() const noexcept; // constexpr since C++20169 size_type capacity() const noexcept; // constexpr since C++20170 171 void resize(size_type n, value_type c); // constexpr since C++20172 void resize(size_type n); // constexpr since C++20173 174 template<class Operation>175 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23176 177 void reserve(size_type res_arg); // constexpr since C++20178 void reserve(); // deprecated in C++20, removed in C++26179 void shrink_to_fit(); // constexpr since C++20180 void clear() noexcept; // constexpr since C++20181 bool empty() const noexcept; // constexpr since C++20182 183 const_reference operator[](size_type pos) const; // constexpr since C++20184 reference operator[](size_type pos); // constexpr since C++20185 186 const_reference at(size_type n) const; // constexpr since C++20187 reference at(size_type n); // constexpr since C++20188 189 basic_string& operator+=(const basic_string& str); // constexpr since C++20190 template <class T>191 basic_string& operator+=(const T& t); // C++17, constexpr since C++20192 basic_string& operator+=(const value_type* s); // constexpr since C++20193 basic_string& operator+=(value_type c); // constexpr since C++20194 basic_string& operator+=(initializer_list<value_type>); // constexpr since C++20195 196 basic_string& append(const basic_string& str); // constexpr since C++20197 template <class T>198 basic_string& append(const T& t); // C++17, constexpr since C++20199 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20200 template <class T>201 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20202 basic_string& append(const value_type* s, size_type n); // constexpr since C++20203 basic_string& append(const value_type* s); // constexpr since C++20204 basic_string& append(size_type n, value_type c); // constexpr since C++20205 template<class InputIterator>206 basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20207 template<container-compatible-range<charT> R>208 constexpr basic_string& append_range(R&& rg); // C++23209 basic_string& append(initializer_list<value_type>); // constexpr since C++20210 211 void push_back(value_type c); // constexpr since C++20212 void pop_back(); // constexpr since C++20213 reference front(); // constexpr since C++20214 const_reference front() const; // constexpr since C++20215 reference back(); // constexpr since C++20216 const_reference back() const; // constexpr since C++20217 218 basic_string& assign(const basic_string& str); // constexpr since C++20219 template <class T>220 basic_string& assign(const T& t); // C++17, constexpr since C++20221 basic_string& assign(basic_string&& str); // constexpr since C++20222 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20223 template <class T>224 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20225 basic_string& assign(const value_type* s, size_type n); // constexpr since C++20226 basic_string& assign(const value_type* s); // constexpr since C++20227 basic_string& assign(size_type n, value_type c); // constexpr since C++20228 template<class InputIterator>229 basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20230 template<container-compatible-range<charT> R>231 constexpr basic_string& assign_range(R&& rg); // C++23232 basic_string& assign(initializer_list<value_type>); // constexpr since C++20233 234 basic_string& insert(size_type pos1, const basic_string& str); // constexpr since C++20235 template <class T>236 basic_string& insert(size_type pos1, const T& t); // constexpr since C++20237 basic_string& insert(size_type pos1, const basic_string& str,238 size_type pos2, size_type n2=npos); // constexpr since C++20239 template <class T>240 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n=npos); // C++17, constexpr since C++20241 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20242 basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20243 basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20244 iterator insert(const_iterator p, value_type c); // constexpr since C++20245 iterator insert(const_iterator p, size_type n, value_type c); // constexpr since C++20246 template<class InputIterator>247 iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20248 template<container-compatible-range<charT> R>249 constexpr iterator insert_range(const_iterator p, R&& rg); // C++23250 iterator insert(const_iterator p, initializer_list<value_type>); // constexpr since C++20251 252 basic_string& erase(size_type pos = 0, size_type n = npos); // constexpr since C++20253 iterator erase(const_iterator position); // constexpr since C++20254 iterator erase(const_iterator first, const_iterator last); // constexpr since C++20255 256 basic_string& replace(size_type pos1, size_type n1, const basic_string& str); // constexpr since C++20257 template <class T>258 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17, constexpr since C++20259 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,260 size_type pos2, size_type n2=npos); // C++14, constexpr since C++20261 template <class T>262 basic_string& replace(size_type pos1, size_type n1, const T& t,263 size_type pos2, size_type n2=npos); // C++17, constexpr since C++20264 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20265 basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20266 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20267 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20268 template <class T>269 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17, constexpr since C++20270 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20271 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s); // constexpr since C++20272 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c); // constexpr since C++20273 template<class InputIterator>274 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20275 template<container-compatible-range<charT> R>276 constexpr basic_string& replace_with_range(const_iterator i1, const_iterator i2, R&& rg); // C++23277 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>); // constexpr since C++20278 279 size_type copy(value_type* s, size_type n, size_type pos = 0) const; // constexpr since C++20280 basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr in C++20, removed in C++23281 basic_string substr(size_type pos = 0, size_type n = npos) const&; // since C++23282 constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&; // since C++23283 constexpr basic_string_view<charT, traits> subview(size_type pos = 0,284 size_type n = npos) const; // since C++26285 void swap(basic_string& str)286 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||287 allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20288 289 const value_type* c_str() const noexcept; // constexpr since C++20290 const value_type* data() const noexcept; // constexpr since C++20291 value_type* data() noexcept; // C++17, constexpr since C++20292 293 allocator_type get_allocator() const noexcept; // constexpr since C++20294 295 size_type find(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20296 template <class T>297 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20298 size_type find(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20299 size_type find(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20300 size_type find(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20301 302 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20303 template <class T>304 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20305 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20306 size_type rfind(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20307 size_type rfind(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20308 309 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20310 template <class T>311 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20312 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20313 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20314 size_type find_first_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20315 316 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20317 template <class T>318 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension, constexpr since C++20319 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20320 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20321 size_type find_last_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20322 323 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20324 template <class T>325 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20326 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20327 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20328 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20329 330 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20331 template <class T>332 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20333 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20334 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20335 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20336 337 int compare(const basic_string& str) const noexcept; // constexpr since C++20338 template <class T>339 int compare(const T& t) const noexcept; // C++17, noexcept as an extension, constexpr since C++20340 int compare(size_type pos1, size_type n1, const basic_string& str) const; // constexpr since C++20341 template <class T>342 int compare(size_type pos1, size_type n1, const T& t) const; // C++17, constexpr since C++20343 int compare(size_type pos1, size_type n1, const basic_string& str,344 size_type pos2, size_type n2=npos) const; // C++14, constexpr since C++20345 template <class T>346 int compare(size_type pos1, size_type n1, const T& t,347 size_type pos2, size_type n2=npos) const; // C++17, constexpr since C++20348 int compare(const value_type* s) const noexcept; // constexpr since C++20349 int compare(size_type pos1, size_type n1, const value_type* s) const; // constexpr since C++20350 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const; // constexpr since C++20351 352 constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20353 constexpr bool starts_with(charT c) const noexcept; // C++20354 constexpr bool starts_with(const charT* s) const; // C++20355 constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20356 constexpr bool ends_with(charT c) const noexcept; // C++20357 constexpr bool ends_with(const charT* s) const; // C++20358 359 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++23360 constexpr bool contains(charT c) const noexcept; // C++23361 constexpr bool contains(const charT* s) const; // C++23362};363 364template<class InputIterator,365 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>366basic_string(InputIterator, InputIterator, Allocator = Allocator())367 -> basic_string<typename iterator_traits<InputIterator>::value_type,368 char_traits<typename iterator_traits<InputIterator>::value_type>,369 Allocator>; // C++17370 371template<ranges::input_range R,372 class Allocator = allocator<ranges::range_value_t<R>>>373 basic_string(from_range_t, R&&, Allocator = Allocator())374 -> basic_string<ranges::range_value_t<R>, char_traits<ranges::range_value_t<R>>,375 Allocator>; // C++23376 377template<class charT,378 class traits,379 class Allocator = allocator<charT>>380 explicit basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())381 -> basic_string<charT, traits, Allocator>; // C++17382 383template<class charT,384 class traits,385 class Allocator = allocator<charT>>386 basic_string(basic_string_view<charT, traits>,387 typename see below::size_type, typename see below::size_type,388 const Allocator& = Allocator())389 -> basic_string<charT, traits, Allocator>; // C++17390 391template<class charT, class traits, class Allocator>392basic_string<charT, traits, Allocator>393operator+(const basic_string<charT, traits, Allocator>& lhs,394 const basic_string<charT, traits, Allocator>& rhs); // constexpr since C++20395 396template<class charT, class traits, class Allocator>397basic_string<charT, traits, Allocator>398operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); // constexpr since C++20399 400template<class charT, class traits, class Allocator>401basic_string<charT, traits, Allocator>402operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20403 404template<class charT, class traits, class Allocator>405basic_string<charT, traits, Allocator>406operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); // constexpr since C++20407 408template<class charT, class traits, class Allocator>409basic_string<charT, traits, Allocator>410operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); // constexpr since C++20411 412template<class charT, class traits, class Allocator>413 constexpr basic_string<charT, traits, Allocator>414 operator+(const basic_string<charT, traits, Allocator>& lhs,415 type_identity_t<basic_string_view<charT, traits>> rhs); // Since C++26416template<class charT, class traits, class Allocator>417 constexpr basic_string<charT, traits, Allocator>418 operator+(basic_string<charT, traits, Allocator>&& lhs,419 type_identity_t<basic_string_view<charT, traits>> rhs); // Since C++26420template<class charT, class traits, class Allocator>421 constexpr basic_string<charT, traits, Allocator>422 operator+(type_identity_t<basic_string_view<charT, traits>> lhs,423 const basic_string<charT, traits, Allocator>& rhs); // Since C++26424template<class charT, class traits, class Allocator>425 constexpr basic_string<charT, traits, Allocator>426 operator+(type_identity_t<basic_string_view<charT, traits>> lhs,427 basic_string<charT, traits, Allocator>&& rhs); // Since C++26428 429 430template<class charT, class traits, class Allocator>431bool operator==(const basic_string<charT, traits, Allocator>& lhs,432 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20433 434template<class charT, class traits, class Allocator>435bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20436 437template<class charT, class traits, class Allocator>438bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20439 440template<class charT, class traits, class Allocator>441bool operator!=(const basic_string<charT,traits,Allocator>& lhs,442 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20443 444template<class charT, class traits, class Allocator>445bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20446 447template<class charT, class traits, class Allocator>448bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20449 450template<class charT, class traits, class Allocator>451bool operator< (const basic_string<charT, traits, Allocator>& lhs,452 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20453 454template<class charT, class traits, class Allocator>455bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20456 457template<class charT, class traits, class Allocator>458bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20459 460template<class charT, class traits, class Allocator>461bool operator> (const basic_string<charT, traits, Allocator>& lhs,462 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20463 464template<class charT, class traits, class Allocator>465bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20466 467template<class charT, class traits, class Allocator>468bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20469 470template<class charT, class traits, class Allocator>471bool operator<=(const basic_string<charT, traits, Allocator>& lhs,472 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20473 474template<class charT, class traits, class Allocator>475bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20476 477template<class charT, class traits, class Allocator>478bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20479 480template<class charT, class traits, class Allocator>481bool operator>=(const basic_string<charT, traits, Allocator>& lhs,482 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20483 484template<class charT, class traits, class Allocator>485bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20486 487template<class charT, class traits, class Allocator>488bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20489 490template<class charT, class traits, class Allocator> // since C++20491constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,492 const basic_string<charT, traits, Allocator>& rhs) noexcept;493 494template<class charT, class traits, class Allocator> // since C++20495constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,496 const charT* rhs) noexcept;497 498template<class charT, class traits, class Allocator>499void swap(basic_string<charT, traits, Allocator>& lhs,500 basic_string<charT, traits, Allocator>& rhs)501 noexcept(noexcept(lhs.swap(rhs))); // constexpr since C++20502 503template<class charT, class traits, class Allocator>504basic_istream<charT, traits>&505operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);506 507template<class charT, class traits, class Allocator>508basic_ostream<charT, traits>&509operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);510 511template<class charT, class traits, class Allocator>512basic_istream<charT, traits>&513getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,514 charT delim);515 516template<class charT, class traits, class Allocator>517basic_istream<charT, traits>&518getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);519 520template<class charT, class traits, class Allocator, class U>521constexpr typename basic_string<charT, traits, Allocator>::size_type522erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20523template<class charT, class traits, class Allocator, class Predicate>524constexpr typename basic_string<charT, traits, Allocator>::size_type525erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20526 527typedef basic_string<char> string;528typedef basic_string<wchar_t> wstring;529typedef basic_string<char8_t> u8string; // C++20530typedef basic_string<char16_t> u16string;531typedef basic_string<char32_t> u32string;532 533int stoi (const string& str, size_t* idx = nullptr, int base = 10);534long stol (const string& str, size_t* idx = nullptr, int base = 10);535unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);536long long stoll (const string& str, size_t* idx = nullptr, int base = 10);537unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);538 539float stof (const string& str, size_t* idx = nullptr);540double stod (const string& str, size_t* idx = nullptr);541long double stold(const string& str, size_t* idx = nullptr);542 543string to_string(int val);544string to_string(unsigned val);545string to_string(long val);546string to_string(unsigned long val);547string to_string(long long val);548string to_string(unsigned long long val);549string to_string(float val);550string to_string(double val);551string to_string(long double val);552 553int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);554long stol (const wstring& str, size_t* idx = nullptr, int base = 10);555unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);556long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);557unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);558 559float stof (const wstring& str, size_t* idx = nullptr);560double stod (const wstring& str, size_t* idx = nullptr);561long double stold(const wstring& str, size_t* idx = nullptr);562 563wstring to_wstring(int val);564wstring to_wstring(unsigned val);565wstring to_wstring(long val);566wstring to_wstring(unsigned long val);567wstring to_wstring(long long val);568wstring to_wstring(unsigned long long val);569wstring to_wstring(float val);570wstring to_wstring(double val);571wstring to_wstring(long double val);572 573template <> struct hash<string>;574template <> struct hash<u8string>; // C++20575template <> struct hash<u16string>;576template <> struct hash<u32string>;577template <> struct hash<wstring>;578 579basic_string<char> operator""s( const char *str, size_t len ); // C++14, constexpr since C++20580basic_string<wchar_t> operator""s( const wchar_t *str, size_t len ); // C++14, constexpr since C++20581constexpr basic_string<char8_t> operator""s( const char8_t *str, size_t len ); // C++20582basic_string<char16_t> operator""s( const char16_t *str, size_t len ); // C++14, constexpr since C++20583basic_string<char32_t> operator""s( const char32_t *str, size_t len ); // C++14, constexpr since C++20584 585} // std586 587*/588 589// clang-format on590 591#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)592# include <__cxx03/string>593#else594# include <__algorithm/max.h>595# include <__algorithm/min.h>596# include <__algorithm/remove.h>597# include <__algorithm/remove_if.h>598# include <__assert>599# include <__config>600# include <__debug_utils/sanitizers.h>601# include <__format/enable_insertable.h>602# include <__functional/hash.h>603# include <__functional/is_transparent.h>604# include <__functional/unary_function.h>605# include <__fwd/string.h>606# include <__iterator/bounded_iter.h>607# include <__iterator/distance.h>608# include <__iterator/iterator_traits.h>609# include <__iterator/reverse_iterator.h>610# include <__iterator/wrap_iter.h>611# include <__memory/addressof.h>612# include <__memory/allocate_at_least.h>613# include <__memory/allocator.h>614# include <__memory/allocator_traits.h>615# include <__memory/compressed_pair.h>616# include <__memory/construct_at.h>617# include <__memory/noexcept_move_assign_container.h>618# include <__memory/pointer_traits.h>619# include <__memory/swap_allocator.h>620# include <__memory_resource/polymorphic_allocator.h>621# include <__ranges/access.h>622# include <__ranges/concepts.h>623# include <__ranges/container_compatible_range.h>624# include <__ranges/from_range.h>625# include <__string/char_traits.h>626# include <__string/extern_template_lists.h>627# include <__type_traits/conditional.h>628# include <__type_traits/enable_if.h>629# include <__type_traits/is_allocator.h>630# include <__type_traits/is_array.h>631# include <__type_traits/is_convertible.h>632# include <__type_traits/is_generic_transparent_comparator.h>633# include <__type_traits/is_nothrow_assignable.h>634# include <__type_traits/is_nothrow_constructible.h>635# include <__type_traits/is_same.h>636# include <__type_traits/is_standard_layout.h>637# include <__type_traits/is_trivially_constructible.h>638# include <__type_traits/is_trivially_copyable.h>639# include <__type_traits/is_trivially_relocatable.h>640# include <__type_traits/remove_cvref.h>641# include <__utility/default_three_way_comparator.h>642# include <__utility/exception_guard.h>643# include <__utility/forward.h>644# include <__utility/is_pointer_in_range.h>645# include <__utility/move.h>646# include <__utility/no_destroy.h>647# include <__utility/scope_guard.h>648# include <__utility/swap.h>649# include <climits>650# include <cstdio> // EOF651# include <cstring>652# include <limits>653# include <stdexcept>654# include <string_view>655# include <version>656 657# if _LIBCPP_HAS_WIDE_CHARACTERS658# include <cwchar>659# endif660 661// standard-mandated includes662 663// [iterator.range]664# include <__iterator/access.h>665# include <__iterator/data.h>666# include <__iterator/empty.h>667# include <__iterator/reverse_access.h>668# include <__iterator/size.h>669 670// [string.syn]671# include <compare>672# include <initializer_list>673 674# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)675# pragma GCC system_header676# endif677 678_LIBCPP_PUSH_MACROS679# include <__undef_macros>680 681# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN682# define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS __attribute__((__no_sanitize__("address")))683// This macro disables AddressSanitizer (ASan) instrumentation for a specific function,684// allowing memory accesses that would normally trigger ASan errors to proceed without crashing.685// This is useful for accessing parts of objects memory, which should not be accessed,686// such as unused bytes in short strings, that should never be accessed687// by other parts of the program.688# else689# define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS690# endif691 692_LIBCPP_BEGIN_NAMESPACE_STD693 694// basic_string695 696template <class _CharT, class _Traits, class _Allocator>697_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>698__concatenate_strings(const _Allocator& __alloc,699 __type_identity_t<basic_string_view<_CharT, _Traits> > __str1,700 __type_identity_t<basic_string_view<_CharT, _Traits> > __str2);701 702template <class _Iter>703inline const bool __string_is_trivial_iterator_v = false;704 705template <class _Tp>706inline const bool __string_is_trivial_iterator_v<_Tp*> = is_arithmetic<_Tp>::value;707 708template <class _Iter>709inline const bool __string_is_trivial_iterator_v<__wrap_iter<_Iter> > = __string_is_trivial_iterator_v<_Iter>;710 711template <class _CharT, class _Traits, class _Tp>712inline const bool __can_be_converted_to_string_view_v =713 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&714 !is_convertible<const _Tp&, const _CharT*>::value;715 716struct __uninitialized_size_tag {};717struct __init_with_sentinel_tag {};718 719template <size_t _PaddingSize>720struct __padding {721 char __padding_[_PaddingSize];722};723 724template <>725struct __padding<0> {};726 727template <class _CharT, class _Traits, class _Allocator>728class basic_string {729public:730 using __self _LIBCPP_NODEBUG = basic_string;731 using __self_view _LIBCPP_NODEBUG = basic_string_view<_CharT, _Traits>;732 using traits_type = _Traits;733 using value_type = _CharT;734 using allocator_type = _Allocator;735 using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>;736 using size_type = typename __alloc_traits::size_type;737 using difference_type = typename __alloc_traits::difference_type;738 using reference = value_type&;739 using const_reference = const value_type&;740 using pointer = typename __alloc_traits::pointer;741 using const_pointer = typename __alloc_traits::const_pointer;742 743 // A basic_string contains the following members which may be trivially relocatable:744 // - pointer: is currently assumed to be trivially relocatable, but is still checked in case that changes745 // - size_type: is always trivially relocatable, since it has to be an integral type746 // - value_type: is always trivially relocatable, since it has to be trivial747 // - unsigned char: is a fundamental type, so it's trivially relocatable748 // - allocator_type: may or may not be trivially relocatable, so it's checked749 //750 // This string implementation doesn't contain any references into itself. It only contains a bit that says whether751 // it is in small or large string mode, so the entire structure is trivially relocatable if its members are.752# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN753 // When compiling with AddressSanitizer (ASan), basic_string cannot be trivially754 // relocatable. Because the object's memory might be poisoned when its content755 // is kept inside objects memory (short string optimization), instead of in allocated756 // external memory. In such cases, the destructor is responsible for unpoisoning757 // the memory to avoid triggering false positives.758 // Therefore it's crucial to ensure the destructor is called.759 using __trivially_relocatable = void;760# else761 using __trivially_relocatable _LIBCPP_NODEBUG = __conditional_t<762 __libcpp_is_trivially_relocatable<allocator_type>::value && __libcpp_is_trivially_relocatable<pointer>::value,763 basic_string,764 void>;765# endif766 767# if __has_feature(address_sanitizer) && _LIBCPP_INSTRUMENTED_WITH_ASAN768 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __asan_volatile_wrapper(pointer const& __ptr) const {769 if (__libcpp_is_constant_evaluated())770 return __ptr;771 772 pointer volatile __copy_ptr = __ptr;773 774 return const_cast<pointer&>(__copy_ptr);775 }776 777 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer778 __asan_volatile_wrapper(const_pointer const& __ptr) const {779 if (__libcpp_is_constant_evaluated())780 return __ptr;781 782 const_pointer volatile __copy_ptr = __ptr;783 784 return const_cast<const_pointer&>(__copy_ptr);785 }786# define _LIBCPP_ASAN_VOLATILE_WRAPPER(PTR) __asan_volatile_wrapper(PTR)787# else788# define _LIBCPP_ASAN_VOLATILE_WRAPPER(PTR) PTR789# endif790 791 static_assert(!is_array<value_type>::value, "Character type of basic_string must not be an array");792 static_assert(is_standard_layout<value_type>::value, "Character type of basic_string must be standard-layout");793 static_assert(is_trivially_default_constructible<value_type>::value,794 "Character type of basic_string must be trivially default constructible");795 static_assert(is_trivially_copyable<value_type>::value, "Character type of basic_string must be trivially copyable");796 static_assert(is_same<_CharT, typename traits_type::char_type>::value,797 "traits_type::char_type must be the same type as CharT");798 static_assert(is_same<typename allocator_type::value_type, value_type>::value,799 "Allocator::value_type must be same type as value_type");800 static_assert(__check_valid_allocator<allocator_type>::value, "");801 802# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING803 // Users might provide custom allocators, and prior to C++20 we have no existing way to detect whether the allocator's804 // pointer type is contiguous (though it has to be by the Standard). Using the wrapper type ensures the iterator is805 // considered contiguous.806 using iterator = __bounded_iter<__wrap_iter<pointer> >;807 using const_iterator = __bounded_iter<__wrap_iter<const_pointer> >;808# else809 using iterator = __wrap_iter<pointer>;810 using const_iterator = __wrap_iter<const_pointer>;811# endif812 using reverse_iterator = std::reverse_iterator<iterator>;813 using const_reverse_iterator = std::reverse_iterator<const_iterator>;814 815 using __alloc_result _LIBCPP_NODEBUG = __allocation_result<pointer, size_type>;816 817private:818 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");819 820# ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT821 822 struct __long {823 __long() = default;824 825 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __long(__alloc_result __alloc, size_type __size)826 : __data_(__alloc.ptr), __size_(__size), __cap_(__alloc.count / __endian_factor), __is_long_(true) {827 _LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__alloc.count), "Long capacity should always be larger than the SSO");828 }829 830 pointer __data_;831 size_type __size_;832 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;833 size_type __is_long_ : 1;834 };835 836 enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };837 838 struct __short {839 value_type __data_[__min_cap];840 _LIBCPP_NO_UNIQUE_ADDRESS __padding<sizeof(value_type) - 1> __padding_;841 unsigned char __size_ : 7;842 unsigned char __is_long_ : 1;843 };844 845 // The __endian_factor is required because the field we use to store the size846 // has one fewer bit than it would if it were not a bitfield.847 //848 // If the LSB is used to store the short-flag in the short string representation,849 // we have to multiply the size by two when it is stored and divide it by two when850 // it is loaded to make sure that we always store an even number. In the long string851 // representation, we can ignore this because we can assume that we always allocate852 // an even amount of value_types.853 //854 // If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.855 // This does not impact the short string representation, since we never need the MSB856 // for representing the size of a short string anyway.857 858# ifdef _LIBCPP_BIG_ENDIAN859 static const size_type __endian_factor = 2;860# else861 static const size_type __endian_factor = 1;862# endif863 864# else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT865 866# ifdef _LIBCPP_BIG_ENDIAN867 static const size_type __endian_factor = 1;868# else869 static const size_type __endian_factor = 2;870# endif871 872 // Attribute 'packed' is used to keep the layout compatible with the873 // previous definition that did not use bit fields. This is because on874 // some platforms bit fields have a default size rather than the actual875 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.876 struct __long {877 __long() = default;878 879 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __long(__alloc_result __alloc, size_type __size)880 : __is_long_(true), __cap_(__alloc.count / __endian_factor), __size_(__size), __data_(__alloc.ptr) {881 _LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__alloc.count), "Long capacity should always be larger than the SSO");882 }883 884 struct _LIBCPP_PACKED {885 size_type __is_long_ : 1;886 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;887 };888 size_type __size_;889 pointer __data_;890 };891 892 enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };893 894 struct __short {895 struct _LIBCPP_PACKED {896 unsigned char __is_long_ : 1;897 unsigned char __size_ : 7;898 };899 _LIBCPP_NO_UNIQUE_ADDRESS __padding<sizeof(value_type) - 1> __padding_;900 value_type __data_[__min_cap];901 };902 903# endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT904 905 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");906 907 union __rep {908 __short __s;909 __long __l;910 911 __rep() = default;912 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __rep(__short __r) : __s(__r) {}913 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __rep(__long __r) : __l(__r) {}914 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __rep(__uninitialized_tag) {}915 };916 917 _LIBCPP_COMPRESSED_PAIR(__rep, __rep_, allocator_type, __alloc_);918 919 // annotate the string with its size() at scope exit. The string has to be in a valid state at that point.920 struct __annotate_new_size {921 basic_string& __str_;922 923 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __annotate_new_size(basic_string& __str) : __str_(__str) {}924 925 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()() { __str_.__annotate_new(__str_.size()); }926 };927 928 // Construct a string with the given allocator and enough storage to hold `__size` characters, but929 // don't initialize the characters. The contents of the string, including the null terminator, must be930 // initialized separately.931 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(932 __uninitialized_size_tag, size_type __size, const allocator_type& __a)933 : __alloc_(__a) {934 __init_internal_buffer(__size);935 }936 937 template <class _Iter, class _Sent>938 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20939 basic_string(__init_with_sentinel_tag, _Iter __first, _Sent __last, const allocator_type& __a)940 : __alloc_(__a) {941 __init_with_sentinel(std::move(__first), std::move(__last));942 }943 944 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator __make_iterator(pointer __p) {945# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING946 // Bound the iterator according to the size (and not the capacity, unlike vector).947 //948 // By the Standard, string iterators are generally not guaranteed to stay valid when the container is modified,949 // regardless of whether reallocation occurs. This allows us to check for out-of-bounds accesses using logical size,950 // a stricter check, since correct code can never rely on being able to access newly-added elements via an existing951 // iterator.952 return std::__make_bounded_iter(953 std::__wrap_iter<pointer>(__p),954 std::__wrap_iter<pointer>(__get_pointer()),955 std::__wrap_iter<pointer>(__get_pointer() + size()));956# else957 return iterator(__p);958# endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING959 }960 961 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator __make_const_iterator(const_pointer __p) const {962# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING963 // Bound the iterator according to the size (and not the capacity, unlike vector).964 return std::__make_bounded_iter(965 std::__wrap_iter<const_pointer>(__p),966 std::__wrap_iter<const_pointer>(__get_pointer()),967 std::__wrap_iter<const_pointer>(__get_pointer() + size()));968# else969 return const_iterator(__p);970# endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING971 }972 973public:974 _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;975 976 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()977 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)978# if _LIBCPP_STD_VER >= 20 // TODO(LLVM 23): Remove this condition; this is a workaround for https://llvm.org/PR154567979 : __rep_(__short())980# else981 : __rep_()982# endif983 {984 __annotate_new(0);985 }986 987 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)988# if _LIBCPP_STD_VER <= 14989 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)990# else991 _NOEXCEPT992# endif993# if _LIBCPP_STD_VER >= 20 // TODO(LLVM 23): Remove this condition; this is a workaround for https://llvm.org/PR154567994 : __rep_(__short()),995# else996 : __rep_(),997# endif998 __alloc_(__a) {999 __annotate_new(0);1000 }1001 1002 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string(const basic_string& __str)1003 : __alloc_(__alloc_traits::select_on_container_copy_construction(__str.__alloc_)) {1004 if (!__str.__is_long()) {1005 __rep_ = __str.__rep_;1006 __annotate_new(__get_short_size());1007 } else1008 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());1009 }1010 1011 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS1012 basic_string(const basic_string& __str, const allocator_type& __a)1013 : __alloc_(__a) {1014 if (!__str.__is_long()) {1015 __rep_ = __str.__rep_;1016 __annotate_new(__get_short_size());1017 } else1018 __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());1019 }1020 1021# ifndef _LIBCPP_CXX03_LANG1022 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)1023# if _LIBCPP_STD_VER <= 141024 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)1025# else1026 _NOEXCEPT1027# endif1028 // Turning off ASan instrumentation for variable initialization with _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS1029 // does not work consistently during initialization of __r_, so we instead unpoison __str's memory manually first.1030 // __str's memory needs to be unpoisoned only in the case where it's a short string.1031 : __rep_([](basic_string& __s) -> decltype(__s.__rep_)&& {1032 if (!__s.__is_long())1033 __s.__annotate_delete();1034 return std::move(__s.__rep_);1035 }(__str)),1036 __alloc_(std::move(__str.__alloc_)) {1037 __str.__rep_ = __rep();1038 __str.__annotate_new(0);1039 if (!__is_long())1040 __annotate_new(size());1041 }1042 1043 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)1044 : __alloc_(__a) {1045 if (__str.__is_long() && __a != __str.__alloc_) // copy, not move1046 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());1047 else {1048 if (__libcpp_is_constant_evaluated())1049 __rep_ = __rep();1050 if (!__str.__is_long())1051 __str.__annotate_delete();1052 __rep_ = __str.__rep_;1053 __str.__rep_ = __rep();1054 __str.__annotate_new(0);1055 if (!__is_long() && this != std::addressof(__str))1056 __annotate_new(size());1057 }1058 }1059# endif // _LIBCPP_CXX03_LANG1060 1061 template <__enable_if_t<__is_allocator_v<_Allocator>, int> = 0>1062 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* _LIBCPP_DIAGNOSE_NULLPTR __s) {1063 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "basic_string(const char*) detected nullptr");1064 __init(__s, traits_type::length(__s));1065 }1066 1067 template <__enable_if_t<__is_allocator_v<_Allocator>, int> = 0>1068 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX201069 basic_string(const _CharT* _LIBCPP_DIAGNOSE_NULLPTR __s, const _Allocator& __a)1070 : __alloc_(__a) {1071 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");1072 __init(__s, traits_type::length(__s));1073 }1074 1075# if _LIBCPP_STD_VER >= 231076 basic_string(nullptr_t) = delete;1077# endif1078 1079 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n)1080 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {1081 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");1082 __init(__s, __n);1083 }1084 1085 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX201086 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)1087 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero")1088 : __alloc_(__a) {1089 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");1090 __init(__s, __n);1091 }1092 1093 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c) { __init(__n, __c); }1094 1095# if _LIBCPP_STD_VER >= 231096 _LIBCPP_HIDE_FROM_ABI constexpr basic_string(1097 basic_string&& __str, size_type __pos, const _Allocator& __alloc = _Allocator())1098 : basic_string(std::move(__str), __pos, npos, __alloc) {}1099 1100 _LIBCPP_HIDE_FROM_ABI constexpr basic_string(1101 basic_string&& __str, size_type __pos, size_type __n, const _Allocator& __alloc = _Allocator())1102 : __alloc_(__alloc) {1103 if (__pos > __str.size())1104 this->__throw_out_of_range();1105 1106 auto __len = std::min<size_type>(__n, __str.size() - __pos);1107 if (__alloc_traits::is_always_equal::value || __alloc == __str.__alloc_) {1108 __move_assign(std::move(__str), __pos, __len);1109 } else {1110 // Perform a copy because the allocators are not compatible.1111 __init(__str.data() + __pos, __len);1112 }1113 }1114# endif1115 1116 template <__enable_if_t<__is_allocator_v<_Allocator>, int> = 0>1117 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c, const _Allocator& __a)1118 : __alloc_(__a) {1119 __init(__n, __c);1120 }1121 1122 _LIBCPP_CONSTEXPR_SINCE_CXX201123 basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator())1124 : __alloc_(__a) {1125 size_type __str_sz = __str.size();1126 if (__pos > __str_sz)1127 this->__throw_out_of_range();1128 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));1129 }1130 1131 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX201132 basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())1133 : __alloc_(__a) {1134 size_type __str_sz = __str.size();1135 if (__pos > __str_sz)1136 this->__throw_out_of_range();1137 __init(__str.data() + __pos, __str_sz - __pos);1138 }1139 1140 template <class _Tp,1141 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1142 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,1143 int> = 0>1144 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX201145 basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type())1146 : __alloc_(__a) {1147 __self_view __sv0 = __t;1148 __self_view __sv = __sv0.substr(__pos, __n);1149 __init(__sv.data(), __sv.size());1150 }1151 1152 template <class _Tp,1153 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1154 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,1155 int> = 0>1156 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const _Tp& __t) {1157 __self_view __sv = __t;1158 __init(__sv.data(), __sv.size());1159 }1160 1161 template <class _Tp,1162 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1163 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,1164 int> = 0>1165 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const _Tp& __t, const allocator_type& __a)1166 : __alloc_(__a) {1167 __self_view __sv = __t;1168 __init(__sv.data(), __sv.size());1169 }1170 1171 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>1172 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last) {1173 __init(__first, __last);1174 }1175 1176 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>1177 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX201178 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)1179 : __alloc_(__a) {1180 __init(__first, __last);1181 }1182 1183# if _LIBCPP_STD_VER >= 231184 template <_ContainerCompatibleRange<_CharT> _Range>1185 _LIBCPP_HIDE_FROM_ABI constexpr basic_string(1186 from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())1187 : __alloc_(__a) {1188 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {1189 __init_with_size(ranges::begin(__range), ranges::end(__range), ranges::distance(__range));1190 } else {1191 __init_with_sentinel(ranges::begin(__range), ranges::end(__range));1192 }1193 }1194# endif1195 1196# ifndef _LIBCPP_CXX03_LANG1197 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il) {1198 __init(__il.begin(), __il.end());1199 }1200 1201 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)1202 : __alloc_(__a) {1203 __init(__il.begin(), __il.end());1204 }1205# endif // _LIBCPP_CXX03_LANG1206 1207 // TODO(boomanaiden154): Once we mark this in destructors as dead on return,1208 // we can use a normal call to __reset_internal_buffer and remove the extra1209 // __rep constructor.1210 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string() { __reset_internal_buffer(__rep(__uninitialized_tag())); }1211 1212 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator __self_view() const _NOEXCEPT {1213 return __self_view(typename __self_view::__assume_valid(), data(), size());1214 }1215 1216 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string&1217 operator=(const basic_string& __str);1218 1219 template <class _Tp,1220 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1221 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,1222 int> = 0>1223 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {1224 __self_view __sv = __t;1225 return assign(__sv);1226 }1227 1228# ifndef _LIBCPP_CXX03_LANG1229 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1230 operator=(basic_string&& __str) noexcept(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {1231 __move_assign(__str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());1232 return *this;1233 }1234 1235 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(initializer_list<value_type> __il) {1236 return assign(__il.begin(), __il.size());1237 }1238# endif1239 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1240 operator=(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) {1241 return assign(__s);1242 }1243# if _LIBCPP_STD_VER >= 231244 basic_string& operator=(nullptr_t) = delete;1245# endif1246 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);1247 1248 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() _NOEXCEPT {1249 return __make_iterator(__get_pointer());1250 }1251 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator begin() const _NOEXCEPT {1252 return __make_const_iterator(__get_pointer());1253 }1254 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() _NOEXCEPT {1255 return __make_iterator(__get_pointer() + size());1256 }1257 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator end() const _NOEXCEPT {1258 return __make_const_iterator(__get_pointer() + size());1259 }1260 1261 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rbegin() _NOEXCEPT {1262 return reverse_iterator(end());1263 }1264 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator1265 rbegin() const _NOEXCEPT {1266 return const_reverse_iterator(end());1267 }1268 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rend() _NOEXCEPT {1269 return reverse_iterator(begin());1270 }1271 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator rend() const _NOEXCEPT {1272 return const_reverse_iterator(begin());1273 }1274 1275 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cbegin() const _NOEXCEPT {1276 return begin();1277 }1278 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cend() const _NOEXCEPT {1279 return end();1280 }1281 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator1282 crbegin() const _NOEXCEPT {1283 return rbegin();1284 }1285 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator crend() const _NOEXCEPT {1286 return rend();1287 }1288 1289 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT {1290 return __is_long() ? __get_long_size() : __get_short_size();1291 }1292 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {1293 return size();1294 }1295 1296 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT {1297 if (size_type __m = __alloc_traits::max_size(__alloc_); __m <= std::numeric_limits<size_type>::max() / 2) {1298 size_type __res = __m - __alignment;1299 1300 // When the __endian_factor == 2, our string representation assumes that the capacity1301 // (including the null terminator) is always even, so we have to make sure the lowest bit isn't set when the1302 // string grows to max_size()1303 if (__endian_factor == 2)1304 __res &= ~size_type(1);1305 1306 // We have to allocate space for the null terminator, but max_size() doesn't include it.1307 return __res - 1;1308 } else {1309 bool __uses_lsb = __endian_factor == 2;1310 return __uses_lsb ? __m - __alignment - 1 : (__m / 2) - __alignment - 1;1311 }1312 }1313 1314 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {1315 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;1316 }1317 1318 _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);1319 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }1320 1321 _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);1322 1323# if _LIBCPP_STD_VER >= 231324 template <class _Op>1325 _LIBCPP_HIDE_FROM_ABI constexpr void resize_and_overwrite(size_type __n, _Op __op) {1326 using __result_type = decltype(std::move(__op)(data(), auto(__n)));1327 static_assert(__integer_like<__result_type>, "Operation return type must be integer-like");1328 size_type __sz = size();1329 size_type __cap = capacity();1330 if (__n > __cap)1331 __grow_by_without_replace(__cap, __n - __cap, __sz, __sz, 0);1332 __annotate_delete();1333 __set_size(__n);1334 __annotate_new(__n);1335 __erase_to_end(std::move(__op)(data(), auto(__n)));1336 }1337# endif1338 1339# if _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_STRING_RESERVE)1340 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }1341# endif1342 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;1343 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;1344 1345 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool empty() const _NOEXCEPT {1346 return size() == 0;1347 }1348 1349 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference1350 operator[](size_type __pos) const _NOEXCEPT {1351 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");1352 if (__builtin_constant_p(__pos) && !__fits_in_sso(__pos)) {1353 return *(__get_long_pointer() + __pos);1354 }1355 return *(data() + __pos);1356 }1357 1358 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference1359 operator[](size_type __pos) _NOEXCEPT {1360 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");1361 if (__builtin_constant_p(__pos) && !__fits_in_sso(__pos)) {1362 return *(__get_long_pointer() + __pos);1363 }1364 return *(__get_pointer() + __pos);1365 }1366 1367 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;1368 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);1369 1370 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {1371 return append(__str);1372 }1373 1374 template <class _Tp,1375 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1376 !is_same<__remove_cvref_t<_Tp>, basic_string >::value,1377 int> = 0>1378 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const _Tp& __t) {1379 __self_view __sv = __t;1380 return append(__sv);1381 }1382 1383 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1384 operator+=(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) {1385 return append(__s);1386 }1387 1388 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {1389 push_back(__c);1390 return *this;1391 }1392 1393# ifndef _LIBCPP_CXX03_LANG1394 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(initializer_list<value_type> __il) {1395 return append(__il);1396 }1397# endif // _LIBCPP_CXX03_LANG1398 1399 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str) {1400 return append(__str.data(), __str.size());1401 }1402 1403 template <class _Tp,1404 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1405 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,1406 int> = 0>1407 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const _Tp& __t) {1408 __self_view __sv = __t;1409 return append(__sv.data(), __sv.size());1410 }1411 1412 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str, size_type __pos, size_type __n = npos);1413 1414 template <class _Tp,1415 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1416 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,1417 int> = 0>1418 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1419 append(const _Tp& __t, size_type __pos, size_type __n = npos) {1420 __self_view __sv = __t;1421 size_type __sz = __sv.size();1422 if (__pos > __sz)1423 __throw_out_of_range();1424 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));1425 }1426 1427 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n)1428 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero");1429 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);1430 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);1431 1432 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>1433 _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1434 append(_InputIterator __first, _InputIterator __last) {1435 const basic_string __temp(__first, __last, __alloc_);1436 append(__temp.data(), __temp.size());1437 return *this;1438 }1439 1440 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>1441 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1442 append(_ForwardIterator __first, _ForwardIterator __last) {1443 size_type __sz = size();1444 size_type __cap = capacity();1445 size_type __n = static_cast<size_type>(std::distance(__first, __last));1446 if (__n == 0)1447 return *this;1448 1449 if (__string_is_trivial_iterator_v<_ForwardIterator> && !__addr_in_range(*__first)) {1450 if (__cap - __sz < __n)1451 __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);1452 __annotate_increase(__n);1453 auto __end = __copy_non_overlapping_range(__first, __last, std::__to_address(__get_pointer() + __sz));1454 traits_type::assign(*__end, value_type());1455 __set_size(__sz + __n);1456 return *this;1457 } else {1458 const basic_string __temp(__first, __last, __alloc_);1459 return append(__temp.data(), __temp.size());1460 }1461 }1462 1463# if _LIBCPP_STD_VER >= 231464 template <_ContainerCompatibleRange<_CharT> _Range>1465 _LIBCPP_HIDE_FROM_ABI constexpr basic_string& append_range(_Range&& __range) {1466 insert_range(end(), std::forward<_Range>(__range));1467 return *this;1468 }1469# endif1470 1471# ifndef _LIBCPP_CXX03_LANG1472 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(initializer_list<value_type> __il) {1473 return append(__il.begin(), __il.size());1474 }1475# endif // _LIBCPP_CXX03_LANG1476 1477 _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);1478 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();1479 1480 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT {1481 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::front(): string is empty");1482 return *__get_pointer();1483 }1484 1485 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT {1486 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::front(): string is empty");1487 return *data();1488 }1489 1490 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT {1491 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::back(): string is empty");1492 return *(__get_pointer() + size() - 1);1493 }1494 1495 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT {1496 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::back(): string is empty");1497 return *(data() + size() - 1);1498 }1499 1500 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1501 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const _Tp& __t) {1502 __self_view __sv = __t;1503 return assign(__sv.data(), __sv.size());1504 }1505 1506# if _LIBCPP_STD_VER >= 201507 _LIBCPP_HIDE_FROM_ABI constexpr void __move_assign(basic_string&& __str, size_type __pos, size_type __len) {1508 // Pilfer the allocation from __str.1509 _LIBCPP_ASSERT_INTERNAL(__alloc_ == __str.__alloc_, "__move_assign called with wrong allocator");1510 size_type __old_sz = __str.size();1511 if (!__str.__is_long())1512 __str.__annotate_delete();1513 __rep_ = __str.__rep_;1514 __str.__rep_ = __rep();1515 __str.__annotate_new(0);1516 1517 _Traits::move(data(), data() + __pos, __len);1518 __set_size(__len);1519 _Traits::assign(data()[__len], value_type());1520 1521 if (!__is_long()) {1522 __annotate_new(__len);1523 } else if (__old_sz > __len) {1524 __annotate_shrink(__old_sz);1525 }1526 }1527# endif1528 1529 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str) {1530 return *this = __str;1531 }1532# ifndef _LIBCPP_CXX03_LANG1533 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1534 assign(basic_string&& __str) noexcept(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {1535 *this = std::move(__str);1536 return *this;1537 }1538# endif1539 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n = npos);1540 1541 template <class _Tp,1542 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1543 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,1544 int> = 0>1545 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1546 assign(const _Tp& __t, size_type __pos, size_type __n = npos) {1547 __self_view __sv = __t;1548 size_type __sz = __sv.size();1549 if (__pos > __sz)1550 __throw_out_of_range();1551 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));1552 }1553 1554 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n)1555 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero");1556 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);1557 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);1558 1559 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>1560 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1561 assign(_InputIterator __first, _InputIterator __last) {1562 __assign_with_sentinel(__first, __last);1563 return *this;1564 }1565 1566 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>1567 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1568 assign(_ForwardIterator __first, _ForwardIterator __last) {1569 if (__string_is_trivial_iterator_v<_ForwardIterator>) {1570 size_type __n = static_cast<size_type>(std::distance(__first, __last));1571 __assign_trivial(__first, __last, __n);1572 } else {1573 __assign_with_sentinel(__first, __last);1574 }1575 1576 return *this;1577 }1578 1579# if _LIBCPP_STD_VER >= 231580 template <_ContainerCompatibleRange<_CharT> _Range>1581 _LIBCPP_HIDE_FROM_ABI constexpr basic_string& assign_range(_Range&& __range) {1582 if constexpr (__string_is_trivial_iterator_v<ranges::iterator_t<_Range>> &&1583 (ranges::forward_range<_Range> || ranges::sized_range<_Range>)) {1584 size_type __n = static_cast<size_type>(ranges::distance(__range));1585 __assign_trivial(ranges::begin(__range), ranges::end(__range), __n);1586 1587 } else {1588 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));1589 }1590 1591 return *this;1592 }1593# endif1594 1595# ifndef _LIBCPP_CXX03_LANG1596 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(initializer_list<value_type> __il) {1597 return assign(__il.begin(), __il.size());1598 }1599# endif // _LIBCPP_CXX03_LANG1600 1601 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1602 insert(size_type __pos1, const basic_string& __str) {1603 return insert(__pos1, __str.data(), __str.size());1604 }1605 1606 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1607 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos1, const _Tp& __t) {1608 __self_view __sv = __t;1609 return insert(__pos1, __sv.data(), __sv.size());1610 }1611 1612 template <class _Tp,1613 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1614 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,1615 int> = 0>1616 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1617 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n = npos) {1618 __self_view __sv = __t;1619 size_type __str_sz = __sv.size();1620 if (__pos2 > __str_sz)1621 __throw_out_of_range();1622 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));1623 }1624 1625 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1626 insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n = npos);1627 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n)1628 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero");1629 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);1630 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);1631 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);1632 1633# if _LIBCPP_STD_VER >= 231634 template <_ContainerCompatibleRange<_CharT> _Range>1635 _LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {1636 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {1637 auto __n = static_cast<size_type>(ranges::distance(__range));1638 return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);1639 1640 } else {1641 basic_string __temp(from_range, std::forward<_Range>(__range), __alloc_);1642 return insert(__position, __temp.data(), __temp.data() + __temp.size());1643 }1644 }1645# endif1646 1647 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator1648 insert(const_iterator __pos, size_type __n, value_type __c) {1649 difference_type __p = __pos - begin();1650 insert(static_cast<size_type>(__p), __n, __c);1651 return begin() + __p;1652 }1653 1654 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>1655 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator1656 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last) {1657 const basic_string __temp(__first, __last, __alloc_);1658 return insert(__pos, __temp.data(), __temp.data() + __temp.size());1659 }1660 1661 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>1662 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator1663 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last) {1664 auto __n = static_cast<size_type>(std::distance(__first, __last));1665 return __insert_with_size(__pos, __first, __last, __n);1666 }1667 1668# ifndef _LIBCPP_CXX03_LANG1669 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator1670 insert(const_iterator __pos, initializer_list<value_type> __il) {1671 return insert(__pos, __il.begin(), __il.end());1672 }1673# endif // _LIBCPP_CXX03_LANG1674 1675 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);1676 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __pos);1677 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __first, const_iterator __last);1678 1679 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1680 replace(size_type __pos1, size_type __n1, const basic_string& __str) {1681 return replace(__pos1, __n1, __str.data(), __str.size());1682 }1683 1684 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1685 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1686 replace(size_type __pos1, size_type __n1, const _Tp& __t) {1687 __self_view __sv = __t;1688 return replace(__pos1, __n1, __sv.data(), __sv.size());1689 }1690 1691 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1692 replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2 = npos);1693 1694 template <class _Tp,1695 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&1696 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,1697 int> = 0>1698 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1699 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2 = npos) {1700 __self_view __sv = __t;1701 size_type __str_sz = __sv.size();1702 if (__pos2 > __str_sz)1703 __throw_out_of_range();1704 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));1705 }1706 1707 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1708 replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)1709 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n2 != 0 && __s == nullptr, " if n2 is not zero");1710 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1711 replace(size_type __pos, size_type __n1, const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);1712 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);1713 1714 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1715 replace(const_iterator __i1, const_iterator __i2, const basic_string& __str) {1716 return replace(1717 static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __str.data(), __str.size());1718 }1719 1720 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1721 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1722 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) {1723 __self_view __sv = __t;1724 return replace(__i1 - begin(), __i2 - __i1, __sv);1725 }1726 1727 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1728 replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n) {1729 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);1730 }1731 1732 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1733 replace(const_iterator __i1, const_iterator __i2, const value_type* __s) {1734 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);1735 }1736 1737 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1738 replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c) {1739 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);1740 }1741 1742 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>1743 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1744 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2) {1745 const basic_string __temp(__j1, __j2, __alloc_);1746 return replace(__i1, __i2, __temp);1747 }1748 1749# if _LIBCPP_STD_VER >= 231750 template <_ContainerCompatibleRange<_CharT> _Range>1751 _LIBCPP_HIDE_FROM_ABI constexpr basic_string&1752 replace_with_range(const_iterator __i1, const_iterator __i2, _Range&& __range) {1753 basic_string __temp(from_range, std::forward<_Range>(__range), __alloc_);1754 return replace(__i1, __i2, __temp);1755 }1756# endif1757 1758# ifndef _LIBCPP_CXX03_LANG1759 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&1760 replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il) {1761 return replace(__i1, __i2, __il.begin(), __il.end());1762 }1763# endif // _LIBCPP_CXX03_LANG1764 1765 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;1766 1767# if _LIBCPP_STD_VER <= 201768 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI1769 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string substr(size_type __pos = 0, size_type __n = npos) const {1770 return basic_string(*this, __pos, __n);1771 }1772# else1773 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr basic_string substr(size_type __pos = 0, size_type __n = npos) const& {1774 return basic_string(*this, __pos, __n);1775 }1776 1777 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr basic_string substr(size_type __pos = 0, size_type __n = npos) && {1778 return basic_string(std::move(*this), __pos, __n);1779 }1780# endif1781# if _LIBCPP_STD_VER >= 261782 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __self_view subview(size_type __pos = 0, size_type __n = npos) const {1783 return __self_view(*this).subview(__pos, __n);1784 }1785# endif1786 1787 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(basic_string& __str)1788# if _LIBCPP_STD_VER >= 141789 _NOEXCEPT;1790# else1791 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);1792# endif1793 1794 // [string.ops]1795 // ------------1796 1797 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const value_type* c_str() const _NOEXCEPT {1798 return data();1799 }1800 1801 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const value_type* data() const _NOEXCEPT {1802 return std::__to_address(__get_pointer());1803 }1804# if _LIBCPP_STD_VER >= 171805 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 value_type* data() _NOEXCEPT {1806 return std::__to_address(__get_pointer());1807 }1808# endif1809 1810 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator_type get_allocator() const _NOEXCEPT {1811 return __alloc_;1812 }1813 1814 // find1815 1816 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1817 find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT {1818 return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __str.data(), __pos, __str.size());1819 }1820 1821 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1822 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1823 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT {1824 __self_view __sv = __t;1825 return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __sv.data(), __pos, __sv.size());1826 }1827 1828 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1829 find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT1830 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {1831 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find(): received nullptr");1832 return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);1833 }1834 1835 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1836 find(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = 0) const _NOEXCEPT {1837 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find(): received nullptr");1838 return std::__str_find<value_type, size_type, traits_type, npos>(1839 data(), size(), __s, __pos, traits_type::length(__s));1840 }1841 1842 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT {1843 return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);1844 }1845 1846 // rfind1847 1848 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1849 rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT {1850 return std::__str_rfind<value_type, size_type, traits_type, npos>(1851 data(), size(), __str.data(), __pos, __str.size());1852 }1853 1854 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1855 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1856 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT {1857 __self_view __sv = __t;1858 return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __sv.data(), __pos, __sv.size());1859 }1860 1861 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1862 rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT1863 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {1864 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");1865 return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);1866 }1867 1868 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1869 rfind(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = npos) const _NOEXCEPT {1870 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::rfind(): received nullptr");1871 return std::__str_rfind<value_type, size_type, traits_type, npos>(1872 data(), size(), __s, __pos, traits_type::length(__s));1873 }1874 1875 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1876 rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT {1877 return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);1878 }1879 1880 // find_first_of1881 1882 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1883 find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT {1884 return std::__str_find_first_of<value_type, size_type, traits_type, npos>(1885 data(), size(), __str.data(), __pos, __str.size());1886 }1887 1888 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1889 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1890 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT {1891 __self_view __sv = __t;1892 return std::__str_find_first_of<value_type, size_type, traits_type, npos>(1893 data(), size(), __sv.data(), __pos, __sv.size());1894 }1895 1896 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1897 find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT1898 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {1899 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");1900 return std::__str_find_first_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);1901 }1902 1903 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1904 find_first_of(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = 0) const _NOEXCEPT {1905 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_first_of(): received nullptr");1906 return std::__str_find_first_of<value_type, size_type, traits_type, npos>(1907 data(), size(), __s, __pos, traits_type::length(__s));1908 }1909 1910 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1911 find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT {1912 return find(__c, __pos);1913 }1914 1915 // find_last_of1916 1917 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1918 find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT {1919 return std::__str_find_last_of<value_type, size_type, traits_type, npos>(1920 data(), size(), __str.data(), __pos, __str.size());1921 }1922 1923 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1924 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1925 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT {1926 __self_view __sv = __t;1927 return std::__str_find_last_of<value_type, size_type, traits_type, npos>(1928 data(), size(), __sv.data(), __pos, __sv.size());1929 }1930 1931 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1932 find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT1933 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {1934 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");1935 return std::__str_find_last_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);1936 }1937 1938 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1939 find_last_of(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = npos) const _NOEXCEPT {1940 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_last_of(): received nullptr");1941 return std::__str_find_last_of<value_type, size_type, traits_type, npos>(1942 data(), size(), __s, __pos, traits_type::length(__s));1943 }1944 1945 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1946 find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT {1947 return rfind(__c, __pos);1948 }1949 1950 // find_first_not_of1951 1952 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1953 find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT {1954 return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(1955 data(), size(), __str.data(), __pos, __str.size());1956 }1957 1958 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1959 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1960 find_first_not_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT {1961 __self_view __sv = __t;1962 return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(1963 data(), size(), __sv.data(), __pos, __sv.size());1964 }1965 1966 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1967 find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT1968 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {1969 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");1970 return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);1971 }1972 1973 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1974 find_first_not_of(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = 0) const _NOEXCEPT {1975 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_first_not_of(): received nullptr");1976 return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(1977 data(), size(), __s, __pos, traits_type::length(__s));1978 }1979 1980 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1981 find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT {1982 return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);1983 }1984 1985 // find_last_not_of1986 1987 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1988 find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT {1989 return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(1990 data(), size(), __str.data(), __pos, __str.size());1991 }1992 1993 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>1994 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type1995 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT {1996 __self_view __sv = __t;1997 return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(1998 data(), size(), __sv.data(), __pos, __sv.size());1999 }2000 2001 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type2002 find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT2003 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {2004 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");2005 return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);2006 }2007 2008 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type2009 find_last_not_of(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = npos) const _NOEXCEPT {2010 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_last_not_of(): received nullptr");2011 return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(2012 data(), size(), __s, __pos, traits_type::length(__s));2013 }2014 2015 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type2016 find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT {2017 return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);2018 }2019 2020 // compare2021 2022 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int2023 compare(const basic_string& __str) const _NOEXCEPT {2024 return compare(__self_view(__str));2025 }2026 2027 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>2028 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const _Tp& __t) const _NOEXCEPT {2029 __self_view __sv = __t;2030 size_t __lhs_sz = size();2031 size_t __rhs_sz = __sv.size();2032 int __result = traits_type::compare(data(), __sv.data(), std::min(__lhs_sz, __rhs_sz));2033 if (__result != 0)2034 return __result;2035 if (__lhs_sz < __rhs_sz)2036 return -1;2037 if (__lhs_sz > __rhs_sz)2038 return 1;2039 return 0;2040 }2041 2042 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>2043 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int2044 compare(size_type __pos1, size_type __n1, const _Tp& __t) const {2045 __self_view __sv = __t;2046 return compare(__pos1, __n1, __sv.data(), __sv.size());2047 }2048 2049 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int2050 compare(size_type __pos1, size_type __n1, const basic_string& __str) const {2051 return compare(__pos1, __n1, __str.data(), __str.size());2052 }2053 2054 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 int2055 compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2 = npos) const {2056 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);2057 }2058 2059 template <class _Tp,2060 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&2061 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,2062 int> = 0>2063 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int2064 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2 = npos) const {2065 __self_view __sv = __t;2066 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));2067 }2068 2069 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 int2070 compare(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const _NOEXCEPT {2071 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::compare(): received nullptr");2072 return compare(0, npos, __s, traits_type::length(__s));2073 }2074 2075 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 int2076 compare(size_type __pos1, size_type __n1, const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const {2077 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::compare(): received nullptr");2078 return compare(__pos1, __n1, __s, traits_type::length(__s));2079 }2080 2081 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 int2082 compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const2083 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n2 != 0 && __s == nullptr, " if n2 is not zero");2084 2085 // starts_with2086 2087# if _LIBCPP_STD_VER >= 202088 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(__self_view __sv) const noexcept {2089 return __self_view(typename __self_view::__assume_valid(), data(), size()).starts_with(__sv);2090 }2091 2092 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(value_type __c) const noexcept {2093 return !empty() && _Traits::eq(front(), __c);2094 }2095 2096 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool2097 starts_with(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const noexcept {2098 return starts_with(__self_view(__s));2099 }2100 2101 // ends_with2102 2103 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(__self_view __sv) const noexcept {2104 return __self_view(typename __self_view::__assume_valid(), data(), size()).ends_with(__sv);2105 }2106 2107 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(value_type __c) const noexcept {2108 return !empty() && _Traits::eq(back(), __c);2109 }2110 2111 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool2112 ends_with(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const noexcept {2113 return ends_with(__self_view(__s));2114 }2115# endif2116 2117 // contains2118 2119# if _LIBCPP_STD_VER >= 232120 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool contains(__self_view __sv) const noexcept {2121 return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__sv);2122 }2123 2124 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool contains(value_type __c) const noexcept {2125 return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__c);2126 }2127 2128 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool2129 contains(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const {2130 return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__s);2131 }2132# endif2133 2134 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;2135 2136private:2137 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS bool2138 __is_long() const _NOEXCEPT {2139 if (__libcpp_is_constant_evaluated() && __builtin_constant_p(__rep_.__l.__is_long_)) {2140 return __rep_.__l.__is_long_;2141 }2142 return __rep_.__s.__is_long_;2143 }2144 2145 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) { return __sz < __min_cap; }2146 2147 template <class _Iterator, class _Sentinel>2148 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void2149 __assign_trivial(_Iterator __first, _Sentinel __last, size_type __n);2150 2151 template <class _Iterator, class _Sentinel>2152 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __first, _Sentinel __last);2153 2154 // Copy [__first, __last) into [__dest, __dest + (__last - __first)). Assumes that the ranges don't overlap.2155 template <class _ForwardIter, class _Sent>2156 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 static value_type*2157 __copy_non_overlapping_range(_ForwardIter __first, _Sent __last, value_type* __dest) {2158# ifndef _LIBCPP_CXX03_LANG2159 if constexpr (__libcpp_is_contiguous_iterator<_ForwardIter>::value &&2160 is_same<value_type, __remove_cvref_t<decltype(*__first)>>::value &&2161 is_same<_ForwardIter, _Sent>::value) {2162 _LIBCPP_ASSERT_INTERNAL(2163 !std::__is_overlapping_range(std::__to_address(__first), std::__to_address(__last), __dest),2164 "__copy_non_overlapping_range called with an overlapping range!");2165 traits_type::copy(__dest, std::__to_address(__first), __last - __first);2166 return __dest + (__last - __first);2167 }2168# endif2169 2170 for (; __first != __last; ++__first)2171 traits_type::assign(*__dest++, *__first);2172 return __dest;2173 }2174 2175 template <class _ForwardIterator, class _Sentinel>2176 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 iterator2177 __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _Sentinel __last) {2178 size_type __sz = size();2179 size_type __cap = capacity();2180 value_type* __p;2181 if (__cap - __sz >= __n) {2182 __annotate_increase(__n);2183 __p = std::__to_address(__get_pointer());2184 size_type __n_move = __sz - __ip;2185 if (__n_move != 0)2186 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);2187 } else {2188 __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);2189 __p = std::__to_address(__get_long_pointer());2190 }2191 __sz += __n;2192 __set_size(__sz);2193 traits_type::assign(__p[__sz], value_type());2194 __copy_non_overlapping_range(std::move(__first), std::move(__last), __p + __ip);2195 2196 return begin() + __ip;2197 }2198 2199 template <class _Iterator, class _Sentinel>2200 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator2201 __insert_with_size(const_iterator __pos, _Iterator __first, _Sentinel __last, size_type __n);2202 2203 // internal buffer accessors2204 // -------------------------2205 2206 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void2207 __set_short_size(size_type __s) _NOEXCEPT {2208 _LIBCPP_ASSERT_INTERNAL(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");2209 __rep_.__s.__size_ = __s;2210 __rep_.__s.__is_long_ = false;2211 }2212 2213 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS size_type2214 __get_short_size() const _NOEXCEPT {2215 _LIBCPP_ASSERT_INTERNAL(!__rep_.__s.__is_long_, "String has to be short when trying to get the short size");2216 return __rep_.__s.__size_;2217 }2218 2219 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_long_size(size_type __s) _NOEXCEPT {2220 __rep_.__l.__size_ = __s;2221 }2222 2223 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __get_long_size() const _NOEXCEPT {2224 _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long size");2225 return __rep_.__l.__size_;2226 }2227 2228 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_size(size_type __s) _NOEXCEPT {2229 if (__is_long())2230 __set_long_size(__s);2231 else2232 __set_short_size(__s);2233 }2234 2235 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __get_long_cap() const _NOEXCEPT {2236 _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long capacity");2237 return __rep_.__l.__cap_ * __endian_factor;2238 }2239 2240 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __get_long_pointer() _NOEXCEPT {2241 _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long pointer");2242 return _LIBCPP_ASAN_VOLATILE_WRAPPER(__rep_.__l.__data_);2243 }2244 2245 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer __get_long_pointer() const _NOEXCEPT {2246 _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long pointer");2247 return _LIBCPP_ASAN_VOLATILE_WRAPPER(__rep_.__l.__data_);2248 }2249 2250 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS pointer2251 __get_short_pointer() _NOEXCEPT {2252 return _LIBCPP_ASAN_VOLATILE_WRAPPER(pointer_traits<pointer>::pointer_to(__rep_.__s.__data_[0]));2253 }2254 2255 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS const_pointer2256 __get_short_pointer() const _NOEXCEPT {2257 return _LIBCPP_ASAN_VOLATILE_WRAPPER(pointer_traits<const_pointer>::pointer_to(__rep_.__s.__data_[0]));2258 }2259 2260 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __get_pointer() _NOEXCEPT {2261 return __is_long() ? __get_long_pointer() : __get_short_pointer();2262 }2263 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer __get_pointer() const _NOEXCEPT {2264 return __is_long() ? __get_long_pointer() : __get_short_pointer();2265 }2266 2267 // Internal buffer management2268 // --------------------------2269 //2270 // These functions are only responsible for managing the buffer itself, not the value inside the buffer. As such,2271 // none of these facilities ensure that there is a null terminator at `data()[size()]`.2272 2273 // Allocate a buffer of __capacity size with __alloc and return it2274 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 __long2275 __allocate_long_buffer(_Allocator& __alloc, size_type __capacity) {2276 _LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__capacity),2277 "Trying to allocate long buffer for a capacity what would fit into the small buffer");2278 auto __buffer = std::__allocate_at_least(__alloc, __align_allocation_size(__capacity));2279 2280 if (__libcpp_is_constant_evaluated()) {2281 for (size_type __i = 0; __i != __buffer.count; ++__i)2282 std::__construct_at(std::addressof(__buffer.ptr[__i]));2283 }2284 2285 return __long(__buffer, __capacity);2286 }2287 2288 // Replace the current buffer with __new_rep. Deallocate the old long buffer if it exists.2289 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reset_internal_buffer(__rep __new_rep = __short()) {2290 __annotate_delete();2291 if (__is_long())2292 __alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());2293 __rep_ = __new_rep;2294 }2295 2296 // Initialize the internal buffer to hold __size elements2297 // The elements and null terminator have to be set by the caller2298 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __init_internal_buffer(size_type __size) {2299 if (__libcpp_is_constant_evaluated())2300 __rep_ = __rep();2301 2302 if (__size > max_size())2303 __throw_length_error();2304 2305 if (__fits_in_sso(__size)) {2306 __set_short_size(__size);2307 __annotate_new(__size);2308 return __get_short_pointer();2309 } else {2310 __rep_.__l = __allocate_long_buffer(__alloc_, __size);2311 __annotate_new(__size);2312 return __get_long_pointer();2313 }2314 }2315 2316 // ASan annotation helpers2317 // -----------------------2318 2319 // The following functions are no-ops outside of AddressSanitizer mode.2320 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void2321 __annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {2322 (void)__old_mid;2323 (void)__new_mid;2324# if _LIBCPP_INSTRUMENTED_WITH_ASAN2325# if defined(__APPLE__)2326 // TODO: remove after addressing issue #96099 (https://llvm.org/PR96099)2327 if (!__is_long())2328 return;2329# endif2330 std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity() + 1, __old_mid, __new_mid);2331# endif2332 }2333 2334 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_new(size_type __current_size) const _NOEXCEPT {2335 __annotate_contiguous_container(data() + capacity() + 1, data() + __current_size + 1);2336 }2337 2338 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_delete() const _NOEXCEPT {2339 __annotate_contiguous_container(data() + size() + 1, data() + capacity() + 1);2340 }2341 2342 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_increase(size_type __n) const _NOEXCEPT {2343 __annotate_contiguous_container(data() + size() + 1, data() + size() + 1 + __n);2344 }2345 2346 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_shrink(size_type __old_size) const _NOEXCEPT {2347 __annotate_contiguous_container(data() + __old_size + 1, data() + size() + 1);2348 }2349 2350 // Disable ASan annotations and enable them again when going out of scope. It is assumed that the string is in a valid2351 // state at that point, so `size()` can be called safely.2352 struct [[__nodiscard__]] __annotation_guard {2353 __annotation_guard(const __annotation_guard&) = delete;2354 __annotation_guard& operator=(const __annotation_guard&) = delete;2355 2356 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __annotation_guard(basic_string& __str) : __str_(__str) {2357 __str_.__annotate_delete();2358 }2359 2360 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__annotation_guard() { __str_.__annotate_new(__str_.size()); }2361 2362 basic_string& __str_;2363 };2364 2365 template <size_type __a>2366 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __align_it(size_type __s) _NOEXCEPT {2367 return (__s + (__a - 1)) & ~(__a - 1);2368 }2369 enum { __alignment = 8 };2370 2371 // This makes sure that we're using a capacity with some extra alignment, since allocators almost always over-align2372 // the allocations anyways, improving memory usage. More importantly, this ensures that the lowest bit is never set2373 // if __endian_factor == 2, allowing us to store whether we're in the long string inside the lowest bit.2374 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type2375 __align_allocation_size(size_type __size) _NOEXCEPT {2376 _LIBCPP_ASSERT_INTERNAL(2377 !__fits_in_sso(__size), "Trying to align allocation of a size which would fit into the SSO");2378 const size_type __boundary = sizeof(value_type) < __alignment ? __alignment / sizeof(value_type) : __endian_factor;2379 size_type __guess = __align_it<__boundary>(__size + 1);2380 if (__guess == __min_cap + 1)2381 __guess += __endian_factor;2382 2383 _LIBCPP_ASSERT_INTERNAL(__guess >= __size, "aligned allocation size is below the requested size");2384 return __guess;2385 }2386 2387 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type2388 __get_amortized_growth_capacity(size_type __required_capacity) {2389 size_type __max_size = max_size();2390 if (__required_capacity > __max_size)2391 __throw_length_error();2392 size_type __current_cap = capacity();2393 _LIBCPP_ASSERT_INTERNAL(2394 __current_cap < __required_capacity, "Trying to grow string even though there is enough capacity already?");2395 if (__current_cap > __max_size / 2 - __alignment)2396 return __max_size;2397 return std::max(__required_capacity, 2 * __current_cap);2398 }2399 2400 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(const value_type* __s, size_type __sz);2401 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(size_type __n, value_type __c);2402 2403 // Slow path for the (inlined) copy constructor for 'long' strings.2404 // Always externally instantiated and not inlined.2405 // Requires that __s is zero terminated.2406 // The main reason for this function to exist is because for unstable, we2407 // want to allow inlining of the copy constructor. However, we don't want2408 // to call the __init() functions as those are marked as inline which may2409 // result in over-aggressive inlining by the compiler, where our aim is2410 // to only inline the fast path code directly in the ctor.2411 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void __init_copy_ctor_external(const value_type* __s, size_type __sz);2412 2413 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>2414 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(_InputIterator __first, _InputIterator __last);2415 2416 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>2417 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(_ForwardIterator __first, _ForwardIterator __last);2418 2419 template <class _InputIterator, class _Sentinel>2420 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void2421 __init_with_sentinel(_InputIterator __first, _Sentinel __last);2422 template <class _InputIterator, class _Sentinel>2423 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void2424 __init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz);2425 2426 _LIBCPP_CONSTEXPR_SINCE_CXX202427# if _LIBCPP_ABI_VERSION >= 2 // We want to use the function in the dylib in ABIv12428 _LIBCPP_HIDE_FROM_ABI2429# endif2430 _LIBCPP_DEPRECATED_("use __grow_by_without_replace") void __grow_by(2431 size_type __old_cap,2432 size_type __delta_cap,2433 size_type __old_sz,2434 size_type __n_copy,2435 size_type __n_del,2436 size_type __n_add = 0);2437 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __grow_by_without_replace(2438 size_type __old_cap,2439 size_type __delta_cap,2440 size_type __old_sz,2441 size_type __n_copy,2442 size_type __n_del,2443 size_type __n_add = 0);2444 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __grow_by_and_replace(2445 size_type __old_cap,2446 size_type __delta_cap,2447 size_type __old_sz,2448 size_type __n_copy,2449 size_type __n_del,2450 size_type __n_add,2451 const value_type* __p_new_stuff);2452 2453 // __assign_no_alias is invoked for assignment operations where we2454 // have proof that the input does not alias the current instance.2455 // For example, operator=(basic_string) performs a 'self' check.2456 template <bool __is_short>2457 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_no_alias(const value_type* __s, size_type __n);2458 2459 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_to_end(size_type __pos) {2460 _LIBCPP_ASSERT_INTERNAL(__pos <= capacity(), "Trying to erase at position outside the strings capacity!");2461 __null_terminate_at(std::__to_address(__get_pointer()), __pos);2462 }2463 2464 // __erase_external_with_move is invoked for erase() invocations where2465 // `n ~= npos`, likely requiring memory moves on the string data.2466 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void __erase_external_with_move(size_type __pos, size_type __n);2467 2468 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str) {2469 __copy_assign_alloc(2470 __str, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());2471 }2472 2473 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str, true_type) {2474 if (__alloc_ == __str.__alloc_)2475 __alloc_ = __str.__alloc_;2476 else {2477 if (!__str.__is_long()) {2478 __reset_internal_buffer();2479 __alloc_ = __str.__alloc_;2480 } else {2481 __annotate_delete();2482 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));2483 auto __alloc = __str.__alloc_;2484 __reset_internal_buffer(__allocate_long_buffer(__alloc, __str.size()));2485 __alloc_ = std::move(__alloc);2486 }2487 }2488 }2489 2490 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void2491 __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT {}2492 2493# ifndef _LIBCPP_CXX03_LANG2494 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void2495 __move_assign(basic_string& __str, false_type) noexcept(__alloc_traits::is_always_equal::value);2496 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void2497 __move_assign(basic_string& __str, true_type)2498# if _LIBCPP_STD_VER >= 172499 noexcept;2500# else2501 noexcept(is_nothrow_move_assignable<allocator_type>::value);2502# endif2503# endif2504 2505 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __str)2506 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||2507 is_nothrow_move_assignable<allocator_type>::value) {2508 __move_assign_alloc(2509 __str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());2510 }2511 2512 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __c, true_type)2513 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {2514 __alloc_ = std::move(__c.__alloc_);2515 }2516 2517 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string&, false_type) _NOEXCEPT {}2518 2519 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s);2520 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s, size_type __n);2521 2522 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.2523 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_short(const value_type* __s, size_type __n) {2524 size_type __old_size = size();2525 if (__n > __old_size)2526 __annotate_increase(__n - __old_size);2527 pointer __p;2528 if (__is_long()) {2529 __set_long_size(__n);2530 __p = __get_long_pointer();2531 } else {2532 __set_short_size(__n);2533 __p = __get_short_pointer();2534 }2535 traits_type::move(std::__to_address(__p), __s, __n);2536 traits_type::assign(__p[__n], value_type());2537 if (__old_size > __n)2538 __annotate_shrink(__old_size);2539 return *this;2540 }2541 2542 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&2543 __null_terminate_at(value_type* __p, size_type __newsz) {2544 size_type __old_size = size();2545 if (__newsz > __old_size)2546 __annotate_increase(__newsz - __old_size);2547 __set_size(__newsz);2548 traits_type::assign(__p[__newsz], value_type());2549 if (__old_size > __newsz)2550 __annotate_shrink(__old_size);2551 return *this;2552 }2553 2554 template <class _Tp>2555 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __addr_in_range(const _Tp& __v) const {2556 return std::__is_pointer_in_range(data(), data() + size() + 1, std::addressof(__v));2557 }2558 2559 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_length_error() {2560 std::__throw_length_error("basic_string");2561 }2562 2563 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_out_of_range() {2564 std::__throw_out_of_range("basic_string");2565 }2566 2567 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string2568 __concatenate_strings<>(const _Allocator&, __type_identity_t<__self_view>, __type_identity_t<__self_view>);2569 2570 template <class _CharT2, class _Traits2, class _Allocator2>2571 friend inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool2572 operator==(const basic_string<_CharT2, _Traits2, _Allocator2>&, const _CharT2*) _NOEXCEPT;2573 2574 // These functions aren't used anymore but are part of out ABI, so we need to provide them in the dylib for backwards2575 // compatibility2576# ifdef _LIBCPP_BUILDING_LIBRARY2577 void __init(const value_type* __s, size_type __sz, size_type __reserve);2578# endif2579};2580 2581// These declarations must appear before any functions are implicitly used2582// so that they have the correct visibility specifier.2583# define _LIBCPP_DECLARE(...) extern template _LIBCPP_EXPORTED_FROM_ABI __VA_ARGS__;2584# ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION2585_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)2586# if _LIBCPP_HAS_WIDE_CHARACTERS2587_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)2588# endif2589# else2590_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)2591# if _LIBCPP_HAS_WIDE_CHARACTERS2592_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)2593# endif2594# endif2595# undef _LIBCPP_DECLARE2596 2597# if _LIBCPP_STD_VER <= 17 || !__has_builtin(__builtin_lt_synthesizes_from_spaceship)2598template <class _CharT, class _Traits, class _Alloc>2599struct __default_three_way_comparator<basic_string<_CharT, _Traits, _Alloc>, basic_string<_CharT, _Traits, _Alloc> > {2600 using __string_t _LIBCPP_NODEBUG = basic_string<_CharT, _Traits, _Alloc>;2601 2602 _LIBCPP_HIDE_FROM_ABI static int operator()(const __string_t& __lhs, const __string_t& __rhs) {2603 auto __min_len = std::min(__lhs.size(), __rhs.size());2604 auto __ret = _Traits::compare(__lhs.data(), __rhs.data(), __min_len);2605 if (__ret == 0)2606 return __lhs.size() == __rhs.size() ? 0 : __lhs.size() < __rhs.size() ? -1 : 1;2607 return __ret;2608 }2609};2610# endif2611 2612template <class _Comparator, class _CharT, class _Traits, class _Alloc>2613inline const bool __is_transparently_comparable_v<_Comparator,2614 basic_string<_CharT, _Traits, _Alloc>,2615 const _CharT*,2616 __enable_if_t<__is_generic_transparent_comparator_v<_Comparator> > > =2617 true;2618 2619template <class _Comparator, class _CharT, class _Traits, class _Alloc, size_t _Np>2620inline const bool __is_transparently_comparable_v<_Comparator,2621 basic_string<_CharT, _Traits, _Alloc>,2622 _CharT[_Np],2623 __enable_if_t<__is_generic_transparent_comparator_v<_Comparator> > > =2624 true;2625 2626# if _LIBCPP_STD_VER >= 172627template <class _InputIterator,2628 class _CharT = __iterator_value_type<_InputIterator>,2629 class _Allocator = allocator<_CharT>,2630 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,2631 class = enable_if_t<__is_allocator_v<_Allocator>>>2632basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())2633 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;2634 2635template <class _CharT,2636 class _Traits,2637 class _Allocator = allocator<_CharT>,2638 class = enable_if_t<__is_allocator_v<_Allocator>>>2639explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())2640 -> basic_string<_CharT, _Traits, _Allocator>;2641 2642template <class _CharT,2643 class _Traits,2644 class _Allocator = allocator<_CharT>,2645 class = enable_if_t<__is_allocator_v<_Allocator>>,2646 class _Sz = typename allocator_traits<_Allocator>::size_type >2647basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())2648 -> basic_string<_CharT, _Traits, _Allocator>;2649# endif2650 2651# if _LIBCPP_STD_VER >= 232652template <ranges::input_range _Range,2653 class _Allocator = allocator<ranges::range_value_t<_Range>>,2654 class = enable_if_t<__is_allocator_v<_Allocator>>>2655basic_string(from_range_t, _Range&&, _Allocator = _Allocator())2656 -> basic_string<ranges::range_value_t<_Range>, char_traits<ranges::range_value_t<_Range>>, _Allocator>;2657# endif2658 2659template <class _CharT, class _Traits, class _Allocator>2660_LIBCPP_CONSTEXPR_SINCE_CXX20 void2661basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz) {2662 pointer __p = __init_internal_buffer(__sz);2663 traits_type::copy(std::__to_address(__p), __s, __sz);2664 traits_type::assign(__p[__sz], value_type());2665}2666 2667template <class _CharT, class _Traits, class _Allocator>2668_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void2669basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(const value_type* __s, size_type __sz) {2670 pointer __p = __init_internal_buffer(__sz);2671 traits_type::copy(std::__to_address(__p), __s, __sz + 1);2672}2673 2674template <class _CharT, class _Traits, class _Allocator>2675_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c) {2676 pointer __p = __init_internal_buffer(__n);2677 traits_type::assign(std::__to_address(__p), __n, __c);2678 traits_type::assign(__p[__n], value_type());2679}2680 2681template <class _CharT, class _Traits, class _Allocator>2682template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >2683_LIBCPP_CONSTEXPR_SINCE_CXX20 void2684basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last) {2685 __init_with_sentinel(std::move(__first), std::move(__last));2686}2687 2688template <class _CharT, class _Traits, class _Allocator>2689template <class _InputIterator, class _Sentinel>2690_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void2691basic_string<_CharT, _Traits, _Allocator>::__init_with_sentinel(_InputIterator __first, _Sentinel __last) {2692 __rep_ = __rep();2693 __annotate_new(0);2694 2695 auto __guard = std::__make_exception_guard([this] { __reset_internal_buffer(); });2696 for (; __first != __last; ++__first)2697 push_back(*__first);2698 __guard.__complete();2699}2700 2701template <class _CharT, class _Traits, class _Allocator>2702template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >2703_LIBCPP_CONSTEXPR_SINCE_CXX20 void2704basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last) {2705 size_type __sz = static_cast<size_type>(std::distance(__first, __last));2706 __init_with_size(__first, __last, __sz);2707}2708 2709template <class _CharT, class _Traits, class _Allocator>2710template <class _InputIterator, class _Sentinel>2711_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void2712basic_string<_CharT, _Traits, _Allocator>::__init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz) {2713 pointer __p = __init_internal_buffer(__sz);2714 2715 auto __guard = std::__make_exception_guard([this] { __reset_internal_buffer(); });2716 auto __end = __copy_non_overlapping_range(std::move(__first), std::move(__last), std::__to_address(__p));2717 traits_type::assign(*__end, value_type());2718 __guard.__complete();2719}2720 2721template <class _CharT, class _Traits, class _Allocator>2722_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace(2723 size_type __old_cap,2724 size_type __delta_cap,2725 size_type __old_sz,2726 size_type __n_copy,2727 size_type __n_del,2728 size_type __n_add,2729 const value_type* __p_new_stuff) {2730 __long __buffer = __allocate_long_buffer(__alloc_, __get_amortized_growth_capacity(__old_cap + __delta_cap));2731 pointer __old_p = __get_pointer();2732 __annotate_delete();2733 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));2734 if (__n_copy != 0)2735 traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);2736 if (__n_add != 0)2737 traits_type::copy(std::__to_address(__buffer.__data_) + __n_copy, __p_new_stuff, __n_add);2738 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;2739 if (__sec_cp_sz != 0)2740 traits_type::copy(std::__to_address(__buffer.__data_) + __n_copy + __n_add,2741 std::__to_address(__old_p) + __n_copy + __n_del,2742 __sec_cp_sz);2743 __buffer.__size_ = __n_copy + __n_add + __sec_cp_sz;2744 traits_type::assign(__buffer.__data_[__buffer.__size_], value_type());2745 __reset_internal_buffer(__buffer);2746}2747 2748// __grow_by is deprecated because it does not set the size. It may not update the size when the size is changed, and it2749// may also not set the size at all when the string was short initially. This leads to unpredictable size value. It is2750// not removed or changed to avoid breaking the ABI.2751template <class _CharT, class _Traits, class _Allocator>2752void _LIBCPP_CONSTEXPR_SINCE_CXX202753# if _LIBCPP_ABI_VERSION >= 2 // We want to use the function in the dylib in ABIv12754_LIBCPP_HIDE_FROM_ABI2755# endif2756_LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Traits, _Allocator>::__grow_by(2757 size_type __old_cap,2758 size_type __delta_cap,2759 size_type __old_sz,2760 size_type __n_copy,2761 size_type __n_del,2762 size_type __n_add) {2763 __long __buffer = __allocate_long_buffer(__alloc_, __get_amortized_growth_capacity(__old_cap + __delta_cap));2764 pointer __old_p = __get_pointer();2765 if (__n_copy != 0)2766 traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);2767 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;2768 if (__sec_cp_sz != 0)2769 traits_type::copy(std::__to_address(__buffer.__data_) + __n_copy + __n_add,2770 std::__to_address(__old_p) + __n_copy + __n_del,2771 __sec_cp_sz);2772 2773 // This is -1 to make sure the caller sets the size properly, since old versions of this function didn't set the size2774 // at all.2775 __buffer.__size_ = -1;2776 __reset_internal_buffer(__buffer);2777}2778 2779template <class _CharT, class _Traits, class _Allocator>2780void _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI2781basic_string<_CharT, _Traits, _Allocator>::__grow_by_without_replace(2782 size_type __old_cap,2783 size_type __delta_cap,2784 size_type __old_sz,2785 size_type __n_copy,2786 size_type __n_del,2787 size_type __n_add) {2788 __annotate_delete();2789 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));2790 _LIBCPP_SUPPRESS_DEPRECATED_PUSH2791 __grow_by(__old_cap, __delta_cap, __old_sz, __n_copy, __n_del, __n_add);2792 _LIBCPP_SUPPRESS_DEPRECATED_POP2793 // Due to the ABI of __grow_by we have to set the size after calling it.2794 __set_long_size(__old_sz - __n_del + __n_add);2795}2796 2797// assign2798 2799template <class _CharT, class _Traits, class _Allocator>2800template <bool __is_short>2801_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&2802basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {2803 const auto __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();2804 const auto __size = __is_short ? __get_short_size() : __get_long_size();2805 if (__n >= __cap) {2806 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __size, 0, __size, __n, __s);2807 return *this;2808 }2809 2810 __annotate_delete();2811 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));2812 pointer __p;2813 if (__is_short) {2814 __p = __get_short_pointer();2815 __set_short_size(__n);2816 } else {2817 __p = __get_long_pointer();2818 __set_long_size(__n);2819 }2820 traits_type::copy(std::__to_address(__p), __s, __n);2821 traits_type::assign(__p[__n], value_type());2822 return *this;2823}2824 2825template <class _CharT, class _Traits, class _Allocator>2826_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&2827basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {2828 const auto __cap = capacity();2829 const auto __size = size();2830 if (__cap >= __n) {2831 if (__n > __size)2832 __annotate_increase(__n - __size);2833 value_type* __p = std::__to_address(__get_pointer());2834 traits_type::move(__p, __s, __n);2835 return __null_terminate_at(__p, __n);2836 } else {2837 __grow_by_and_replace(__cap, __n - __cap, __size, 0, __size, __n, __s);2838 return *this;2839 }2840}2841 2842template <class _CharT, class _Traits, class _Allocator>2843_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&2844basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n) {2845 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::assign received nullptr");2846 return (__builtin_constant_p(__n) && __fits_in_sso(__n)) ? __assign_short(__s, __n) : __assign_external(__s, __n);2847}2848 2849template <class _CharT, class _Traits, class _Allocator>2850_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&2851basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c) {2852 size_type __cap = capacity();2853 size_type __old_size = size();2854 if (__cap < __n) {2855 __grow_by_without_replace(__cap, __n - __cap, __old_size, 0, __old_size);2856 __annotate_increase(__n);2857 } else if (__n > __old_size)2858 __annotate_increase(__n - __old_size);2859 value_type* __p = std::__to_address(__get_pointer());2860 traits_type::assign(__p, __n, __c);2861 return __null_terminate_at(__p, __n);2862}2863 2864template <class _CharT, class _Traits, class _Allocator>2865_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&2866basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {2867 size_type __old_size = size();2868 if (__old_size == 0)2869 __annotate_increase(1);2870 pointer __p;2871 if (__is_long()) {2872 __p = __get_long_pointer();2873 __set_long_size(1);2874 } else {2875 __p = __get_short_pointer();2876 __set_short_size(1);2877 }2878 traits_type::assign(*__p, __c);2879 traits_type::assign(*++__p, value_type());2880 if (__old_size > 1)2881 __annotate_shrink(__old_size);2882 return *this;2883}2884 2885template <class _CharT, class _Traits, class _Allocator>2886_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string<_CharT, _Traits, _Allocator>&2887basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str) {2888 if (this == std::addressof(__str))2889 return *this;2890 2891 __copy_assign_alloc(__str);2892 2893 if (__is_long())2894 return __assign_no_alias<false>(__str.data(), __str.size());2895 2896 if (__str.__is_long())2897 return __assign_no_alias<true>(__str.data(), __str.size());2898 2899 __annotate_delete();2900 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));2901 __rep_ = __str.__rep_;2902 2903 return *this;2904}2905 2906# ifndef _LIBCPP_CXX03_LANG2907 2908template <class _CharT, class _Traits, class _Allocator>2909inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__move_assign(2910 basic_string& __str, false_type) noexcept(__alloc_traits::is_always_equal::value) {2911 if (__alloc_ != __str.__alloc_)2912 assign(__str);2913 else2914 __move_assign(__str, true_type());2915}2916 2917template <class _CharT, class _Traits, class _Allocator>2918inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void2919basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)2920# if _LIBCPP_STD_VER >= 172921 noexcept2922# else2923 noexcept(is_nothrow_move_assignable<allocator_type>::value)2924# endif2925{2926 __annotate_delete();2927 if (__is_long()) {2928 __reset_internal_buffer();2929# if _LIBCPP_STD_VER <= 142930 if (!is_nothrow_move_assignable<allocator_type>::value) {2931 __set_short_size(0);2932 traits_type::assign(__get_short_pointer()[0], value_type());2933 __annotate_new(0);2934 }2935# endif2936 }2937 size_type __str_old_size = __str.size();2938 bool __str_was_short = !__str.__is_long();2939 2940 __move_assign_alloc(__str);2941 __rep_ = __str.__rep_;2942 __str.__set_short_size(0);2943 traits_type::assign(__str.__get_short_pointer()[0], value_type());2944 2945 if (__str_was_short && this != std::addressof(__str))2946 __str.__annotate_shrink(__str_old_size);2947 else2948 // ASan annotations: was long, so object memory is unpoisoned as new.2949 // Or is same as *this, and __annotate_delete() was called.2950 __str.__annotate_new(0);2951 2952 // ASan annotations: Guard against `std::string s; s = std::move(s);`2953 // You can find more here: https://en.cppreference.com/w/cpp/utility/move2954 // Quote: "Unless otherwise specified, all standard library objects that have been moved2955 // from are placed in a "valid but unspecified state", meaning the object's class2956 // invariants hold (so functions without preconditions, such as the assignment operator,2957 // can be safely used on the object after it was moved from):"2958 // Quote: "v = std::move(v); // the value of v is unspecified"2959 if (!__is_long() && std::addressof(__str) != this)2960 // If it is long string, delete was never called on original __str's buffer.2961 __annotate_new(__get_short_size());2962}2963 2964# endif2965 2966template <class _CharT, class _Traits, class _Allocator>2967template <class _InputIterator, class _Sentinel>2968_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void2969basic_string<_CharT, _Traits, _Allocator>::__assign_with_sentinel(_InputIterator __first, _Sentinel __last) {2970 const basic_string __temp(__init_with_sentinel_tag(), std::move(__first), std::move(__last), __alloc_);2971 assign(__temp.data(), __temp.size());2972}2973 2974template <class _CharT, class _Traits, class _Allocator>2975template <class _Iterator, class _Sentinel>2976_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void2977basic_string<_CharT, _Traits, _Allocator>::__assign_trivial(_Iterator __first, _Sentinel __last, size_type __n) {2978 _LIBCPP_ASSERT_INTERNAL(2979 __string_is_trivial_iterator_v<_Iterator>, "The iterator type given to `__assign_trivial` must be trivial");2980 2981 size_type __old_size = size();2982 size_type __cap = capacity();2983 if (__cap < __n) {2984 // Unlike `append` functions, if the input range points into the string itself, there is no case that the input2985 // range could get invalidated by reallocation:2986 // 1. If the input range is a subset of the string itself, its size cannot exceed the capacity of the string,2987 // thus no reallocation would happen.2988 // 2. In the exotic case where the input range is the byte representation of the string itself, the string2989 // object itself stays valid even if reallocation happens.2990 size_type __sz = size();2991 __grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);2992 __annotate_increase(__n);2993 } else if (__n > __old_size)2994 __annotate_increase(__n - __old_size);2995 pointer __p = __get_pointer();2996 for (; __first != __last; ++__p, (void)++__first)2997 traits_type::assign(*__p, *__first);2998 traits_type::assign(*__p, value_type());2999 __set_size(__n);3000 if (__n < __old_size)3001 __annotate_shrink(__old_size);3002}3003 3004template <class _CharT, class _Traits, class _Allocator>3005_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3006basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n) {3007 size_type __sz = __str.size();3008 if (__pos > __sz)3009 this->__throw_out_of_range();3010 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));3011}3012 3013template <class _CharT, class _Traits, class _Allocator>3014_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&3015basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {3016 return __assign_external(__s, traits_type::length(__s));3017}3018 3019template <class _CharT, class _Traits, class _Allocator>3020_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3021basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {3022 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");3023 if (auto __len = traits_type::length(__s); __builtin_constant_p(__len) && __fits_in_sso(__len))3024 return __assign_short(__s, __len);3025 return __assign_external(__s);3026}3027// append3028 3029template <class _CharT, class _Traits, class _Allocator>3030_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3031basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n) {3032 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::append received nullptr");3033 size_type __cap = capacity();3034 size_type __sz = size();3035 if (__cap - __sz < __n) {3036 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);3037 return *this;3038 }3039 3040 if (__n == 0)3041 return *this;3042 3043 __annotate_increase(__n);3044 value_type* __p = std::__to_address(__get_pointer());3045 traits_type::copy(__p + __sz, __s, __n);3046 __sz += __n;3047 __set_size(__sz);3048 traits_type::assign(__p[__sz], value_type());3049 return *this;3050}3051 3052template <class _CharT, class _Traits, class _Allocator>3053_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3054basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c) {3055 if (__n == 0)3056 return *this;3057 3058 size_type __cap = capacity();3059 size_type __sz = size();3060 if (__cap - __sz < __n)3061 __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);3062 __annotate_increase(__n);3063 pointer __p = __get_pointer();3064 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);3065 __sz += __n;3066 __set_size(__sz);3067 traits_type::assign(__p[__sz], value_type());3068 return *this;3069}3070 3071template <class _CharT, class _Traits, class _Allocator>3072_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c) {3073 bool __is_short = !__is_long();3074 size_type __cap;3075 size_type __sz;3076 if (__is_short) {3077 __cap = __min_cap - 1;3078 __sz = __get_short_size();3079 } else {3080 __cap = __get_long_cap() - 1;3081 __sz = __get_long_size();3082 }3083 if (__sz == __cap) {3084 __grow_by_without_replace(__cap, 1, __sz, __sz, 0);3085 __is_short = false; // the string is always long after __grow_by3086 }3087 __annotate_increase(1);3088 pointer __p;3089 if (__is_short) {3090 __p = __get_short_pointer() + __sz;3091 __set_short_size(__sz + 1);3092 } else {3093 __p = __get_long_pointer() + __sz;3094 __set_long_size(__sz + 1);3095 }3096 traits_type::assign(*__p, __c);3097 traits_type::assign(*++__p, value_type());3098}3099 3100template <class _CharT, class _Traits, class _Allocator>3101_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3102basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n) {3103 size_type __sz = __str.size();3104 if (__pos > __sz)3105 this->__throw_out_of_range();3106 return append(__str.data() + __pos, std::min(__n, __sz - __pos));3107}3108 3109template <class _CharT, class _Traits, class _Allocator>3110_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3111basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s) {3112 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::append received nullptr");3113 return append(__s, traits_type::length(__s));3114}3115 3116// insert3117 3118template <class _CharT, class _Traits, class _Allocator>3119_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3120basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n) {3121 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::insert received nullptr");3122 size_type __sz = size();3123 if (__pos > __sz)3124 this->__throw_out_of_range();3125 size_type __cap = capacity();3126 3127 if (__cap - __sz < __n) {3128 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);3129 return *this;3130 }3131 3132 if (__n == 0)3133 return *this;3134 3135 __annotate_increase(__n);3136 value_type* __p = std::__to_address(__get_pointer());3137 size_type __n_move = __sz - __pos;3138 if (__n_move != 0) {3139 if (std::__is_pointer_in_range(__p + __pos, __p + __sz, __s))3140 __s += __n;3141 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);3142 }3143 traits_type::move(__p + __pos, __s, __n);3144 __sz += __n;3145 __set_size(__sz);3146 traits_type::assign(__p[__sz], value_type());3147 return *this;3148}3149 3150template <class _CharT, class _Traits, class _Allocator>3151_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3152basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c) {3153 size_type __sz = size();3154 if (__pos > __sz)3155 this->__throw_out_of_range();3156 3157 if (__n == 0)3158 return *this;3159 3160 size_type __cap = capacity();3161 value_type* __p;3162 if (__cap - __sz >= __n) {3163 __annotate_increase(__n);3164 __p = std::__to_address(__get_pointer());3165 size_type __n_move = __sz - __pos;3166 if (__n_move != 0)3167 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);3168 } else {3169 __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);3170 __p = std::__to_address(__get_long_pointer());3171 }3172 traits_type::assign(__p + __pos, __n, __c);3173 __sz += __n;3174 __set_size(__sz);3175 traits_type::assign(__p[__sz], value_type());3176 return *this;3177}3178 3179template <class _CharT, class _Traits, class _Allocator>3180template <class _Iterator, class _Sentinel>3181_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator3182basic_string<_CharT, _Traits, _Allocator>::__insert_with_size(3183 const_iterator __pos, _Iterator __first, _Sentinel __last, size_type __n) {3184 size_type __ip = static_cast<size_type>(__pos - begin());3185 if (__n == 0)3186 return begin() + __ip;3187 3188 if (__string_is_trivial_iterator_v<_Iterator> && !__addr_in_range(*__first)) {3189 return __insert_from_safe_copy(__n, __ip, std::move(__first), std::move(__last));3190 } else {3191 const basic_string __temp(__init_with_sentinel_tag(), std::move(__first), std::move(__last), __alloc_);3192 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());3193 }3194}3195 3196template <class _CharT, class _Traits, class _Allocator>3197_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3198basic_string<_CharT, _Traits, _Allocator>::insert(3199 size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n) {3200 size_type __str_sz = __str.size();3201 if (__pos2 > __str_sz)3202 this->__throw_out_of_range();3203 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));3204}3205 3206template <class _CharT, class _Traits, class _Allocator>3207_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3208basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s) {3209 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::insert received nullptr");3210 return insert(__pos, __s, traits_type::length(__s));3211}3212 3213template <class _CharT, class _Traits, class _Allocator>3214_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator3215basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c) {3216 size_type __ip = static_cast<size_type>(__pos - begin());3217 size_type __sz = size();3218 size_type __cap = capacity();3219 value_type* __p;3220 if (__cap == __sz) {3221 __grow_by_without_replace(__cap, 1, __sz, __ip, 0, 1);3222 __p = std::__to_address(__get_long_pointer());3223 } else {3224 __annotate_increase(1);3225 __p = std::__to_address(__get_pointer());3226 size_type __n_move = __sz - __ip;3227 if (__n_move != 0)3228 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);3229 }3230 traits_type::assign(__p[__ip], __c);3231 traits_type::assign(__p[++__sz], value_type());3232 __set_size(__sz);3233 return begin() + static_cast<difference_type>(__ip);3234}3235 3236// replace3237 3238template <class _CharT, class _Traits, class _Allocator>3239_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3240basic_string<_CharT, _Traits, _Allocator>::replace(3241 size_type __pos, size_type __n1, const value_type* __s, size_type __n2)3242 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {3243 _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::replace received nullptr");3244 size_type __sz = size();3245 if (__pos > __sz)3246 this->__throw_out_of_range();3247 __n1 = std::min(__n1, __sz - __pos);3248 size_type __cap = capacity();3249 if (__cap - __sz + __n1 < __n2) {3250 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);3251 return *this;3252 }3253 3254 value_type* __p = std::__to_address(__get_pointer());3255 if (__n1 != __n2) {3256 if (__n2 > __n1)3257 __annotate_increase(__n2 - __n1);3258 size_type __n_move = __sz - __pos - __n1;3259 if (__n_move != 0) {3260 if (__n1 > __n2) {3261 traits_type::move(__p + __pos, __s, __n2);3262 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);3263 return __null_terminate_at(__p, __sz + (__n2 - __n1));3264 }3265 if (std::__is_pointer_in_range(__p + __pos + 1, __p + __sz, __s)) {3266 if (__p + __pos + __n1 <= __s) {3267 __s += __n2 - __n1;3268 } else { // __p + __pos < __s < __p + __pos + __n13269 traits_type::move(__p + __pos, __s, __n1);3270 __pos += __n1;3271 __s += __n2;3272 __n2 -= __n1;3273 __n1 = 0;3274 }3275 }3276 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);3277 }3278 }3279 traits_type::move(__p + __pos, __s, __n2);3280 return __null_terminate_at(__p, __sz + (__n2 - __n1));3281}3282 3283template <class _CharT, class _Traits, class _Allocator>3284_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3285basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c) {3286 size_type __sz = size();3287 if (__pos > __sz)3288 this->__throw_out_of_range();3289 __n1 = std::min(__n1, __sz - __pos);3290 size_type __cap = capacity();3291 value_type* __p;3292 if (__cap - __sz + __n1 >= __n2) {3293 __p = std::__to_address(__get_pointer());3294 if (__n1 != __n2) {3295 if (__n2 > __n1)3296 __annotate_increase(__n2 - __n1);3297 size_type __n_move = __sz - __pos - __n1;3298 if (__n_move != 0)3299 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);3300 }3301 } else {3302 __grow_by_without_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);3303 __p = std::__to_address(__get_long_pointer());3304 }3305 traits_type::assign(__p + __pos, __n2, __c);3306 return __null_terminate_at(__p, __sz - (__n1 - __n2));3307}3308 3309template <class _CharT, class _Traits, class _Allocator>3310_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3311basic_string<_CharT, _Traits, _Allocator>::replace(3312 size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) {3313 size_type __str_sz = __str.size();3314 if (__pos2 > __str_sz)3315 this->__throw_out_of_range();3316 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));3317}3318 3319template <class _CharT, class _Traits, class _Allocator>3320_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3321basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s) {3322 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::replace received nullptr");3323 return replace(__pos, __n1, __s, traits_type::length(__s));3324}3325 3326// erase3327 3328// 'externally instantiated' erase() implementation, called when __n != npos.3329// Does not check __pos against size()3330template <class _CharT, class _Traits, class _Allocator>3331_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void3332basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(size_type __pos, size_type __n) {3333 if (__n == 0)3334 return;3335 3336 size_type __sz = size();3337 value_type* __p = std::__to_address(__get_pointer());3338 __n = std::min(__n, __sz - __pos);3339 size_type __n_move = __sz - __pos - __n;3340 if (__n_move != 0)3341 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);3342 __null_terminate_at(__p, __sz - __n);3343}3344 3345template <class _CharT, class _Traits, class _Allocator>3346_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&3347basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n) {3348 if (__pos > size())3349 this->__throw_out_of_range();3350 if (__n == npos) {3351 __erase_to_end(__pos);3352 } else {3353 __erase_external_with_move(__pos, __n);3354 }3355 return *this;3356}3357 3358template <class _CharT, class _Traits, class _Allocator>3359inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator3360basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos) {3361 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(3362 __pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");3363 iterator __b = begin();3364 size_type __r = static_cast<size_type>(__pos - __b);3365 erase(__r, 1);3366 return __b + static_cast<difference_type>(__r);3367}3368 3369template <class _CharT, class _Traits, class _Allocator>3370inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator3371basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last) {3372 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "string::erase(first, last) called with invalid range");3373 iterator __b = begin();3374 size_type __r = static_cast<size_type>(__first - __b);3375 erase(__r, static_cast<size_type>(__last - __first));3376 return __b + static_cast<difference_type>(__r);3377}3378 3379template <class _CharT, class _Traits, class _Allocator>3380inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pop_back() {3381 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::pop_back(): string is already empty");3382 __erase_to_end(size() - 1);3383}3384 3385template <class _CharT, class _Traits, class _Allocator>3386inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {3387 size_type __old_size;3388 if (__is_long()) {3389 __old_size = __get_long_size();3390 traits_type::assign(*__get_long_pointer(), value_type());3391 __set_long_size(0);3392 } else {3393 __old_size = __get_short_size();3394 traits_type::assign(*__get_short_pointer(), value_type());3395 __set_short_size(0);3396 }3397 __annotate_shrink(__old_size);3398}3399 3400template <class _CharT, class _Traits, class _Allocator>3401_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c) {3402 size_type __sz = size();3403 if (__n > __sz)3404 append(__n - __sz, __c);3405 else3406 __erase_to_end(__n);3407}3408 3409template <class _CharT, class _Traits, class _Allocator>3410_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity) {3411 if (__requested_capacity > max_size())3412 this->__throw_length_error();3413 3414 // Make sure reserve(n) never shrinks. This is technically only required in C++203415 // and later (since P0966R1), however we provide consistent behavior in all Standard3416 // modes because this function is instantiated in the shared library.3417 if (__requested_capacity <= capacity())3418 return;3419 3420 __annotation_guard __g(*this);3421 __long __buffer = __allocate_long_buffer(__alloc_, __requested_capacity);3422 __buffer.__size_ = size();3423 traits_type::copy(std::__to_address(__buffer.__data_), data(), __buffer.__size_ + 1);3424 __reset_internal_buffer(__buffer);3425}3426 3427template <class _CharT, class _Traits, class _Allocator>3428inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT {3429 if (!__is_long())3430 return;3431 3432 const auto __ptr = __get_long_pointer();3433 const auto __size = __get_long_size();3434 const auto __cap = __get_long_cap();3435 3436 // We're a long string and we're shrinking into the small buffer.3437 if (__fits_in_sso(__size)) {3438 __annotation_guard __g(*this);3439 __set_short_size(__size);3440 traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);3441 __alloc_traits::deallocate(__alloc_, __ptr, __cap);3442 return;3443 }3444 3445 if (__align_allocation_size(__size) == __cap)3446 return;3447 3448# if _LIBCPP_HAS_EXCEPTIONS3449 try {3450# endif // _LIBCPP_HAS_EXCEPTIONS3451 __annotation_guard __g(*this);3452 __long __buffer = __allocate_long_buffer(__alloc_, __size);3453 3454 // The Standard mandates shrink_to_fit() does not increase the capacity.3455 // With equal capacity keep the existing buffer. This avoids extra work3456 // due to swapping the elements.3457 if (__buffer.__cap_ * __endian_factor - 1 >= capacity()) {3458 __alloc_traits::deallocate(__alloc_, __buffer.__data_, __buffer.__cap_ * __endian_factor);3459 return;3460 }3461 3462 traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__get_long_pointer()), __size + 1);3463 __reset_internal_buffer(__buffer);3464# if _LIBCPP_HAS_EXCEPTIONS3465 } catch (...) {3466 return;3467 }3468# endif // _LIBCPP_HAS_EXCEPTIONS3469}3470 3471template <class _CharT, class _Traits, class _Allocator>3472_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::const_reference3473basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const {3474 if (__n >= size())3475 this->__throw_out_of_range();3476 return (*this)[__n];3477}3478 3479template <class _CharT, class _Traits, class _Allocator>3480_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::reference3481basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) {3482 if (__n >= size())3483 this->__throw_out_of_range();3484 return (*this)[__n];3485}3486 3487template <class _CharT, class _Traits, class _Allocator>3488_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type3489basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const {3490 size_type __sz = size();3491 if (__pos > __sz)3492 this->__throw_out_of_range();3493 size_type __rlen = std::min(__n, __sz - __pos);3494 traits_type::copy(__s, data() + __pos, __rlen);3495 return __rlen;3496}3497 3498template <class _CharT, class _Traits, class _Allocator>3499inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)3500# if _LIBCPP_STD_VER >= 143501 _NOEXCEPT3502# else3503 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)3504# endif3505{3506 _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(3507 __alloc_traits::propagate_on_container_swap::value || __alloc_traits::is_always_equal::value ||3508 __alloc_ == __str.__alloc_,3509 "swapping non-equal allocators");3510 if (!__is_long())3511 __annotate_delete();3512 if (this != std::addressof(__str) && !__str.__is_long())3513 __str.__annotate_delete();3514 std::swap(__rep_, __str.__rep_);3515 std::__swap_allocator(__alloc_, __str.__alloc_);3516 if (!__is_long())3517 __annotate_new(__get_short_size());3518 if (this != std::addressof(__str) && !__str.__is_long())3519 __str.__annotate_new(__str.__get_short_size());3520}3521 3522// compare3523 3524template <class _CharT, class _Traits, class _Allocator>3525inline _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(3526 size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const {3527 _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");3528 size_type __sz = size();3529 if (__pos1 > __sz || __n2 == npos)3530 this->__throw_out_of_range();3531 size_type __rlen = std::min(__n1, __sz - __pos1);3532 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));3533 if (__r == 0) {3534 if (__rlen < __n2)3535 __r = -1;3536 else if (__rlen > __n2)3537 __r = 1;3538 }3539 return __r;3540}3541 3542// __invariants3543 3544template <class _CharT, class _Traits, class _Allocator>3545inline _LIBCPP_CONSTEXPR_SINCE_CXX20 bool basic_string<_CharT, _Traits, _Allocator>::__invariants() const {3546 if (size() > capacity())3547 return false;3548 if (capacity() < __min_cap - 1)3549 return false;3550 if (data() == nullptr)3551 return false;3552 if (!_Traits::eq(data()[size()], value_type()))3553 return false;3554 return true;3555}3556 3557// operator==3558 3559template <class _CharT, class _Traits, class _Allocator>3560inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool3561operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3562 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3563 size_t __lhs_sz = __lhs.size();3564 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs_sz) == 0;3565}3566 3567template <class _CharT, class _Traits, class _Allocator>3568inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool3569operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3570 const _CharT* _LIBCPP_DIAGNOSE_NULLPTR __rhs) _NOEXCEPT {3571 _LIBCPP_ASSERT_NON_NULL(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");3572 3573 using _String = basic_string<_CharT, _Traits, _Allocator>;3574 3575 size_t __rhs_len = _Traits::length(__rhs);3576 if (__builtin_constant_p(__rhs_len) && !_String::__fits_in_sso(__rhs_len)) {3577 if (!__lhs.__is_long())3578 return false;3579 }3580 if (__rhs_len != __lhs.size())3581 return false;3582 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;3583}3584 3585# if _LIBCPP_STD_VER <= 173586template <class _CharT, class _Traits, class _Allocator>3587inline _LIBCPP_HIDE_FROM_ABI bool3588operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3589 return __rhs == __lhs;3590}3591# endif // _LIBCPP_STD_VER <= 173592 3593# if _LIBCPP_STD_VER >= 203594 3595template <class _CharT, class _Traits, class _Allocator>3596_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3597 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {3598 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);3599}3600 3601template <class _CharT, class _Traits, class _Allocator>3602_LIBCPP_HIDE_FROM_ABI constexpr auto3603operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {3604 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);3605}3606 3607# else // _LIBCPP_STD_VER >= 203608 3609template <class _CharT, class _Traits, class _Allocator>3610inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3611 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3612 return !(__lhs == __rhs);3613}3614 3615template <class _CharT, class _Traits, class _Allocator>3616inline _LIBCPP_HIDE_FROM_ABI bool3617operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3618 return !(__lhs == __rhs);3619}3620 3621template <class _CharT, class _Traits, class _Allocator>3622inline _LIBCPP_HIDE_FROM_ABI bool3623operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {3624 return !(__lhs == __rhs);3625}3626 3627// operator<3628 3629template <class _CharT, class _Traits, class _Allocator>3630inline _LIBCPP_HIDE_FROM_ABI bool operator<(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3631 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3632 return __lhs.compare(__rhs) < 0;3633}3634 3635template <class _CharT, class _Traits, class _Allocator>3636inline _LIBCPP_HIDE_FROM_ABI bool3637operator<(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {3638 return __lhs.compare(__rhs) < 0;3639}3640 3641template <class _CharT, class _Traits, class _Allocator>3642inline _LIBCPP_HIDE_FROM_ABI bool3643operator<(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3644 return __rhs.compare(__lhs) > 0;3645}3646 3647// operator>3648 3649template <class _CharT, class _Traits, class _Allocator>3650inline _LIBCPP_HIDE_FROM_ABI bool operator>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3651 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3652 return __rhs < __lhs;3653}3654 3655template <class _CharT, class _Traits, class _Allocator>3656inline _LIBCPP_HIDE_FROM_ABI bool3657operator>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {3658 return __rhs < __lhs;3659}3660 3661template <class _CharT, class _Traits, class _Allocator>3662inline _LIBCPP_HIDE_FROM_ABI bool3663operator>(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3664 return __rhs < __lhs;3665}3666 3667// operator<=3668 3669template <class _CharT, class _Traits, class _Allocator>3670inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3671 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3672 return !(__rhs < __lhs);3673}3674 3675template <class _CharT, class _Traits, class _Allocator>3676inline _LIBCPP_HIDE_FROM_ABI bool3677operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {3678 return !(__rhs < __lhs);3679}3680 3681template <class _CharT, class _Traits, class _Allocator>3682inline _LIBCPP_HIDE_FROM_ABI bool3683operator<=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3684 return !(__rhs < __lhs);3685}3686 3687// operator>=3688 3689template <class _CharT, class _Traits, class _Allocator>3690inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3691 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3692 return !(__lhs < __rhs);3693}3694 3695template <class _CharT, class _Traits, class _Allocator>3696inline _LIBCPP_HIDE_FROM_ABI bool3697operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {3698 return !(__lhs < __rhs);3699}3700 3701template <class _CharT, class _Traits, class _Allocator>3702inline _LIBCPP_HIDE_FROM_ABI bool3703operator>=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {3704 return !(__lhs < __rhs);3705}3706# endif // _LIBCPP_STD_VER >= 203707 3708// operator +3709 3710template <class _CharT, class _Traits, class _Allocator>3711_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3712__concatenate_strings(const _Allocator& __alloc,3713 __type_identity_t<basic_string_view<_CharT, _Traits> > __str1,3714 __type_identity_t<basic_string_view<_CharT, _Traits> > __str2) {3715 using _String = basic_string<_CharT, _Traits, _Allocator>;3716 _String __r(__uninitialized_size_tag(),3717 __str1.size() + __str2.size(),3718 _String::__alloc_traits::select_on_container_copy_construction(__alloc));3719 auto __ptr = std::__to_address(__r.__get_pointer());3720 _Traits::copy(__ptr, __str1.data(), __str1.size());3721 _Traits::copy(__ptr + __str1.size(), __str2.data(), __str2.size());3722 _Traits::assign(__ptr[__str1.size() + __str2.size()], _CharT());3723 return __r;3724}3725 3726template <class _CharT, class _Traits, class _Allocator>3727_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3728operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3729 const basic_string<_CharT, _Traits, _Allocator>& __rhs) {3730 return std::__concatenate_strings<_CharT, _Traits>(__lhs.get_allocator(), __lhs, __rhs);3731}3732 3733template <class _CharT, class _Traits, class _Allocator>3734_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3735operator+(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {3736 return std::__concatenate_strings<_CharT, _Traits>(__rhs.get_allocator(), __lhs, __rhs);3737}3738 3739extern template _LIBCPP_EXPORTED_FROM_ABI string operator+3740 <char, char_traits<char>, allocator<char> >(char const*, string const&);3741 3742template <class _CharT, class _Traits, class _Allocator>3743_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3744operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {3745 return std::__concatenate_strings<_CharT, _Traits>(3746 __rhs.get_allocator(), basic_string_view<_CharT, _Traits>(std::addressof(__lhs), 1), __rhs);3747}3748 3749template <class _CharT, class _Traits, class _Allocator>3750_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3751operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {3752 return std::__concatenate_strings<_CharT, _Traits>(__lhs.get_allocator(), __lhs, __rhs);3753}3754 3755template <class _CharT, class _Traits, class _Allocator>3756_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3757operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs) {3758 return std::__concatenate_strings<_CharT, _Traits>(3759 __lhs.get_allocator(), __lhs, basic_string_view<_CharT, _Traits>(std::addressof(__rhs), 1));3760}3761# if _LIBCPP_STD_VER >= 263762 3763template <class _CharT, class _Traits, class _Allocator>3764_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>3765operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,3766 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) {3767 return std::__concatenate_strings<_CharT, _Traits>(__lhs.get_allocator(), __lhs, __rhs);3768}3769 3770template <class _CharT, class _Traits, class _Allocator>3771_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>3772operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,3773 const basic_string<_CharT, _Traits, _Allocator>& __rhs) {3774 return std::__concatenate_strings<_CharT, _Traits>(__rhs.get_allocator(), __lhs, __rhs);3775}3776 3777# endif // _LIBCPP_STD_VER >= 263778 3779# ifndef _LIBCPP_CXX03_LANG3780 3781template <class _CharT, class _Traits, class _Allocator>3782inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3783operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {3784 return std::move(__lhs.append(__rhs));3785}3786 3787template <class _CharT, class _Traits, class _Allocator>3788inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3789operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {3790 return std::move(__rhs.insert(0, __lhs));3791}3792 3793template <class _CharT, class _Traits, class _Allocator>3794inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3795operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {3796 return std::move(__lhs.append(__rhs));3797}3798 3799template <class _CharT, class _Traits, class _Allocator>3800inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3801operator+(const _CharT* __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {3802 return std::move(__rhs.insert(0, __lhs));3803}3804 3805template <class _CharT, class _Traits, class _Allocator>3806inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3807operator+(_CharT __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {3808 __rhs.insert(__rhs.begin(), __lhs);3809 return std::move(__rhs);3810}3811 3812template <class _CharT, class _Traits, class _Allocator>3813inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3814operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs) {3815 return std::move(__lhs.append(__rhs));3816}3817 3818template <class _CharT, class _Traits, class _Allocator>3819inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>3820operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs) {3821 __lhs.push_back(__rhs);3822 return std::move(__lhs);3823}3824 3825# endif // _LIBCPP_CXX03_LANG3826 3827# if _LIBCPP_STD_VER >= 263828 3829template <class _CharT, class _Traits, class _Allocator>3830_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>3831operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs,3832 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) {3833 return std::move(__lhs.append(__rhs));3834}3835 3836template <class _CharT, class _Traits, class _Allocator>3837_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>3838operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,3839 basic_string<_CharT, _Traits, _Allocator>&& __rhs) {3840 return std::move(__rhs.insert(0, __lhs));3841}3842 3843# endif // _LIBCPP_STD_VER >= 263844 3845// swap3846 3847template <class _CharT, class _Traits, class _Allocator>3848inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void3849swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>& __rhs)3850 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs))) {3851 __lhs.swap(__rhs);3852}3853 3854[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI int stoi(const string& __str, size_t* __idx = nullptr, int __base = 10);3855[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long3856stoul(const string& __str, size_t* __idx = nullptr, int __base = 10);3857[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long stol(const string& __str, size_t* __idx = nullptr, int __base = 10);3858[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long long3859stoll(const string& __str, size_t* __idx = nullptr, int __base = 10);3860[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long long3861stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);3862 3863[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI float stof(const string& __str, size_t* __idx = nullptr);3864[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI double stod(const string& __str, size_t* __idx = nullptr);3865[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long double stold(const string& __str, size_t* __idx = nullptr);3866 3867[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(int __val);3868[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned __val);3869[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long __val);3870[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long __val);3871[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long long __val);3872[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long long __val);3873[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(float __val);3874[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(double __val);3875[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long double __val);3876 3877# if _LIBCPP_HAS_WIDE_CHARACTERS3878[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI int stoi(const wstring& __str, size_t* __idx = nullptr, int __base = 10);3879[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long stol(const wstring& __str, size_t* __idx = nullptr, int __base = 10);3880[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long3881stoul(const wstring& __str, size_t* __idx = nullptr, int __base = 10);3882[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long long3883stoll(const wstring& __str, size_t* __idx = nullptr, int __base = 10);3884[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long long3885stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);3886 3887[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI float stof(const wstring& __str, size_t* __idx = nullptr);3888[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI double stod(const wstring& __str, size_t* __idx = nullptr);3889[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long double stold(const wstring& __str, size_t* __idx = nullptr);3890 3891[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(int __val);3892[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned __val);3893[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long __val);3894[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long __val);3895[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long long __val);3896[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long long __val);3897[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(float __val);3898[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(double __val);3899[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long double __val);3900# endif // _LIBCPP_HAS_WIDE_CHARACTERS3901 3902template <class _CharT, class _Traits, class _Allocator>3903_LIBCPP_TEMPLATE_DATA_VIS const typename basic_string<_CharT, _Traits, _Allocator>::size_type3904 basic_string<_CharT, _Traits, _Allocator>::npos;3905 3906template <class _CharT, class _Allocator>3907struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t> {3908 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t3909 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT {3910 return std::__do_string_hash(__val.data(), __val.data() + __val.size());3911 }3912};3913 3914template <class _Allocator>3915struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};3916 3917# if _LIBCPP_HAS_CHAR8_T3918template <class _Allocator>3919struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};3920# endif3921 3922template <class _Allocator>3923struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};3924 3925template <class _Allocator>3926struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};3927 3928# if _LIBCPP_HAS_WIDE_CHARACTERS3929template <class _Allocator>3930struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};3931# endif3932 3933template <class _CharT, class _Traits, class _Allocator>3934_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&3935operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str);3936 3937template <class _CharT, class _Traits, class _Allocator>3938_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&3939operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);3940 3941template <class _CharT, class _Traits, class _Allocator>3942_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&3943getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);3944 3945template <class _CharT, class _Traits, class _Allocator>3946inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&3947getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);3948 3949template <class _CharT, class _Traits, class _Allocator>3950inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&3951getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);3952 3953template <class _CharT, class _Traits, class _Allocator>3954inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&3955getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str);3956 3957# if _LIBCPP_STD_VER >= 203958template <class _CharT, class _Traits, class _Allocator, class _Up>3959inline _LIBCPP_HIDE_FROM_ABI constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type3960erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {3961 auto __old_size = __str.size();3962 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());3963 return __old_size - __str.size();3964}3965 3966template <class _CharT, class _Traits, class _Allocator, class _Predicate>3967inline _LIBCPP_HIDE_FROM_ABI constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type3968erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred) {3969 auto __old_size = __str.size();3970 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred), __str.end());3971 return __old_size - __str.size();3972}3973# endif3974 3975# if _LIBCPP_STD_VER >= 143976// Literal suffixes for basic_string [basic.string.literals]3977inline namespace literals {3978inline namespace string_literals {3979[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char>3980operator""s(const char* __str, size_t __len) {3981 return basic_string<char>(__str, __len);3982}3983 3984# if _LIBCPP_HAS_WIDE_CHARACTERS3985[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI3986_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<wchar_t> operator""s(const wchar_t* __str, size_t __len) {3987 return basic_string<wchar_t>(__str, __len);3988}3989# endif3990 3991# if _LIBCPP_HAS_CHAR8_T3992[[__nodiscard__]] inline3993 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<char8_t> operator""s(const char8_t* __str, size_t __len) {3994 return basic_string<char8_t>(__str, __len);3995}3996# endif3997 3998[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI3999_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char16_t> operator""s(const char16_t* __str, size_t __len) {4000 return basic_string<char16_t>(__str, __len);4001}4002 4003[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char32_t>4004operator""s(const char32_t* __str, size_t __len) {4005 return basic_string<char32_t>(__str, __len);4006}4007} // namespace string_literals4008} // namespace literals4009 4010# if _LIBCPP_STD_VER >= 204011template <>4012inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;4013# if _LIBCPP_HAS_WIDE_CHARACTERS4014template <>4015inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;4016# endif4017# endif4018 4019# endif4020 4021_LIBCPP_END_NAMESPACE_STD4022 4023_LIBCPP_POP_MACROS4024 4025# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 204026# include <algorithm>4027# include <concepts>4028# include <cstdlib>4029# include <iterator>4030# include <new>4031# include <optional>4032# include <type_traits>4033# include <typeinfo>4034# include <utility>4035# endif4036#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)4037 4038#endif // _LIBCPP_STRING4039