string 157 KB

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