string 192 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220
  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-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_STRING
  10. #define _LIBCPP_STRING
  11. // clang-format off
  12. /*
  13. string synopsis
  14. #include <compare>
  15. #include <initializer_list>
  16. namespace std
  17. {
  18. template <class stateT>
  19. class fpos
  20. {
  21. private:
  22. stateT st;
  23. public:
  24. fpos(streamoff = streamoff());
  25. operator streamoff() const;
  26. stateT state() const;
  27. void state(stateT);
  28. fpos& operator+=(streamoff);
  29. fpos operator+ (streamoff) const;
  30. fpos& operator-=(streamoff);
  31. fpos operator- (streamoff) const;
  32. };
  33. template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
  34. template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
  35. template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
  36. template <class charT>
  37. struct char_traits
  38. {
  39. using char_type = charT;
  40. using int_type = ...;
  41. using off_type = streamoff;
  42. using pos_type = streampos;
  43. using state_type = mbstate_t;
  44. using comparison_category = strong_ordering; // Since C++20 only for the specializations
  45. // char, wchar_t, char8_t, char16_t, and char32_t.
  46. static void assign(char_type& c1, const char_type& c2) noexcept;
  47. static constexpr bool eq(char_type c1, char_type c2) noexcept;
  48. static constexpr bool lt(char_type c1, char_type c2) noexcept;
  49. static int compare(const char_type* s1, const char_type* s2, size_t n);
  50. static size_t length(const char_type* s);
  51. static const char_type* find(const char_type* s, size_t n, const char_type& a);
  52. static char_type* move(char_type* s1, const char_type* s2, size_t n);
  53. static char_type* copy(char_type* s1, const char_type* s2, size_t n);
  54. static char_type* assign(char_type* s, size_t n, char_type a);
  55. static constexpr int_type not_eof(int_type c) noexcept;
  56. static constexpr char_type to_char_type(int_type c) noexcept;
  57. static constexpr int_type to_int_type(char_type c) noexcept;
  58. static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
  59. static constexpr int_type eof() noexcept;
  60. };
  61. template <> struct char_traits<char>;
  62. template <> struct char_traits<wchar_t>;
  63. template <> struct char_traits<char8_t>; // C++20
  64. template <> struct char_traits<char16_t>;
  65. template <> struct char_traits<char32_t>;
  66. template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
  67. class basic_string
  68. {
  69. static_assert(sizeof(_CharT) <= 4, "libc++ implementation of std::basic_string does not support extra-wide character types");
  70. public:
  71. // types:
  72. typedef traits traits_type;
  73. typedef typename traits_type::char_type value_type;
  74. typedef Allocator allocator_type;
  75. typedef typename allocator_type::size_type size_type;
  76. typedef typename allocator_type::difference_type difference_type;
  77. typedef typename allocator_type::reference reference;
  78. typedef typename allocator_type::const_reference const_reference;
  79. typedef typename allocator_type::pointer pointer;
  80. typedef typename allocator_type::const_pointer const_pointer;
  81. typedef implementation-defined iterator;
  82. typedef implementation-defined const_iterator;
  83. typedef std::reverse_iterator<iterator> reverse_iterator;
  84. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  85. static const size_type npos = -1;
  86. basic_string()
  87. noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20
  88. explicit basic_string(const allocator_type& a); // constexpr since C++20
  89. basic_string(const basic_string& str); // constexpr since C++20
  90. basic_string(basic_string&& str)
  91. noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20
  92. basic_string(const basic_string& str, size_type pos,
  93. const allocator_type& a = allocator_type()); // constexpr since C++20
  94. basic_string(const basic_string& str, size_type pos, size_type n,
  95. const Allocator& a = Allocator()); // constexpr since C++20
  96. constexpr basic_string(
  97. basic_string&& str, size_type pos, const Allocator& a = Allocator()); // since C++23
  98. constexpr basic_string(
  99. basic_string&& str, size_type pos, size_type n, const Allocator& a = Allocator()); // since C++23
  100. template<class T>
  101. basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17, constexpr since C++20
  102. template <class T>
  103. explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17, constexpr since C++20
  104. basic_string(const value_type* s, const allocator_type& a = allocator_type()); // constexpr since C++20
  105. basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type()); // constexpr since C++20
  106. basic_string(nullptr_t) = delete; // C++23
  107. basic_string(size_type n, value_type c, const allocator_type& a = allocator_type()); // constexpr since C++20
  108. template<class InputIterator>
  109. basic_string(InputIterator begin, InputIterator end,
  110. const allocator_type& a = allocator_type()); // constexpr since C++20
  111. template<container-compatible-range<charT> R>
  112. constexpr basic_string(from_range_t, R&& rg, const Allocator& a = Allocator()); // since C++23
  113. basic_string(initializer_list<value_type>, const Allocator& = Allocator()); // constexpr since C++20
  114. basic_string(const basic_string&, const Allocator&); // constexpr since C++20
  115. basic_string(basic_string&&, const Allocator&); // constexpr since C++20
  116. ~basic_string(); // constexpr since C++20
  117. operator basic_string_view<charT, traits>() const noexcept; // constexpr since C++20
  118. basic_string& operator=(const basic_string& str); // constexpr since C++20
  119. template <class T>
  120. basic_string& operator=(const T& t); // C++17, constexpr since C++20
  121. basic_string& operator=(basic_string&& str)
  122. noexcept(
  123. allocator_type::propagate_on_container_move_assignment::value ||
  124. allocator_type::is_always_equal::value ); // C++17, constexpr since C++20
  125. basic_string& operator=(const value_type* s); // constexpr since C++20
  126. basic_string& operator=(nullptr_t) = delete; // C++23
  127. basic_string& operator=(value_type c); // constexpr since C++20
  128. basic_string& operator=(initializer_list<value_type>); // constexpr since C++20
  129. iterator begin() noexcept; // constexpr since C++20
  130. const_iterator begin() const noexcept; // constexpr since C++20
  131. iterator end() noexcept; // constexpr since C++20
  132. const_iterator end() const noexcept; // constexpr since C++20
  133. reverse_iterator rbegin() noexcept; // constexpr since C++20
  134. const_reverse_iterator rbegin() const noexcept; // constexpr since C++20
  135. reverse_iterator rend() noexcept; // constexpr since C++20
  136. const_reverse_iterator rend() const noexcept; // constexpr since C++20
  137. const_iterator cbegin() const noexcept; // constexpr since C++20
  138. const_iterator cend() const noexcept; // constexpr since C++20
  139. const_reverse_iterator crbegin() const noexcept; // constexpr since C++20
  140. const_reverse_iterator crend() const noexcept; // constexpr since C++20
  141. size_type size() const noexcept; // constexpr since C++20
  142. size_type length() const noexcept; // constexpr since C++20
  143. size_type max_size() const noexcept; // constexpr since C++20
  144. size_type capacity() const noexcept; // constexpr since C++20
  145. void resize(size_type n, value_type c); // constexpr since C++20
  146. void resize(size_type n); // constexpr since C++20
  147. template<class Operation>
  148. constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
  149. void reserve(size_type res_arg); // constexpr since C++20
  150. void reserve(); // deprecated in C++20, removed in C++26
  151. void shrink_to_fit(); // constexpr since C++20
  152. void clear() noexcept; // constexpr since C++20
  153. bool empty() const noexcept; // constexpr since C++20
  154. const_reference operator[](size_type pos) const; // constexpr since C++20
  155. reference operator[](size_type pos); // constexpr since C++20
  156. const_reference at(size_type n) const; // constexpr since C++20
  157. reference at(size_type n); // constexpr since C++20
  158. basic_string& operator+=(const basic_string& str); // constexpr since C++20
  159. template <class T>
  160. basic_string& operator+=(const T& t); // C++17, constexpr since C++20
  161. basic_string& operator+=(const value_type* s); // constexpr since C++20
  162. basic_string& operator+=(value_type c); // constexpr since C++20
  163. basic_string& operator+=(initializer_list<value_type>); // constexpr since C++20
  164. basic_string& append(const basic_string& str); // constexpr since C++20
  165. template <class T>
  166. basic_string& append(const T& t); // C++17, constexpr since C++20
  167. basic_string& append(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
  168. template <class T>
  169. basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
  170. basic_string& append(const value_type* s, size_type n); // constexpr since C++20
  171. basic_string& append(const value_type* s); // constexpr since C++20
  172. basic_string& append(size_type n, value_type c); // constexpr since C++20
  173. template<class InputIterator>
  174. basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
  175. template<container-compatible-range<charT> R>
  176. constexpr basic_string& append_range(R&& rg); // C++23
  177. basic_string& append(initializer_list<value_type>); // constexpr since C++20
  178. void push_back(value_type c); // constexpr since C++20
  179. void pop_back(); // constexpr since C++20
  180. reference front(); // constexpr since C++20
  181. const_reference front() const; // constexpr since C++20
  182. reference back(); // constexpr since C++20
  183. const_reference back() const; // constexpr since C++20
  184. basic_string& assign(const basic_string& str); // constexpr since C++20
  185. template <class T>
  186. basic_string& assign(const T& t); // C++17, constexpr since C++20
  187. basic_string& assign(basic_string&& str); // constexpr since C++20
  188. basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
  189. template <class T>
  190. basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
  191. basic_string& assign(const value_type* s, size_type n); // constexpr since C++20
  192. basic_string& assign(const value_type* s); // constexpr since C++20
  193. basic_string& assign(size_type n, value_type c); // constexpr since C++20
  194. template<class InputIterator>
  195. basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
  196. template<container-compatible-range<charT> R>
  197. constexpr basic_string& assign_range(R&& rg); // C++23
  198. basic_string& assign(initializer_list<value_type>); // constexpr since C++20
  199. basic_string& insert(size_type pos1, const basic_string& str); // constexpr since C++20
  200. template <class T>
  201. basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
  202. basic_string& insert(size_type pos1, const basic_string& str,
  203. size_type pos2, size_type n); // constexpr since C++20
  204. template <class T>
  205. basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
  206. basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
  207. basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
  208. basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
  209. iterator insert(const_iterator p, value_type c); // constexpr since C++20
  210. iterator insert(const_iterator p, size_type n, value_type c); // constexpr since C++20
  211. template<class InputIterator>
  212. iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20
  213. template<container-compatible-range<charT> R>
  214. constexpr iterator insert_range(const_iterator p, R&& rg); // C++23
  215. iterator insert(const_iterator p, initializer_list<value_type>); // constexpr since C++20
  216. basic_string& erase(size_type pos = 0, size_type n = npos); // constexpr since C++20
  217. iterator erase(const_iterator position); // constexpr since C++20
  218. iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
  219. basic_string& replace(size_type pos1, size_type n1, const basic_string& str); // constexpr since C++20
  220. template <class T>
  221. basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17, constexpr since C++20
  222. basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
  223. size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
  224. template <class T>
  225. basic_string& replace(size_type pos1, size_type n1, const T& t,
  226. size_type pos2, size_type n); // C++17, constexpr since C++20
  227. basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
  228. basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
  229. basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
  230. basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20
  231. template <class T>
  232. basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17, constexpr since C++20
  233. basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
  234. basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s); // constexpr since C++20
  235. basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c); // constexpr since C++20
  236. template<class InputIterator>
  237. basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
  238. template<container-compatible-range<charT> R>
  239. constexpr basic_string& replace_with_range(const_iterator i1, const_iterator i2, R&& rg); // C++23
  240. basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>); // constexpr since C++20
  241. size_type copy(value_type* s, size_type n, size_type pos = 0) const; // constexpr since C++20
  242. basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr in C++20, removed in C++23
  243. basic_string substr(size_type pos = 0, size_type n = npos) const&; // since C++23
  244. constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&; // since C++23
  245. void swap(basic_string& str)
  246. noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
  247. allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
  248. const value_type* c_str() const noexcept; // constexpr since C++20
  249. const value_type* data() const noexcept; // constexpr since C++20
  250. value_type* data() noexcept; // C++17, constexpr since C++20
  251. allocator_type get_allocator() const noexcept; // constexpr since C++20
  252. size_type find(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
  253. template <class T>
  254. size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
  255. size_type find(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
  256. size_type find(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
  257. size_type find(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
  258. size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
  259. template <class T>
  260. size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
  261. size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
  262. size_type rfind(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
  263. size_type rfind(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
  264. size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
  265. template <class T>
  266. size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
  267. size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
  268. size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
  269. size_type find_first_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
  270. size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
  271. template <class T>
  272. size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension, constexpr since C++20
  273. size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
  274. size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
  275. size_type find_last_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
  276. size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
  277. template <class T>
  278. size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
  279. size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
  280. size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
  281. size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
  282. size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
  283. template <class T>
  284. size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
  285. size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
  286. size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
  287. size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
  288. int compare(const basic_string& str) const noexcept; // constexpr since C++20
  289. template <class T>
  290. int compare(const T& t) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
  291. int compare(size_type pos1, size_type n1, const basic_string& str) const; // constexpr since C++20
  292. template <class T>
  293. int compare(size_type pos1, size_type n1, const T& t) const; // C++17, constexpr since C++20
  294. int compare(size_type pos1, size_type n1, const basic_string& str,
  295. size_type pos2, size_type n2=npos) const; // C++14, constexpr since C++20
  296. template <class T>
  297. int compare(size_type pos1, size_type n1, const T& t,
  298. size_type pos2, size_type n2=npos) const; // C++17, constexpr since C++20
  299. int compare(const value_type* s) const noexcept; // constexpr since C++20
  300. int compare(size_type pos1, size_type n1, const value_type* s) const; // constexpr since C++20
  301. int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const; // constexpr since C++20
  302. constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
  303. constexpr bool starts_with(charT c) const noexcept; // C++20
  304. constexpr bool starts_with(const charT* s) const; // C++20
  305. constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
  306. constexpr bool ends_with(charT c) const noexcept; // C++20
  307. constexpr bool ends_with(const charT* s) const; // C++20
  308. constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++23
  309. constexpr bool contains(charT c) const noexcept; // C++23
  310. constexpr bool contains(const charT* s) const; // C++23
  311. };
  312. template<class InputIterator,
  313. class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
  314. basic_string(InputIterator, InputIterator, Allocator = Allocator())
  315. -> basic_string<typename iterator_traits<InputIterator>::value_type,
  316. char_traits<typename iterator_traits<InputIterator>::value_type>,
  317. Allocator>; // C++17
  318. template<ranges::input_range R,
  319. class Allocator = allocator<ranges::range_value_t<R>>>
  320. basic_string(from_range_t, R&&, Allocator = Allocator())
  321. -> basic_string<ranges::range_value_t<R>, char_traits<ranges::range_value_t<R>>,
  322. Allocator>; // C++23
  323. template<class charT,
  324. class traits,
  325. class Allocator = allocator<charT>>
  326. explicit basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
  327. -> basic_string<charT, traits, Allocator>; // C++17
  328. template<class charT,
  329. class traits,
  330. class Allocator = allocator<charT>>
  331. basic_string(basic_string_view<charT, traits>,
  332. typename see below::size_type, typename see below::size_type,
  333. const Allocator& = Allocator())
  334. -> basic_string<charT, traits, Allocator>; // C++17
  335. template<class charT, class traits, class Allocator>
  336. basic_string<charT, traits, Allocator>
  337. operator+(const basic_string<charT, traits, Allocator>& lhs,
  338. const basic_string<charT, traits, Allocator>& rhs); // constexpr since C++20
  339. template<class charT, class traits, class Allocator>
  340. basic_string<charT, traits, Allocator>
  341. operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); // constexpr since C++20
  342. template<class charT, class traits, class Allocator>
  343. basic_string<charT, traits, Allocator>
  344. operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20
  345. template<class charT, class traits, class Allocator>
  346. basic_string<charT, traits, Allocator>
  347. operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); // constexpr since C++20
  348. template<class charT, class traits, class Allocator>
  349. basic_string<charT, traits, Allocator>
  350. operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); // constexpr since C++20
  351. template<class charT, class traits, class Allocator>
  352. bool operator==(const basic_string<charT, traits, Allocator>& lhs,
  353. const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
  354. template<class charT, class traits, class Allocator>
  355. bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  356. template<class charT, class traits, class Allocator>
  357. bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
  358. template<class charT, class traits, class Allocator>
  359. bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
  360. const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  361. template<class charT, class traits, class Allocator>
  362. bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  363. template<class charT, class traits, class Allocator>
  364. bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
  365. template<class charT, class traits, class Allocator>
  366. bool operator< (const basic_string<charT, traits, Allocator>& lhs,
  367. const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  368. template<class charT, class traits, class Allocator>
  369. bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
  370. template<class charT, class traits, class Allocator>
  371. bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  372. template<class charT, class traits, class Allocator>
  373. bool operator> (const basic_string<charT, traits, Allocator>& lhs,
  374. const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  375. template<class charT, class traits, class Allocator>
  376. bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
  377. template<class charT, class traits, class Allocator>
  378. bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  379. template<class charT, class traits, class Allocator>
  380. bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
  381. const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  382. template<class charT, class traits, class Allocator>
  383. bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
  384. template<class charT, class traits, class Allocator>
  385. bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  386. template<class charT, class traits, class Allocator>
  387. bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
  388. const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  389. template<class charT, class traits, class Allocator>
  390. bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
  391. template<class charT, class traits, class Allocator>
  392. bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
  393. template<class charT, class traits, class Allocator> // since C++20
  394. constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
  395. const basic_string<charT, traits, Allocator>& rhs) noexcept;
  396. template<class charT, class traits, class Allocator> // since C++20
  397. constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
  398. const charT* rhs) noexcept;
  399. template<class charT, class traits, class Allocator>
  400. void swap(basic_string<charT, traits, Allocator>& lhs,
  401. basic_string<charT, traits, Allocator>& rhs)
  402. noexcept(noexcept(lhs.swap(rhs))); // constexpr since C++20
  403. template<class charT, class traits, class Allocator>
  404. basic_istream<charT, traits>&
  405. operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
  406. template<class charT, class traits, class Allocator>
  407. basic_ostream<charT, traits>&
  408. operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
  409. template<class charT, class traits, class Allocator>
  410. basic_istream<charT, traits>&
  411. getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
  412. charT delim);
  413. template<class charT, class traits, class Allocator>
  414. basic_istream<charT, traits>&
  415. getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
  416. template<class charT, class traits, class Allocator, class U>
  417. typename basic_string<charT, traits, Allocator>::size_type
  418. erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
  419. template<class charT, class traits, class Allocator, class Predicate>
  420. typename basic_string<charT, traits, Allocator>::size_type
  421. erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
  422. typedef basic_string<char> string;
  423. typedef basic_string<wchar_t> wstring;
  424. typedef basic_string<char8_t> u8string; // C++20
  425. typedef basic_string<char16_t> u16string;
  426. typedef basic_string<char32_t> u32string;
  427. int stoi (const string& str, size_t* idx = nullptr, int base = 10);
  428. long stol (const string& str, size_t* idx = nullptr, int base = 10);
  429. unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
  430. long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
  431. unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
  432. float stof (const string& str, size_t* idx = nullptr);
  433. double stod (const string& str, size_t* idx = nullptr);
  434. long double stold(const string& str, size_t* idx = nullptr);
  435. string to_string(int val);
  436. string to_string(unsigned val);
  437. string to_string(long val);
  438. string to_string(unsigned long val);
  439. string to_string(long long val);
  440. string to_string(unsigned long long val);
  441. string to_string(float val);
  442. string to_string(double val);
  443. string to_string(long double val);
  444. int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
  445. long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
  446. unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
  447. long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
  448. unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
  449. float stof (const wstring& str, size_t* idx = nullptr);
  450. double stod (const wstring& str, size_t* idx = nullptr);
  451. long double stold(const wstring& str, size_t* idx = nullptr);
  452. wstring to_wstring(int val);
  453. wstring to_wstring(unsigned val);
  454. wstring to_wstring(long val);
  455. wstring to_wstring(unsigned long val);
  456. wstring to_wstring(long long val);
  457. wstring to_wstring(unsigned long long val);
  458. wstring to_wstring(float val);
  459. wstring to_wstring(double val);
  460. wstring to_wstring(long double val);
  461. template <> struct hash<string>;
  462. template <> struct hash<u8string>; // C++20
  463. template <> struct hash<u16string>;
  464. template <> struct hash<u32string>;
  465. template <> struct hash<wstring>;
  466. basic_string<char> operator""s( const char *str, size_t len ); // C++14, constexpr since C++20
  467. basic_string<wchar_t> operator""s( const wchar_t *str, size_t len ); // C++14, constexpr since C++20
  468. constexpr basic_string<char8_t> operator""s( const char8_t *str, size_t len ); // C++20
  469. basic_string<char16_t> operator""s( const char16_t *str, size_t len ); // C++14, constexpr since C++20
  470. basic_string<char32_t> operator""s( const char32_t *str, size_t len ); // C++14, constexpr since C++20
  471. } // std
  472. */
  473. // clang-format on
  474. #include <__algorithm/max.h>
  475. #include <__algorithm/min.h>
  476. #include <__algorithm/remove.h>
  477. #include <__algorithm/remove_if.h>
  478. #include <__assert>
  479. #include <__config>
  480. #include <__format/enable_insertable.h>
  481. #include <__functional/hash.h>
  482. #include <__functional/unary_function.h>
  483. #include <__fwd/string.h>
  484. #include <__ios/fpos.h>
  485. #include <__iterator/distance.h>
  486. #include <__iterator/iterator_traits.h>
  487. #include <__iterator/reverse_iterator.h>
  488. #include <__iterator/wrap_iter.h>
  489. #include <__memory/addressof.h>
  490. #include <__memory/allocate_at_least.h>
  491. #include <__memory/allocator.h>
  492. #include <__memory/allocator_traits.h>
  493. #include <__memory/compressed_pair.h>
  494. #include <__memory/construct_at.h>
  495. #include <__memory/pointer_traits.h>
  496. #include <__memory/swap_allocator.h>
  497. #include <__memory_resource/polymorphic_allocator.h>
  498. #include <__ranges/access.h>
  499. #include <__ranges/concepts.h>
  500. #include <__ranges/container_compatible_range.h>
  501. #include <__ranges/from_range.h>
  502. #include <__ranges/size.h>
  503. #include <__string/char_traits.h>
  504. #include <__string/extern_template_lists.h>
  505. #include <__type_traits/conditional.h>
  506. #include <__type_traits/is_allocator.h>
  507. #include <__type_traits/is_array.h>
  508. #include <__type_traits/is_convertible.h>
  509. #include <__type_traits/is_nothrow_assignable.h>
  510. #include <__type_traits/is_nothrow_constructible.h>
  511. #include <__type_traits/is_same.h>
  512. #include <__type_traits/is_standard_layout.h>
  513. #include <__type_traits/is_trivial.h>
  514. #include <__type_traits/is_trivially_relocatable.h>
  515. #include <__type_traits/noexcept_move_assign_container.h>
  516. #include <__type_traits/remove_cvref.h>
  517. #include <__type_traits/void_t.h>
  518. #include <__utility/auto_cast.h>
  519. #include <__utility/declval.h>
  520. #include <__utility/forward.h>
  521. #include <__utility/is_pointer_in_range.h>
  522. #include <__utility/move.h>
  523. #include <__utility/swap.h>
  524. #include <__utility/unreachable.h>
  525. #include <climits>
  526. #include <cstdio> // EOF
  527. #include <cstring>
  528. #include <limits>
  529. #include <stdexcept>
  530. #include <string_view>
  531. #include <version>
  532. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  533. # include <cwchar>
  534. #endif
  535. // standard-mandated includes
  536. // [iterator.range]
  537. #include <__iterator/access.h>
  538. #include <__iterator/data.h>
  539. #include <__iterator/empty.h>
  540. #include <__iterator/reverse_access.h>
  541. #include <__iterator/size.h>
  542. // [string.syn]
  543. #include <compare>
  544. #include <initializer_list>
  545. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  546. # pragma GCC system_header
  547. #endif
  548. _LIBCPP_PUSH_MACROS
  549. #include <__undef_macros>
  550. #if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
  551. # define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS __attribute__((__no_sanitize__("address")))
  552. // This macro disables AddressSanitizer (ASan) instrumentation for a specific function,
  553. // allowing memory accesses that would normally trigger ASan errors to proceed without crashing.
  554. // This is useful for accessing parts of objects memory, which should not be accessed,
  555. // such as unused bytes in short strings, that should never be accessed
  556. // by other parts of the program.
  557. #else
  558. # define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
  559. #endif
  560. #define _LIBCPP_SHORT_STRING_ANNOTATIONS_ALLOWED false
  561. _LIBCPP_BEGIN_NAMESPACE_STD
  562. // basic_string
  563. template <class _CharT, class _Traits, class _Allocator>
  564. basic_string<_CharT, _Traits, _Allocator> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  565. operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const basic_string<_CharT, _Traits, _Allocator>& __y);
  566. template <class _CharT, class _Traits, class _Allocator>
  567. _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  568. operator+(const _CharT* __x, const basic_string<_CharT, _Traits, _Allocator>& __y);
  569. template <class _CharT, class _Traits, class _Allocator>
  570. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  571. operator+(_CharT __x, const basic_string<_CharT, _Traits, _Allocator>& __y);
  572. template <class _CharT, class _Traits, class _Allocator>
  573. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  574. operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
  575. template <class _CharT, class _Traits, class _Allocator>
  576. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  577. operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
  578. extern template _LIBCPP_EXPORTED_FROM_ABI string operator+
  579. <char, char_traits<char>, allocator<char> >(char const*, string const&);
  580. template <class _Iter>
  581. struct __string_is_trivial_iterator : public false_type {};
  582. template <class _Tp>
  583. struct __string_is_trivial_iterator<_Tp*> : public is_arithmetic<_Tp> {};
  584. template <class _Iter>
  585. struct __string_is_trivial_iterator<__wrap_iter<_Iter> > : public __string_is_trivial_iterator<_Iter> {};
  586. template <class _CharT, class _Traits, class _Tp>
  587. struct __can_be_converted_to_string_view
  588. : public _BoolConstant< is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
  589. !is_convertible<const _Tp&, const _CharT*>::value > {};
  590. struct __uninitialized_size_tag {};
  591. struct __init_with_sentinel_tag {};
  592. template <class _CharT, class _Traits, class _Allocator>
  593. class basic_string {
  594. private:
  595. using __default_allocator_type = allocator<_CharT>;
  596. public:
  597. typedef basic_string __self;
  598. typedef basic_string_view<_CharT, _Traits> __self_view;
  599. typedef _Traits traits_type;
  600. typedef _CharT value_type;
  601. typedef _Allocator allocator_type;
  602. typedef allocator_traits<allocator_type> __alloc_traits;
  603. typedef typename __alloc_traits::size_type size_type;
  604. typedef typename __alloc_traits::difference_type difference_type;
  605. typedef value_type& reference;
  606. typedef const value_type& const_reference;
  607. typedef typename __alloc_traits::pointer pointer;
  608. typedef typename __alloc_traits::const_pointer const_pointer;
  609. // A basic_string contains the following members which may be trivially relocatable:
  610. // - pointer: is currently assumed to be trivially relocatable, but is still checked in case that changes
  611. // - size_type: is always trivially relocatable, since it has to be an integral type
  612. // - value_type: is always trivially relocatable, since it has to be trivial
  613. // - unsigned char: is a fundamental type, so it's trivially relocatable
  614. // - allocator_type: may or may not be trivially relocatable, so it's checked
  615. //
  616. // This string implementation doesn't contain any references into itself. It only contains a bit that says whether
  617. // it is in small or large string mode, so the entire structure is trivially relocatable if its members are.
  618. using __trivially_relocatable = __conditional_t<
  619. __libcpp_is_trivially_relocatable<allocator_type>::value && __libcpp_is_trivially_relocatable<pointer>::value,
  620. basic_string,
  621. void>;
  622. static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
  623. static_assert((is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
  624. static_assert((is_trivial<value_type>::value), "Character type of basic_string must be trivial");
  625. static_assert((is_same<_CharT, typename traits_type::char_type>::value),
  626. "traits_type::char_type must be the same type as CharT");
  627. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  628. "Allocator::value_type must be same type as value_type");
  629. static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
  630. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  631. "original allocator");
  632. #if _YNDX_LIBCPP_MAKE_STRING_ITERATOR_POINTERS == 1
  633. typedef pointer iterator;
  634. typedef const_pointer const_iterator;
  635. #else
  636. // TODO: Implement iterator bounds checking without requiring the global database.
  637. typedef __wrap_iter<pointer> iterator;
  638. typedef __wrap_iter<const_pointer> const_iterator;
  639. #endif
  640. typedef std::reverse_iterator<iterator> reverse_iterator;
  641. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  642. private:
  643. static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
  644. #ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  645. struct __long {
  646. pointer __data_;
  647. size_type __size_;
  648. size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
  649. size_type __is_long_ : 1;
  650. };
  651. enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };
  652. struct __short {
  653. value_type __data_[__min_cap];
  654. unsigned char __padding_[sizeof(value_type) - 1];
  655. unsigned char __size_ : 7;
  656. unsigned char __is_long_ : 1;
  657. };
  658. // The __endian_factor is required because the field we use to store the size
  659. // has one fewer bit than it would if it were not a bitfield.
  660. //
  661. // If the LSB is used to store the short-flag in the short string representation,
  662. // we have to multiply the size by two when it is stored and divide it by two when
  663. // it is loaded to make sure that we always store an even number. In the long string
  664. // representation, we can ignore this because we can assume that we always allocate
  665. // an even amount of value_types.
  666. //
  667. // If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
  668. // This does not impact the short string representation, since we never need the MSB
  669. // for representing the size of a short string anyway.
  670. # ifdef _LIBCPP_BIG_ENDIAN
  671. static const size_type __endian_factor = 2;
  672. # else
  673. static const size_type __endian_factor = 1;
  674. # endif
  675. #else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  676. # ifdef _LIBCPP_BIG_ENDIAN
  677. static const size_type __endian_factor = 1;
  678. # else
  679. static const size_type __endian_factor = 2;
  680. # endif
  681. // Attribute 'packed' is used to keep the layout compatible with the
  682. // previous definition that did not use bit fields. This is because on
  683. // some platforms bit fields have a default size rather than the actual
  684. // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
  685. struct __long {
  686. struct _LIBCPP_PACKED {
  687. size_type __is_long_ : 1;
  688. size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
  689. };
  690. size_type __size_;
  691. pointer __data_;
  692. };
  693. enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };
  694. struct __short {
  695. struct _LIBCPP_PACKED {
  696. unsigned char __is_long_ : 1;
  697. unsigned char __size_ : 7;
  698. };
  699. char __padding_[sizeof(value_type) - 1];
  700. value_type __data_[__min_cap];
  701. };
  702. #endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  703. static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
  704. union __ulx {
  705. __long __lx;
  706. __short __lxx;
  707. };
  708. enum { __n_words = sizeof(__ulx) / sizeof(size_type) };
  709. struct __raw {
  710. size_type __words[__n_words];
  711. };
  712. struct __rep {
  713. union {
  714. __short __s;
  715. __long __l;
  716. __raw __r;
  717. };
  718. };
  719. __compressed_pair<__rep, allocator_type> __r_;
  720. // Construct a string with the given allocator and enough storage to hold `__size` characters, but
  721. // don't initialize the characters. The contents of the string, including the null terminator, must be
  722. // initialized separately.
  723. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
  724. __uninitialized_size_tag, size_type __size, const allocator_type& __a)
  725. : __r_(__default_init_tag(), __a) {
  726. if (__size > max_size())
  727. __throw_length_error();
  728. if (__fits_in_sso(__size)) {
  729. __r_.first() = __rep();
  730. __set_short_size(__size);
  731. } else {
  732. auto __capacity = __recommend(__size) + 1;
  733. auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
  734. __begin_lifetime(__allocation, __capacity);
  735. __set_long_cap(__capacity);
  736. __set_long_pointer(__allocation);
  737. __set_long_size(__size);
  738. }
  739. __annotate_new(__size);
  740. }
  741. template <class _Iter, class _Sent>
  742. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  743. basic_string(__init_with_sentinel_tag, _Iter __first, _Sent __last, const allocator_type& __a)
  744. : __r_(__default_init_tag(), __a) {
  745. __init_with_sentinel(std::move(__first), std::move(__last));
  746. }
  747. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator __make_iterator(pointer __p) { return iterator(__p); }
  748. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator __make_const_iterator(const_pointer __p) const {
  749. return const_iterator(__p);
  750. }
  751. public:
  752. _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
  753. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
  754. _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
  755. : __r_(__value_init_tag(), __default_init_tag()) {
  756. __annotate_new(0);
  757. }
  758. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
  759. #if _LIBCPP_STD_VER <= 14
  760. _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
  761. #else
  762. _NOEXCEPT
  763. #endif
  764. : __r_(__value_init_tag(), __a) {
  765. __annotate_new(0);
  766. }
  767. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string(const basic_string& __str)
  768. : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc())) {
  769. if (!__str.__is_long()) {
  770. __r_.first() = __str.__r_.first();
  771. __annotate_new(__get_short_size());
  772. } else
  773. __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
  774. }
  775. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
  776. basic_string(const basic_string& __str, const allocator_type& __a)
  777. : __r_(__default_init_tag(), __a) {
  778. if (!__str.__is_long()) {
  779. __r_.first() = __str.__r_.first();
  780. __annotate_new(__get_short_size());
  781. } else
  782. __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
  783. }
  784. #ifndef _LIBCPP_CXX03_LANG
  785. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)
  786. # if _LIBCPP_STD_VER <= 14
  787. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
  788. # else
  789. _NOEXCEPT
  790. # endif
  791. // Turning off ASan instrumentation for variable initialization with _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
  792. // does not work consistently during initialization of __r_, so we instead unpoison __str's memory manually first.
  793. // __str's memory needs to be unpoisoned only in the case where it's a short string.
  794. : __r_([](basic_string& __s) -> decltype(__s.__r_)&& {
  795. if (!__s.__is_long())
  796. __s.__annotate_delete();
  797. return std::move(__s.__r_);
  798. }(__str)) {
  799. __str.__r_.first() = __rep();
  800. __str.__annotate_new(0);
  801. if (!__is_long())
  802. __annotate_new(size());
  803. }
  804. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)
  805. : __r_(__default_init_tag(), __a) {
  806. if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
  807. __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
  808. else {
  809. if (__libcpp_is_constant_evaluated())
  810. __r_.first() = __rep();
  811. if (!__str.__is_long())
  812. __str.__annotate_delete();
  813. __r_.first() = __str.__r_.first();
  814. __str.__r_.first() = __rep();
  815. __str.__annotate_new(0);
  816. if (!__is_long() && this != &__str)
  817. __annotate_new(size());
  818. }
  819. }
  820. #endif // _LIBCPP_CXX03_LANG
  821. template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
  822. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s)
  823. : __r_(__default_init_tag(), __default_init_tag()) {
  824. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "basic_string(const char*) detected nullptr");
  825. __init(__s, traits_type::length(__s));
  826. }
  827. template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
  828. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, const _Allocator& __a)
  829. : __r_(__default_init_tag(), __a) {
  830. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
  831. __init(__s, traits_type::length(__s));
  832. }
  833. #if _LIBCPP_STD_VER >= 20
  834. basic_string(nullptr_t) = delete;
  835. #endif
  836. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(nullptr_t, size_t) = delete;
  837. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n)
  838. : __r_(__default_init_tag(), __default_init_tag()) {
  839. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
  840. __init(__s, __n);
  841. }
  842. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(nullptr_t, size_t, const _Allocator&) = delete;
  843. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  844. basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
  845. : __r_(__default_init_tag(), __a) {
  846. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
  847. __init(__s, __n);
  848. }
  849. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c)
  850. : __r_(__default_init_tag(), __default_init_tag()) {
  851. __init(__n, __c);
  852. }
  853. #if _LIBCPP_STD_VER >= 23
  854. _LIBCPP_HIDE_FROM_ABI constexpr basic_string(
  855. basic_string&& __str, size_type __pos, const _Allocator& __alloc = _Allocator())
  856. : basic_string(std::move(__str), __pos, npos, __alloc) {}
  857. _LIBCPP_HIDE_FROM_ABI constexpr basic_string(
  858. basic_string&& __str, size_type __pos, size_type __n, const _Allocator& __alloc = _Allocator())
  859. : __r_(__default_init_tag(), __alloc) {
  860. if (__pos > __str.size())
  861. __throw_out_of_range();
  862. auto __len = std::min<size_type>(__n, __str.size() - __pos);
  863. if (__alloc_traits::is_always_equal::value || __alloc == __str.__alloc()) {
  864. __move_assign(std::move(__str), __pos, __len);
  865. } else {
  866. // Perform a copy because the allocators are not compatible.
  867. __init(__str.data() + __pos, __len);
  868. }
  869. }
  870. #endif
  871. template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
  872. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c, const _Allocator& __a)
  873. : __r_(__default_init_tag(), __a) {
  874. __init(__n, __c);
  875. }
  876. _LIBCPP_CONSTEXPR_SINCE_CXX20
  877. basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator())
  878. : __r_(__default_init_tag(), __a) {
  879. size_type __str_sz = __str.size();
  880. if (__pos > __str_sz)
  881. __throw_out_of_range();
  882. __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
  883. }
  884. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  885. basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())
  886. : __r_(__default_init_tag(), __a) {
  887. size_type __str_sz = __str.size();
  888. if (__pos > __str_sz)
  889. __throw_out_of_range();
  890. __init(__str.data() + __pos, __str_sz - __pos);
  891. }
  892. template <class _Tp,
  893. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  894. !__is_same_uncvref<_Tp, basic_string>::value,
  895. int> = 0>
  896. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
  897. basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type())
  898. : __r_(__default_init_tag(), __a) {
  899. __self_view __sv0 = __t;
  900. __self_view __sv = __sv0.substr(__pos, __n);
  901. __init(__sv.data(), __sv.size());
  902. }
  903. template <class _Tp,
  904. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  905. !__is_same_uncvref<_Tp, basic_string>::value,
  906. int> = 0>
  907. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const _Tp& __t)
  908. : __r_(__default_init_tag(), __default_init_tag()) {
  909. __self_view __sv = __t;
  910. __init(__sv.data(), __sv.size());
  911. }
  912. template <class _Tp,
  913. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  914. !__is_same_uncvref<_Tp, basic_string>::value,
  915. int> = 0>
  916. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
  917. const _Tp& __t, const allocator_type& __a)
  918. : __r_(__default_init_tag(), __a) {
  919. __self_view __sv = __t;
  920. __init(__sv.data(), __sv.size());
  921. }
  922. template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
  923. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last)
  924. : __r_(__default_init_tag(), __default_init_tag()) {
  925. __init(__first, __last);
  926. }
  927. template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
  928. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  929. basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
  930. : __r_(__default_init_tag(), __a) {
  931. __init(__first, __last);
  932. }
  933. #if _LIBCPP_STD_VER >= 23
  934. template <_ContainerCompatibleRange<_CharT> _Range>
  935. _LIBCPP_HIDE_FROM_ABI constexpr basic_string(
  936. from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
  937. : __r_(__default_init_tag(), __a) {
  938. if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
  939. __init_with_size(ranges::begin(__range), ranges::end(__range), ranges::distance(__range));
  940. } else {
  941. __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
  942. }
  943. }
  944. #endif
  945. #ifndef _LIBCPP_CXX03_LANG
  946. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il)
  947. : __r_(__default_init_tag(), __default_init_tag()) {
  948. __init(__il.begin(), __il.end());
  949. }
  950. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)
  951. : __r_(__default_init_tag(), __a) {
  952. __init(__il.begin(), __il.end());
  953. }
  954. #endif // _LIBCPP_CXX03_LANG
  955. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string() {
  956. __annotate_delete();
  957. if (__is_long())
  958. __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
  959. }
  960. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator __self_view() const _NOEXCEPT {
  961. return __self_view(data(), size());
  962. }
  963. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string&
  964. operator=(const basic_string& __str);
  965. template <class _Tp,
  966. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  967. !__is_same_uncvref<_Tp, basic_string>::value,
  968. int> = 0>
  969. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
  970. __self_view __sv = __t;
  971. return assign(__sv);
  972. }
  973. #ifndef _LIBCPP_CXX03_LANG
  974. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(basic_string&& __str)
  975. _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
  976. __move_assign(__str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
  977. return *this;
  978. }
  979. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(initializer_list<value_type> __il) {
  980. return assign(__il.begin(), __il.size());
  981. }
  982. #endif
  983. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const value_type* __s) {
  984. return assign(__s);
  985. }
  986. #if _LIBCPP_STD_VER >= 20
  987. basic_string& operator=(nullptr_t) = delete;
  988. #endif
  989. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
  990. #ifndef _YNDX_LIBCPP_MAKE_STRING_ITERATOR_POINTERS == 1
  991. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() _NOEXCEPT {
  992. return __make_iterator(__get_pointer());
  993. }
  994. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator begin() const _NOEXCEPT {
  995. return __make_const_iterator(__get_pointer());
  996. }
  997. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() _NOEXCEPT {
  998. return __make_iterator(__get_pointer() + size());
  999. }
  1000. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator end() const _NOEXCEPT {
  1001. return __make_const_iterator(__get_pointer() + size());
  1002. }
  1003. #else
  1004. // It is necessary to keep the list of constructors matching the one above it.
  1005. // Made to support pointer iterators
  1006. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() _NOEXCEPT { return iterator(__get_pointer()); }
  1007. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator begin() const _NOEXCEPT {
  1008. return const_iterator(__get_pointer());
  1009. }
  1010. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() _NOEXCEPT {
  1011. return iterator(__get_pointer() + size());
  1012. }
  1013. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator end() const _NOEXCEPT {
  1014. return const_iterator(__get_pointer() + size());
  1015. }
  1016. #endif // _YNDX_LIBCPP_MAKE_STRING_ITERATOR_POINTERS == 1
  1017. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rbegin() _NOEXCEPT {
  1018. return reverse_iterator(end());
  1019. }
  1020. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator rbegin() const _NOEXCEPT {
  1021. return const_reverse_iterator(end());
  1022. }
  1023. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rend() _NOEXCEPT {
  1024. return reverse_iterator(begin());
  1025. }
  1026. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator rend() const _NOEXCEPT {
  1027. return const_reverse_iterator(begin());
  1028. }
  1029. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cbegin() const _NOEXCEPT { return begin(); }
  1030. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cend() const _NOEXCEPT { return end(); }
  1031. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator crbegin() const _NOEXCEPT {
  1032. return rbegin();
  1033. }
  1034. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
  1035. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT {
  1036. return __is_long() ? __get_long_size() : __get_short_size();
  1037. }
  1038. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT { return size(); }
  1039. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT {
  1040. size_type __m = __alloc_traits::max_size(__alloc());
  1041. if (__m <= std::numeric_limits<size_type>::max() / 2) {
  1042. return __m - __alignment;
  1043. } else {
  1044. bool __uses_lsb = __endian_factor == 2;
  1045. return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
  1046. }
  1047. }
  1048. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
  1049. return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
  1050. }
  1051. #if _YNDX_LIBCXX_ENABLE_STRING_RESIZE_UNINITIALIZED == 1
  1052. inline void resize_uninitialized(size_type __n) { __resize_default_init(__n); }
  1053. #endif
  1054. _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
  1055. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
  1056. _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
  1057. #if _LIBCPP_STD_VER >= 23
  1058. template <class _Op>
  1059. _LIBCPP_HIDE_FROM_ABI constexpr void resize_and_overwrite(size_type __n, _Op __op) {
  1060. __resize_default_init(__n);
  1061. __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
  1062. }
  1063. #endif
  1064. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
  1065. #if _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_STRING_RESERVE)
  1066. _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
  1067. #endif
  1068. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
  1069. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
  1070. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool empty() const _NOEXCEPT {
  1071. return size() == 0;
  1072. }
  1073. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator[](size_type __pos) const _NOEXCEPT {
  1074. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");
  1075. if (__builtin_constant_p(__pos) && !__fits_in_sso(__pos)) {
  1076. return *(__get_long_pointer() + __pos);
  1077. }
  1078. return *(data() + __pos);
  1079. }
  1080. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT {
  1081. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");
  1082. if (__builtin_constant_p(__pos) && !__fits_in_sso(__pos)) {
  1083. return *(__get_long_pointer() + __pos);
  1084. }
  1085. return *(__get_pointer() + __pos);
  1086. }
  1087. _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
  1088. _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
  1089. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
  1090. return append(__str);
  1091. }
  1092. template <class _Tp,
  1093. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  1094. !__is_same_uncvref<_Tp, basic_string >::value,
  1095. int> = 0>
  1096. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1097. operator+=(const _Tp& __t) {
  1098. __self_view __sv = __t;
  1099. return append(__sv);
  1100. }
  1101. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const value_type* __s) {
  1102. return append(__s);
  1103. }
  1104. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
  1105. push_back(__c);
  1106. return *this;
  1107. }
  1108. #ifndef _LIBCPP_CXX03_LANG
  1109. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(initializer_list<value_type> __il) {
  1110. return append(__il);
  1111. }
  1112. #endif // _LIBCPP_CXX03_LANG
  1113. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str) {
  1114. return append(__str.data(), __str.size());
  1115. }
  1116. template <class _Tp,
  1117. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  1118. !__is_same_uncvref<_Tp, basic_string>::value,
  1119. int> = 0>
  1120. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1121. append(const _Tp& __t) {
  1122. __self_view __sv = __t;
  1123. return append(__sv.data(), __sv.size());
  1124. }
  1125. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str, size_type __pos, size_type __n = npos);
  1126. template <class _Tp,
  1127. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  1128. !__is_same_uncvref<_Tp, basic_string>::value,
  1129. int> = 0>
  1130. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
  1131. basic_string&
  1132. append(const _Tp& __t, size_type __pos, size_type __n = npos);
  1133. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n);
  1134. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s);
  1135. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
  1136. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __append_default_init(size_type __n);
  1137. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
  1138. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1139. append(_InputIterator __first, _InputIterator __last) {
  1140. const basic_string __temp(__first, __last, __alloc());
  1141. append(__temp.data(), __temp.size());
  1142. return *this;
  1143. }
  1144. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
  1145. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1146. append(_ForwardIterator __first, _ForwardIterator __last);
  1147. #if _LIBCPP_STD_VER >= 23
  1148. template <_ContainerCompatibleRange<_CharT> _Range>
  1149. _LIBCPP_HIDE_FROM_ABI constexpr basic_string& append_range(_Range&& __range) {
  1150. insert_range(end(), std::forward<_Range>(__range));
  1151. return *this;
  1152. }
  1153. #endif
  1154. #ifndef _LIBCPP_CXX03_LANG
  1155. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(initializer_list<value_type> __il) {
  1156. return append(__il.begin(), __il.size());
  1157. }
  1158. #endif // _LIBCPP_CXX03_LANG
  1159. _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
  1160. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
  1161. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT {
  1162. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::front(): string is empty");
  1163. return *__get_pointer();
  1164. }
  1165. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT {
  1166. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::front(): string is empty");
  1167. return *data();
  1168. }
  1169. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT {
  1170. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::back(): string is empty");
  1171. return *(__get_pointer() + size() - 1);
  1172. }
  1173. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT {
  1174. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::back(): string is empty");
  1175. return *(data() + size() - 1);
  1176. }
  1177. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1178. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1179. assign(const _Tp& __t) {
  1180. __self_view __sv = __t;
  1181. return assign(__sv.data(), __sv.size());
  1182. }
  1183. #if _LIBCPP_STD_VER >= 20
  1184. _LIBCPP_HIDE_FROM_ABI constexpr void __move_assign(basic_string&& __str, size_type __pos, size_type __len) {
  1185. // Pilfer the allocation from __str.
  1186. _LIBCPP_ASSERT_INTERNAL(__alloc() == __str.__alloc(), "__move_assign called with wrong allocator");
  1187. size_type __old_sz = __str.size();
  1188. if (!__str.__is_long())
  1189. __str.__annotate_delete();
  1190. __r_.first() = __str.__r_.first();
  1191. __str.__r_.first() = __rep();
  1192. __str.__annotate_new(0);
  1193. _Traits::move(data(), data() + __pos, __len);
  1194. __set_size(__len);
  1195. _Traits::assign(data()[__len], value_type());
  1196. if (!__is_long()) {
  1197. __annotate_new(__len);
  1198. } else if (__old_sz > __len) {
  1199. __annotate_shrink(__old_sz);
  1200. }
  1201. }
  1202. #endif
  1203. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str) {
  1204. return *this = __str;
  1205. }
  1206. #ifndef _LIBCPP_CXX03_LANG
  1207. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(basic_string&& __str)
  1208. _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
  1209. *this = std::move(__str);
  1210. return *this;
  1211. }
  1212. #endif
  1213. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n = npos);
  1214. template <class _Tp,
  1215. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  1216. !__is_same_uncvref<_Tp, basic_string>::value,
  1217. int> = 0>
  1218. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1219. assign(const _Tp& __t, size_type __pos, size_type __n = npos);
  1220. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n);
  1221. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s);
  1222. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
  1223. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
  1224. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1225. assign(_InputIterator __first, _InputIterator __last);
  1226. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
  1227. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1228. assign(_ForwardIterator __first, _ForwardIterator __last);
  1229. #if _LIBCPP_STD_VER >= 23
  1230. template <_ContainerCompatibleRange<_CharT> _Range>
  1231. _LIBCPP_HIDE_FROM_ABI constexpr basic_string& assign_range(_Range&& __range) {
  1232. if constexpr (__string_is_trivial_iterator<ranges::iterator_t<_Range>>::value &&
  1233. (ranges::forward_range<_Range> || ranges::sized_range<_Range>)) {
  1234. size_type __n = static_cast<size_type>(ranges::distance(__range));
  1235. __assign_trivial(ranges::begin(__range), ranges::end(__range), __n);
  1236. } else {
  1237. __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
  1238. }
  1239. return *this;
  1240. }
  1241. #endif
  1242. #ifndef _LIBCPP_CXX03_LANG
  1243. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(initializer_list<value_type> __il) {
  1244. return assign(__il.begin(), __il.size());
  1245. }
  1246. #endif // _LIBCPP_CXX03_LANG
  1247. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1248. insert(size_type __pos1, const basic_string& __str) {
  1249. return insert(__pos1, __str.data(), __str.size());
  1250. }
  1251. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1252. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1253. insert(size_type __pos1, const _Tp& __t) {
  1254. __self_view __sv = __t;
  1255. return insert(__pos1, __sv.data(), __sv.size());
  1256. }
  1257. template <class _Tp,
  1258. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  1259. !__is_same_uncvref<_Tp, basic_string>::value,
  1260. int> = 0>
  1261. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1262. insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n = npos);
  1263. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1264. insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n = npos);
  1265. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
  1266. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s);
  1267. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
  1268. _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
  1269. #if _LIBCPP_STD_VER >= 23
  1270. template <_ContainerCompatibleRange<_CharT> _Range>
  1271. _LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
  1272. if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
  1273. auto __n = static_cast<size_type>(ranges::distance(__range));
  1274. return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);
  1275. } else {
  1276. basic_string __temp(from_range, std::forward<_Range>(__range), __alloc());
  1277. return insert(__position, __temp.data(), __temp.data() + __temp.size());
  1278. }
  1279. }
  1280. #endif
  1281. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
  1282. insert(const_iterator __pos, size_type __n, value_type __c) {
  1283. difference_type __p = __pos - begin();
  1284. insert(static_cast<size_type>(__p), __n, __c);
  1285. return begin() + __p;
  1286. }
  1287. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
  1288. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
  1289. insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
  1290. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
  1291. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
  1292. insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
  1293. #ifndef _LIBCPP_CXX03_LANG
  1294. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
  1295. insert(const_iterator __pos, initializer_list<value_type> __il) {
  1296. return insert(__pos, __il.begin(), __il.end());
  1297. }
  1298. #endif // _LIBCPP_CXX03_LANG
  1299. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
  1300. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __pos);
  1301. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __first, const_iterator __last);
  1302. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1303. replace(size_type __pos1, size_type __n1, const basic_string& __str) {
  1304. return replace(__pos1, __n1, __str.data(), __str.size());
  1305. }
  1306. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1307. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1308. replace(size_type __pos1, size_type __n1, const _Tp& __t) {
  1309. __self_view __sv = __t;
  1310. return replace(__pos1, __n1, __sv.data(), __sv.size());
  1311. }
  1312. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1313. replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2 = npos);
  1314. template <class _Tp,
  1315. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  1316. !__is_same_uncvref<_Tp, basic_string>::value,
  1317. int> = 0>
  1318. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1319. replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2 = npos);
  1320. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1321. replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
  1322. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
  1323. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
  1324. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1325. replace(const_iterator __i1, const_iterator __i2, const basic_string& __str) {
  1326. return replace(
  1327. static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __str.data(), __str.size());
  1328. }
  1329. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1330. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1331. replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) {
  1332. __self_view __sv = __t;
  1333. return replace(__i1 - begin(), __i2 - __i1, __sv);
  1334. }
  1335. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1336. replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n) {
  1337. return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
  1338. }
  1339. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1340. replace(const_iterator __i1, const_iterator __i2, const value_type* __s) {
  1341. return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
  1342. }
  1343. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1344. replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c) {
  1345. return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
  1346. }
  1347. template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
  1348. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1349. replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
  1350. #if _LIBCPP_STD_VER >= 23
  1351. template <_ContainerCompatibleRange<_CharT> _Range>
  1352. _LIBCPP_HIDE_FROM_ABI constexpr basic_string&
  1353. replace_with_range(const_iterator __i1, const_iterator __i2, _Range&& __range) {
  1354. basic_string __temp(from_range, std::forward<_Range>(__range), __alloc());
  1355. return replace(__i1, __i2, __temp);
  1356. }
  1357. #endif
  1358. #ifndef _LIBCPP_CXX03_LANG
  1359. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1360. replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il) {
  1361. return replace(__i1, __i2, __il.begin(), __il.end());
  1362. }
  1363. #endif // _LIBCPP_CXX03_LANG
  1364. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
  1365. #if _LIBCPP_STD_VER <= 20
  1366. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string
  1367. substr(size_type __pos = 0, size_type __n = npos) const {
  1368. return basic_string(*this, __pos, __n);
  1369. }
  1370. #else
  1371. _LIBCPP_HIDE_FROM_ABI constexpr basic_string substr(size_type __pos = 0, size_type __n = npos) const& {
  1372. return basic_string(*this, __pos, __n);
  1373. }
  1374. _LIBCPP_HIDE_FROM_ABI constexpr basic_string substr(size_type __pos = 0, size_type __n = npos) && {
  1375. return basic_string(std::move(*this), __pos, __n);
  1376. }
  1377. #endif
  1378. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(basic_string& __str)
  1379. #if _LIBCPP_STD_VER >= 14
  1380. _NOEXCEPT;
  1381. #else
  1382. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<allocator_type>::value);
  1383. #endif
  1384. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const value_type* c_str() const _NOEXCEPT { return data(); }
  1385. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const value_type* data() const _NOEXCEPT {
  1386. return std::__to_address(__get_pointer());
  1387. }
  1388. #if _LIBCPP_STD_VER >= 17
  1389. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 value_type* data() _NOEXCEPT {
  1390. return std::__to_address(__get_pointer());
  1391. }
  1392. #endif
  1393. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator_type get_allocator() const _NOEXCEPT {
  1394. return __alloc();
  1395. }
  1396. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1397. find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
  1398. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1399. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1400. find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
  1401. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1402. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1403. find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
  1404. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
  1405. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1406. rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
  1407. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1408. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1409. rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
  1410. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1411. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1412. rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
  1413. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
  1414. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1415. find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
  1416. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1417. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1418. find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
  1419. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1420. find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1421. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1422. find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
  1423. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1424. find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
  1425. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1426. find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
  1427. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1428. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1429. find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
  1430. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1431. find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1432. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1433. find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
  1434. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1435. find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
  1436. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1437. find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
  1438. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1439. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1440. find_first_not_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
  1441. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1442. find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1443. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1444. find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
  1445. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1446. find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
  1447. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1448. find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
  1449. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1450. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1451. find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
  1452. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1453. find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1454. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1455. find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
  1456. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
  1457. find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
  1458. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const basic_string& __str) const _NOEXCEPT;
  1459. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1460. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  1461. compare(const _Tp& __t) const _NOEXCEPT;
  1462. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
  1463. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  1464. compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
  1465. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  1466. compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
  1467. _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  1468. compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2 = npos) const;
  1469. template <class _Tp,
  1470. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  1471. !__is_same_uncvref<_Tp, basic_string>::value,
  1472. int> = 0>
  1473. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  1474. compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2 = npos) const;
  1475. _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const value_type* __s) const _NOEXCEPT;
  1476. _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
  1477. _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  1478. compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
  1479. #if _LIBCPP_STD_VER >= 20
  1480. constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(__self_view __sv) const noexcept {
  1481. return __self_view(data(), size()).starts_with(__sv);
  1482. }
  1483. constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(value_type __c) const noexcept {
  1484. return !empty() && _Traits::eq(front(), __c);
  1485. }
  1486. constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(const value_type* __s) const noexcept {
  1487. return starts_with(__self_view(__s));
  1488. }
  1489. constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(__self_view __sv) const noexcept {
  1490. return __self_view(data(), size()).ends_with(__sv);
  1491. }
  1492. constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(value_type __c) const noexcept {
  1493. return !empty() && _Traits::eq(back(), __c);
  1494. }
  1495. constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(const value_type* __s) const noexcept {
  1496. return ends_with(__self_view(__s));
  1497. }
  1498. #endif
  1499. #if _LIBCPP_STD_VER >= 20
  1500. constexpr _LIBCPP_HIDE_FROM_ABI bool contains(__self_view __sv) const noexcept {
  1501. return __self_view(data(), size()).contains(__sv);
  1502. }
  1503. constexpr _LIBCPP_HIDE_FROM_ABI bool contains(value_type __c) const noexcept {
  1504. return __self_view(data(), size()).contains(__c);
  1505. }
  1506. constexpr _LIBCPP_HIDE_FROM_ABI bool contains(const value_type* __s) const {
  1507. return __self_view(data(), size()).contains(__s);
  1508. }
  1509. #endif
  1510. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
  1511. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
  1512. private:
  1513. template <class _Alloc>
  1514. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool friend
  1515. operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
  1516. const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
  1517. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
  1518. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS bool
  1519. __is_long() const _NOEXCEPT {
  1520. if (__libcpp_is_constant_evaluated() && __builtin_constant_p(__r_.first().__l.__is_long_)) {
  1521. return __r_.first().__l.__is_long_;
  1522. }
  1523. return __r_.first().__s.__is_long_;
  1524. }
  1525. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __begin_lifetime(pointer __begin, size_type __n) {
  1526. #if _LIBCPP_STD_VER >= 20
  1527. if (__libcpp_is_constant_evaluated()) {
  1528. for (size_type __i = 0; __i != __n; ++__i)
  1529. std::construct_at(std::addressof(__begin[__i]));
  1530. }
  1531. #else
  1532. (void)__begin;
  1533. (void)__n;
  1534. #endif // _LIBCPP_STD_VER >= 20
  1535. }
  1536. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) { return __sz < __min_cap; }
  1537. template <class _Iterator, class _Sentinel>
  1538. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
  1539. __assign_trivial(_Iterator __first, _Sentinel __last, size_type __n);
  1540. template <class _Iterator, class _Sentinel>
  1541. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
  1542. template <class _ForwardIterator, class _Sentinel>
  1543. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 iterator
  1544. __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _Sentinel __last) {
  1545. size_type __sz = size();
  1546. size_type __cap = capacity();
  1547. value_type* __p;
  1548. if (__cap - __sz >= __n) {
  1549. __annotate_increase(__n);
  1550. __p = std::__to_address(__get_pointer());
  1551. size_type __n_move = __sz - __ip;
  1552. if (__n_move != 0)
  1553. traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
  1554. } else {
  1555. __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
  1556. __p = std::__to_address(__get_long_pointer());
  1557. }
  1558. __sz += __n;
  1559. __set_size(__sz);
  1560. traits_type::assign(__p[__sz], value_type());
  1561. for (__p += __ip; __first != __last; ++__p, ++__first)
  1562. traits_type::assign(*__p, *__first);
  1563. return begin() + __ip;
  1564. }
  1565. template <class _Iterator, class _Sentinel>
  1566. _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
  1567. __insert_with_size(const_iterator __pos, _Iterator __first, _Sentinel __last, size_type __n);
  1568. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
  1569. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
  1570. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void
  1571. __set_short_size(size_type __s) _NOEXCEPT {
  1572. _LIBCPP_ASSERT_INTERNAL(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
  1573. __r_.first().__s.__size_ = __s;
  1574. __r_.first().__s.__is_long_ = false;
  1575. }
  1576. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS size_type
  1577. __get_short_size() const _NOEXCEPT {
  1578. _LIBCPP_ASSERT_INTERNAL(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
  1579. return __r_.first().__s.__size_;
  1580. }
  1581. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_long_size(size_type __s) _NOEXCEPT {
  1582. __r_.first().__l.__size_ = __s;
  1583. }
  1584. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __get_long_size() const _NOEXCEPT {
  1585. return __r_.first().__l.__size_;
  1586. }
  1587. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_size(size_type __s) _NOEXCEPT {
  1588. if (__is_long())
  1589. __set_long_size(__s);
  1590. else
  1591. __set_short_size(__s);
  1592. }
  1593. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_long_cap(size_type __s) _NOEXCEPT {
  1594. __r_.first().__l.__cap_ = __s / __endian_factor;
  1595. __r_.first().__l.__is_long_ = true;
  1596. }
  1597. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __get_long_cap() const _NOEXCEPT {
  1598. return __r_.first().__l.__cap_ * __endian_factor;
  1599. }
  1600. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_long_pointer(pointer __p) _NOEXCEPT {
  1601. __r_.first().__l.__data_ = __p;
  1602. }
  1603. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __get_long_pointer() _NOEXCEPT {
  1604. return __r_.first().__l.__data_;
  1605. }
  1606. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer __get_long_pointer() const _NOEXCEPT {
  1607. return __r_.first().__l.__data_;
  1608. }
  1609. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __get_short_pointer() _NOEXCEPT {
  1610. return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);
  1611. }
  1612. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer __get_short_pointer() const _NOEXCEPT {
  1613. return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);
  1614. }
  1615. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __get_pointer() _NOEXCEPT {
  1616. return __is_long() ? __get_long_pointer() : __get_short_pointer();
  1617. }
  1618. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer __get_pointer() const _NOEXCEPT {
  1619. return __is_long() ? __get_long_pointer() : __get_short_pointer();
  1620. }
  1621. // The following functions are no-ops outside of AddressSanitizer mode.
  1622. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1623. __annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {
  1624. (void)__old_mid;
  1625. (void)__new_mid;
  1626. #if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
  1627. const void* __begin = data();
  1628. const void* __end = data() + capacity() + 1;
  1629. if (__asan_annotate_container_with_allocator<allocator_type>::value && !__libcpp_is_constant_evaluated())
  1630. __sanitizer_annotate_contiguous_container(__begin, __end, __old_mid, __new_mid);
  1631. #endif
  1632. }
  1633. // ASan: short string is poisoned if and only if this function returns true.
  1634. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __asan_short_string_is_annotated() const _NOEXCEPT {
  1635. return _LIBCPP_SHORT_STRING_ANNOTATIONS_ALLOWED && !__libcpp_is_constant_evaluated();
  1636. }
  1637. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_new(size_type __current_size) const _NOEXCEPT {
  1638. (void)__current_size;
  1639. #if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
  1640. if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
  1641. __annotate_contiguous_container(data() + capacity() + 1, data() + __current_size + 1);
  1642. #endif
  1643. }
  1644. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_delete() const _NOEXCEPT {
  1645. #if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
  1646. if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
  1647. __annotate_contiguous_container(data() + size() + 1, data() + capacity() + 1);
  1648. #endif
  1649. }
  1650. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_increase(size_type __n) const _NOEXCEPT {
  1651. (void)__n;
  1652. #if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
  1653. if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
  1654. __annotate_contiguous_container(data() + size() + 1, data() + size() + 1 + __n);
  1655. #endif
  1656. }
  1657. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
  1658. (void)__old_size;
  1659. #if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
  1660. if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
  1661. __annotate_contiguous_container(data() + __old_size + 1, data() + size() + 1);
  1662. #endif
  1663. }
  1664. template <size_type __a>
  1665. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __align_it(size_type __s) _NOEXCEPT {
  1666. return (__s + (__a - 1)) & ~(__a - 1);
  1667. }
  1668. enum { __alignment = 8 };
  1669. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __s) _NOEXCEPT {
  1670. if (__s < __min_cap) {
  1671. return static_cast<size_type>(__min_cap) - 1;
  1672. }
  1673. const size_type __boundary = sizeof(value_type) < __alignment ? __alignment / sizeof(value_type) : 1;
  1674. size_type __guess = __align_it<__boundary>(__s + 1) - 1;
  1675. if (__guess == __min_cap)
  1676. ++__guess;
  1677. return __guess;
  1678. }
  1679. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(const value_type* __s, size_type __sz, size_type __reserve);
  1680. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(const value_type* __s, size_type __sz);
  1681. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(size_type __n, value_type __c);
  1682. // Slow path for the (inlined) copy constructor for 'long' strings.
  1683. // Always externally instantiated and not inlined.
  1684. // Requires that __s is zero terminated.
  1685. // The main reason for this function to exist is because for unstable, we
  1686. // want to allow inlining of the copy constructor. However, we don't want
  1687. // to call the __init() functions as those are marked as inline which may
  1688. // result in over-aggressive inlining by the compiler, where our aim is
  1689. // to only inline the fast path code directly in the ctor.
  1690. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void __init_copy_ctor_external(const value_type* __s, size_type __sz);
  1691. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
  1692. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(_InputIterator __first, _InputIterator __last);
  1693. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
  1694. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(_ForwardIterator __first, _ForwardIterator __last);
  1695. template <class _InputIterator, class _Sentinel>
  1696. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1697. __init_with_sentinel(_InputIterator __first, _Sentinel __last);
  1698. template <class _InputIterator, class _Sentinel>
  1699. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1700. __init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz);
  1701. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1702. #if _LIBCPP_ABI_VERSION >= 2 // We want to use the function in the dylib in ABIv1
  1703. _LIBCPP_HIDE_FROM_ABI
  1704. #endif
  1705. _LIBCPP_DEPRECATED_("use __grow_by_without_replace") void __grow_by(
  1706. size_type __old_cap,
  1707. size_type __delta_cap,
  1708. size_type __old_sz,
  1709. size_type __n_copy,
  1710. size_type __n_del,
  1711. size_type __n_add = 0);
  1712. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __grow_by_without_replace(
  1713. size_type __old_cap,
  1714. size_type __delta_cap,
  1715. size_type __old_sz,
  1716. size_type __n_copy,
  1717. size_type __n_del,
  1718. size_type __n_add = 0);
  1719. _LIBCPP_CONSTEXPR_SINCE_CXX20 void __grow_by_and_replace(
  1720. size_type __old_cap,
  1721. size_type __delta_cap,
  1722. size_type __old_sz,
  1723. size_type __n_copy,
  1724. size_type __n_del,
  1725. size_type __n_add,
  1726. const value_type* __p_new_stuff);
  1727. // __assign_no_alias is invoked for assignment operations where we
  1728. // have proof that the input does not alias the current instance.
  1729. // For example, operator=(basic_string) performs a 'self' check.
  1730. template <bool __is_short>
  1731. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_no_alias(const value_type* __s, size_type __n);
  1732. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_to_end(size_type __pos) {
  1733. __null_terminate_at(std::__to_address(__get_pointer()), __pos);
  1734. }
  1735. // __erase_external_with_move is invoked for erase() invocations where
  1736. // `n ~= npos`, likely requiring memory moves on the string data.
  1737. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void __erase_external_with_move(size_type __pos, size_type __n);
  1738. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str) {
  1739. __copy_assign_alloc(
  1740. __str, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
  1741. }
  1742. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str, true_type) {
  1743. if (__alloc() == __str.__alloc())
  1744. __alloc() = __str.__alloc();
  1745. else {
  1746. if (!__str.__is_long()) {
  1747. __clear_and_shrink();
  1748. __alloc() = __str.__alloc();
  1749. } else {
  1750. __annotate_delete();
  1751. allocator_type __a = __str.__alloc();
  1752. auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
  1753. __begin_lifetime(__allocation.ptr, __allocation.count);
  1754. if (__is_long())
  1755. __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
  1756. __alloc() = std::move(__a);
  1757. __set_long_pointer(__allocation.ptr);
  1758. __set_long_cap(__allocation.count);
  1759. __set_long_size(__str.size());
  1760. __annotate_new(__get_long_size());
  1761. }
  1762. }
  1763. }
  1764. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1765. __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT {}
  1766. #ifndef _LIBCPP_CXX03_LANG
  1767. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign(basic_string& __str, false_type)
  1768. _NOEXCEPT_(__alloc_traits::is_always_equal::value);
  1769. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void
  1770. __move_assign(basic_string& __str, true_type)
  1771. # if _LIBCPP_STD_VER >= 17
  1772. _NOEXCEPT;
  1773. # else
  1774. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
  1775. # endif
  1776. #endif
  1777. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __str)
  1778. _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
  1779. is_nothrow_move_assignable<allocator_type>::value) {
  1780. __move_assign_alloc(
  1781. __str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
  1782. }
  1783. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __c, true_type)
  1784. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
  1785. __alloc() = std::move(__c.__alloc());
  1786. }
  1787. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string&, false_type) _NOEXCEPT {}
  1788. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s);
  1789. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s, size_type __n);
  1790. // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
  1791. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_short(const value_type* __s, size_type __n) {
  1792. size_type __old_size = size();
  1793. if (__n > __old_size)
  1794. __annotate_increase(__n - __old_size);
  1795. pointer __p =
  1796. __is_long() ? (__set_long_size(__n), __get_long_pointer()) : (__set_short_size(__n), __get_short_pointer());
  1797. traits_type::move(std::__to_address(__p), __s, __n);
  1798. traits_type::assign(__p[__n], value_type());
  1799. if (__old_size > __n)
  1800. __annotate_shrink(__old_size);
  1801. return *this;
  1802. }
  1803. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
  1804. __null_terminate_at(value_type* __p, size_type __newsz) {
  1805. size_type __old_size = size();
  1806. if (__newsz > __old_size)
  1807. __annotate_increase(__newsz - __old_size);
  1808. __set_size(__newsz);
  1809. traits_type::assign(__p[__newsz], value_type());
  1810. if (__old_size > __newsz)
  1811. __annotate_shrink(__old_size);
  1812. return *this;
  1813. }
  1814. template <class _Tp>
  1815. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __addr_in_range(const _Tp& __v) const {
  1816. return std::__is_pointer_in_range(data(), data() + size() + 1, std::addressof(__v));
  1817. }
  1818. _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI void __throw_length_error() const {
  1819. std::__throw_length_error("basic_string");
  1820. }
  1821. _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI void __throw_out_of_range() const {
  1822. std::__throw_out_of_range("basic_string");
  1823. }
  1824. friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(const basic_string&, const basic_string&);
  1825. friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(const value_type*, const basic_string&);
  1826. friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(value_type, const basic_string&);
  1827. friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(const basic_string&, const value_type*);
  1828. friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(const basic_string&, value_type);
  1829. };
  1830. // These declarations must appear before any functions are implicitly used
  1831. // so that they have the correct visibility specifier.
  1832. #define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
  1833. #ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
  1834. _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
  1835. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  1836. _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
  1837. # endif
  1838. #else
  1839. _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
  1840. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  1841. _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
  1842. # endif
  1843. #endif
  1844. #undef _LIBCPP_DECLARE
  1845. #if _LIBCPP_STD_VER >= 17
  1846. template <class _InputIterator,
  1847. class _CharT = __iter_value_type<_InputIterator>,
  1848. class _Allocator = allocator<_CharT>,
  1849. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  1850. class = enable_if_t<__is_allocator<_Allocator>::value> >
  1851. basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
  1852. -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
  1853. template <class _CharT,
  1854. class _Traits,
  1855. class _Allocator = allocator<_CharT>,
  1856. class = enable_if_t<__is_allocator<_Allocator>::value> >
  1857. explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
  1858. -> basic_string<_CharT, _Traits, _Allocator>;
  1859. template <class _CharT,
  1860. class _Traits,
  1861. class _Allocator = allocator<_CharT>,
  1862. class = enable_if_t<__is_allocator<_Allocator>::value>,
  1863. class _Sz = typename allocator_traits<_Allocator>::size_type >
  1864. basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
  1865. -> basic_string<_CharT, _Traits, _Allocator>;
  1866. #endif
  1867. #if _LIBCPP_STD_VER >= 23
  1868. template <ranges::input_range _Range,
  1869. class _Allocator = allocator<ranges::range_value_t<_Range>>,
  1870. class = enable_if_t<__is_allocator<_Allocator>::value> >
  1871. basic_string(from_range_t, _Range&&, _Allocator = _Allocator())
  1872. -> basic_string<ranges::range_value_t<_Range>, char_traits<ranges::range_value_t<_Range>>, _Allocator>;
  1873. #endif
  1874. template <class _CharT, class _Traits, class _Allocator>
  1875. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1876. basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz, size_type __reserve) {
  1877. if (__libcpp_is_constant_evaluated())
  1878. __r_.first() = __rep();
  1879. if (__reserve > max_size())
  1880. __throw_length_error();
  1881. pointer __p;
  1882. if (__fits_in_sso(__reserve)) {
  1883. __set_short_size(__sz);
  1884. __p = __get_short_pointer();
  1885. } else {
  1886. auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
  1887. __p = __allocation.ptr;
  1888. __begin_lifetime(__p, __allocation.count);
  1889. __set_long_pointer(__p);
  1890. __set_long_cap(__allocation.count);
  1891. __set_long_size(__sz);
  1892. }
  1893. traits_type::copy(std::__to_address(__p), __s, __sz);
  1894. traits_type::assign(__p[__sz], value_type());
  1895. __annotate_new(__sz);
  1896. }
  1897. template <class _CharT, class _Traits, class _Allocator>
  1898. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1899. basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz) {
  1900. if (__libcpp_is_constant_evaluated())
  1901. __r_.first() = __rep();
  1902. if (__sz > max_size())
  1903. __throw_length_error();
  1904. pointer __p;
  1905. if (__fits_in_sso(__sz)) {
  1906. __set_short_size(__sz);
  1907. __p = __get_short_pointer();
  1908. } else {
  1909. auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
  1910. __p = __allocation.ptr;
  1911. __begin_lifetime(__p, __allocation.count);
  1912. __set_long_pointer(__p);
  1913. __set_long_cap(__allocation.count);
  1914. __set_long_size(__sz);
  1915. }
  1916. traits_type::copy(std::__to_address(__p), __s, __sz);
  1917. traits_type::assign(__p[__sz], value_type());
  1918. __annotate_new(__sz);
  1919. }
  1920. template <class _CharT, class _Traits, class _Allocator>
  1921. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
  1922. basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(const value_type* __s, size_type __sz) {
  1923. if (__libcpp_is_constant_evaluated())
  1924. __r_.first() = __rep();
  1925. pointer __p;
  1926. if (__fits_in_sso(__sz)) {
  1927. __p = __get_short_pointer();
  1928. __set_short_size(__sz);
  1929. } else {
  1930. if (__sz > max_size())
  1931. __throw_length_error();
  1932. auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
  1933. __p = __allocation.ptr;
  1934. __begin_lifetime(__p, __allocation.count);
  1935. __set_long_pointer(__p);
  1936. __set_long_cap(__allocation.count);
  1937. __set_long_size(__sz);
  1938. }
  1939. traits_type::copy(std::__to_address(__p), __s, __sz + 1);
  1940. __annotate_new(__sz);
  1941. }
  1942. template <class _CharT, class _Traits, class _Allocator>
  1943. _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c) {
  1944. if (__libcpp_is_constant_evaluated())
  1945. __r_.first() = __rep();
  1946. if (__n > max_size())
  1947. __throw_length_error();
  1948. pointer __p;
  1949. if (__fits_in_sso(__n)) {
  1950. __set_short_size(__n);
  1951. __p = __get_short_pointer();
  1952. } else {
  1953. auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
  1954. __p = __allocation.ptr;
  1955. __begin_lifetime(__p, __allocation.count);
  1956. __set_long_pointer(__p);
  1957. __set_long_cap(__allocation.count);
  1958. __set_long_size(__n);
  1959. }
  1960. traits_type::assign(std::__to_address(__p), __n, __c);
  1961. traits_type::assign(__p[__n], value_type());
  1962. __annotate_new(__n);
  1963. }
  1964. template <class _CharT, class _Traits, class _Allocator>
  1965. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
  1966. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1967. basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last) {
  1968. __init_with_sentinel(std::move(__first), std::move(__last));
  1969. }
  1970. template <class _CharT, class _Traits, class _Allocator>
  1971. template <class _InputIterator, class _Sentinel>
  1972. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1973. basic_string<_CharT, _Traits, _Allocator>::__init_with_sentinel(_InputIterator __first, _Sentinel __last) {
  1974. __r_.first() = __rep();
  1975. __annotate_new(0);
  1976. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1977. try {
  1978. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1979. for (; __first != __last; ++__first)
  1980. push_back(*__first);
  1981. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1982. } catch (...) {
  1983. __annotate_delete();
  1984. if (__is_long())
  1985. __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
  1986. throw;
  1987. }
  1988. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1989. }
  1990. template <class _CharT, class _Traits, class _Allocator>
  1991. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
  1992. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1993. basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last) {
  1994. size_type __sz = static_cast<size_type>(std::distance(__first, __last));
  1995. __init_with_size(__first, __last, __sz);
  1996. }
  1997. template <class _CharT, class _Traits, class _Allocator>
  1998. template <class _InputIterator, class _Sentinel>
  1999. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2000. basic_string<_CharT, _Traits, _Allocator>::__init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz) {
  2001. if (__libcpp_is_constant_evaluated())
  2002. __r_.first() = __rep();
  2003. if (__sz > max_size())
  2004. __throw_length_error();
  2005. pointer __p;
  2006. if (__fits_in_sso(__sz)) {
  2007. __set_short_size(__sz);
  2008. __p = __get_short_pointer();
  2009. } else {
  2010. auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
  2011. __p = __allocation.ptr;
  2012. __begin_lifetime(__p, __allocation.count);
  2013. __set_long_pointer(__p);
  2014. __set_long_cap(__allocation.count);
  2015. __set_long_size(__sz);
  2016. }
  2017. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2018. try {
  2019. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2020. for (; __first != __last; ++__first, (void)++__p)
  2021. traits_type::assign(*__p, *__first);
  2022. traits_type::assign(*__p, value_type());
  2023. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2024. } catch (...) {
  2025. if (__is_long())
  2026. __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
  2027. throw;
  2028. }
  2029. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2030. __annotate_new(__sz);
  2031. }
  2032. template <class _CharT, class _Traits, class _Allocator>
  2033. _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace(
  2034. size_type __old_cap,
  2035. size_type __delta_cap,
  2036. size_type __old_sz,
  2037. size_type __n_copy,
  2038. size_type __n_del,
  2039. size_type __n_add,
  2040. const value_type* __p_new_stuff) {
  2041. size_type __ms = max_size();
  2042. if (__delta_cap > __ms - __old_cap - 1)
  2043. __throw_length_error();
  2044. pointer __old_p = __get_pointer();
  2045. size_type __cap =
  2046. __old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms - 1;
  2047. __annotate_delete();
  2048. auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
  2049. pointer __p = __allocation.ptr;
  2050. __begin_lifetime(__p, __allocation.count);
  2051. if (__n_copy != 0)
  2052. traits_type::copy(std::__to_address(__p), std::__to_address(__old_p), __n_copy);
  2053. if (__n_add != 0)
  2054. traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
  2055. size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
  2056. if (__sec_cp_sz != 0)
  2057. traits_type::copy(
  2058. std::__to_address(__p) + __n_copy + __n_add, std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
  2059. if (__old_cap + 1 != __min_cap)
  2060. __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
  2061. __set_long_pointer(__p);
  2062. __set_long_cap(__allocation.count);
  2063. __old_sz = __n_copy + __n_add + __sec_cp_sz;
  2064. __set_long_size(__old_sz);
  2065. traits_type::assign(__p[__old_sz], value_type());
  2066. __annotate_new(__old_sz);
  2067. }
  2068. // __grow_by is deprecated because it does not set the size. It may not update the size when the size is changed, and it
  2069. // may also not set the size at all when the string was short initially. This leads to unpredictable size value. It is
  2070. // not removed or changed to avoid breaking the ABI.
  2071. template <class _CharT, class _Traits, class _Allocator>
  2072. void _LIBCPP_CONSTEXPR_SINCE_CXX20
  2073. #if _LIBCPP_ABI_VERSION >= 2 // We want to use the function in the dylib in ABIv1
  2074. _LIBCPP_HIDE_FROM_ABI
  2075. #endif
  2076. _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Traits, _Allocator>::__grow_by(
  2077. size_type __old_cap,
  2078. size_type __delta_cap,
  2079. size_type __old_sz,
  2080. size_type __n_copy,
  2081. size_type __n_del,
  2082. size_type __n_add) {
  2083. size_type __ms = max_size();
  2084. if (__delta_cap > __ms - __old_cap)
  2085. __throw_length_error();
  2086. pointer __old_p = __get_pointer();
  2087. size_type __cap =
  2088. __old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms - 1;
  2089. __annotate_delete();
  2090. auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
  2091. pointer __p = __allocation.ptr;
  2092. __begin_lifetime(__p, __allocation.count);
  2093. if (__n_copy != 0)
  2094. traits_type::copy(std::__to_address(__p), std::__to_address(__old_p), __n_copy);
  2095. size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
  2096. if (__sec_cp_sz != 0)
  2097. traits_type::copy(
  2098. std::__to_address(__p) + __n_copy + __n_add, std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
  2099. if (__old_cap + 1 != __min_cap)
  2100. __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
  2101. __set_long_pointer(__p);
  2102. __set_long_cap(__allocation.count);
  2103. }
  2104. template <class _CharT, class _Traits, class _Allocator>
  2105. void _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  2106. basic_string<_CharT, _Traits, _Allocator>::__grow_by_without_replace(
  2107. size_type __old_cap,
  2108. size_type __delta_cap,
  2109. size_type __old_sz,
  2110. size_type __n_copy,
  2111. size_type __n_del,
  2112. size_type __n_add) {
  2113. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  2114. __grow_by(__old_cap, __delta_cap, __old_sz, __n_copy, __n_del, __n_add);
  2115. _LIBCPP_SUPPRESS_DEPRECATED_POP
  2116. __set_long_size(__old_sz - __n_del + __n_add);
  2117. __annotate_new(__old_sz - __n_del + __n_add);
  2118. }
  2119. // assign
  2120. template <class _CharT, class _Traits, class _Allocator>
  2121. template <bool __is_short>
  2122. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
  2123. basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
  2124. size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
  2125. if (__n < __cap) {
  2126. size_type __old_size = __is_short ? __get_short_size() : __get_long_size();
  2127. if (__n > __old_size)
  2128. __annotate_increase(__n - __old_size);
  2129. pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
  2130. __is_short ? __set_short_size(__n) : __set_long_size(__n);
  2131. traits_type::copy(std::__to_address(__p), __s, __n);
  2132. traits_type::assign(__p[__n], value_type());
  2133. if (__old_size > __n)
  2134. __annotate_shrink(__old_size);
  2135. } else {
  2136. size_type __sz = __is_short ? __get_short_size() : __get_long_size();
  2137. __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
  2138. }
  2139. return *this;
  2140. }
  2141. template <class _CharT, class _Traits, class _Allocator>
  2142. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
  2143. basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
  2144. size_type __cap = capacity();
  2145. if (__cap >= __n) {
  2146. size_type __old_size = size();
  2147. if (__n > __old_size)
  2148. __annotate_increase(__n - __old_size);
  2149. value_type* __p = std::__to_address(__get_pointer());
  2150. traits_type::move(__p, __s, __n);
  2151. return __null_terminate_at(__p, __n);
  2152. } else {
  2153. size_type __sz = size();
  2154. __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
  2155. return *this;
  2156. }
  2157. }
  2158. template <class _CharT, class _Traits, class _Allocator>
  2159. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2160. basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n) {
  2161. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::assign received nullptr");
  2162. return (__builtin_constant_p(__n) && __fits_in_sso(__n)) ? __assign_short(__s, __n) : __assign_external(__s, __n);
  2163. }
  2164. template <class _CharT, class _Traits, class _Allocator>
  2165. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2166. basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c) {
  2167. size_type __cap = capacity();
  2168. size_type __old_size = size();
  2169. if (__cap < __n) {
  2170. size_type __sz = size();
  2171. __grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
  2172. __annotate_increase(__n);
  2173. } else if (__n > __old_size)
  2174. __annotate_increase(__n - __old_size);
  2175. value_type* __p = std::__to_address(__get_pointer());
  2176. traits_type::assign(__p, __n, __c);
  2177. return __null_terminate_at(__p, __n);
  2178. }
  2179. template <class _CharT, class _Traits, class _Allocator>
  2180. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2181. basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
  2182. pointer __p;
  2183. size_type __old_size = size();
  2184. if (__old_size == 0)
  2185. __annotate_increase(1);
  2186. if (__is_long()) {
  2187. __p = __get_long_pointer();
  2188. __set_long_size(1);
  2189. } else {
  2190. __p = __get_short_pointer();
  2191. __set_short_size(1);
  2192. }
  2193. traits_type::assign(*__p, __c);
  2194. traits_type::assign(*++__p, value_type());
  2195. if (__old_size > 1)
  2196. __annotate_shrink(__old_size);
  2197. return *this;
  2198. }
  2199. template <class _CharT, class _Traits, class _Allocator>
  2200. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string<_CharT, _Traits, _Allocator>&
  2201. basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str) {
  2202. if (this != std::addressof(__str)) {
  2203. __copy_assign_alloc(__str);
  2204. if (!__is_long()) {
  2205. if (!__str.__is_long()) {
  2206. size_type __old_size = __get_short_size();
  2207. if (__get_short_size() < __str.__get_short_size())
  2208. __annotate_increase(__str.__get_short_size() - __get_short_size());
  2209. __r_.first() = __str.__r_.first();
  2210. if (__old_size > __get_short_size())
  2211. __annotate_shrink(__old_size);
  2212. } else {
  2213. return __assign_no_alias<true>(__str.data(), __str.size());
  2214. }
  2215. } else {
  2216. return __assign_no_alias<false>(__str.data(), __str.size());
  2217. }
  2218. }
  2219. return *this;
  2220. }
  2221. #ifndef _LIBCPP_CXX03_LANG
  2222. template <class _CharT, class _Traits, class _Allocator>
  2223. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2224. basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
  2225. _NOEXCEPT_(__alloc_traits::is_always_equal::value) {
  2226. if (__alloc() != __str.__alloc())
  2227. assign(__str);
  2228. else
  2229. __move_assign(__str, true_type());
  2230. }
  2231. template <class _CharT, class _Traits, class _Allocator>
  2232. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void
  2233. basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
  2234. # if _LIBCPP_STD_VER >= 17
  2235. _NOEXCEPT
  2236. # else
  2237. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  2238. # endif
  2239. {
  2240. __annotate_delete();
  2241. if (__is_long()) {
  2242. __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
  2243. # if _LIBCPP_STD_VER <= 14
  2244. if (!is_nothrow_move_assignable<allocator_type>::value) {
  2245. __set_short_size(0);
  2246. traits_type::assign(__get_short_pointer()[0], value_type());
  2247. __annotate_new(0);
  2248. }
  2249. # endif
  2250. }
  2251. size_type __str_old_size = __str.size();
  2252. bool __str_was_short = !__str.__is_long();
  2253. __move_assign_alloc(__str);
  2254. __r_.first() = __str.__r_.first();
  2255. __str.__set_short_size(0);
  2256. traits_type::assign(__str.__get_short_pointer()[0], value_type());
  2257. if (__str_was_short && this != &__str)
  2258. __str.__annotate_shrink(__str_old_size);
  2259. else
  2260. // ASan annotations: was long, so object memory is unpoisoned as new.
  2261. // Or is same as *this, and __annotate_delete() was called.
  2262. __str.__annotate_new(0);
  2263. // ASan annotations: Guard against `std::string s; s = std::move(s);`
  2264. // You can find more here: https://en.cppreference.com/w/cpp/utility/move
  2265. // Quote: "Unless otherwise specified, all standard library objects that have been moved
  2266. // from are placed in a "valid but unspecified state", meaning the object's class
  2267. // invariants hold (so functions without preconditions, such as the assignment operator,
  2268. // can be safely used on the object after it was moved from):"
  2269. // Quote: "v = std::move(v); // the value of v is unspecified"
  2270. if (!__is_long() && &__str != this)
  2271. // If it is long string, delete was never called on original __str's buffer.
  2272. __annotate_new(__get_short_size());
  2273. }
  2274. #endif
  2275. template <class _CharT, class _Traits, class _Allocator>
  2276. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
  2277. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2278. basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last) {
  2279. __assign_with_sentinel(__first, __last);
  2280. return *this;
  2281. }
  2282. template <class _CharT, class _Traits, class _Allocator>
  2283. template <class _InputIterator, class _Sentinel>
  2284. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2285. basic_string<_CharT, _Traits, _Allocator>::__assign_with_sentinel(_InputIterator __first, _Sentinel __last) {
  2286. const basic_string __temp(__init_with_sentinel_tag(), std::move(__first), std::move(__last), __alloc());
  2287. assign(__temp.data(), __temp.size());
  2288. }
  2289. template <class _CharT, class _Traits, class _Allocator>
  2290. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
  2291. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2292. basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) {
  2293. if (__string_is_trivial_iterator<_ForwardIterator>::value) {
  2294. size_type __n = static_cast<size_type>(std::distance(__first, __last));
  2295. __assign_trivial(__first, __last, __n);
  2296. } else {
  2297. __assign_with_sentinel(__first, __last);
  2298. }
  2299. return *this;
  2300. }
  2301. template <class _CharT, class _Traits, class _Allocator>
  2302. template <class _Iterator, class _Sentinel>
  2303. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
  2304. basic_string<_CharT, _Traits, _Allocator>::__assign_trivial(_Iterator __first, _Sentinel __last, size_type __n) {
  2305. _LIBCPP_ASSERT_INTERNAL(
  2306. __string_is_trivial_iterator<_Iterator>::value, "The iterator type given to `__assign_trivial` must be trivial");
  2307. size_type __old_size = size();
  2308. size_type __cap = capacity();
  2309. if (__cap < __n) {
  2310. // Unlike `append` functions, if the input range points into the string itself, there is no case that the input
  2311. // range could get invalidated by reallocation:
  2312. // 1. If the input range is a subset of the string itself, its size cannot exceed the capacity of the string,
  2313. // thus no reallocation would happen.
  2314. // 2. In the exotic case where the input range is the byte representation of the string itself, the string
  2315. // object itself stays valid even if reallocation happens.
  2316. size_type __sz = size();
  2317. __grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
  2318. __annotate_increase(__n);
  2319. } else if (__n > __old_size)
  2320. __annotate_increase(__n - __old_size);
  2321. pointer __p = __get_pointer();
  2322. for (; __first != __last; ++__p, (void)++__first)
  2323. traits_type::assign(*__p, *__first);
  2324. traits_type::assign(*__p, value_type());
  2325. __set_size(__n);
  2326. if (__n < __old_size)
  2327. __annotate_shrink(__old_size);
  2328. }
  2329. template <class _CharT, class _Traits, class _Allocator>
  2330. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2331. basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n) {
  2332. size_type __sz = __str.size();
  2333. if (__pos > __sz)
  2334. __throw_out_of_range();
  2335. return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
  2336. }
  2337. template <class _CharT, class _Traits, class _Allocator>
  2338. template <class _Tp,
  2339. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  2340. !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
  2341. int> >
  2342. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2343. basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp& __t, size_type __pos, size_type __n) {
  2344. __self_view __sv = __t;
  2345. size_type __sz = __sv.size();
  2346. if (__pos > __sz)
  2347. __throw_out_of_range();
  2348. return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
  2349. }
  2350. template <class _CharT, class _Traits, class _Allocator>
  2351. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
  2352. basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
  2353. return __assign_external(__s, traits_type::length(__s));
  2354. }
  2355. template <class _CharT, class _Traits, class _Allocator>
  2356. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2357. basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
  2358. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
  2359. return __builtin_constant_p(*__s)
  2360. ? (__fits_in_sso(traits_type::length(__s)) ? __assign_short(__s, traits_type::length(__s))
  2361. : __assign_external(__s, traits_type::length(__s)))
  2362. : __assign_external(__s);
  2363. }
  2364. // append
  2365. template <class _CharT, class _Traits, class _Allocator>
  2366. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2367. basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n) {
  2368. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::append received nullptr");
  2369. size_type __cap = capacity();
  2370. size_type __sz = size();
  2371. if (__cap - __sz >= __n) {
  2372. if (__n) {
  2373. __annotate_increase(__n);
  2374. value_type* __p = std::__to_address(__get_pointer());
  2375. traits_type::copy(__p + __sz, __s, __n);
  2376. __sz += __n;
  2377. __set_size(__sz);
  2378. traits_type::assign(__p[__sz], value_type());
  2379. }
  2380. } else
  2381. __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
  2382. return *this;
  2383. }
  2384. template <class _CharT, class _Traits, class _Allocator>
  2385. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2386. basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c) {
  2387. if (__n) {
  2388. size_type __cap = capacity();
  2389. size_type __sz = size();
  2390. if (__cap - __sz < __n)
  2391. __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);
  2392. __annotate_increase(__n);
  2393. pointer __p = __get_pointer();
  2394. traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
  2395. __sz += __n;
  2396. __set_size(__sz);
  2397. traits_type::assign(__p[__sz], value_type());
  2398. }
  2399. return *this;
  2400. }
  2401. template <class _CharT, class _Traits, class _Allocator>
  2402. _LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
  2403. basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n) {
  2404. if (__n) {
  2405. size_type __cap = capacity();
  2406. size_type __sz = size();
  2407. if (__cap - __sz < __n)
  2408. __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);
  2409. __annotate_increase(__n);
  2410. pointer __p = __get_pointer();
  2411. __sz += __n;
  2412. __set_size(__sz);
  2413. traits_type::assign(__p[__sz], value_type());
  2414. }
  2415. }
  2416. template <class _CharT, class _Traits, class _Allocator>
  2417. _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c) {
  2418. bool __is_short = !__is_long();
  2419. size_type __cap;
  2420. size_type __sz;
  2421. if (__is_short) {
  2422. __cap = __min_cap - 1;
  2423. __sz = __get_short_size();
  2424. } else {
  2425. __cap = __get_long_cap() - 1;
  2426. __sz = __get_long_size();
  2427. }
  2428. if (__sz == __cap) {
  2429. __grow_by_without_replace(__cap, 1, __sz, __sz, 0);
  2430. __annotate_increase(1);
  2431. __is_short = false; // the string is always long after __grow_by
  2432. } else
  2433. __annotate_increase(1);
  2434. pointer __p = __get_pointer();
  2435. if (__is_short) {
  2436. __p = __get_short_pointer() + __sz;
  2437. __set_short_size(__sz + 1);
  2438. } else {
  2439. __p = __get_long_pointer() + __sz;
  2440. __set_long_size(__sz + 1);
  2441. }
  2442. traits_type::assign(*__p, __c);
  2443. traits_type::assign(*++__p, value_type());
  2444. }
  2445. template <class _CharT, class _Traits, class _Allocator>
  2446. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
  2447. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2448. basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last) {
  2449. size_type __sz = size();
  2450. size_type __cap = capacity();
  2451. size_type __n = static_cast<size_type>(std::distance(__first, __last));
  2452. if (__n) {
  2453. if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first)) {
  2454. if (__cap - __sz < __n)
  2455. __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);
  2456. __annotate_increase(__n);
  2457. pointer __p = __get_pointer() + __sz;
  2458. for (; __first != __last; ++__p, (void)++__first)
  2459. traits_type::assign(*__p, *__first);
  2460. traits_type::assign(*__p, value_type());
  2461. __set_size(__sz + __n);
  2462. } else {
  2463. const basic_string __temp(__first, __last, __alloc());
  2464. append(__temp.data(), __temp.size());
  2465. }
  2466. }
  2467. return *this;
  2468. }
  2469. template <class _CharT, class _Traits, class _Allocator>
  2470. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2471. basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n) {
  2472. size_type __sz = __str.size();
  2473. if (__pos > __sz)
  2474. __throw_out_of_range();
  2475. return append(__str.data() + __pos, std::min(__n, __sz - __pos));
  2476. }
  2477. template <class _CharT, class _Traits, class _Allocator>
  2478. template <class _Tp,
  2479. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  2480. !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
  2481. int> >
  2482. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2483. basic_string<_CharT, _Traits, _Allocator>::append(const _Tp& __t, size_type __pos, size_type __n) {
  2484. __self_view __sv = __t;
  2485. size_type __sz = __sv.size();
  2486. if (__pos > __sz)
  2487. __throw_out_of_range();
  2488. return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
  2489. }
  2490. template <class _CharT, class _Traits, class _Allocator>
  2491. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2492. basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s) {
  2493. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::append received nullptr");
  2494. return append(__s, traits_type::length(__s));
  2495. }
  2496. // insert
  2497. template <class _CharT, class _Traits, class _Allocator>
  2498. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2499. basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n) {
  2500. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::insert received nullptr");
  2501. size_type __sz = size();
  2502. if (__pos > __sz)
  2503. __throw_out_of_range();
  2504. size_type __cap = capacity();
  2505. if (__cap - __sz >= __n) {
  2506. if (__n) {
  2507. __annotate_increase(__n);
  2508. value_type* __p = std::__to_address(__get_pointer());
  2509. size_type __n_move = __sz - __pos;
  2510. if (__n_move != 0) {
  2511. if (std::__is_pointer_in_range(__p + __pos, __p + __sz, __s))
  2512. __s += __n;
  2513. traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
  2514. }
  2515. traits_type::move(__p + __pos, __s, __n);
  2516. __sz += __n;
  2517. __set_size(__sz);
  2518. traits_type::assign(__p[__sz], value_type());
  2519. }
  2520. } else
  2521. __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
  2522. return *this;
  2523. }
  2524. template <class _CharT, class _Traits, class _Allocator>
  2525. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2526. basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c) {
  2527. size_type __sz = size();
  2528. if (__pos > __sz)
  2529. __throw_out_of_range();
  2530. if (__n) {
  2531. size_type __cap = capacity();
  2532. value_type* __p;
  2533. if (__cap - __sz >= __n) {
  2534. __annotate_increase(__n);
  2535. __p = std::__to_address(__get_pointer());
  2536. size_type __n_move = __sz - __pos;
  2537. if (__n_move != 0)
  2538. traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
  2539. } else {
  2540. __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
  2541. __p = std::__to_address(__get_long_pointer());
  2542. }
  2543. traits_type::assign(__p + __pos, __n, __c);
  2544. __sz += __n;
  2545. __set_size(__sz);
  2546. traits_type::assign(__p[__sz], value_type());
  2547. }
  2548. return *this;
  2549. }
  2550. template <class _CharT, class _Traits, class _Allocator>
  2551. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
  2552. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2553. basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last) {
  2554. const basic_string __temp(__first, __last, __alloc());
  2555. return insert(__pos, __temp.data(), __temp.data() + __temp.size());
  2556. }
  2557. template <class _CharT, class _Traits, class _Allocator>
  2558. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
  2559. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2560. basic_string<_CharT, _Traits, _Allocator>::insert(
  2561. const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last) {
  2562. auto __n = static_cast<size_type>(std::distance(__first, __last));
  2563. return __insert_with_size(__pos, __first, __last, __n);
  2564. }
  2565. template <class _CharT, class _Traits, class _Allocator>
  2566. template <class _Iterator, class _Sentinel>
  2567. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2568. basic_string<_CharT, _Traits, _Allocator>::__insert_with_size(
  2569. const_iterator __pos, _Iterator __first, _Sentinel __last, size_type __n) {
  2570. size_type __ip = static_cast<size_type>(__pos - begin());
  2571. if (__n == 0)
  2572. return begin() + __ip;
  2573. if (__string_is_trivial_iterator<_Iterator>::value && !__addr_in_range(*__first)) {
  2574. return __insert_from_safe_copy(__n, __ip, __first, __last);
  2575. } else {
  2576. const basic_string __temp(__init_with_sentinel_tag(), __first, __last, __alloc());
  2577. return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
  2578. }
  2579. }
  2580. template <class _CharT, class _Traits, class _Allocator>
  2581. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2582. basic_string<_CharT, _Traits, _Allocator>::insert(
  2583. size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n) {
  2584. size_type __str_sz = __str.size();
  2585. if (__pos2 > __str_sz)
  2586. __throw_out_of_range();
  2587. return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
  2588. }
  2589. template <class _CharT, class _Traits, class _Allocator>
  2590. template <class _Tp,
  2591. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  2592. !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
  2593. int> >
  2594. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2595. basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n) {
  2596. __self_view __sv = __t;
  2597. size_type __str_sz = __sv.size();
  2598. if (__pos2 > __str_sz)
  2599. __throw_out_of_range();
  2600. return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
  2601. }
  2602. template <class _CharT, class _Traits, class _Allocator>
  2603. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2604. basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s) {
  2605. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::insert received nullptr");
  2606. return insert(__pos, __s, traits_type::length(__s));
  2607. }
  2608. template <class _CharT, class _Traits, class _Allocator>
  2609. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2610. basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c) {
  2611. size_type __ip = static_cast<size_type>(__pos - begin());
  2612. size_type __sz = size();
  2613. size_type __cap = capacity();
  2614. value_type* __p;
  2615. if (__cap == __sz) {
  2616. __grow_by_without_replace(__cap, 1, __sz, __ip, 0, 1);
  2617. __p = std::__to_address(__get_long_pointer());
  2618. } else {
  2619. __annotate_increase(1);
  2620. __p = std::__to_address(__get_pointer());
  2621. size_type __n_move = __sz - __ip;
  2622. if (__n_move != 0)
  2623. traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
  2624. }
  2625. traits_type::assign(__p[__ip], __c);
  2626. traits_type::assign(__p[++__sz], value_type());
  2627. __set_size(__sz);
  2628. return begin() + static_cast<difference_type>(__ip);
  2629. }
  2630. // replace
  2631. template <class _CharT, class _Traits, class _Allocator>
  2632. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2633. basic_string<_CharT, _Traits, _Allocator>::replace(
  2634. size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
  2635. _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
  2636. _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
  2637. size_type __sz = size();
  2638. if (__pos > __sz)
  2639. __throw_out_of_range();
  2640. __n1 = std::min(__n1, __sz - __pos);
  2641. size_type __cap = capacity();
  2642. if (__cap - __sz + __n1 >= __n2) {
  2643. value_type* __p = std::__to_address(__get_pointer());
  2644. if (__n1 != __n2) {
  2645. if (__n2 > __n1)
  2646. __annotate_increase(__n2 - __n1);
  2647. size_type __n_move = __sz - __pos - __n1;
  2648. if (__n_move != 0) {
  2649. if (__n1 > __n2) {
  2650. traits_type::move(__p + __pos, __s, __n2);
  2651. traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
  2652. return __null_terminate_at(__p, __sz + (__n2 - __n1));
  2653. }
  2654. if (std::__is_pointer_in_range(__p + __pos + 1, __p + __sz, __s)) {
  2655. if (__p + __pos + __n1 <= __s)
  2656. __s += __n2 - __n1;
  2657. else // __p + __pos < __s < __p + __pos + __n1
  2658. {
  2659. traits_type::move(__p + __pos, __s, __n1);
  2660. __pos += __n1;
  2661. __s += __n2;
  2662. __n2 -= __n1;
  2663. __n1 = 0;
  2664. }
  2665. }
  2666. traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
  2667. }
  2668. }
  2669. traits_type::move(__p + __pos, __s, __n2);
  2670. return __null_terminate_at(__p, __sz + (__n2 - __n1));
  2671. } else
  2672. __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
  2673. return *this;
  2674. }
  2675. template <class _CharT, class _Traits, class _Allocator>
  2676. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2677. basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c) {
  2678. size_type __sz = size();
  2679. if (__pos > __sz)
  2680. __throw_out_of_range();
  2681. __n1 = std::min(__n1, __sz - __pos);
  2682. size_type __cap = capacity();
  2683. value_type* __p;
  2684. if (__cap - __sz + __n1 >= __n2) {
  2685. __p = std::__to_address(__get_pointer());
  2686. if (__n1 != __n2) {
  2687. if (__n2 > __n1)
  2688. __annotate_increase(__n2 - __n1);
  2689. size_type __n_move = __sz - __pos - __n1;
  2690. if (__n_move != 0)
  2691. traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
  2692. }
  2693. } else {
  2694. __grow_by_without_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
  2695. __p = std::__to_address(__get_long_pointer());
  2696. }
  2697. traits_type::assign(__p + __pos, __n2, __c);
  2698. return __null_terminate_at(__p, __sz - (__n1 - __n2));
  2699. }
  2700. template <class _CharT, class _Traits, class _Allocator>
  2701. template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> >
  2702. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2703. basic_string<_CharT, _Traits, _Allocator>::replace(
  2704. const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2) {
  2705. const basic_string __temp(__j1, __j2, __alloc());
  2706. return replace(__i1, __i2, __temp);
  2707. }
  2708. template <class _CharT, class _Traits, class _Allocator>
  2709. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2710. basic_string<_CharT, _Traits, _Allocator>::replace(
  2711. size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) {
  2712. size_type __str_sz = __str.size();
  2713. if (__pos2 > __str_sz)
  2714. __throw_out_of_range();
  2715. return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
  2716. }
  2717. template <class _CharT, class _Traits, class _Allocator>
  2718. template <class _Tp,
  2719. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  2720. !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
  2721. int> >
  2722. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2723. basic_string<_CharT, _Traits, _Allocator>::replace(
  2724. size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2) {
  2725. __self_view __sv = __t;
  2726. size_type __str_sz = __sv.size();
  2727. if (__pos2 > __str_sz)
  2728. __throw_out_of_range();
  2729. return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
  2730. }
  2731. template <class _CharT, class _Traits, class _Allocator>
  2732. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2733. basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s) {
  2734. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::replace received nullptr");
  2735. return replace(__pos, __n1, __s, traits_type::length(__s));
  2736. }
  2737. // erase
  2738. // 'externally instantiated' erase() implementation, called when __n != npos.
  2739. // Does not check __pos against size()
  2740. template <class _CharT, class _Traits, class _Allocator>
  2741. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
  2742. basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(size_type __pos, size_type __n) {
  2743. if (__n) {
  2744. size_type __sz = size();
  2745. value_type* __p = std::__to_address(__get_pointer());
  2746. __n = std::min(__n, __sz - __pos);
  2747. size_type __n_move = __sz - __pos - __n;
  2748. if (__n_move != 0)
  2749. traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
  2750. __null_terminate_at(__p, __sz - __n);
  2751. }
  2752. }
  2753. template <class _CharT, class _Traits, class _Allocator>
  2754. _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
  2755. basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n) {
  2756. if (__pos > size())
  2757. __throw_out_of_range();
  2758. if (__n == npos) {
  2759. __erase_to_end(__pos);
  2760. } else {
  2761. __erase_external_with_move(__pos, __n);
  2762. }
  2763. return *this;
  2764. }
  2765. template <class _CharT, class _Traits, class _Allocator>
  2766. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2767. basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos) {
  2768. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  2769. __pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
  2770. iterator __b = begin();
  2771. size_type __r = static_cast<size_type>(__pos - __b);
  2772. erase(__r, 1);
  2773. return __b + static_cast<difference_type>(__r);
  2774. }
  2775. template <class _CharT, class _Traits, class _Allocator>
  2776. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2777. basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last) {
  2778. _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "string::erase(first, last) called with invalid range");
  2779. iterator __b = begin();
  2780. size_type __r = static_cast<size_type>(__first - __b);
  2781. erase(__r, static_cast<size_type>(__last - __first));
  2782. return __b + static_cast<difference_type>(__r);
  2783. }
  2784. template <class _CharT, class _Traits, class _Allocator>
  2785. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pop_back() {
  2786. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::pop_back(): string is already empty");
  2787. __erase_to_end(size() - 1);
  2788. }
  2789. template <class _CharT, class _Traits, class _Allocator>
  2790. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
  2791. size_type __old_size = size();
  2792. if (__is_long()) {
  2793. traits_type::assign(*__get_long_pointer(), value_type());
  2794. __set_long_size(0);
  2795. } else {
  2796. traits_type::assign(*__get_short_pointer(), value_type());
  2797. __set_short_size(0);
  2798. }
  2799. __annotate_shrink(__old_size);
  2800. }
  2801. template <class _CharT, class _Traits, class _Allocator>
  2802. _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c) {
  2803. size_type __sz = size();
  2804. if (__n > __sz)
  2805. append(__n - __sz, __c);
  2806. else
  2807. __erase_to_end(__n);
  2808. }
  2809. template <class _CharT, class _Traits, class _Allocator>
  2810. _LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
  2811. basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n) {
  2812. size_type __sz = size();
  2813. if (__n > __sz) {
  2814. __append_default_init(__n - __sz);
  2815. } else
  2816. __erase_to_end(__n);
  2817. }
  2818. template <class _CharT, class _Traits, class _Allocator>
  2819. _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity) {
  2820. if (__requested_capacity > max_size())
  2821. __throw_length_error();
  2822. // Make sure reserve(n) never shrinks. This is technically only required in C++20
  2823. // and later (since P0966R1), however we provide consistent behavior in all Standard
  2824. // modes because this function is instantiated in the shared library.
  2825. if (__requested_capacity <= capacity())
  2826. return;
  2827. size_type __target_capacity = std::max(__requested_capacity, size());
  2828. __target_capacity = __recommend(__target_capacity);
  2829. if (__target_capacity == capacity())
  2830. return;
  2831. __shrink_or_extend(__target_capacity);
  2832. }
  2833. template <class _CharT, class _Traits, class _Allocator>
  2834. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT {
  2835. size_type __target_capacity = __recommend(size());
  2836. if (__target_capacity == capacity())
  2837. return;
  2838. __shrink_or_extend(__target_capacity);
  2839. }
  2840. template <class _CharT, class _Traits, class _Allocator>
  2841. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2842. basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity) {
  2843. __annotate_delete();
  2844. size_type __cap = capacity();
  2845. size_type __sz = size();
  2846. pointer __new_data, __p;
  2847. bool __was_long, __now_long;
  2848. if (__fits_in_sso(__target_capacity)) {
  2849. __was_long = true;
  2850. __now_long = false;
  2851. __new_data = __get_short_pointer();
  2852. __p = __get_long_pointer();
  2853. } else {
  2854. if (__target_capacity > __cap) {
  2855. auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
  2856. __new_data = __allocation.ptr;
  2857. __target_capacity = __allocation.count - 1;
  2858. } else {
  2859. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2860. try {
  2861. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2862. auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
  2863. __new_data = __allocation.ptr;
  2864. __target_capacity = __allocation.count - 1;
  2865. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2866. } catch (...) {
  2867. return;
  2868. }
  2869. #else // _LIBCPP_HAS_NO_EXCEPTIONS
  2870. if (__new_data == nullptr)
  2871. return;
  2872. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2873. }
  2874. __begin_lifetime(__new_data, __target_capacity + 1);
  2875. __now_long = true;
  2876. __was_long = __is_long();
  2877. __p = __get_pointer();
  2878. }
  2879. traits_type::copy(std::__to_address(__new_data), std::__to_address(__p), size() + 1);
  2880. if (__was_long)
  2881. __alloc_traits::deallocate(__alloc(), __p, __cap + 1);
  2882. if (__now_long) {
  2883. __set_long_cap(__target_capacity + 1);
  2884. __set_long_size(__sz);
  2885. __set_long_pointer(__new_data);
  2886. } else
  2887. __set_short_size(__sz);
  2888. __annotate_new(__sz);
  2889. }
  2890. template <class _CharT, class _Traits, class _Allocator>
  2891. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::const_reference
  2892. basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const {
  2893. if (__n >= size())
  2894. __throw_out_of_range();
  2895. return (*this)[__n];
  2896. }
  2897. template <class _CharT, class _Traits, class _Allocator>
  2898. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::reference
  2899. basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) {
  2900. if (__n >= size())
  2901. __throw_out_of_range();
  2902. return (*this)[__n];
  2903. }
  2904. template <class _CharT, class _Traits, class _Allocator>
  2905. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2906. basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const {
  2907. size_type __sz = size();
  2908. if (__pos > __sz)
  2909. __throw_out_of_range();
  2910. size_type __rlen = std::min(__n, __sz - __pos);
  2911. traits_type::copy(__s, data() + __pos, __rlen);
  2912. return __rlen;
  2913. }
  2914. template <class _CharT, class _Traits, class _Allocator>
  2915. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
  2916. #if _LIBCPP_STD_VER >= 14
  2917. _NOEXCEPT
  2918. #else
  2919. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<allocator_type>::value)
  2920. #endif
  2921. {
  2922. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  2923. __alloc_traits::propagate_on_container_swap::value || __alloc_traits::is_always_equal::value ||
  2924. __alloc() == __str.__alloc(),
  2925. "swapping non-equal allocators");
  2926. if (!__is_long())
  2927. __annotate_delete();
  2928. if (this != &__str && !__str.__is_long())
  2929. __str.__annotate_delete();
  2930. std::swap(__r_.first(), __str.__r_.first());
  2931. std::__swap_allocator(__alloc(), __str.__alloc());
  2932. if (!__is_long())
  2933. __annotate_new(__get_short_size());
  2934. if (this != &__str && !__str.__is_long())
  2935. __str.__annotate_new(__str.__get_short_size());
  2936. }
  2937. // find
  2938. template <class _Traits>
  2939. struct _LIBCPP_HIDDEN __traits_eq {
  2940. typedef typename _Traits::char_type char_type;
  2941. _LIBCPP_HIDE_FROM_ABI bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT {
  2942. return _Traits::eq(__x, __y);
  2943. }
  2944. };
  2945. template <class _CharT, class _Traits, class _Allocator>
  2946. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2947. basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  2948. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find(): received nullptr");
  2949. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  2950. }
  2951. template <class _CharT, class _Traits, class _Allocator>
  2952. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2953. basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str, size_type __pos) const _NOEXCEPT {
  2954. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __str.data(), __pos, __str.size());
  2955. }
  2956. template <class _CharT, class _Traits, class _Allocator>
  2957. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
  2958. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2959. basic_string<_CharT, _Traits, _Allocator>::find(const _Tp& __t, size_type __pos) const _NOEXCEPT {
  2960. __self_view __sv = __t;
  2961. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __sv.data(), __pos, __sv.size());
  2962. }
  2963. template <class _CharT, class _Traits, class _Allocator>
  2964. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2965. basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s, size_type __pos) const _NOEXCEPT {
  2966. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find(): received nullptr");
  2967. return std::__str_find<value_type, size_type, traits_type, npos>(
  2968. data(), size(), __s, __pos, traits_type::length(__s));
  2969. }
  2970. template <class _CharT, class _Traits, class _Allocator>
  2971. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2972. basic_string<_CharT, _Traits, _Allocator>::find(value_type __c, size_type __pos) const _NOEXCEPT {
  2973. return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  2974. }
  2975. // rfind
  2976. template <class _CharT, class _Traits, class _Allocator>
  2977. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2978. basic_string<_CharT, _Traits, _Allocator>::rfind(
  2979. const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  2980. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
  2981. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  2982. }
  2983. template <class _CharT, class _Traits, class _Allocator>
  2984. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2985. basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str, size_type __pos) const _NOEXCEPT {
  2986. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __str.data(), __pos, __str.size());
  2987. }
  2988. template <class _CharT, class _Traits, class _Allocator>
  2989. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
  2990. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2991. basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t, size_type __pos) const _NOEXCEPT {
  2992. __self_view __sv = __t;
  2993. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __sv.data(), __pos, __sv.size());
  2994. }
  2995. template <class _CharT, class _Traits, class _Allocator>
  2996. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2997. basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s, size_type __pos) const _NOEXCEPT {
  2998. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::rfind(): received nullptr");
  2999. return std::__str_rfind<value_type, size_type, traits_type, npos>(
  3000. data(), size(), __s, __pos, traits_type::length(__s));
  3001. }
  3002. template <class _CharT, class _Traits, class _Allocator>
  3003. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3004. basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c, size_type __pos) const _NOEXCEPT {
  3005. return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  3006. }
  3007. // find_first_of
  3008. template <class _CharT, class _Traits, class _Allocator>
  3009. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3010. basic_string<_CharT, _Traits, _Allocator>::find_first_of(
  3011. const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  3012. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
  3013. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  3014. }
  3015. template <class _CharT, class _Traits, class _Allocator>
  3016. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3017. basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str, size_type __pos) const _NOEXCEPT {
  3018. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
  3019. data(), size(), __str.data(), __pos, __str.size());
  3020. }
  3021. template <class _CharT, class _Traits, class _Allocator>
  3022. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
  3023. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3024. basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t, size_type __pos) const _NOEXCEPT {
  3025. __self_view __sv = __t;
  3026. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
  3027. data(), size(), __sv.data(), __pos, __sv.size());
  3028. }
  3029. template <class _CharT, class _Traits, class _Allocator>
  3030. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3031. basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s, size_type __pos) const _NOEXCEPT {
  3032. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_first_of(): received nullptr");
  3033. return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
  3034. data(), size(), __s, __pos, traits_type::length(__s));
  3035. }
  3036. template <class _CharT, class _Traits, class _Allocator>
  3037. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3038. basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c, size_type __pos) const _NOEXCEPT {
  3039. return find(__c, __pos);
  3040. }
  3041. // find_last_of
  3042. template <class _CharT, class _Traits, class _Allocator>
  3043. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3044. basic_string<_CharT, _Traits, _Allocator>::find_last_of(
  3045. const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  3046. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
  3047. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  3048. }
  3049. template <class _CharT, class _Traits, class _Allocator>
  3050. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3051. basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str, size_type __pos) const _NOEXCEPT {
  3052. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
  3053. data(), size(), __str.data(), __pos, __str.size());
  3054. }
  3055. template <class _CharT, class _Traits, class _Allocator>
  3056. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
  3057. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3058. basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t, size_type __pos) const _NOEXCEPT {
  3059. __self_view __sv = __t;
  3060. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
  3061. data(), size(), __sv.data(), __pos, __sv.size());
  3062. }
  3063. template <class _CharT, class _Traits, class _Allocator>
  3064. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3065. basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s, size_type __pos) const _NOEXCEPT {
  3066. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_last_of(): received nullptr");
  3067. return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
  3068. data(), size(), __s, __pos, traits_type::length(__s));
  3069. }
  3070. template <class _CharT, class _Traits, class _Allocator>
  3071. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3072. basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c, size_type __pos) const _NOEXCEPT {
  3073. return rfind(__c, __pos);
  3074. }
  3075. // find_first_not_of
  3076. template <class _CharT, class _Traits, class _Allocator>
  3077. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3078. basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(
  3079. const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  3080. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
  3081. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  3082. }
  3083. template <class _CharT, class _Traits, class _Allocator>
  3084. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3085. basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(
  3086. const basic_string& __str, size_type __pos) const _NOEXCEPT {
  3087. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
  3088. data(), size(), __str.data(), __pos, __str.size());
  3089. }
  3090. template <class _CharT, class _Traits, class _Allocator>
  3091. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
  3092. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3093. basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t, size_type __pos) const _NOEXCEPT {
  3094. __self_view __sv = __t;
  3095. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
  3096. data(), size(), __sv.data(), __pos, __sv.size());
  3097. }
  3098. template <class _CharT, class _Traits, class _Allocator>
  3099. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3100. basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s, size_type __pos) const _NOEXCEPT {
  3101. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_first_not_of(): received nullptr");
  3102. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
  3103. data(), size(), __s, __pos, traits_type::length(__s));
  3104. }
  3105. template <class _CharT, class _Traits, class _Allocator>
  3106. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3107. basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c, size_type __pos) const _NOEXCEPT {
  3108. return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  3109. }
  3110. // find_last_not_of
  3111. template <class _CharT, class _Traits, class _Allocator>
  3112. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3113. basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(
  3114. const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
  3115. _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
  3116. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
  3117. }
  3118. template <class _CharT, class _Traits, class _Allocator>
  3119. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3120. basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(
  3121. const basic_string& __str, size_type __pos) const _NOEXCEPT {
  3122. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
  3123. data(), size(), __str.data(), __pos, __str.size());
  3124. }
  3125. template <class _CharT, class _Traits, class _Allocator>
  3126. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
  3127. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3128. basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t, size_type __pos) const _NOEXCEPT {
  3129. __self_view __sv = __t;
  3130. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
  3131. data(), size(), __sv.data(), __pos, __sv.size());
  3132. }
  3133. template <class _CharT, class _Traits, class _Allocator>
  3134. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3135. basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s, size_type __pos) const _NOEXCEPT {
  3136. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_last_not_of(): received nullptr");
  3137. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
  3138. data(), size(), __s, __pos, traits_type::length(__s));
  3139. }
  3140. template <class _CharT, class _Traits, class _Allocator>
  3141. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3142. basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c, size_type __pos) const _NOEXCEPT {
  3143. return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
  3144. }
  3145. // compare
  3146. template <class _CharT, class _Traits, class _Allocator>
  3147. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
  3148. _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT {
  3149. __self_view __sv = __t;
  3150. size_t __lhs_sz = size();
  3151. size_t __rhs_sz = __sv.size();
  3152. int __result = traits_type::compare(data(), __sv.data(), std::min(__lhs_sz, __rhs_sz));
  3153. if (__result != 0)
  3154. return __result;
  3155. if (__lhs_sz < __rhs_sz)
  3156. return -1;
  3157. if (__lhs_sz > __rhs_sz)
  3158. return 1;
  3159. return 0;
  3160. }
  3161. template <class _CharT, class _Traits, class _Allocator>
  3162. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  3163. basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT {
  3164. return compare(__self_view(__str));
  3165. }
  3166. template <class _CharT, class _Traits, class _Allocator>
  3167. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(
  3168. size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const {
  3169. _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
  3170. size_type __sz = size();
  3171. if (__pos1 > __sz || __n2 == npos)
  3172. __throw_out_of_range();
  3173. size_type __rlen = std::min(__n1, __sz - __pos1);
  3174. int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
  3175. if (__r == 0) {
  3176. if (__rlen < __n2)
  3177. __r = -1;
  3178. else if (__rlen > __n2)
  3179. __r = 1;
  3180. }
  3181. return __r;
  3182. }
  3183. template <class _CharT, class _Traits, class _Allocator>
  3184. template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
  3185. _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  3186. basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const _Tp& __t) const {
  3187. __self_view __sv = __t;
  3188. return compare(__pos1, __n1, __sv.data(), __sv.size());
  3189. }
  3190. template <class _CharT, class _Traits, class _Allocator>
  3191. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  3192. basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str) const {
  3193. return compare(__pos1, __n1, __str.data(), __str.size());
  3194. }
  3195. template <class _CharT, class _Traits, class _Allocator>
  3196. template <class _Tp,
  3197. __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
  3198. !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
  3199. int> >
  3200. _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(
  3201. size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2) const {
  3202. __self_view __sv = __t;
  3203. return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  3204. }
  3205. template <class _CharT, class _Traits, class _Allocator>
  3206. _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(
  3207. size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) const {
  3208. return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
  3209. }
  3210. template <class _CharT, class _Traits, class _Allocator>
  3211. _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  3212. basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT {
  3213. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::compare(): received nullptr");
  3214. return compare(0, npos, __s, traits_type::length(__s));
  3215. }
  3216. template <class _CharT, class _Traits, class _Allocator>
  3217. _LIBCPP_CONSTEXPR_SINCE_CXX20 int
  3218. basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const value_type* __s) const {
  3219. _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::compare(): received nullptr");
  3220. return compare(__pos1, __n1, __s, traits_type::length(__s));
  3221. }
  3222. // __invariants
  3223. template <class _CharT, class _Traits, class _Allocator>
  3224. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 bool basic_string<_CharT, _Traits, _Allocator>::__invariants() const {
  3225. if (size() > capacity())
  3226. return false;
  3227. if (capacity() < __min_cap - 1)
  3228. return false;
  3229. if (data() == nullptr)
  3230. return false;
  3231. if (!_Traits::eq(data()[size()], value_type()))
  3232. return false;
  3233. return true;
  3234. }
  3235. // __clear_and_shrink
  3236. template <class _CharT, class _Traits, class _Allocator>
  3237. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT {
  3238. clear();
  3239. if (__is_long()) {
  3240. __annotate_delete();
  3241. __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
  3242. __r_.first() = __rep();
  3243. }
  3244. }
  3245. // operator==
  3246. template <class _CharT, class _Traits, class _Allocator>
  3247. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
  3248. operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3249. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3250. #if _LIBCPP_STD_VER >= 20
  3251. return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
  3252. #else
  3253. size_t __lhs_sz = __lhs.size();
  3254. return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs_sz) == 0;
  3255. #endif
  3256. }
  3257. template <class _Allocator>
  3258. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
  3259. operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
  3260. const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT {
  3261. size_t __lhs_sz = __lhs.size();
  3262. if (__lhs_sz != __rhs.size())
  3263. return false;
  3264. const char* __lp = __lhs.data();
  3265. const char* __rp = __rhs.data();
  3266. if (__lhs.__is_long())
  3267. return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
  3268. for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
  3269. if (*__lp != *__rp)
  3270. return false;
  3271. return true;
  3272. }
  3273. #if _LIBCPP_STD_VER <= 17
  3274. template <class _CharT, class _Traits, class _Allocator>
  3275. inline _LIBCPP_HIDE_FROM_ABI bool
  3276. operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3277. typedef basic_string<_CharT, _Traits, _Allocator> _String;
  3278. _LIBCPP_ASSERT_NON_NULL(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
  3279. size_t __lhs_len = _Traits::length(__lhs);
  3280. if (__lhs_len != __rhs.size())
  3281. return false;
  3282. return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
  3283. }
  3284. #endif // _LIBCPP_STD_VER <= 17
  3285. template <class _CharT, class _Traits, class _Allocator>
  3286. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
  3287. operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
  3288. #if _LIBCPP_STD_VER >= 20
  3289. return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
  3290. #else
  3291. typedef basic_string<_CharT, _Traits, _Allocator> _String;
  3292. _LIBCPP_ASSERT_NON_NULL(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
  3293. size_t __rhs_len = _Traits::length(__rhs);
  3294. if (__rhs_len != __lhs.size())
  3295. return false;
  3296. return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
  3297. #endif
  3298. }
  3299. #if _LIBCPP_STD_VER >= 20
  3300. template <class _CharT, class _Traits, class _Allocator>
  3301. _LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3302. const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
  3303. return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
  3304. }
  3305. template <class _CharT, class _Traits, class _Allocator>
  3306. _LIBCPP_HIDE_FROM_ABI constexpr auto
  3307. operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
  3308. return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
  3309. }
  3310. #else // _LIBCPP_STD_VER >= 20
  3311. template <class _CharT, class _Traits, class _Allocator>
  3312. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3313. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3314. return !(__lhs == __rhs);
  3315. }
  3316. template <class _CharT, class _Traits, class _Allocator>
  3317. inline _LIBCPP_HIDE_FROM_ABI bool
  3318. operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3319. return !(__lhs == __rhs);
  3320. }
  3321. template <class _CharT, class _Traits, class _Allocator>
  3322. inline _LIBCPP_HIDE_FROM_ABI bool
  3323. operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
  3324. return !(__lhs == __rhs);
  3325. }
  3326. // operator<
  3327. template <class _CharT, class _Traits, class _Allocator>
  3328. inline _LIBCPP_HIDE_FROM_ABI bool operator<(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3329. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3330. return __lhs.compare(__rhs) < 0;
  3331. }
  3332. template <class _CharT, class _Traits, class _Allocator>
  3333. inline _LIBCPP_HIDE_FROM_ABI bool
  3334. operator<(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
  3335. return __lhs.compare(__rhs) < 0;
  3336. }
  3337. template <class _CharT, class _Traits, class _Allocator>
  3338. inline _LIBCPP_HIDE_FROM_ABI bool
  3339. operator<(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3340. return __rhs.compare(__lhs) > 0;
  3341. }
  3342. // operator>
  3343. template <class _CharT, class _Traits, class _Allocator>
  3344. inline _LIBCPP_HIDE_FROM_ABI bool operator>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3345. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3346. return __rhs < __lhs;
  3347. }
  3348. template <class _CharT, class _Traits, class _Allocator>
  3349. inline _LIBCPP_HIDE_FROM_ABI bool
  3350. operator>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
  3351. return __rhs < __lhs;
  3352. }
  3353. template <class _CharT, class _Traits, class _Allocator>
  3354. inline _LIBCPP_HIDE_FROM_ABI bool
  3355. operator>(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3356. return __rhs < __lhs;
  3357. }
  3358. // operator<=
  3359. template <class _CharT, class _Traits, class _Allocator>
  3360. inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3361. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3362. return !(__rhs < __lhs);
  3363. }
  3364. template <class _CharT, class _Traits, class _Allocator>
  3365. inline _LIBCPP_HIDE_FROM_ABI bool
  3366. operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
  3367. return !(__rhs < __lhs);
  3368. }
  3369. template <class _CharT, class _Traits, class _Allocator>
  3370. inline _LIBCPP_HIDE_FROM_ABI bool
  3371. operator<=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3372. return !(__rhs < __lhs);
  3373. }
  3374. // operator>=
  3375. template <class _CharT, class _Traits, class _Allocator>
  3376. inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3377. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3378. return !(__lhs < __rhs);
  3379. }
  3380. template <class _CharT, class _Traits, class _Allocator>
  3381. inline _LIBCPP_HIDE_FROM_ABI bool
  3382. operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
  3383. return !(__lhs < __rhs);
  3384. }
  3385. template <class _CharT, class _Traits, class _Allocator>
  3386. inline _LIBCPP_HIDE_FROM_ABI bool
  3387. operator>=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
  3388. return !(__lhs < __rhs);
  3389. }
  3390. #endif // _LIBCPP_STD_VER >= 20
  3391. // operator +
  3392. template <class _CharT, class _Traits, class _Allocator>
  3393. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3394. operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3395. const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
  3396. using _String = basic_string<_CharT, _Traits, _Allocator>;
  3397. auto __lhs_sz = __lhs.size();
  3398. auto __rhs_sz = __rhs.size();
  3399. _String __r(__uninitialized_size_tag(),
  3400. __lhs_sz + __rhs_sz,
  3401. _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
  3402. auto __ptr = std::__to_address(__r.__get_pointer());
  3403. _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
  3404. _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
  3405. _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
  3406. return __r;
  3407. }
  3408. template <class _CharT, class _Traits, class _Allocator>
  3409. _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3410. operator+(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
  3411. using _String = basic_string<_CharT, _Traits, _Allocator>;
  3412. auto __lhs_sz = _Traits::length(__lhs);
  3413. auto __rhs_sz = __rhs.size();
  3414. _String __r(__uninitialized_size_tag(),
  3415. __lhs_sz + __rhs_sz,
  3416. _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
  3417. auto __ptr = std::__to_address(__r.__get_pointer());
  3418. _Traits::copy(__ptr, __lhs, __lhs_sz);
  3419. _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
  3420. _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
  3421. return __r;
  3422. }
  3423. template <class _CharT, class _Traits, class _Allocator>
  3424. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3425. operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
  3426. using _String = basic_string<_CharT, _Traits, _Allocator>;
  3427. typename _String::size_type __rhs_sz = __rhs.size();
  3428. _String __r(__uninitialized_size_tag(),
  3429. __rhs_sz + 1,
  3430. _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
  3431. auto __ptr = std::__to_address(__r.__get_pointer());
  3432. _Traits::assign(__ptr, 1, __lhs);
  3433. _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
  3434. _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
  3435. return __r;
  3436. }
  3437. template <class _CharT, class _Traits, class _Allocator>
  3438. inline _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3439. operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
  3440. using _String = basic_string<_CharT, _Traits, _Allocator>;
  3441. typename _String::size_type __lhs_sz = __lhs.size();
  3442. typename _String::size_type __rhs_sz = _Traits::length(__rhs);
  3443. _String __r(__uninitialized_size_tag(),
  3444. __lhs_sz + __rhs_sz,
  3445. _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
  3446. auto __ptr = std::__to_address(__r.__get_pointer());
  3447. _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
  3448. _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
  3449. _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
  3450. return __r;
  3451. }
  3452. template <class _CharT, class _Traits, class _Allocator>
  3453. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3454. operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs) {
  3455. using _String = basic_string<_CharT, _Traits, _Allocator>;
  3456. typename _String::size_type __lhs_sz = __lhs.size();
  3457. _String __r(__uninitialized_size_tag(),
  3458. __lhs_sz + 1,
  3459. _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
  3460. auto __ptr = std::__to_address(__r.__get_pointer());
  3461. _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
  3462. _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
  3463. _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
  3464. return __r;
  3465. }
  3466. #ifndef _LIBCPP_CXX03_LANG
  3467. template <class _CharT, class _Traits, class _Allocator>
  3468. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3469. operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
  3470. return std::move(__lhs.append(__rhs));
  3471. }
  3472. template <class _CharT, class _Traits, class _Allocator>
  3473. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3474. operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
  3475. return std::move(__rhs.insert(0, __lhs));
  3476. }
  3477. template <class _CharT, class _Traits, class _Allocator>
  3478. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3479. operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
  3480. return std::move(__lhs.append(__rhs));
  3481. }
  3482. template <class _CharT, class _Traits, class _Allocator>
  3483. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3484. operator+(const _CharT* __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
  3485. return std::move(__rhs.insert(0, __lhs));
  3486. }
  3487. template <class _CharT, class _Traits, class _Allocator>
  3488. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3489. operator+(_CharT __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
  3490. __rhs.insert(__rhs.begin(), __lhs);
  3491. return std::move(__rhs);
  3492. }
  3493. template <class _CharT, class _Traits, class _Allocator>
  3494. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3495. operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs) {
  3496. return std::move(__lhs.append(__rhs));
  3497. }
  3498. template <class _CharT, class _Traits, class _Allocator>
  3499. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
  3500. operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs) {
  3501. __lhs.push_back(__rhs);
  3502. return std::move(__lhs);
  3503. }
  3504. #endif // _LIBCPP_CXX03_LANG
  3505. // swap
  3506. template <class _CharT, class _Traits, class _Allocator>
  3507. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  3508. swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>& __rhs)
  3509. _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs))) {
  3510. __lhs.swap(__rhs);
  3511. }
  3512. _LIBCPP_EXPORTED_FROM_ABI int stoi(const string& __str, size_t* __idx = nullptr, int __base = 10);
  3513. _LIBCPP_EXPORTED_FROM_ABI long stol(const string& __str, size_t* __idx = nullptr, int __base = 10);
  3514. _LIBCPP_EXPORTED_FROM_ABI unsigned long stoul(const string& __str, size_t* __idx = nullptr, int __base = 10);
  3515. _LIBCPP_EXPORTED_FROM_ABI long long stoll(const string& __str, size_t* __idx = nullptr, int __base = 10);
  3516. _LIBCPP_EXPORTED_FROM_ABI unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
  3517. _LIBCPP_EXPORTED_FROM_ABI float stof(const string& __str, size_t* __idx = nullptr);
  3518. _LIBCPP_EXPORTED_FROM_ABI double stod(const string& __str, size_t* __idx = nullptr);
  3519. _LIBCPP_EXPORTED_FROM_ABI long double stold(const string& __str, size_t* __idx = nullptr);
  3520. _LIBCPP_EXPORTED_FROM_ABI string to_string(int __val);
  3521. _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned __val);
  3522. _LIBCPP_EXPORTED_FROM_ABI string to_string(long __val);
  3523. _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long __val);
  3524. _LIBCPP_EXPORTED_FROM_ABI string to_string(long long __val);
  3525. _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long long __val);
  3526. _LIBCPP_EXPORTED_FROM_ABI string to_string(float __val);
  3527. _LIBCPP_EXPORTED_FROM_ABI string to_string(double __val);
  3528. _LIBCPP_EXPORTED_FROM_ABI string to_string(long double __val);
  3529. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3530. _LIBCPP_EXPORTED_FROM_ABI int stoi(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
  3531. _LIBCPP_EXPORTED_FROM_ABI long stol(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
  3532. _LIBCPP_EXPORTED_FROM_ABI unsigned long stoul(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
  3533. _LIBCPP_EXPORTED_FROM_ABI long long stoll(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
  3534. _LIBCPP_EXPORTED_FROM_ABI unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
  3535. _LIBCPP_EXPORTED_FROM_ABI float stof(const wstring& __str, size_t* __idx = nullptr);
  3536. _LIBCPP_EXPORTED_FROM_ABI double stod(const wstring& __str, size_t* __idx = nullptr);
  3537. _LIBCPP_EXPORTED_FROM_ABI long double stold(const wstring& __str, size_t* __idx = nullptr);
  3538. _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(int __val);
  3539. _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned __val);
  3540. _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long __val);
  3541. _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long __val);
  3542. _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long long __val);
  3543. _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long long __val);
  3544. _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(float __val);
  3545. _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(double __val);
  3546. _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long double __val);
  3547. #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3548. template <class _CharT, class _Traits, class _Allocator>
  3549. _LIBCPP_TEMPLATE_DATA_VIS const typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3550. basic_string<_CharT, _Traits, _Allocator>::npos;
  3551. template <class _CharT, class _Allocator>
  3552. struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t> {
  3553. _LIBCPP_HIDE_FROM_ABI size_t
  3554. operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT {
  3555. return std::__do_string_hash(__val.data(), __val.data() + __val.size());
  3556. }
  3557. };
  3558. template <class _Allocator>
  3559. struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
  3560. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  3561. template <class _Allocator>
  3562. struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
  3563. #endif
  3564. template <class _Allocator>
  3565. struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
  3566. template <class _Allocator>
  3567. struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
  3568. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3569. template <class _Allocator>
  3570. struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
  3571. #endif
  3572. template <class _CharT, class _Traits, class _Allocator>
  3573. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  3574. operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str);
  3575. template <class _CharT, class _Traits, class _Allocator>
  3576. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  3577. operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
  3578. template <class _CharT, class _Traits, class _Allocator>
  3579. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  3580. getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
  3581. template <class _CharT, class _Traits, class _Allocator>
  3582. inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  3583. getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
  3584. template <class _CharT, class _Traits, class _Allocator>
  3585. inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  3586. getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
  3587. template <class _CharT, class _Traits, class _Allocator>
  3588. inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  3589. getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
  3590. #if _LIBCPP_STD_VER >= 20
  3591. template <class _CharT, class _Traits, class _Allocator, class _Up>
  3592. inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3593. erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
  3594. auto __old_size = __str.size();
  3595. __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
  3596. return __old_size - __str.size();
  3597. }
  3598. template <class _CharT, class _Traits, class _Allocator, class _Predicate>
  3599. inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3600. erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred) {
  3601. auto __old_size = __str.size();
  3602. __str.erase(std::remove_if(__str.begin(), __str.end(), __pred), __str.end());
  3603. return __old_size - __str.size();
  3604. }
  3605. #endif
  3606. #if _LIBCPP_STD_VER >= 14
  3607. // Literal suffixes for basic_string [basic.string.literals]
  3608. inline namespace literals {
  3609. inline namespace string_literals {
  3610. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char>
  3611. operator""s(const char* __str, size_t __len) {
  3612. return basic_string<char>(__str, __len);
  3613. }
  3614. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3615. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<wchar_t>
  3616. operator""s(const wchar_t* __str, size_t __len) {
  3617. return basic_string<wchar_t>(__str, __len);
  3618. }
  3619. # endif
  3620. # ifndef _LIBCPP_HAS_NO_CHAR8_T
  3621. inline _LIBCPP_HIDE_FROM_ABI constexpr basic_string<char8_t> operator""s(const char8_t* __str, size_t __len) {
  3622. return basic_string<char8_t>(__str, __len);
  3623. }
  3624. # endif
  3625. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char16_t>
  3626. operator""s(const char16_t* __str, size_t __len) {
  3627. return basic_string<char16_t>(__str, __len);
  3628. }
  3629. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char32_t>
  3630. operator""s(const char32_t* __str, size_t __len) {
  3631. return basic_string<char32_t>(__str, __len);
  3632. }
  3633. } // namespace string_literals
  3634. } // namespace literals
  3635. # if _LIBCPP_STD_VER >= 20
  3636. template <>
  3637. inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
  3638. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3639. template <>
  3640. inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
  3641. # endif
  3642. # endif
  3643. #endif
  3644. _LIBCPP_END_NAMESPACE_STD
  3645. _LIBCPP_POP_MACROS
  3646. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  3647. # include <algorithm>
  3648. # include <concepts>
  3649. # include <cstdlib>
  3650. # include <iterator>
  3651. # include <new>
  3652. # include <type_traits>
  3653. # include <typeinfo>
  3654. # include <utility>
  3655. #endif
  3656. #endif // _LIBCPP_STRING