string 189 KB

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