string 192 KB

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