string 192 KB

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