string 191 KB

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