basic_string.h 241 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873
  1. // Components for manipulating sequences of characters -*- C++ -*-
  2. // Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/basic_string.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{string}
  23. */
  24. //
  25. // ISO C++ 14882: 21 Strings library
  26. //
  27. #ifndef _BASIC_STRING_H
  28. #define _BASIC_STRING_H 1
  29. #pragma GCC system_header
  30. #include <ext/atomicity.h>
  31. #include <ext/alloc_traits.h>
  32. #include <debug/debug.h>
  33. #if __cplusplus >= 201103L
  34. #include <initializer_list>
  35. #endif
  36. #if __cplusplus >= 201703L
  37. # include <string_view>
  38. #endif
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. #if _GLIBCXX_USE_CXX11_ABI
  43. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  44. /**
  45. * @class basic_string basic_string.h <string>
  46. * @brief Managing sequences of characters and character-like objects.
  47. *
  48. * @ingroup strings
  49. * @ingroup sequences
  50. *
  51. * @tparam _CharT Type of character
  52. * @tparam _Traits Traits for character type, defaults to
  53. * char_traits<_CharT>.
  54. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  55. *
  56. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  57. * <a href="tables.html#66">reversible container</a>, and a
  58. * <a href="tables.html#67">sequence</a>. Of the
  59. * <a href="tables.html#68">optional sequence requirements</a>, only
  60. * @c push_back, @c at, and @c %array access are supported.
  61. */
  62. template<typename _CharT, typename _Traits, typename _Alloc>
  63. class basic_string
  64. {
  65. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  66. rebind<_CharT>::other _Char_alloc_type;
  67. typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
  68. // Types:
  69. public:
  70. typedef _Traits traits_type;
  71. typedef typename _Traits::char_type value_type;
  72. typedef _Char_alloc_type allocator_type;
  73. typedef typename _Alloc_traits::size_type size_type;
  74. typedef typename _Alloc_traits::difference_type difference_type;
  75. typedef typename _Alloc_traits::reference reference;
  76. typedef typename _Alloc_traits::const_reference const_reference;
  77. typedef typename _Alloc_traits::pointer pointer;
  78. typedef typename _Alloc_traits::const_pointer const_pointer;
  79. typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
  80. typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
  81. const_iterator;
  82. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  83. typedef std::reverse_iterator<iterator> reverse_iterator;
  84. /// Value returned by various member functions when they fail.
  85. static const size_type npos = static_cast<size_type>(-1);
  86. protected:
  87. // type used for positions in insert, erase etc.
  88. #if __cplusplus < 201103L
  89. typedef iterator __const_iterator;
  90. #else
  91. typedef const_iterator __const_iterator;
  92. #endif
  93. private:
  94. #if __cplusplus >= 201703L
  95. // A helper type for avoiding boiler-plate.
  96. typedef basic_string_view<_CharT, _Traits> __sv_type;
  97. template<typename _Tp, typename _Res>
  98. using _If_sv = enable_if_t<
  99. __and_<is_convertible<const _Tp&, __sv_type>,
  100. __not_<is_convertible<const _Tp*, const basic_string*>>,
  101. __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
  102. _Res>;
  103. // Allows an implicit conversion to __sv_type.
  104. static __sv_type
  105. _S_to_string_view(__sv_type __svt) noexcept
  106. { return __svt; }
  107. // Wraps a string_view by explicit conversion and thus
  108. // allows to add an internal constructor that does not
  109. // participate in overload resolution when a string_view
  110. // is provided.
  111. struct __sv_wrapper
  112. {
  113. explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
  114. __sv_type _M_sv;
  115. };
  116. /**
  117. * @brief Only internally used: Construct string from a string view
  118. * wrapper.
  119. * @param __svw string view wrapper.
  120. * @param __a Allocator to use.
  121. */
  122. explicit
  123. basic_string(__sv_wrapper __svw, const _Alloc& __a)
  124. : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
  125. #endif
  126. // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
  127. struct _Alloc_hider : allocator_type // TODO check __is_final
  128. {
  129. #if __cplusplus < 201103L
  130. _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
  131. : allocator_type(__a), _M_p(__dat) { }
  132. #else
  133. _Alloc_hider(pointer __dat, const _Alloc& __a)
  134. : allocator_type(__a), _M_p(__dat) { }
  135. _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
  136. : allocator_type(std::move(__a)), _M_p(__dat) { }
  137. #endif
  138. pointer _M_p; // The actual data.
  139. };
  140. _Alloc_hider _M_dataplus;
  141. size_type _M_string_length;
  142. enum { _S_local_capacity = 15 / sizeof(_CharT) };
  143. union
  144. {
  145. _CharT _M_local_buf[_S_local_capacity + 1];
  146. size_type _M_allocated_capacity;
  147. };
  148. void
  149. _M_data(pointer __p)
  150. { _M_dataplus._M_p = __p; }
  151. void
  152. _M_length(size_type __length)
  153. { _M_string_length = __length; }
  154. pointer
  155. _M_data() const
  156. { return _M_dataplus._M_p; }
  157. pointer
  158. _M_local_data()
  159. {
  160. #if __cplusplus >= 201103L
  161. return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
  162. #else
  163. return pointer(_M_local_buf);
  164. #endif
  165. }
  166. const_pointer
  167. _M_local_data() const
  168. {
  169. #if __cplusplus >= 201103L
  170. return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
  171. #else
  172. return const_pointer(_M_local_buf);
  173. #endif
  174. }
  175. void
  176. _M_capacity(size_type __capacity)
  177. { _M_allocated_capacity = __capacity; }
  178. void
  179. _M_set_length(size_type __n)
  180. {
  181. _M_length(__n);
  182. traits_type::assign(_M_data()[__n], _CharT());
  183. }
  184. bool
  185. _M_is_local() const
  186. { return _M_data() == _M_local_data(); }
  187. // Create & Destroy
  188. pointer
  189. _M_create(size_type&, size_type);
  190. void
  191. _M_dispose()
  192. {
  193. if (!_M_is_local())
  194. _M_destroy(_M_allocated_capacity);
  195. }
  196. void
  197. _M_destroy(size_type __size) throw()
  198. { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
  199. // _M_construct_aux is used to implement the 21.3.1 para 15 which
  200. // requires special behaviour if _InIterator is an integral type
  201. template<typename _InIterator>
  202. void
  203. _M_construct_aux(_InIterator __beg, _InIterator __end,
  204. std::__false_type)
  205. {
  206. typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
  207. _M_construct(__beg, __end, _Tag());
  208. }
  209. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  210. // 438. Ambiguity in the "do the right thing" clause
  211. template<typename _Integer>
  212. void
  213. _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
  214. { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
  215. void
  216. _M_construct_aux_2(size_type __req, _CharT __c)
  217. { _M_construct(__req, __c); }
  218. template<typename _InIterator>
  219. void
  220. _M_construct(_InIterator __beg, _InIterator __end)
  221. {
  222. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  223. _M_construct_aux(__beg, __end, _Integral());
  224. }
  225. // For Input Iterators, used in istreambuf_iterators, etc.
  226. template<typename _InIterator>
  227. void
  228. _M_construct(_InIterator __beg, _InIterator __end,
  229. std::input_iterator_tag);
  230. // For forward_iterators up to random_access_iterators, used for
  231. // string::iterator, _CharT*, etc.
  232. template<typename _FwdIterator>
  233. void
  234. _M_construct(_FwdIterator __beg, _FwdIterator __end,
  235. std::forward_iterator_tag);
  236. void
  237. _M_construct(size_type __req, _CharT __c);
  238. allocator_type&
  239. _M_get_allocator()
  240. { return _M_dataplus; }
  241. const allocator_type&
  242. _M_get_allocator() const
  243. { return _M_dataplus; }
  244. private:
  245. #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
  246. // The explicit instantiations in misc-inst.cc require this due to
  247. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
  248. template<typename _Tp, bool _Requires =
  249. !__are_same<_Tp, _CharT*>::__value
  250. && !__are_same<_Tp, const _CharT*>::__value
  251. && !__are_same<_Tp, iterator>::__value
  252. && !__are_same<_Tp, const_iterator>::__value>
  253. struct __enable_if_not_native_iterator
  254. { typedef basic_string& __type; };
  255. template<typename _Tp>
  256. struct __enable_if_not_native_iterator<_Tp, false> { };
  257. #endif
  258. size_type
  259. _M_check(size_type __pos, const char* __s) const
  260. {
  261. if (__pos > this->size())
  262. __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
  263. "this->size() (which is %zu)"),
  264. __s, __pos, this->size());
  265. return __pos;
  266. }
  267. void
  268. _M_check_length(size_type __n1, size_type __n2, const char* __s) const
  269. {
  270. if (this->max_size() - (this->size() - __n1) < __n2)
  271. __throw_length_error(__N(__s));
  272. }
  273. // NB: _M_limit doesn't check for a bad __pos value.
  274. size_type
  275. _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
  276. {
  277. const bool __testoff = __off < this->size() - __pos;
  278. return __testoff ? __off : this->size() - __pos;
  279. }
  280. // True if _Rep and source do not overlap.
  281. bool
  282. _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  283. {
  284. return (less<const _CharT*>()(__s, _M_data())
  285. || less<const _CharT*>()(_M_data() + this->size(), __s));
  286. }
  287. // When __n = 1 way faster than the general multichar
  288. // traits_type::copy/move/assign.
  289. static void
  290. _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
  291. {
  292. if (__n == 1)
  293. traits_type::assign(*__d, *__s);
  294. else
  295. traits_type::copy(__d, __s, __n);
  296. }
  297. static void
  298. _S_move(_CharT* __d, const _CharT* __s, size_type __n)
  299. {
  300. if (__n == 1)
  301. traits_type::assign(*__d, *__s);
  302. else
  303. traits_type::move(__d, __s, __n);
  304. }
  305. static void
  306. _S_assign(_CharT* __d, size_type __n, _CharT __c)
  307. {
  308. if (__n == 1)
  309. traits_type::assign(*__d, __c);
  310. else
  311. traits_type::assign(__d, __n, __c);
  312. }
  313. // _S_copy_chars is a separate template to permit specialization
  314. // to optimize for the common case of pointers as iterators.
  315. template<class _Iterator>
  316. static void
  317. _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
  318. {
  319. for (; __k1 != __k2; ++__k1, (void)++__p)
  320. traits_type::assign(*__p, *__k1); // These types are off.
  321. }
  322. static void
  323. _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
  324. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  325. static void
  326. _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
  327. _GLIBCXX_NOEXCEPT
  328. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  329. static void
  330. _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
  331. { _S_copy(__p, __k1, __k2 - __k1); }
  332. static void
  333. _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
  334. _GLIBCXX_NOEXCEPT
  335. { _S_copy(__p, __k1, __k2 - __k1); }
  336. static int
  337. _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
  338. {
  339. const difference_type __d = difference_type(__n1 - __n2);
  340. if (__d > __gnu_cxx::__numeric_traits<int>::__max)
  341. return __gnu_cxx::__numeric_traits<int>::__max;
  342. else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
  343. return __gnu_cxx::__numeric_traits<int>::__min;
  344. else
  345. return int(__d);
  346. }
  347. void
  348. _M_assign(const basic_string&);
  349. void
  350. _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
  351. size_type __len2);
  352. void
  353. _M_erase(size_type __pos, size_type __n);
  354. public:
  355. // Construct/copy/destroy:
  356. // NB: We overload ctors in some cases instead of using default
  357. // arguments, per 17.4.4.4 para. 2 item 2.
  358. /**
  359. * @brief Default constructor creates an empty string.
  360. */
  361. basic_string()
  362. _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
  363. : _M_dataplus(_M_local_data())
  364. { _M_set_length(0); }
  365. /**
  366. * @brief Construct an empty string using allocator @a a.
  367. */
  368. explicit
  369. basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
  370. : _M_dataplus(_M_local_data(), __a)
  371. { _M_set_length(0); }
  372. /**
  373. * @brief Construct string with copy of value of @a __str.
  374. * @param __str Source string.
  375. */
  376. basic_string(const basic_string& __str)
  377. : _M_dataplus(_M_local_data(),
  378. _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
  379. { _M_construct(__str._M_data(), __str._M_data() + __str.length()); }
  380. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  381. // 2583. no way to supply an allocator for basic_string(str, pos)
  382. /**
  383. * @brief Construct string as copy of a substring.
  384. * @param __str Source string.
  385. * @param __pos Index of first character to copy from.
  386. * @param __a Allocator to use.
  387. */
  388. basic_string(const basic_string& __str, size_type __pos,
  389. const _Alloc& __a = _Alloc())
  390. : _M_dataplus(_M_local_data(), __a)
  391. {
  392. const _CharT* __start = __str._M_data()
  393. + __str._M_check(__pos, "basic_string::basic_string");
  394. _M_construct(__start, __start + __str._M_limit(__pos, npos));
  395. }
  396. /**
  397. * @brief Construct string as copy of a substring.
  398. * @param __str Source string.
  399. * @param __pos Index of first character to copy from.
  400. * @param __n Number of characters to copy.
  401. */
  402. basic_string(const basic_string& __str, size_type __pos,
  403. size_type __n)
  404. : _M_dataplus(_M_local_data())
  405. {
  406. const _CharT* __start = __str._M_data()
  407. + __str._M_check(__pos, "basic_string::basic_string");
  408. _M_construct(__start, __start + __str._M_limit(__pos, __n));
  409. }
  410. /**
  411. * @brief Construct string as copy of a substring.
  412. * @param __str Source string.
  413. * @param __pos Index of first character to copy from.
  414. * @param __n Number of characters to copy.
  415. * @param __a Allocator to use.
  416. */
  417. basic_string(const basic_string& __str, size_type __pos,
  418. size_type __n, const _Alloc& __a)
  419. : _M_dataplus(_M_local_data(), __a)
  420. {
  421. const _CharT* __start
  422. = __str._M_data() + __str._M_check(__pos, "string::string");
  423. _M_construct(__start, __start + __str._M_limit(__pos, __n));
  424. }
  425. /**
  426. * @brief Construct string initialized by a character %array.
  427. * @param __s Source character %array.
  428. * @param __n Number of characters to copy.
  429. * @param __a Allocator to use (default is default allocator).
  430. *
  431. * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
  432. * has no special meaning.
  433. */
  434. basic_string(const _CharT* __s, size_type __n,
  435. const _Alloc& __a = _Alloc())
  436. : _M_dataplus(_M_local_data(), __a)
  437. { _M_construct(__s, __s + __n); }
  438. /**
  439. * @brief Construct string as copy of a C string.
  440. * @param __s Source C string.
  441. * @param __a Allocator to use (default is default allocator).
  442. */
  443. #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  444. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  445. // 3076. basic_string CTAD ambiguity
  446. template<typename = _RequireAllocator<_Alloc>>
  447. #endif
  448. basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
  449. : _M_dataplus(_M_local_data(), __a)
  450. { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); }
  451. /**
  452. * @brief Construct string as multiple characters.
  453. * @param __n Number of characters.
  454. * @param __c Character to use.
  455. * @param __a Allocator to use (default is default allocator).
  456. */
  457. #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  458. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  459. // 3076. basic_string CTAD ambiguity
  460. template<typename = _RequireAllocator<_Alloc>>
  461. #endif
  462. basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
  463. : _M_dataplus(_M_local_data(), __a)
  464. { _M_construct(__n, __c); }
  465. #if __cplusplus >= 201103L
  466. /**
  467. * @brief Move construct string.
  468. * @param __str Source string.
  469. *
  470. * The newly-created string contains the exact contents of @a __str.
  471. * @a __str is a valid, but unspecified string.
  472. **/
  473. basic_string(basic_string&& __str) noexcept
  474. : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
  475. {
  476. if (__str._M_is_local())
  477. {
  478. traits_type::copy(_M_local_buf, __str._M_local_buf,
  479. _S_local_capacity + 1);
  480. }
  481. else
  482. {
  483. _M_data(__str._M_data());
  484. _M_capacity(__str._M_allocated_capacity);
  485. }
  486. // Must use _M_length() here not _M_set_length() because
  487. // basic_stringbuf relies on writing into unallocated capacity so
  488. // we mess up the contents if we put a '\0' in the string.
  489. _M_length(__str.length());
  490. __str._M_data(__str._M_local_data());
  491. __str._M_set_length(0);
  492. }
  493. /**
  494. * @brief Construct string from an initializer %list.
  495. * @param __l std::initializer_list of characters.
  496. * @param __a Allocator to use (default is default allocator).
  497. */
  498. basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
  499. : _M_dataplus(_M_local_data(), __a)
  500. { _M_construct(__l.begin(), __l.end()); }
  501. basic_string(const basic_string& __str, const _Alloc& __a)
  502. : _M_dataplus(_M_local_data(), __a)
  503. { _M_construct(__str.begin(), __str.end()); }
  504. basic_string(basic_string&& __str, const _Alloc& __a)
  505. noexcept(_Alloc_traits::_S_always_equal())
  506. : _M_dataplus(_M_local_data(), __a)
  507. {
  508. if (__str._M_is_local())
  509. {
  510. traits_type::copy(_M_local_buf, __str._M_local_buf,
  511. _S_local_capacity + 1);
  512. _M_length(__str.length());
  513. __str._M_set_length(0);
  514. }
  515. else if (_Alloc_traits::_S_always_equal()
  516. || __str.get_allocator() == __a)
  517. {
  518. _M_data(__str._M_data());
  519. _M_length(__str.length());
  520. _M_capacity(__str._M_allocated_capacity);
  521. __str._M_data(__str._M_local_buf);
  522. __str._M_set_length(0);
  523. }
  524. else
  525. _M_construct(__str.begin(), __str.end());
  526. }
  527. #endif // C++11
  528. /**
  529. * @brief Construct string as copy of a range.
  530. * @param __beg Start of range.
  531. * @param __end End of range.
  532. * @param __a Allocator to use (default is default allocator).
  533. */
  534. #if __cplusplus >= 201103L
  535. template<typename _InputIterator,
  536. typename = std::_RequireInputIter<_InputIterator>>
  537. #else
  538. template<typename _InputIterator>
  539. #endif
  540. basic_string(_InputIterator __beg, _InputIterator __end,
  541. const _Alloc& __a = _Alloc())
  542. : _M_dataplus(_M_local_data(), __a)
  543. { _M_construct(__beg, __end); }
  544. #if __cplusplus >= 201703L
  545. /**
  546. * @brief Construct string from a substring of a string_view.
  547. * @param __t Source object convertible to string view.
  548. * @param __pos The index of the first character to copy from __t.
  549. * @param __n The number of characters to copy from __t.
  550. * @param __a Allocator to use.
  551. */
  552. template<typename _Tp, typename = _If_sv<_Tp, void>>
  553. basic_string(const _Tp& __t, size_type __pos, size_type __n,
  554. const _Alloc& __a = _Alloc())
  555. : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
  556. /**
  557. * @brief Construct string from a string_view.
  558. * @param __t Source object convertible to string view.
  559. * @param __a Allocator to use (default is default allocator).
  560. */
  561. template<typename _Tp, typename = _If_sv<_Tp, void>>
  562. explicit
  563. basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
  564. : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
  565. #endif // C++17
  566. /**
  567. * @brief Destroy the string instance.
  568. */
  569. ~basic_string()
  570. { _M_dispose(); }
  571. /**
  572. * @brief Assign the value of @a str to this string.
  573. * @param __str Source string.
  574. */
  575. basic_string&
  576. operator=(const basic_string& __str)
  577. {
  578. #if __cplusplus >= 201103L
  579. if (_Alloc_traits::_S_propagate_on_copy_assign())
  580. {
  581. if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
  582. && _M_get_allocator() != __str._M_get_allocator())
  583. {
  584. // Propagating allocator cannot free existing storage so must
  585. // deallocate it before replacing current allocator.
  586. if (__str.size() <= _S_local_capacity)
  587. {
  588. _M_destroy(_M_allocated_capacity);
  589. _M_data(_M_local_data());
  590. _M_set_length(0);
  591. }
  592. else
  593. {
  594. const auto __len = __str.size();
  595. auto __alloc = __str._M_get_allocator();
  596. // If this allocation throws there are no effects:
  597. auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1);
  598. _M_destroy(_M_allocated_capacity);
  599. _M_data(__ptr);
  600. _M_capacity(__len);
  601. _M_set_length(__len);
  602. }
  603. }
  604. std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
  605. }
  606. #endif
  607. return this->assign(__str);
  608. }
  609. /**
  610. * @brief Copy contents of @a s into this string.
  611. * @param __s Source null-terminated string.
  612. */
  613. basic_string&
  614. operator=(const _CharT* __s)
  615. { return this->assign(__s); }
  616. /**
  617. * @brief Set value to string of length 1.
  618. * @param __c Source character.
  619. *
  620. * Assigning to a character makes this string length 1 and
  621. * (*this)[0] == @a c.
  622. */
  623. basic_string&
  624. operator=(_CharT __c)
  625. {
  626. this->assign(1, __c);
  627. return *this;
  628. }
  629. #if __cplusplus >= 201103L
  630. /**
  631. * @brief Move assign the value of @a str to this string.
  632. * @param __str Source string.
  633. *
  634. * The contents of @a str are moved into this string (without copying).
  635. * @a str is a valid, but unspecified string.
  636. **/
  637. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  638. // 2063. Contradictory requirements for string move assignment
  639. basic_string&
  640. operator=(basic_string&& __str)
  641. noexcept(_Alloc_traits::_S_nothrow_move())
  642. {
  643. if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
  644. && !_Alloc_traits::_S_always_equal()
  645. && _M_get_allocator() != __str._M_get_allocator())
  646. {
  647. // Destroy existing storage before replacing allocator.
  648. _M_destroy(_M_allocated_capacity);
  649. _M_data(_M_local_data());
  650. _M_set_length(0);
  651. }
  652. // Replace allocator if POCMA is true.
  653. std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
  654. if (__str._M_is_local())
  655. {
  656. // We've always got room for a short string, just copy it.
  657. if (__str.size())
  658. this->_S_copy(_M_data(), __str._M_data(), __str.size());
  659. _M_set_length(__str.size());
  660. }
  661. else if (_Alloc_traits::_S_propagate_on_move_assign()
  662. || _Alloc_traits::_S_always_equal()
  663. || _M_get_allocator() == __str._M_get_allocator())
  664. {
  665. // Just move the allocated pointer, our allocator can free it.
  666. pointer __data = nullptr;
  667. size_type __capacity;
  668. if (!_M_is_local())
  669. {
  670. if (_Alloc_traits::_S_always_equal())
  671. {
  672. // __str can reuse our existing storage.
  673. __data = _M_data();
  674. __capacity = _M_allocated_capacity;
  675. }
  676. else // __str can't use it, so free it.
  677. _M_destroy(_M_allocated_capacity);
  678. }
  679. _M_data(__str._M_data());
  680. _M_length(__str.length());
  681. _M_capacity(__str._M_allocated_capacity);
  682. if (__data)
  683. {
  684. __str._M_data(__data);
  685. __str._M_capacity(__capacity);
  686. }
  687. else
  688. __str._M_data(__str._M_local_buf);
  689. }
  690. else // Need to do a deep copy
  691. assign(__str);
  692. __str.clear();
  693. return *this;
  694. }
  695. /**
  696. * @brief Set value to string constructed from initializer %list.
  697. * @param __l std::initializer_list.
  698. */
  699. basic_string&
  700. operator=(initializer_list<_CharT> __l)
  701. {
  702. this->assign(__l.begin(), __l.size());
  703. return *this;
  704. }
  705. #endif // C++11
  706. #if __cplusplus >= 201703L
  707. /**
  708. * @brief Set value to string constructed from a string_view.
  709. * @param __svt An object convertible to string_view.
  710. */
  711. template<typename _Tp>
  712. _If_sv<_Tp, basic_string&>
  713. operator=(const _Tp& __svt)
  714. { return this->assign(__svt); }
  715. /**
  716. * @brief Convert to a string_view.
  717. * @return A string_view.
  718. */
  719. operator __sv_type() const noexcept
  720. { return __sv_type(data(), size()); }
  721. #endif // C++17
  722. // Iterators:
  723. /**
  724. * Returns a read/write iterator that points to the first character in
  725. * the %string.
  726. */
  727. iterator
  728. begin() _GLIBCXX_NOEXCEPT
  729. { return iterator(_M_data()); }
  730. /**
  731. * Returns a read-only (constant) iterator that points to the first
  732. * character in the %string.
  733. */
  734. const_iterator
  735. begin() const _GLIBCXX_NOEXCEPT
  736. { return const_iterator(_M_data()); }
  737. /**
  738. * Returns a read/write iterator that points one past the last
  739. * character in the %string.
  740. */
  741. iterator
  742. end() _GLIBCXX_NOEXCEPT
  743. { return iterator(_M_data() + this->size()); }
  744. /**
  745. * Returns a read-only (constant) iterator that points one past the
  746. * last character in the %string.
  747. */
  748. const_iterator
  749. end() const _GLIBCXX_NOEXCEPT
  750. { return const_iterator(_M_data() + this->size()); }
  751. /**
  752. * Returns a read/write reverse iterator that points to the last
  753. * character in the %string. Iteration is done in reverse element
  754. * order.
  755. */
  756. reverse_iterator
  757. rbegin() _GLIBCXX_NOEXCEPT
  758. { return reverse_iterator(this->end()); }
  759. /**
  760. * Returns a read-only (constant) reverse iterator that points
  761. * to the last character in the %string. Iteration is done in
  762. * reverse element order.
  763. */
  764. const_reverse_iterator
  765. rbegin() const _GLIBCXX_NOEXCEPT
  766. { return const_reverse_iterator(this->end()); }
  767. /**
  768. * Returns a read/write reverse iterator that points to one before the
  769. * first character in the %string. Iteration is done in reverse
  770. * element order.
  771. */
  772. reverse_iterator
  773. rend() _GLIBCXX_NOEXCEPT
  774. { return reverse_iterator(this->begin()); }
  775. /**
  776. * Returns a read-only (constant) reverse iterator that points
  777. * to one before the first character in the %string. Iteration
  778. * is done in reverse element order.
  779. */
  780. const_reverse_iterator
  781. rend() const _GLIBCXX_NOEXCEPT
  782. { return const_reverse_iterator(this->begin()); }
  783. #if __cplusplus >= 201103L
  784. /**
  785. * Returns a read-only (constant) iterator that points to the first
  786. * character in the %string.
  787. */
  788. const_iterator
  789. cbegin() const noexcept
  790. { return const_iterator(this->_M_data()); }
  791. /**
  792. * Returns a read-only (constant) iterator that points one past the
  793. * last character in the %string.
  794. */
  795. const_iterator
  796. cend() const noexcept
  797. { return const_iterator(this->_M_data() + this->size()); }
  798. /**
  799. * Returns a read-only (constant) reverse iterator that points
  800. * to the last character in the %string. Iteration is done in
  801. * reverse element order.
  802. */
  803. const_reverse_iterator
  804. crbegin() const noexcept
  805. { return const_reverse_iterator(this->end()); }
  806. /**
  807. * Returns a read-only (constant) reverse iterator that points
  808. * to one before the first character in the %string. Iteration
  809. * is done in reverse element order.
  810. */
  811. const_reverse_iterator
  812. crend() const noexcept
  813. { return const_reverse_iterator(this->begin()); }
  814. #endif
  815. public:
  816. // Capacity:
  817. /// Returns the number of characters in the string, not including any
  818. /// null-termination.
  819. size_type
  820. size() const _GLIBCXX_NOEXCEPT
  821. { return _M_string_length; }
  822. /// Returns the number of characters in the string, not including any
  823. /// null-termination.
  824. size_type
  825. length() const _GLIBCXX_NOEXCEPT
  826. { return _M_string_length; }
  827. /// Returns the size() of the largest possible %string.
  828. size_type
  829. max_size() const _GLIBCXX_NOEXCEPT
  830. { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
  831. /**
  832. * @brief Resizes the %string to the specified number of characters.
  833. * @param __n Number of characters the %string should contain.
  834. * @param __c Character to fill any new elements.
  835. *
  836. * This function will %resize the %string to the specified
  837. * number of characters. If the number is smaller than the
  838. * %string's current size the %string is truncated, otherwise
  839. * the %string is extended and new elements are %set to @a __c.
  840. */
  841. void
  842. resize(size_type __n, _CharT __c);
  843. /**
  844. * @brief Resizes the %string to the specified number of characters.
  845. * @param __n Number of characters the %string should contain.
  846. *
  847. * This function will resize the %string to the specified length. If
  848. * the new size is smaller than the %string's current size the %string
  849. * is truncated, otherwise the %string is extended and new characters
  850. * are default-constructed. For basic types such as char, this means
  851. * setting them to 0.
  852. */
  853. void
  854. resize(size_type __n)
  855. { this->resize(__n, _CharT()); }
  856. #if __cplusplus >= 201103L
  857. /// A non-binding request to reduce capacity() to size().
  858. void
  859. shrink_to_fit() noexcept
  860. {
  861. #if __cpp_exceptions
  862. if (capacity() > size())
  863. {
  864. try
  865. { reserve(0); }
  866. catch(...)
  867. { }
  868. }
  869. #endif
  870. }
  871. #endif
  872. /**
  873. * Returns the total number of characters that the %string can hold
  874. * before needing to allocate more memory.
  875. */
  876. size_type
  877. capacity() const _GLIBCXX_NOEXCEPT
  878. {
  879. return _M_is_local() ? size_type(_S_local_capacity)
  880. : _M_allocated_capacity;
  881. }
  882. /**
  883. * @brief Attempt to preallocate enough memory for specified number of
  884. * characters.
  885. * @param __res_arg Number of characters required.
  886. * @throw std::length_error If @a __res_arg exceeds @c max_size().
  887. *
  888. * This function attempts to reserve enough memory for the
  889. * %string to hold the specified number of characters. If the
  890. * number requested is more than max_size(), length_error is
  891. * thrown.
  892. *
  893. * The advantage of this function is that if optimal code is a
  894. * necessity and the user can determine the string length that will be
  895. * required, the user can reserve the memory in %advance, and thus
  896. * prevent a possible reallocation of memory and copying of %string
  897. * data.
  898. */
  899. void
  900. reserve(size_type __res_arg = 0);
  901. /**
  902. * Erases the string, making it empty.
  903. */
  904. void
  905. clear() _GLIBCXX_NOEXCEPT
  906. { _M_set_length(0); }
  907. /**
  908. * Returns true if the %string is empty. Equivalent to
  909. * <code>*this == ""</code>.
  910. */
  911. _GLIBCXX_NODISCARD bool
  912. empty() const _GLIBCXX_NOEXCEPT
  913. { return this->size() == 0; }
  914. // Element access:
  915. /**
  916. * @brief Subscript access to the data contained in the %string.
  917. * @param __pos The index of the character to access.
  918. * @return Read-only (constant) reference to the character.
  919. *
  920. * This operator allows for easy, array-style, data access.
  921. * Note that data access with this operator is unchecked and
  922. * out_of_range lookups are not defined. (For checked lookups
  923. * see at().)
  924. */
  925. const_reference
  926. operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
  927. {
  928. __glibcxx_assert(__pos <= size());
  929. return _M_data()[__pos];
  930. }
  931. /**
  932. * @brief Subscript access to the data contained in the %string.
  933. * @param __pos The index of the character to access.
  934. * @return Read/write reference to the character.
  935. *
  936. * This operator allows for easy, array-style, data access.
  937. * Note that data access with this operator is unchecked and
  938. * out_of_range lookups are not defined. (For checked lookups
  939. * see at().)
  940. */
  941. reference
  942. operator[](size_type __pos)
  943. {
  944. // Allow pos == size() both in C++98 mode, as v3 extension,
  945. // and in C++11 mode.
  946. __glibcxx_assert(__pos <= size());
  947. // In pedantic mode be strict in C++98 mode.
  948. _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
  949. return _M_data()[__pos];
  950. }
  951. /**
  952. * @brief Provides access to the data contained in the %string.
  953. * @param __n The index of the character to access.
  954. * @return Read-only (const) reference to the character.
  955. * @throw std::out_of_range If @a n is an invalid index.
  956. *
  957. * This function provides for safer data access. The parameter is
  958. * first checked that it is in the range of the string. The function
  959. * throws out_of_range if the check fails.
  960. */
  961. const_reference
  962. at(size_type __n) const
  963. {
  964. if (__n >= this->size())
  965. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  966. "(which is %zu) >= this->size() "
  967. "(which is %zu)"),
  968. __n, this->size());
  969. return _M_data()[__n];
  970. }
  971. /**
  972. * @brief Provides access to the data contained in the %string.
  973. * @param __n The index of the character to access.
  974. * @return Read/write reference to the character.
  975. * @throw std::out_of_range If @a n is an invalid index.
  976. *
  977. * This function provides for safer data access. The parameter is
  978. * first checked that it is in the range of the string. The function
  979. * throws out_of_range if the check fails.
  980. */
  981. reference
  982. at(size_type __n)
  983. {
  984. if (__n >= size())
  985. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  986. "(which is %zu) >= this->size() "
  987. "(which is %zu)"),
  988. __n, this->size());
  989. return _M_data()[__n];
  990. }
  991. #if __cplusplus >= 201103L
  992. /**
  993. * Returns a read/write reference to the data at the first
  994. * element of the %string.
  995. */
  996. reference
  997. front() noexcept
  998. {
  999. __glibcxx_assert(!empty());
  1000. return operator[](0);
  1001. }
  1002. /**
  1003. * Returns a read-only (constant) reference to the data at the first
  1004. * element of the %string.
  1005. */
  1006. const_reference
  1007. front() const noexcept
  1008. {
  1009. __glibcxx_assert(!empty());
  1010. return operator[](0);
  1011. }
  1012. /**
  1013. * Returns a read/write reference to the data at the last
  1014. * element of the %string.
  1015. */
  1016. reference
  1017. back() noexcept
  1018. {
  1019. __glibcxx_assert(!empty());
  1020. return operator[](this->size() - 1);
  1021. }
  1022. /**
  1023. * Returns a read-only (constant) reference to the data at the
  1024. * last element of the %string.
  1025. */
  1026. const_reference
  1027. back() const noexcept
  1028. {
  1029. __glibcxx_assert(!empty());
  1030. return operator[](this->size() - 1);
  1031. }
  1032. #endif
  1033. // Modifiers:
  1034. /**
  1035. * @brief Append a string to this string.
  1036. * @param __str The string to append.
  1037. * @return Reference to this string.
  1038. */
  1039. basic_string&
  1040. operator+=(const basic_string& __str)
  1041. { return this->append(__str); }
  1042. /**
  1043. * @brief Append a C string.
  1044. * @param __s The C string to append.
  1045. * @return Reference to this string.
  1046. */
  1047. basic_string&
  1048. operator+=(const _CharT* __s)
  1049. { return this->append(__s); }
  1050. /**
  1051. * @brief Append a character.
  1052. * @param __c The character to append.
  1053. * @return Reference to this string.
  1054. */
  1055. basic_string&
  1056. operator+=(_CharT __c)
  1057. {
  1058. this->push_back(__c);
  1059. return *this;
  1060. }
  1061. #if __cplusplus >= 201103L
  1062. /**
  1063. * @brief Append an initializer_list of characters.
  1064. * @param __l The initializer_list of characters to be appended.
  1065. * @return Reference to this string.
  1066. */
  1067. basic_string&
  1068. operator+=(initializer_list<_CharT> __l)
  1069. { return this->append(__l.begin(), __l.size()); }
  1070. #endif // C++11
  1071. #if __cplusplus >= 201703L
  1072. /**
  1073. * @brief Append a string_view.
  1074. * @param __svt An object convertible to string_view to be appended.
  1075. * @return Reference to this string.
  1076. */
  1077. template<typename _Tp>
  1078. _If_sv<_Tp, basic_string&>
  1079. operator+=(const _Tp& __svt)
  1080. { return this->append(__svt); }
  1081. #endif // C++17
  1082. /**
  1083. * @brief Append a string to this string.
  1084. * @param __str The string to append.
  1085. * @return Reference to this string.
  1086. */
  1087. basic_string&
  1088. append(const basic_string& __str)
  1089. { return _M_append(__str._M_data(), __str.size()); }
  1090. /**
  1091. * @brief Append a substring.
  1092. * @param __str The string to append.
  1093. * @param __pos Index of the first character of str to append.
  1094. * @param __n The number of characters to append.
  1095. * @return Reference to this string.
  1096. * @throw std::out_of_range if @a __pos is not a valid index.
  1097. *
  1098. * This function appends @a __n characters from @a __str
  1099. * starting at @a __pos to this string. If @a __n is is larger
  1100. * than the number of available characters in @a __str, the
  1101. * remainder of @a __str is appended.
  1102. */
  1103. basic_string&
  1104. append(const basic_string& __str, size_type __pos, size_type __n = npos)
  1105. { return _M_append(__str._M_data()
  1106. + __str._M_check(__pos, "basic_string::append"),
  1107. __str._M_limit(__pos, __n)); }
  1108. /**
  1109. * @brief Append a C substring.
  1110. * @param __s The C string to append.
  1111. * @param __n The number of characters to append.
  1112. * @return Reference to this string.
  1113. */
  1114. basic_string&
  1115. append(const _CharT* __s, size_type __n)
  1116. {
  1117. __glibcxx_requires_string_len(__s, __n);
  1118. _M_check_length(size_type(0), __n, "basic_string::append");
  1119. return _M_append(__s, __n);
  1120. }
  1121. /**
  1122. * @brief Append a C string.
  1123. * @param __s The C string to append.
  1124. * @return Reference to this string.
  1125. */
  1126. basic_string&
  1127. append(const _CharT* __s)
  1128. {
  1129. __glibcxx_requires_string(__s);
  1130. const size_type __n = traits_type::length(__s);
  1131. _M_check_length(size_type(0), __n, "basic_string::append");
  1132. return _M_append(__s, __n);
  1133. }
  1134. /**
  1135. * @brief Append multiple characters.
  1136. * @param __n The number of characters to append.
  1137. * @param __c The character to use.
  1138. * @return Reference to this string.
  1139. *
  1140. * Appends __n copies of __c to this string.
  1141. */
  1142. basic_string&
  1143. append(size_type __n, _CharT __c)
  1144. { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
  1145. #if __cplusplus >= 201103L
  1146. /**
  1147. * @brief Append an initializer_list of characters.
  1148. * @param __l The initializer_list of characters to append.
  1149. * @return Reference to this string.
  1150. */
  1151. basic_string&
  1152. append(initializer_list<_CharT> __l)
  1153. { return this->append(__l.begin(), __l.size()); }
  1154. #endif // C++11
  1155. /**
  1156. * @brief Append a range of characters.
  1157. * @param __first Iterator referencing the first character to append.
  1158. * @param __last Iterator marking the end of the range.
  1159. * @return Reference to this string.
  1160. *
  1161. * Appends characters in the range [__first,__last) to this string.
  1162. */
  1163. #if __cplusplus >= 201103L
  1164. template<class _InputIterator,
  1165. typename = std::_RequireInputIter<_InputIterator>>
  1166. #else
  1167. template<class _InputIterator>
  1168. #endif
  1169. basic_string&
  1170. append(_InputIterator __first, _InputIterator __last)
  1171. { return this->replace(end(), end(), __first, __last); }
  1172. #if __cplusplus >= 201703L
  1173. /**
  1174. * @brief Append a string_view.
  1175. * @param __svt An object convertible to string_view to be appended.
  1176. * @return Reference to this string.
  1177. */
  1178. template<typename _Tp>
  1179. _If_sv<_Tp, basic_string&>
  1180. append(const _Tp& __svt)
  1181. {
  1182. __sv_type __sv = __svt;
  1183. return this->append(__sv.data(), __sv.size());
  1184. }
  1185. /**
  1186. * @brief Append a range of characters from a string_view.
  1187. * @param __svt An object convertible to string_view to be appended from.
  1188. * @param __pos The position in the string_view to append from.
  1189. * @param __n The number of characters to append from the string_view.
  1190. * @return Reference to this string.
  1191. */
  1192. template<typename _Tp>
  1193. _If_sv<_Tp, basic_string&>
  1194. append(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1195. {
  1196. __sv_type __sv = __svt;
  1197. return _M_append(__sv.data()
  1198. + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
  1199. std::__sv_limit(__sv.size(), __pos, __n));
  1200. }
  1201. #endif // C++17
  1202. /**
  1203. * @brief Append a single character.
  1204. * @param __c Character to append.
  1205. */
  1206. void
  1207. push_back(_CharT __c)
  1208. {
  1209. const size_type __size = this->size();
  1210. if (__size + 1 > this->capacity())
  1211. this->_M_mutate(__size, size_type(0), 0, size_type(1));
  1212. traits_type::assign(this->_M_data()[__size], __c);
  1213. this->_M_set_length(__size + 1);
  1214. }
  1215. /**
  1216. * @brief Set value to contents of another string.
  1217. * @param __str Source string to use.
  1218. * @return Reference to this string.
  1219. */
  1220. basic_string&
  1221. assign(const basic_string& __str)
  1222. {
  1223. this->_M_assign(__str);
  1224. return *this;
  1225. }
  1226. #if __cplusplus >= 201103L
  1227. /**
  1228. * @brief Set value to contents of another string.
  1229. * @param __str Source string to use.
  1230. * @return Reference to this string.
  1231. *
  1232. * This function sets this string to the exact contents of @a __str.
  1233. * @a __str is a valid, but unspecified string.
  1234. */
  1235. basic_string&
  1236. assign(basic_string&& __str)
  1237. noexcept(_Alloc_traits::_S_nothrow_move())
  1238. {
  1239. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1240. // 2063. Contradictory requirements for string move assignment
  1241. return *this = std::move(__str);
  1242. }
  1243. #endif // C++11
  1244. /**
  1245. * @brief Set value to a substring of a string.
  1246. * @param __str The string to use.
  1247. * @param __pos Index of the first character of str.
  1248. * @param __n Number of characters to use.
  1249. * @return Reference to this string.
  1250. * @throw std::out_of_range if @a pos is not a valid index.
  1251. *
  1252. * This function sets this string to the substring of @a __str
  1253. * consisting of @a __n characters at @a __pos. If @a __n is
  1254. * is larger than the number of available characters in @a
  1255. * __str, the remainder of @a __str is used.
  1256. */
  1257. basic_string&
  1258. assign(const basic_string& __str, size_type __pos, size_type __n = npos)
  1259. { return _M_replace(size_type(0), this->size(), __str._M_data()
  1260. + __str._M_check(__pos, "basic_string::assign"),
  1261. __str._M_limit(__pos, __n)); }
  1262. /**
  1263. * @brief Set value to a C substring.
  1264. * @param __s The C string to use.
  1265. * @param __n Number of characters to use.
  1266. * @return Reference to this string.
  1267. *
  1268. * This function sets the value of this string to the first @a __n
  1269. * characters of @a __s. If @a __n is is larger than the number of
  1270. * available characters in @a __s, the remainder of @a __s is used.
  1271. */
  1272. basic_string&
  1273. assign(const _CharT* __s, size_type __n)
  1274. {
  1275. __glibcxx_requires_string_len(__s, __n);
  1276. return _M_replace(size_type(0), this->size(), __s, __n);
  1277. }
  1278. /**
  1279. * @brief Set value to contents of a C string.
  1280. * @param __s The C string to use.
  1281. * @return Reference to this string.
  1282. *
  1283. * This function sets the value of this string to the value of @a __s.
  1284. * The data is copied, so there is no dependence on @a __s once the
  1285. * function returns.
  1286. */
  1287. basic_string&
  1288. assign(const _CharT* __s)
  1289. {
  1290. __glibcxx_requires_string(__s);
  1291. return _M_replace(size_type(0), this->size(), __s,
  1292. traits_type::length(__s));
  1293. }
  1294. /**
  1295. * @brief Set value to multiple characters.
  1296. * @param __n Length of the resulting string.
  1297. * @param __c The character to use.
  1298. * @return Reference to this string.
  1299. *
  1300. * This function sets the value of this string to @a __n copies of
  1301. * character @a __c.
  1302. */
  1303. basic_string&
  1304. assign(size_type __n, _CharT __c)
  1305. { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
  1306. /**
  1307. * @brief Set value to a range of characters.
  1308. * @param __first Iterator referencing the first character to append.
  1309. * @param __last Iterator marking the end of the range.
  1310. * @return Reference to this string.
  1311. *
  1312. * Sets value of string to characters in the range [__first,__last).
  1313. */
  1314. #if __cplusplus >= 201103L
  1315. template<class _InputIterator,
  1316. typename = std::_RequireInputIter<_InputIterator>>
  1317. #else
  1318. template<class _InputIterator>
  1319. #endif
  1320. basic_string&
  1321. assign(_InputIterator __first, _InputIterator __last)
  1322. { return this->replace(begin(), end(), __first, __last); }
  1323. #if __cplusplus >= 201103L
  1324. /**
  1325. * @brief Set value to an initializer_list of characters.
  1326. * @param __l The initializer_list of characters to assign.
  1327. * @return Reference to this string.
  1328. */
  1329. basic_string&
  1330. assign(initializer_list<_CharT> __l)
  1331. { return this->assign(__l.begin(), __l.size()); }
  1332. #endif // C++11
  1333. #if __cplusplus >= 201703L
  1334. /**
  1335. * @brief Set value from a string_view.
  1336. * @param __svt The source object convertible to string_view.
  1337. * @return Reference to this string.
  1338. */
  1339. template<typename _Tp>
  1340. _If_sv<_Tp, basic_string&>
  1341. assign(const _Tp& __svt)
  1342. {
  1343. __sv_type __sv = __svt;
  1344. return this->assign(__sv.data(), __sv.size());
  1345. }
  1346. /**
  1347. * @brief Set value from a range of characters in a string_view.
  1348. * @param __svt The source object convertible to string_view.
  1349. * @param __pos The position in the string_view to assign from.
  1350. * @param __n The number of characters to assign.
  1351. * @return Reference to this string.
  1352. */
  1353. template<typename _Tp>
  1354. _If_sv<_Tp, basic_string&>
  1355. assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1356. {
  1357. __sv_type __sv = __svt;
  1358. return _M_replace(size_type(0), this->size(),
  1359. __sv.data()
  1360. + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
  1361. std::__sv_limit(__sv.size(), __pos, __n));
  1362. }
  1363. #endif // C++17
  1364. #if __cplusplus >= 201103L
  1365. /**
  1366. * @brief Insert multiple characters.
  1367. * @param __p Const_iterator referencing location in string to
  1368. * insert at.
  1369. * @param __n Number of characters to insert
  1370. * @param __c The character to insert.
  1371. * @return Iterator referencing the first inserted char.
  1372. * @throw std::length_error If new length exceeds @c max_size().
  1373. *
  1374. * Inserts @a __n copies of character @a __c starting at the
  1375. * position referenced by iterator @a __p. If adding
  1376. * characters causes the length to exceed max_size(),
  1377. * length_error is thrown. The value of the string doesn't
  1378. * change if an error is thrown.
  1379. */
  1380. iterator
  1381. insert(const_iterator __p, size_type __n, _CharT __c)
  1382. {
  1383. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1384. const size_type __pos = __p - begin();
  1385. this->replace(__p, __p, __n, __c);
  1386. return iterator(this->_M_data() + __pos);
  1387. }
  1388. #else
  1389. /**
  1390. * @brief Insert multiple characters.
  1391. * @param __p Iterator referencing location in string to insert at.
  1392. * @param __n Number of characters to insert
  1393. * @param __c The character to insert.
  1394. * @throw std::length_error If new length exceeds @c max_size().
  1395. *
  1396. * Inserts @a __n copies of character @a __c starting at the
  1397. * position referenced by iterator @a __p. If adding
  1398. * characters causes the length to exceed max_size(),
  1399. * length_error is thrown. The value of the string doesn't
  1400. * change if an error is thrown.
  1401. */
  1402. void
  1403. insert(iterator __p, size_type __n, _CharT __c)
  1404. { this->replace(__p, __p, __n, __c); }
  1405. #endif
  1406. #if __cplusplus >= 201103L
  1407. /**
  1408. * @brief Insert a range of characters.
  1409. * @param __p Const_iterator referencing location in string to
  1410. * insert at.
  1411. * @param __beg Start of range.
  1412. * @param __end End of range.
  1413. * @return Iterator referencing the first inserted char.
  1414. * @throw std::length_error If new length exceeds @c max_size().
  1415. *
  1416. * Inserts characters in range [beg,end). If adding characters
  1417. * causes the length to exceed max_size(), length_error is
  1418. * thrown. The value of the string doesn't change if an error
  1419. * is thrown.
  1420. */
  1421. template<class _InputIterator,
  1422. typename = std::_RequireInputIter<_InputIterator>>
  1423. iterator
  1424. insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
  1425. {
  1426. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1427. const size_type __pos = __p - begin();
  1428. this->replace(__p, __p, __beg, __end);
  1429. return iterator(this->_M_data() + __pos);
  1430. }
  1431. #else
  1432. /**
  1433. * @brief Insert a range of characters.
  1434. * @param __p Iterator referencing location in string to insert at.
  1435. * @param __beg Start of range.
  1436. * @param __end End of range.
  1437. * @throw std::length_error If new length exceeds @c max_size().
  1438. *
  1439. * Inserts characters in range [__beg,__end). If adding
  1440. * characters causes the length to exceed max_size(),
  1441. * length_error is thrown. The value of the string doesn't
  1442. * change if an error is thrown.
  1443. */
  1444. template<class _InputIterator>
  1445. void
  1446. insert(iterator __p, _InputIterator __beg, _InputIterator __end)
  1447. { this->replace(__p, __p, __beg, __end); }
  1448. #endif
  1449. #if __cplusplus >= 201103L
  1450. /**
  1451. * @brief Insert an initializer_list of characters.
  1452. * @param __p Iterator referencing location in string to insert at.
  1453. * @param __l The initializer_list of characters to insert.
  1454. * @throw std::length_error If new length exceeds @c max_size().
  1455. */
  1456. iterator
  1457. insert(const_iterator __p, initializer_list<_CharT> __l)
  1458. { return this->insert(__p, __l.begin(), __l.end()); }
  1459. #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  1460. // See PR libstdc++/83328
  1461. void
  1462. insert(iterator __p, initializer_list<_CharT> __l)
  1463. {
  1464. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1465. this->insert(__p - begin(), __l.begin(), __l.size());
  1466. }
  1467. #endif
  1468. #endif // C++11
  1469. /**
  1470. * @brief Insert value of a string.
  1471. * @param __pos1 Iterator referencing location in string to insert at.
  1472. * @param __str The string to insert.
  1473. * @return Reference to this string.
  1474. * @throw std::length_error If new length exceeds @c max_size().
  1475. *
  1476. * Inserts value of @a __str starting at @a __pos1. If adding
  1477. * characters causes the length to exceed max_size(),
  1478. * length_error is thrown. The value of the string doesn't
  1479. * change if an error is thrown.
  1480. */
  1481. basic_string&
  1482. insert(size_type __pos1, const basic_string& __str)
  1483. { return this->replace(__pos1, size_type(0),
  1484. __str._M_data(), __str.size()); }
  1485. /**
  1486. * @brief Insert a substring.
  1487. * @param __pos1 Iterator referencing location in string to insert at.
  1488. * @param __str The string to insert.
  1489. * @param __pos2 Start of characters in str to insert.
  1490. * @param __n Number of characters to insert.
  1491. * @return Reference to this string.
  1492. * @throw std::length_error If new length exceeds @c max_size().
  1493. * @throw std::out_of_range If @a pos1 > size() or
  1494. * @a __pos2 > @a str.size().
  1495. *
  1496. * Starting at @a pos1, insert @a __n character of @a __str
  1497. * beginning with @a __pos2. If adding characters causes the
  1498. * length to exceed max_size(), length_error is thrown. If @a
  1499. * __pos1 is beyond the end of this string or @a __pos2 is
  1500. * beyond the end of @a __str, out_of_range is thrown. The
  1501. * value of the string doesn't change if an error is thrown.
  1502. */
  1503. basic_string&
  1504. insert(size_type __pos1, const basic_string& __str,
  1505. size_type __pos2, size_type __n = npos)
  1506. { return this->replace(__pos1, size_type(0), __str._M_data()
  1507. + __str._M_check(__pos2, "basic_string::insert"),
  1508. __str._M_limit(__pos2, __n)); }
  1509. /**
  1510. * @brief Insert a C substring.
  1511. * @param __pos Iterator referencing location in string to insert at.
  1512. * @param __s The C string to insert.
  1513. * @param __n The number of characters to insert.
  1514. * @return Reference to this string.
  1515. * @throw std::length_error If new length exceeds @c max_size().
  1516. * @throw std::out_of_range If @a __pos is beyond the end of this
  1517. * string.
  1518. *
  1519. * Inserts the first @a __n characters of @a __s starting at @a
  1520. * __pos. If adding characters causes the length to exceed
  1521. * max_size(), length_error is thrown. If @a __pos is beyond
  1522. * end(), out_of_range is thrown. The value of the string
  1523. * doesn't change if an error is thrown.
  1524. */
  1525. basic_string&
  1526. insert(size_type __pos, const _CharT* __s, size_type __n)
  1527. { return this->replace(__pos, size_type(0), __s, __n); }
  1528. /**
  1529. * @brief Insert a C string.
  1530. * @param __pos Iterator referencing location in string to insert at.
  1531. * @param __s The C string to insert.
  1532. * @return Reference to this string.
  1533. * @throw std::length_error If new length exceeds @c max_size().
  1534. * @throw std::out_of_range If @a pos is beyond the end of this
  1535. * string.
  1536. *
  1537. * Inserts the first @a n characters of @a __s starting at @a __pos. If
  1538. * adding characters causes the length to exceed max_size(),
  1539. * length_error is thrown. If @a __pos is beyond end(), out_of_range is
  1540. * thrown. The value of the string doesn't change if an error is
  1541. * thrown.
  1542. */
  1543. basic_string&
  1544. insert(size_type __pos, const _CharT* __s)
  1545. {
  1546. __glibcxx_requires_string(__s);
  1547. return this->replace(__pos, size_type(0), __s,
  1548. traits_type::length(__s));
  1549. }
  1550. /**
  1551. * @brief Insert multiple characters.
  1552. * @param __pos Index in string to insert at.
  1553. * @param __n Number of characters to insert
  1554. * @param __c The character to insert.
  1555. * @return Reference to this string.
  1556. * @throw std::length_error If new length exceeds @c max_size().
  1557. * @throw std::out_of_range If @a __pos is beyond the end of this
  1558. * string.
  1559. *
  1560. * Inserts @a __n copies of character @a __c starting at index
  1561. * @a __pos. If adding characters causes the length to exceed
  1562. * max_size(), length_error is thrown. If @a __pos > length(),
  1563. * out_of_range is thrown. The value of the string doesn't
  1564. * change if an error is thrown.
  1565. */
  1566. basic_string&
  1567. insert(size_type __pos, size_type __n, _CharT __c)
  1568. { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
  1569. size_type(0), __n, __c); }
  1570. /**
  1571. * @brief Insert one character.
  1572. * @param __p Iterator referencing position in string to insert at.
  1573. * @param __c The character to insert.
  1574. * @return Iterator referencing newly inserted char.
  1575. * @throw std::length_error If new length exceeds @c max_size().
  1576. *
  1577. * Inserts character @a __c at position referenced by @a __p.
  1578. * If adding character causes the length to exceed max_size(),
  1579. * length_error is thrown. If @a __p is beyond end of string,
  1580. * out_of_range is thrown. The value of the string doesn't
  1581. * change if an error is thrown.
  1582. */
  1583. iterator
  1584. insert(__const_iterator __p, _CharT __c)
  1585. {
  1586. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1587. const size_type __pos = __p - begin();
  1588. _M_replace_aux(__pos, size_type(0), size_type(1), __c);
  1589. return iterator(_M_data() + __pos);
  1590. }
  1591. #if __cplusplus >= 201703L
  1592. /**
  1593. * @brief Insert a string_view.
  1594. * @param __pos Iterator referencing position in string to insert at.
  1595. * @param __svt The object convertible to string_view to insert.
  1596. * @return Reference to this string.
  1597. */
  1598. template<typename _Tp>
  1599. _If_sv<_Tp, basic_string&>
  1600. insert(size_type __pos, const _Tp& __svt)
  1601. {
  1602. __sv_type __sv = __svt;
  1603. return this->insert(__pos, __sv.data(), __sv.size());
  1604. }
  1605. /**
  1606. * @brief Insert a string_view.
  1607. * @param __pos Iterator referencing position in string to insert at.
  1608. * @param __svt The object convertible to string_view to insert from.
  1609. * @param __pos Iterator referencing position in string_view to insert
  1610. * from.
  1611. * @param __n The number of characters to insert.
  1612. * @return Reference to this string.
  1613. */
  1614. template<typename _Tp>
  1615. _If_sv<_Tp, basic_string&>
  1616. insert(size_type __pos1, const _Tp& __svt,
  1617. size_type __pos2, size_type __n = npos)
  1618. {
  1619. __sv_type __sv = __svt;
  1620. return this->replace(__pos1, size_type(0),
  1621. __sv.data()
  1622. + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
  1623. std::__sv_limit(__sv.size(), __pos2, __n));
  1624. }
  1625. #endif // C++17
  1626. /**
  1627. * @brief Remove characters.
  1628. * @param __pos Index of first character to remove (default 0).
  1629. * @param __n Number of characters to remove (default remainder).
  1630. * @return Reference to this string.
  1631. * @throw std::out_of_range If @a pos is beyond the end of this
  1632. * string.
  1633. *
  1634. * Removes @a __n characters from this string starting at @a
  1635. * __pos. The length of the string is reduced by @a __n. If
  1636. * there are < @a __n characters to remove, the remainder of
  1637. * the string is truncated. If @a __p is beyond end of string,
  1638. * out_of_range is thrown. The value of the string doesn't
  1639. * change if an error is thrown.
  1640. */
  1641. basic_string&
  1642. erase(size_type __pos = 0, size_type __n = npos)
  1643. {
  1644. _M_check(__pos, "basic_string::erase");
  1645. if (__n == npos)
  1646. this->_M_set_length(__pos);
  1647. else if (__n != 0)
  1648. this->_M_erase(__pos, _M_limit(__pos, __n));
  1649. return *this;
  1650. }
  1651. /**
  1652. * @brief Remove one character.
  1653. * @param __position Iterator referencing the character to remove.
  1654. * @return iterator referencing same location after removal.
  1655. *
  1656. * Removes the character at @a __position from this string. The value
  1657. * of the string doesn't change if an error is thrown.
  1658. */
  1659. iterator
  1660. erase(__const_iterator __position)
  1661. {
  1662. _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
  1663. && __position < end());
  1664. const size_type __pos = __position - begin();
  1665. this->_M_erase(__pos, size_type(1));
  1666. return iterator(_M_data() + __pos);
  1667. }
  1668. /**
  1669. * @brief Remove a range of characters.
  1670. * @param __first Iterator referencing the first character to remove.
  1671. * @param __last Iterator referencing the end of the range.
  1672. * @return Iterator referencing location of first after removal.
  1673. *
  1674. * Removes the characters in the range [first,last) from this string.
  1675. * The value of the string doesn't change if an error is thrown.
  1676. */
  1677. iterator
  1678. erase(__const_iterator __first, __const_iterator __last)
  1679. {
  1680. _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
  1681. && __last <= end());
  1682. const size_type __pos = __first - begin();
  1683. if (__last == end())
  1684. this->_M_set_length(__pos);
  1685. else
  1686. this->_M_erase(__pos, __last - __first);
  1687. return iterator(this->_M_data() + __pos);
  1688. }
  1689. #if __cplusplus >= 201103L
  1690. /**
  1691. * @brief Remove the last character.
  1692. *
  1693. * The string must be non-empty.
  1694. */
  1695. void
  1696. pop_back() noexcept
  1697. {
  1698. __glibcxx_assert(!empty());
  1699. _M_erase(size() - 1, 1);
  1700. }
  1701. #endif // C++11
  1702. /**
  1703. * @brief Replace characters with value from another string.
  1704. * @param __pos Index of first character to replace.
  1705. * @param __n Number of characters to be replaced.
  1706. * @param __str String to insert.
  1707. * @return Reference to this string.
  1708. * @throw std::out_of_range If @a pos is beyond the end of this
  1709. * string.
  1710. * @throw std::length_error If new length exceeds @c max_size().
  1711. *
  1712. * Removes the characters in the range [__pos,__pos+__n) from
  1713. * this string. In place, the value of @a __str is inserted.
  1714. * If @a __pos is beyond end of string, out_of_range is thrown.
  1715. * If the length of the result exceeds max_size(), length_error
  1716. * is thrown. The value of the string doesn't change if an
  1717. * error is thrown.
  1718. */
  1719. basic_string&
  1720. replace(size_type __pos, size_type __n, const basic_string& __str)
  1721. { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
  1722. /**
  1723. * @brief Replace characters with value from another string.
  1724. * @param __pos1 Index of first character to replace.
  1725. * @param __n1 Number of characters to be replaced.
  1726. * @param __str String to insert.
  1727. * @param __pos2 Index of first character of str to use.
  1728. * @param __n2 Number of characters from str to use.
  1729. * @return Reference to this string.
  1730. * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
  1731. * __str.size().
  1732. * @throw std::length_error If new length exceeds @c max_size().
  1733. *
  1734. * Removes the characters in the range [__pos1,__pos1 + n) from this
  1735. * string. In place, the value of @a __str is inserted. If @a __pos is
  1736. * beyond end of string, out_of_range is thrown. If the length of the
  1737. * result exceeds max_size(), length_error is thrown. The value of the
  1738. * string doesn't change if an error is thrown.
  1739. */
  1740. basic_string&
  1741. replace(size_type __pos1, size_type __n1, const basic_string& __str,
  1742. size_type __pos2, size_type __n2 = npos)
  1743. { return this->replace(__pos1, __n1, __str._M_data()
  1744. + __str._M_check(__pos2, "basic_string::replace"),
  1745. __str._M_limit(__pos2, __n2)); }
  1746. /**
  1747. * @brief Replace characters with value of a C substring.
  1748. * @param __pos Index of first character to replace.
  1749. * @param __n1 Number of characters to be replaced.
  1750. * @param __s C string to insert.
  1751. * @param __n2 Number of characters from @a s to use.
  1752. * @return Reference to this string.
  1753. * @throw std::out_of_range If @a pos1 > size().
  1754. * @throw std::length_error If new length exceeds @c max_size().
  1755. *
  1756. * Removes the characters in the range [__pos,__pos + __n1)
  1757. * from this string. In place, the first @a __n2 characters of
  1758. * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
  1759. * @a __pos is beyond end of string, out_of_range is thrown. If
  1760. * the length of result exceeds max_size(), length_error is
  1761. * thrown. The value of the string doesn't change if an error
  1762. * is thrown.
  1763. */
  1764. basic_string&
  1765. replace(size_type __pos, size_type __n1, const _CharT* __s,
  1766. size_type __n2)
  1767. {
  1768. __glibcxx_requires_string_len(__s, __n2);
  1769. return _M_replace(_M_check(__pos, "basic_string::replace"),
  1770. _M_limit(__pos, __n1), __s, __n2);
  1771. }
  1772. /**
  1773. * @brief Replace characters with value of a C string.
  1774. * @param __pos Index of first character to replace.
  1775. * @param __n1 Number of characters to be replaced.
  1776. * @param __s C string to insert.
  1777. * @return Reference to this string.
  1778. * @throw std::out_of_range If @a pos > size().
  1779. * @throw std::length_error If new length exceeds @c max_size().
  1780. *
  1781. * Removes the characters in the range [__pos,__pos + __n1)
  1782. * from this string. In place, the characters of @a __s are
  1783. * inserted. If @a __pos is beyond end of string, out_of_range
  1784. * is thrown. If the length of result exceeds max_size(),
  1785. * length_error is thrown. The value of the string doesn't
  1786. * change if an error is thrown.
  1787. */
  1788. basic_string&
  1789. replace(size_type __pos, size_type __n1, const _CharT* __s)
  1790. {
  1791. __glibcxx_requires_string(__s);
  1792. return this->replace(__pos, __n1, __s, traits_type::length(__s));
  1793. }
  1794. /**
  1795. * @brief Replace characters with multiple characters.
  1796. * @param __pos Index of first character to replace.
  1797. * @param __n1 Number of characters to be replaced.
  1798. * @param __n2 Number of characters to insert.
  1799. * @param __c Character to insert.
  1800. * @return Reference to this string.
  1801. * @throw std::out_of_range If @a __pos > size().
  1802. * @throw std::length_error If new length exceeds @c max_size().
  1803. *
  1804. * Removes the characters in the range [pos,pos + n1) from this
  1805. * string. In place, @a __n2 copies of @a __c are inserted.
  1806. * If @a __pos is beyond end of string, out_of_range is thrown.
  1807. * If the length of result exceeds max_size(), length_error is
  1808. * thrown. The value of the string doesn't change if an error
  1809. * is thrown.
  1810. */
  1811. basic_string&
  1812. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  1813. { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
  1814. _M_limit(__pos, __n1), __n2, __c); }
  1815. /**
  1816. * @brief Replace range of characters with string.
  1817. * @param __i1 Iterator referencing start of range to replace.
  1818. * @param __i2 Iterator referencing end of range to replace.
  1819. * @param __str String value to insert.
  1820. * @return Reference to this string.
  1821. * @throw std::length_error If new length exceeds @c max_size().
  1822. *
  1823. * Removes the characters in the range [__i1,__i2). In place,
  1824. * the value of @a __str is inserted. If the length of result
  1825. * exceeds max_size(), length_error is thrown. The value of
  1826. * the string doesn't change if an error is thrown.
  1827. */
  1828. basic_string&
  1829. replace(__const_iterator __i1, __const_iterator __i2,
  1830. const basic_string& __str)
  1831. { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
  1832. /**
  1833. * @brief Replace range of characters with C substring.
  1834. * @param __i1 Iterator referencing start of range to replace.
  1835. * @param __i2 Iterator referencing end of range to replace.
  1836. * @param __s C string value to insert.
  1837. * @param __n Number of characters from s to insert.
  1838. * @return Reference to this string.
  1839. * @throw std::length_error If new length exceeds @c max_size().
  1840. *
  1841. * Removes the characters in the range [__i1,__i2). In place,
  1842. * the first @a __n characters of @a __s are inserted. If the
  1843. * length of result exceeds max_size(), length_error is thrown.
  1844. * The value of the string doesn't change if an error is
  1845. * thrown.
  1846. */
  1847. basic_string&
  1848. replace(__const_iterator __i1, __const_iterator __i2,
  1849. const _CharT* __s, size_type __n)
  1850. {
  1851. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1852. && __i2 <= end());
  1853. return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
  1854. }
  1855. /**
  1856. * @brief Replace range of characters with C string.
  1857. * @param __i1 Iterator referencing start of range to replace.
  1858. * @param __i2 Iterator referencing end of range to replace.
  1859. * @param __s C string value to insert.
  1860. * @return Reference to this string.
  1861. * @throw std::length_error If new length exceeds @c max_size().
  1862. *
  1863. * Removes the characters in the range [__i1,__i2). In place,
  1864. * the characters of @a __s are inserted. If the length of
  1865. * result exceeds max_size(), length_error is thrown. The
  1866. * value of the string doesn't change if an error is thrown.
  1867. */
  1868. basic_string&
  1869. replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
  1870. {
  1871. __glibcxx_requires_string(__s);
  1872. return this->replace(__i1, __i2, __s, traits_type::length(__s));
  1873. }
  1874. /**
  1875. * @brief Replace range of characters with multiple characters
  1876. * @param __i1 Iterator referencing start of range to replace.
  1877. * @param __i2 Iterator referencing end of range to replace.
  1878. * @param __n Number of characters to insert.
  1879. * @param __c Character to insert.
  1880. * @return Reference to this string.
  1881. * @throw std::length_error If new length exceeds @c max_size().
  1882. *
  1883. * Removes the characters in the range [__i1,__i2). In place,
  1884. * @a __n copies of @a __c are inserted. If the length of
  1885. * result exceeds max_size(), length_error is thrown. The
  1886. * value of the string doesn't change if an error is thrown.
  1887. */
  1888. basic_string&
  1889. replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
  1890. _CharT __c)
  1891. {
  1892. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1893. && __i2 <= end());
  1894. return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
  1895. }
  1896. /**
  1897. * @brief Replace range of characters with range.
  1898. * @param __i1 Iterator referencing start of range to replace.
  1899. * @param __i2 Iterator referencing end of range to replace.
  1900. * @param __k1 Iterator referencing start of range to insert.
  1901. * @param __k2 Iterator referencing end of range to insert.
  1902. * @return Reference to this string.
  1903. * @throw std::length_error If new length exceeds @c max_size().
  1904. *
  1905. * Removes the characters in the range [__i1,__i2). In place,
  1906. * characters in the range [__k1,__k2) are inserted. If the
  1907. * length of result exceeds max_size(), length_error is thrown.
  1908. * The value of the string doesn't change if an error is
  1909. * thrown.
  1910. */
  1911. #if __cplusplus >= 201103L
  1912. template<class _InputIterator,
  1913. typename = std::_RequireInputIter<_InputIterator>>
  1914. basic_string&
  1915. replace(const_iterator __i1, const_iterator __i2,
  1916. _InputIterator __k1, _InputIterator __k2)
  1917. {
  1918. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1919. && __i2 <= end());
  1920. __glibcxx_requires_valid_range(__k1, __k2);
  1921. return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
  1922. std::__false_type());
  1923. }
  1924. #else
  1925. template<class _InputIterator>
  1926. #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
  1927. typename __enable_if_not_native_iterator<_InputIterator>::__type
  1928. #else
  1929. basic_string&
  1930. #endif
  1931. replace(iterator __i1, iterator __i2,
  1932. _InputIterator __k1, _InputIterator __k2)
  1933. {
  1934. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1935. && __i2 <= end());
  1936. __glibcxx_requires_valid_range(__k1, __k2);
  1937. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1938. return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
  1939. }
  1940. #endif
  1941. // Specializations for the common case of pointer and iterator:
  1942. // useful to avoid the overhead of temporary buffering in _M_replace.
  1943. basic_string&
  1944. replace(__const_iterator __i1, __const_iterator __i2,
  1945. _CharT* __k1, _CharT* __k2)
  1946. {
  1947. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1948. && __i2 <= end());
  1949. __glibcxx_requires_valid_range(__k1, __k2);
  1950. return this->replace(__i1 - begin(), __i2 - __i1,
  1951. __k1, __k2 - __k1);
  1952. }
  1953. basic_string&
  1954. replace(__const_iterator __i1, __const_iterator __i2,
  1955. const _CharT* __k1, const _CharT* __k2)
  1956. {
  1957. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1958. && __i2 <= end());
  1959. __glibcxx_requires_valid_range(__k1, __k2);
  1960. return this->replace(__i1 - begin(), __i2 - __i1,
  1961. __k1, __k2 - __k1);
  1962. }
  1963. basic_string&
  1964. replace(__const_iterator __i1, __const_iterator __i2,
  1965. iterator __k1, iterator __k2)
  1966. {
  1967. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1968. && __i2 <= end());
  1969. __glibcxx_requires_valid_range(__k1, __k2);
  1970. return this->replace(__i1 - begin(), __i2 - __i1,
  1971. __k1.base(), __k2 - __k1);
  1972. }
  1973. basic_string&
  1974. replace(__const_iterator __i1, __const_iterator __i2,
  1975. const_iterator __k1, const_iterator __k2)
  1976. {
  1977. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1978. && __i2 <= end());
  1979. __glibcxx_requires_valid_range(__k1, __k2);
  1980. return this->replace(__i1 - begin(), __i2 - __i1,
  1981. __k1.base(), __k2 - __k1);
  1982. }
  1983. #if __cplusplus >= 201103L
  1984. /**
  1985. * @brief Replace range of characters with initializer_list.
  1986. * @param __i1 Iterator referencing start of range to replace.
  1987. * @param __i2 Iterator referencing end of range to replace.
  1988. * @param __l The initializer_list of characters to insert.
  1989. * @return Reference to this string.
  1990. * @throw std::length_error If new length exceeds @c max_size().
  1991. *
  1992. * Removes the characters in the range [__i1,__i2). In place,
  1993. * characters in the range [__k1,__k2) are inserted. If the
  1994. * length of result exceeds max_size(), length_error is thrown.
  1995. * The value of the string doesn't change if an error is
  1996. * thrown.
  1997. */
  1998. basic_string& replace(const_iterator __i1, const_iterator __i2,
  1999. initializer_list<_CharT> __l)
  2000. { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
  2001. #endif // C++11
  2002. #if __cplusplus >= 201703L
  2003. /**
  2004. * @brief Replace range of characters with string_view.
  2005. * @param __pos The position to replace at.
  2006. * @param __n The number of characters to replace.
  2007. * @param __svt The object convertible to string_view to insert.
  2008. * @return Reference to this string.
  2009. */
  2010. template<typename _Tp>
  2011. _If_sv<_Tp, basic_string&>
  2012. replace(size_type __pos, size_type __n, const _Tp& __svt)
  2013. {
  2014. __sv_type __sv = __svt;
  2015. return this->replace(__pos, __n, __sv.data(), __sv.size());
  2016. }
  2017. /**
  2018. * @brief Replace range of characters with string_view.
  2019. * @param __pos1 The position to replace at.
  2020. * @param __n1 The number of characters to replace.
  2021. * @param __svt The object convertible to string_view to insert from.
  2022. * @param __pos2 The position in the string_view to insert from.
  2023. * @param __n2 The number of characters to insert.
  2024. * @return Reference to this string.
  2025. */
  2026. template<typename _Tp>
  2027. _If_sv<_Tp, basic_string&>
  2028. replace(size_type __pos1, size_type __n1, const _Tp& __svt,
  2029. size_type __pos2, size_type __n2 = npos)
  2030. {
  2031. __sv_type __sv = __svt;
  2032. return this->replace(__pos1, __n1,
  2033. __sv.data()
  2034. + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
  2035. std::__sv_limit(__sv.size(), __pos2, __n2));
  2036. }
  2037. /**
  2038. * @brief Replace range of characters with string_view.
  2039. * @param __i1 An iterator referencing the start position
  2040. to replace at.
  2041. * @param __i2 An iterator referencing the end position
  2042. for the replace.
  2043. * @param __svt The object convertible to string_view to insert from.
  2044. * @return Reference to this string.
  2045. */
  2046. template<typename _Tp>
  2047. _If_sv<_Tp, basic_string&>
  2048. replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
  2049. {
  2050. __sv_type __sv = __svt;
  2051. return this->replace(__i1 - begin(), __i2 - __i1, __sv);
  2052. }
  2053. #endif // C++17
  2054. private:
  2055. template<class _Integer>
  2056. basic_string&
  2057. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  2058. _Integer __n, _Integer __val, __true_type)
  2059. { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
  2060. template<class _InputIterator>
  2061. basic_string&
  2062. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  2063. _InputIterator __k1, _InputIterator __k2,
  2064. __false_type);
  2065. basic_string&
  2066. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  2067. _CharT __c);
  2068. basic_string&
  2069. _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
  2070. const size_type __len2);
  2071. basic_string&
  2072. _M_append(const _CharT* __s, size_type __n);
  2073. public:
  2074. /**
  2075. * @brief Copy substring into C string.
  2076. * @param __s C string to copy value into.
  2077. * @param __n Number of characters to copy.
  2078. * @param __pos Index of first character to copy.
  2079. * @return Number of characters actually copied
  2080. * @throw std::out_of_range If __pos > size().
  2081. *
  2082. * Copies up to @a __n characters starting at @a __pos into the
  2083. * C string @a __s. If @a __pos is %greater than size(),
  2084. * out_of_range is thrown.
  2085. */
  2086. size_type
  2087. copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
  2088. /**
  2089. * @brief Swap contents with another string.
  2090. * @param __s String to swap with.
  2091. *
  2092. * Exchanges the contents of this string with that of @a __s in constant
  2093. * time.
  2094. */
  2095. void
  2096. swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
  2097. // String operations:
  2098. /**
  2099. * @brief Return const pointer to null-terminated contents.
  2100. *
  2101. * This is a handle to internal data. Do not modify or dire things may
  2102. * happen.
  2103. */
  2104. const _CharT*
  2105. c_str() const _GLIBCXX_NOEXCEPT
  2106. { return _M_data(); }
  2107. /**
  2108. * @brief Return const pointer to contents.
  2109. *
  2110. * This is a pointer to internal data. It is undefined to modify
  2111. * the contents through the returned pointer. To get a pointer that
  2112. * allows modifying the contents use @c &str[0] instead,
  2113. * (or in C++17 the non-const @c str.data() overload).
  2114. */
  2115. const _CharT*
  2116. data() const _GLIBCXX_NOEXCEPT
  2117. { return _M_data(); }
  2118. #if __cplusplus >= 201703L
  2119. /**
  2120. * @brief Return non-const pointer to contents.
  2121. *
  2122. * This is a pointer to the character sequence held by the string.
  2123. * Modifying the characters in the sequence is allowed.
  2124. */
  2125. _CharT*
  2126. data() noexcept
  2127. { return _M_data(); }
  2128. #endif
  2129. /**
  2130. * @brief Return copy of allocator used to construct this string.
  2131. */
  2132. allocator_type
  2133. get_allocator() const _GLIBCXX_NOEXCEPT
  2134. { return _M_get_allocator(); }
  2135. /**
  2136. * @brief Find position of a C substring.
  2137. * @param __s C string to locate.
  2138. * @param __pos Index of character to search from.
  2139. * @param __n Number of characters from @a s to search for.
  2140. * @return Index of start of first occurrence.
  2141. *
  2142. * Starting from @a __pos, searches forward for the first @a
  2143. * __n characters in @a __s within this string. If found,
  2144. * returns the index where it begins. If not found, returns
  2145. * npos.
  2146. */
  2147. size_type
  2148. find(const _CharT* __s, size_type __pos, size_type __n) const
  2149. _GLIBCXX_NOEXCEPT;
  2150. /**
  2151. * @brief Find position of a string.
  2152. * @param __str String to locate.
  2153. * @param __pos Index of character to search from (default 0).
  2154. * @return Index of start of first occurrence.
  2155. *
  2156. * Starting from @a __pos, searches forward for value of @a __str within
  2157. * this string. If found, returns the index where it begins. If not
  2158. * found, returns npos.
  2159. */
  2160. size_type
  2161. find(const basic_string& __str, size_type __pos = 0) const
  2162. _GLIBCXX_NOEXCEPT
  2163. { return this->find(__str.data(), __pos, __str.size()); }
  2164. #if __cplusplus >= 201703L
  2165. /**
  2166. * @brief Find position of a string_view.
  2167. * @param __svt The object convertible to string_view to locate.
  2168. * @param __pos Index of character to search from (default 0).
  2169. * @return Index of start of first occurrence.
  2170. */
  2171. template<typename _Tp>
  2172. _If_sv<_Tp, size_type>
  2173. find(const _Tp& __svt, size_type __pos = 0) const
  2174. noexcept(is_same<_Tp, __sv_type>::value)
  2175. {
  2176. __sv_type __sv = __svt;
  2177. return this->find(__sv.data(), __pos, __sv.size());
  2178. }
  2179. #endif // C++17
  2180. /**
  2181. * @brief Find position of a C string.
  2182. * @param __s C string to locate.
  2183. * @param __pos Index of character to search from (default 0).
  2184. * @return Index of start of first occurrence.
  2185. *
  2186. * Starting from @a __pos, searches forward for the value of @a
  2187. * __s within this string. If found, returns the index where
  2188. * it begins. If not found, returns npos.
  2189. */
  2190. size_type
  2191. find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2192. {
  2193. __glibcxx_requires_string(__s);
  2194. return this->find(__s, __pos, traits_type::length(__s));
  2195. }
  2196. /**
  2197. * @brief Find position of a character.
  2198. * @param __c Character to locate.
  2199. * @param __pos Index of character to search from (default 0).
  2200. * @return Index of first occurrence.
  2201. *
  2202. * Starting from @a __pos, searches forward for @a __c within
  2203. * this string. If found, returns the index where it was
  2204. * found. If not found, returns npos.
  2205. */
  2206. size_type
  2207. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
  2208. /**
  2209. * @brief Find last position of a string.
  2210. * @param __str String to locate.
  2211. * @param __pos Index of character to search back from (default end).
  2212. * @return Index of start of last occurrence.
  2213. *
  2214. * Starting from @a __pos, searches backward for value of @a
  2215. * __str within this string. If found, returns the index where
  2216. * it begins. If not found, returns npos.
  2217. */
  2218. size_type
  2219. rfind(const basic_string& __str, size_type __pos = npos) const
  2220. _GLIBCXX_NOEXCEPT
  2221. { return this->rfind(__str.data(), __pos, __str.size()); }
  2222. #if __cplusplus >= 201703L
  2223. /**
  2224. * @brief Find last position of a string_view.
  2225. * @param __svt The object convertible to string_view to locate.
  2226. * @param __pos Index of character to search back from (default end).
  2227. * @return Index of start of last occurrence.
  2228. */
  2229. template<typename _Tp>
  2230. _If_sv<_Tp, size_type>
  2231. rfind(const _Tp& __svt, size_type __pos = npos) const
  2232. noexcept(is_same<_Tp, __sv_type>::value)
  2233. {
  2234. __sv_type __sv = __svt;
  2235. return this->rfind(__sv.data(), __pos, __sv.size());
  2236. }
  2237. #endif // C++17
  2238. /**
  2239. * @brief Find last position of a C substring.
  2240. * @param __s C string to locate.
  2241. * @param __pos Index of character to search back from.
  2242. * @param __n Number of characters from s to search for.
  2243. * @return Index of start of last occurrence.
  2244. *
  2245. * Starting from @a __pos, searches backward for the first @a
  2246. * __n characters in @a __s within this string. If found,
  2247. * returns the index where it begins. If not found, returns
  2248. * npos.
  2249. */
  2250. size_type
  2251. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  2252. _GLIBCXX_NOEXCEPT;
  2253. /**
  2254. * @brief Find last position of a C string.
  2255. * @param __s C string to locate.
  2256. * @param __pos Index of character to start search at (default end).
  2257. * @return Index of start of last occurrence.
  2258. *
  2259. * Starting from @a __pos, searches backward for the value of
  2260. * @a __s within this string. If found, returns the index
  2261. * where it begins. If not found, returns npos.
  2262. */
  2263. size_type
  2264. rfind(const _CharT* __s, size_type __pos = npos) const
  2265. {
  2266. __glibcxx_requires_string(__s);
  2267. return this->rfind(__s, __pos, traits_type::length(__s));
  2268. }
  2269. /**
  2270. * @brief Find last position of a character.
  2271. * @param __c Character to locate.
  2272. * @param __pos Index of character to search back from (default end).
  2273. * @return Index of last occurrence.
  2274. *
  2275. * Starting from @a __pos, searches backward for @a __c within
  2276. * this string. If found, returns the index where it was
  2277. * found. If not found, returns npos.
  2278. */
  2279. size_type
  2280. rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
  2281. /**
  2282. * @brief Find position of a character of string.
  2283. * @param __str String containing characters to locate.
  2284. * @param __pos Index of character to search from (default 0).
  2285. * @return Index of first occurrence.
  2286. *
  2287. * Starting from @a __pos, searches forward for one of the
  2288. * characters of @a __str within this string. If found,
  2289. * returns the index where it was found. If not found, returns
  2290. * npos.
  2291. */
  2292. size_type
  2293. find_first_of(const basic_string& __str, size_type __pos = 0) const
  2294. _GLIBCXX_NOEXCEPT
  2295. { return this->find_first_of(__str.data(), __pos, __str.size()); }
  2296. #if __cplusplus >= 201703L
  2297. /**
  2298. * @brief Find position of a character of a string_view.
  2299. * @param __svt An object convertible to string_view containing
  2300. * characters to locate.
  2301. * @param __pos Index of character to search from (default 0).
  2302. * @return Index of first occurrence.
  2303. */
  2304. template<typename _Tp>
  2305. _If_sv<_Tp, size_type>
  2306. find_first_of(const _Tp& __svt, size_type __pos = 0) const
  2307. noexcept(is_same<_Tp, __sv_type>::value)
  2308. {
  2309. __sv_type __sv = __svt;
  2310. return this->find_first_of(__sv.data(), __pos, __sv.size());
  2311. }
  2312. #endif // C++17
  2313. /**
  2314. * @brief Find position of a character of C substring.
  2315. * @param __s String containing characters to locate.
  2316. * @param __pos Index of character to search from.
  2317. * @param __n Number of characters from s to search for.
  2318. * @return Index of first occurrence.
  2319. *
  2320. * Starting from @a __pos, searches forward for one of the
  2321. * first @a __n characters of @a __s within this string. If
  2322. * found, returns the index where it was found. If not found,
  2323. * returns npos.
  2324. */
  2325. size_type
  2326. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  2327. _GLIBCXX_NOEXCEPT;
  2328. /**
  2329. * @brief Find position of a character of C string.
  2330. * @param __s String containing characters to locate.
  2331. * @param __pos Index of character to search from (default 0).
  2332. * @return Index of first occurrence.
  2333. *
  2334. * Starting from @a __pos, searches forward for one of the
  2335. * characters of @a __s within this string. If found, returns
  2336. * the index where it was found. If not found, returns npos.
  2337. */
  2338. size_type
  2339. find_first_of(const _CharT* __s, size_type __pos = 0) const
  2340. _GLIBCXX_NOEXCEPT
  2341. {
  2342. __glibcxx_requires_string(__s);
  2343. return this->find_first_of(__s, __pos, traits_type::length(__s));
  2344. }
  2345. /**
  2346. * @brief Find position of a character.
  2347. * @param __c Character to locate.
  2348. * @param __pos Index of character to search from (default 0).
  2349. * @return Index of first occurrence.
  2350. *
  2351. * Starting from @a __pos, searches forward for the character
  2352. * @a __c within this string. If found, returns the index
  2353. * where it was found. If not found, returns npos.
  2354. *
  2355. * Note: equivalent to find(__c, __pos).
  2356. */
  2357. size_type
  2358. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2359. { return this->find(__c, __pos); }
  2360. /**
  2361. * @brief Find last position of a character of string.
  2362. * @param __str String containing characters to locate.
  2363. * @param __pos Index of character to search back from (default end).
  2364. * @return Index of last occurrence.
  2365. *
  2366. * Starting from @a __pos, searches backward for one of the
  2367. * characters of @a __str within this string. If found,
  2368. * returns the index where it was found. If not found, returns
  2369. * npos.
  2370. */
  2371. size_type
  2372. find_last_of(const basic_string& __str, size_type __pos = npos) const
  2373. _GLIBCXX_NOEXCEPT
  2374. { return this->find_last_of(__str.data(), __pos, __str.size()); }
  2375. #if __cplusplus >= 201703L
  2376. /**
  2377. * @brief Find last position of a character of string.
  2378. * @param __svt An object convertible to string_view containing
  2379. * characters to locate.
  2380. * @param __pos Index of character to search back from (default end).
  2381. * @return Index of last occurrence.
  2382. */
  2383. template<typename _Tp>
  2384. _If_sv<_Tp, size_type>
  2385. find_last_of(const _Tp& __svt, size_type __pos = npos) const
  2386. noexcept(is_same<_Tp, __sv_type>::value)
  2387. {
  2388. __sv_type __sv = __svt;
  2389. return this->find_last_of(__sv.data(), __pos, __sv.size());
  2390. }
  2391. #endif // C++17
  2392. /**
  2393. * @brief Find last position of a character of C substring.
  2394. * @param __s C string containing characters to locate.
  2395. * @param __pos Index of character to search back from.
  2396. * @param __n Number of characters from s to search for.
  2397. * @return Index of last occurrence.
  2398. *
  2399. * Starting from @a __pos, searches backward for one of the
  2400. * first @a __n characters of @a __s within this string. If
  2401. * found, returns the index where it was found. If not found,
  2402. * returns npos.
  2403. */
  2404. size_type
  2405. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  2406. _GLIBCXX_NOEXCEPT;
  2407. /**
  2408. * @brief Find last position of a character of C string.
  2409. * @param __s C string containing characters to locate.
  2410. * @param __pos Index of character to search back from (default end).
  2411. * @return Index of last occurrence.
  2412. *
  2413. * Starting from @a __pos, searches backward for one of the
  2414. * characters of @a __s within this string. If found, returns
  2415. * the index where it was found. If not found, returns npos.
  2416. */
  2417. size_type
  2418. find_last_of(const _CharT* __s, size_type __pos = npos) const
  2419. _GLIBCXX_NOEXCEPT
  2420. {
  2421. __glibcxx_requires_string(__s);
  2422. return this->find_last_of(__s, __pos, traits_type::length(__s));
  2423. }
  2424. /**
  2425. * @brief Find last position of a character.
  2426. * @param __c Character to locate.
  2427. * @param __pos Index of character to search back from (default end).
  2428. * @return Index of last occurrence.
  2429. *
  2430. * Starting from @a __pos, searches backward for @a __c within
  2431. * this string. If found, returns the index where it was
  2432. * found. If not found, returns npos.
  2433. *
  2434. * Note: equivalent to rfind(__c, __pos).
  2435. */
  2436. size_type
  2437. find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  2438. { return this->rfind(__c, __pos); }
  2439. /**
  2440. * @brief Find position of a character not in string.
  2441. * @param __str String containing characters to avoid.
  2442. * @param __pos Index of character to search from (default 0).
  2443. * @return Index of first occurrence.
  2444. *
  2445. * Starting from @a __pos, searches forward for a character not contained
  2446. * in @a __str within this string. If found, returns the index where it
  2447. * was found. If not found, returns npos.
  2448. */
  2449. size_type
  2450. find_first_not_of(const basic_string& __str, size_type __pos = 0) const
  2451. _GLIBCXX_NOEXCEPT
  2452. { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
  2453. #if __cplusplus >= 201703L
  2454. /**
  2455. * @brief Find position of a character not in a string_view.
  2456. * @param __svt A object convertible to string_view containing
  2457. * characters to avoid.
  2458. * @param __pos Index of character to search from (default 0).
  2459. * @return Index of first occurrence.
  2460. */
  2461. template<typename _Tp>
  2462. _If_sv<_Tp, size_type>
  2463. find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
  2464. noexcept(is_same<_Tp, __sv_type>::value)
  2465. {
  2466. __sv_type __sv = __svt;
  2467. return this->find_first_not_of(__sv.data(), __pos, __sv.size());
  2468. }
  2469. #endif // C++17
  2470. /**
  2471. * @brief Find position of a character not in C substring.
  2472. * @param __s C string containing characters to avoid.
  2473. * @param __pos Index of character to search from.
  2474. * @param __n Number of characters from __s to consider.
  2475. * @return Index of first occurrence.
  2476. *
  2477. * Starting from @a __pos, searches forward for a character not
  2478. * contained in the first @a __n characters of @a __s within
  2479. * this string. If found, returns the index where it was
  2480. * found. If not found, returns npos.
  2481. */
  2482. size_type
  2483. find_first_not_of(const _CharT* __s, size_type __pos,
  2484. size_type __n) const _GLIBCXX_NOEXCEPT;
  2485. /**
  2486. * @brief Find position of a character not in C string.
  2487. * @param __s C string containing characters to avoid.
  2488. * @param __pos Index of character to search from (default 0).
  2489. * @return Index of first occurrence.
  2490. *
  2491. * Starting from @a __pos, searches forward for a character not
  2492. * contained in @a __s within this string. If found, returns
  2493. * the index where it was found. If not found, returns npos.
  2494. */
  2495. size_type
  2496. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  2497. _GLIBCXX_NOEXCEPT
  2498. {
  2499. __glibcxx_requires_string(__s);
  2500. return this->find_first_not_of(__s, __pos, traits_type::length(__s));
  2501. }
  2502. /**
  2503. * @brief Find position of a different character.
  2504. * @param __c Character to avoid.
  2505. * @param __pos Index of character to search from (default 0).
  2506. * @return Index of first occurrence.
  2507. *
  2508. * Starting from @a __pos, searches forward for a character
  2509. * other than @a __c within this string. If found, returns the
  2510. * index where it was found. If not found, returns npos.
  2511. */
  2512. size_type
  2513. find_first_not_of(_CharT __c, size_type __pos = 0) const
  2514. _GLIBCXX_NOEXCEPT;
  2515. /**
  2516. * @brief Find last position of a character not in string.
  2517. * @param __str String containing characters to avoid.
  2518. * @param __pos Index of character to search back from (default end).
  2519. * @return Index of last occurrence.
  2520. *
  2521. * Starting from @a __pos, searches backward for a character
  2522. * not contained in @a __str within this string. If found,
  2523. * returns the index where it was found. If not found, returns
  2524. * npos.
  2525. */
  2526. size_type
  2527. find_last_not_of(const basic_string& __str, size_type __pos = npos) const
  2528. _GLIBCXX_NOEXCEPT
  2529. { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
  2530. #if __cplusplus >= 201703L
  2531. /**
  2532. * @brief Find last position of a character not in a string_view.
  2533. * @param __svt An object convertible to string_view containing
  2534. * characters to avoid.
  2535. * @param __pos Index of character to search back from (default end).
  2536. * @return Index of last occurrence.
  2537. */
  2538. template<typename _Tp>
  2539. _If_sv<_Tp, size_type>
  2540. find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
  2541. noexcept(is_same<_Tp, __sv_type>::value)
  2542. {
  2543. __sv_type __sv = __svt;
  2544. return this->find_last_not_of(__sv.data(), __pos, __sv.size());
  2545. }
  2546. #endif // C++17
  2547. /**
  2548. * @brief Find last position of a character not in C substring.
  2549. * @param __s C string containing characters to avoid.
  2550. * @param __pos Index of character to search back from.
  2551. * @param __n Number of characters from s to consider.
  2552. * @return Index of last occurrence.
  2553. *
  2554. * Starting from @a __pos, searches backward for a character not
  2555. * contained in the first @a __n characters of @a __s within this string.
  2556. * If found, returns the index where it was found. If not found,
  2557. * returns npos.
  2558. */
  2559. size_type
  2560. find_last_not_of(const _CharT* __s, size_type __pos,
  2561. size_type __n) const _GLIBCXX_NOEXCEPT;
  2562. /**
  2563. * @brief Find last position of a character not in C string.
  2564. * @param __s C string containing characters to avoid.
  2565. * @param __pos Index of character to search back from (default end).
  2566. * @return Index of last occurrence.
  2567. *
  2568. * Starting from @a __pos, searches backward for a character
  2569. * not contained in @a __s within this string. If found,
  2570. * returns the index where it was found. If not found, returns
  2571. * npos.
  2572. */
  2573. size_type
  2574. find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  2575. _GLIBCXX_NOEXCEPT
  2576. {
  2577. __glibcxx_requires_string(__s);
  2578. return this->find_last_not_of(__s, __pos, traits_type::length(__s));
  2579. }
  2580. /**
  2581. * @brief Find last position of a different character.
  2582. * @param __c Character to avoid.
  2583. * @param __pos Index of character to search back from (default end).
  2584. * @return Index of last occurrence.
  2585. *
  2586. * Starting from @a __pos, searches backward for a character other than
  2587. * @a __c within this string. If found, returns the index where it was
  2588. * found. If not found, returns npos.
  2589. */
  2590. size_type
  2591. find_last_not_of(_CharT __c, size_type __pos = npos) const
  2592. _GLIBCXX_NOEXCEPT;
  2593. /**
  2594. * @brief Get a substring.
  2595. * @param __pos Index of first character (default 0).
  2596. * @param __n Number of characters in substring (default remainder).
  2597. * @return The new string.
  2598. * @throw std::out_of_range If __pos > size().
  2599. *
  2600. * Construct and return a new string using the @a __n
  2601. * characters starting at @a __pos. If the string is too
  2602. * short, use the remainder of the characters. If @a __pos is
  2603. * beyond the end of the string, out_of_range is thrown.
  2604. */
  2605. basic_string
  2606. substr(size_type __pos = 0, size_type __n = npos) const
  2607. { return basic_string(*this,
  2608. _M_check(__pos, "basic_string::substr"), __n); }
  2609. /**
  2610. * @brief Compare to a string.
  2611. * @param __str String to compare against.
  2612. * @return Integer < 0, 0, or > 0.
  2613. *
  2614. * Returns an integer < 0 if this string is ordered before @a
  2615. * __str, 0 if their values are equivalent, or > 0 if this
  2616. * string is ordered after @a __str. Determines the effective
  2617. * length rlen of the strings to compare as the smallest of
  2618. * size() and str.size(). The function then compares the two
  2619. * strings by calling traits::compare(data(), str.data(),rlen).
  2620. * If the result of the comparison is nonzero returns it,
  2621. * otherwise the shorter one is ordered first.
  2622. */
  2623. int
  2624. compare(const basic_string& __str) const
  2625. {
  2626. const size_type __size = this->size();
  2627. const size_type __osize = __str.size();
  2628. const size_type __len = std::min(__size, __osize);
  2629. int __r = traits_type::compare(_M_data(), __str.data(), __len);
  2630. if (!__r)
  2631. __r = _S_compare(__size, __osize);
  2632. return __r;
  2633. }
  2634. #if __cplusplus >= 201703L
  2635. /**
  2636. * @brief Compare to a string_view.
  2637. * @param __svt An object convertible to string_view to compare against.
  2638. * @return Integer < 0, 0, or > 0.
  2639. */
  2640. template<typename _Tp>
  2641. _If_sv<_Tp, int>
  2642. compare(const _Tp& __svt) const
  2643. noexcept(is_same<_Tp, __sv_type>::value)
  2644. {
  2645. __sv_type __sv = __svt;
  2646. const size_type __size = this->size();
  2647. const size_type __osize = __sv.size();
  2648. const size_type __len = std::min(__size, __osize);
  2649. int __r = traits_type::compare(_M_data(), __sv.data(), __len);
  2650. if (!__r)
  2651. __r = _S_compare(__size, __osize);
  2652. return __r;
  2653. }
  2654. /**
  2655. * @brief Compare to a string_view.
  2656. * @param __pos A position in the string to start comparing from.
  2657. * @param __n The number of characters to compare.
  2658. * @param __svt An object convertible to string_view to compare
  2659. * against.
  2660. * @return Integer < 0, 0, or > 0.
  2661. */
  2662. template<typename _Tp>
  2663. _If_sv<_Tp, int>
  2664. compare(size_type __pos, size_type __n, const _Tp& __svt) const
  2665. noexcept(is_same<_Tp, __sv_type>::value)
  2666. {
  2667. __sv_type __sv = __svt;
  2668. return __sv_type(*this).substr(__pos, __n).compare(__sv);
  2669. }
  2670. /**
  2671. * @brief Compare to a string_view.
  2672. * @param __pos1 A position in the string to start comparing from.
  2673. * @param __n1 The number of characters to compare.
  2674. * @param __svt An object convertible to string_view to compare
  2675. * against.
  2676. * @param __pos2 A position in the string_view to start comparing from.
  2677. * @param __n2 The number of characters to compare.
  2678. * @return Integer < 0, 0, or > 0.
  2679. */
  2680. template<typename _Tp>
  2681. _If_sv<_Tp, int>
  2682. compare(size_type __pos1, size_type __n1, const _Tp& __svt,
  2683. size_type __pos2, size_type __n2 = npos) const
  2684. noexcept(is_same<_Tp, __sv_type>::value)
  2685. {
  2686. __sv_type __sv = __svt;
  2687. return __sv_type(*this)
  2688. .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  2689. }
  2690. #endif // C++17
  2691. /**
  2692. * @brief Compare substring to a string.
  2693. * @param __pos Index of first character of substring.
  2694. * @param __n Number of characters in substring.
  2695. * @param __str String to compare against.
  2696. * @return Integer < 0, 0, or > 0.
  2697. *
  2698. * Form the substring of this string from the @a __n characters
  2699. * starting at @a __pos. Returns an integer < 0 if the
  2700. * substring is ordered before @a __str, 0 if their values are
  2701. * equivalent, or > 0 if the substring is ordered after @a
  2702. * __str. Determines the effective length rlen of the strings
  2703. * to compare as the smallest of the length of the substring
  2704. * and @a __str.size(). The function then compares the two
  2705. * strings by calling
  2706. * traits::compare(substring.data(),str.data(),rlen). If the
  2707. * result of the comparison is nonzero returns it, otherwise
  2708. * the shorter one is ordered first.
  2709. */
  2710. int
  2711. compare(size_type __pos, size_type __n, const basic_string& __str) const;
  2712. /**
  2713. * @brief Compare substring to a substring.
  2714. * @param __pos1 Index of first character of substring.
  2715. * @param __n1 Number of characters in substring.
  2716. * @param __str String to compare against.
  2717. * @param __pos2 Index of first character of substring of str.
  2718. * @param __n2 Number of characters in substring of str.
  2719. * @return Integer < 0, 0, or > 0.
  2720. *
  2721. * Form the substring of this string from the @a __n1
  2722. * characters starting at @a __pos1. Form the substring of @a
  2723. * __str from the @a __n2 characters starting at @a __pos2.
  2724. * Returns an integer < 0 if this substring is ordered before
  2725. * the substring of @a __str, 0 if their values are equivalent,
  2726. * or > 0 if this substring is ordered after the substring of
  2727. * @a __str. Determines the effective length rlen of the
  2728. * strings to compare as the smallest of the lengths of the
  2729. * substrings. The function then compares the two strings by
  2730. * calling
  2731. * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
  2732. * If the result of the comparison is nonzero returns it,
  2733. * otherwise the shorter one is ordered first.
  2734. */
  2735. int
  2736. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  2737. size_type __pos2, size_type __n2 = npos) const;
  2738. /**
  2739. * @brief Compare to a C string.
  2740. * @param __s C string to compare against.
  2741. * @return Integer < 0, 0, or > 0.
  2742. *
  2743. * Returns an integer < 0 if this string is ordered before @a __s, 0 if
  2744. * their values are equivalent, or > 0 if this string is ordered after
  2745. * @a __s. Determines the effective length rlen of the strings to
  2746. * compare as the smallest of size() and the length of a string
  2747. * constructed from @a __s. The function then compares the two strings
  2748. * by calling traits::compare(data(),s,rlen). If the result of the
  2749. * comparison is nonzero returns it, otherwise the shorter one is
  2750. * ordered first.
  2751. */
  2752. int
  2753. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
  2754. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2755. // 5 String::compare specification questionable
  2756. /**
  2757. * @brief Compare substring to a C string.
  2758. * @param __pos Index of first character of substring.
  2759. * @param __n1 Number of characters in substring.
  2760. * @param __s C string to compare against.
  2761. * @return Integer < 0, 0, or > 0.
  2762. *
  2763. * Form the substring of this string from the @a __n1
  2764. * characters starting at @a pos. Returns an integer < 0 if
  2765. * the substring is ordered before @a __s, 0 if their values
  2766. * are equivalent, or > 0 if the substring is ordered after @a
  2767. * __s. Determines the effective length rlen of the strings to
  2768. * compare as the smallest of the length of the substring and
  2769. * the length of a string constructed from @a __s. The
  2770. * function then compares the two string by calling
  2771. * traits::compare(substring.data(),__s,rlen). If the result of
  2772. * the comparison is nonzero returns it, otherwise the shorter
  2773. * one is ordered first.
  2774. */
  2775. int
  2776. compare(size_type __pos, size_type __n1, const _CharT* __s) const;
  2777. /**
  2778. * @brief Compare substring against a character %array.
  2779. * @param __pos Index of first character of substring.
  2780. * @param __n1 Number of characters in substring.
  2781. * @param __s character %array to compare against.
  2782. * @param __n2 Number of characters of s.
  2783. * @return Integer < 0, 0, or > 0.
  2784. *
  2785. * Form the substring of this string from the @a __n1
  2786. * characters starting at @a __pos. Form a string from the
  2787. * first @a __n2 characters of @a __s. Returns an integer < 0
  2788. * if this substring is ordered before the string from @a __s,
  2789. * 0 if their values are equivalent, or > 0 if this substring
  2790. * is ordered after the string from @a __s. Determines the
  2791. * effective length rlen of the strings to compare as the
  2792. * smallest of the length of the substring and @a __n2. The
  2793. * function then compares the two strings by calling
  2794. * traits::compare(substring.data(),s,rlen). If the result of
  2795. * the comparison is nonzero returns it, otherwise the shorter
  2796. * one is ordered first.
  2797. *
  2798. * NB: s must have at least n2 characters, &apos;\\0&apos; has
  2799. * no special meaning.
  2800. */
  2801. int
  2802. compare(size_type __pos, size_type __n1, const _CharT* __s,
  2803. size_type __n2) const;
  2804. #if __cplusplus > 201703L
  2805. bool
  2806. starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  2807. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2808. bool
  2809. starts_with(_CharT __x) const noexcept
  2810. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2811. bool
  2812. starts_with(const _CharT* __x) const noexcept
  2813. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2814. bool
  2815. ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  2816. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2817. bool
  2818. ends_with(_CharT __x) const noexcept
  2819. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2820. bool
  2821. ends_with(const _CharT* __x) const noexcept
  2822. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2823. #endif // C++20
  2824. // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
  2825. template<typename, typename, typename> friend class basic_stringbuf;
  2826. };
  2827. _GLIBCXX_END_NAMESPACE_CXX11
  2828. #else // !_GLIBCXX_USE_CXX11_ABI
  2829. // Reference-counted COW string implentation
  2830. /**
  2831. * @class basic_string basic_string.h <string>
  2832. * @brief Managing sequences of characters and character-like objects.
  2833. *
  2834. * @ingroup strings
  2835. * @ingroup sequences
  2836. *
  2837. * @tparam _CharT Type of character
  2838. * @tparam _Traits Traits for character type, defaults to
  2839. * char_traits<_CharT>.
  2840. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  2841. *
  2842. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  2843. * <a href="tables.html#66">reversible container</a>, and a
  2844. * <a href="tables.html#67">sequence</a>. Of the
  2845. * <a href="tables.html#68">optional sequence requirements</a>, only
  2846. * @c push_back, @c at, and @c %array access are supported.
  2847. *
  2848. * @doctodo
  2849. *
  2850. *
  2851. * Documentation? What's that?
  2852. * Nathan Myers <ncm@cantrip.org>.
  2853. *
  2854. * A string looks like this:
  2855. *
  2856. * @code
  2857. * [_Rep]
  2858. * _M_length
  2859. * [basic_string<char_type>] _M_capacity
  2860. * _M_dataplus _M_refcount
  2861. * _M_p ----------------> unnamed array of char_type
  2862. * @endcode
  2863. *
  2864. * Where the _M_p points to the first character in the string, and
  2865. * you cast it to a pointer-to-_Rep and subtract 1 to get a
  2866. * pointer to the header.
  2867. *
  2868. * This approach has the enormous advantage that a string object
  2869. * requires only one allocation. All the ugliness is confined
  2870. * within a single %pair of inline functions, which each compile to
  2871. * a single @a add instruction: _Rep::_M_data(), and
  2872. * string::_M_rep(); and the allocation function which gets a
  2873. * block of raw bytes and with room enough and constructs a _Rep
  2874. * object at the front.
  2875. *
  2876. * The reason you want _M_data pointing to the character %array and
  2877. * not the _Rep is so that the debugger can see the string
  2878. * contents. (Probably we should add a non-inline member to get
  2879. * the _Rep for the debugger to use, so users can check the actual
  2880. * string length.)
  2881. *
  2882. * Note that the _Rep object is a POD so that you can have a
  2883. * static <em>empty string</em> _Rep object already @a constructed before
  2884. * static constructors have run. The reference-count encoding is
  2885. * chosen so that a 0 indicates one reference, so you never try to
  2886. * destroy the empty-string _Rep object.
  2887. *
  2888. * All but the last paragraph is considered pretty conventional
  2889. * for a C++ string implementation.
  2890. */
  2891. // 21.3 Template class basic_string
  2892. template<typename _CharT, typename _Traits, typename _Alloc>
  2893. class basic_string
  2894. {
  2895. typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
  2896. // Types:
  2897. public:
  2898. typedef _Traits traits_type;
  2899. typedef typename _Traits::char_type value_type;
  2900. typedef _Alloc allocator_type;
  2901. typedef typename _CharT_alloc_type::size_type size_type;
  2902. typedef typename _CharT_alloc_type::difference_type difference_type;
  2903. #if __cplusplus < 201103L
  2904. typedef typename _CharT_alloc_type::reference reference;
  2905. typedef typename _CharT_alloc_type::const_reference const_reference;
  2906. #else
  2907. typedef value_type& reference;
  2908. typedef const value_type& const_reference;
  2909. #endif
  2910. typedef typename _CharT_alloc_type::pointer pointer;
  2911. typedef typename _CharT_alloc_type::const_pointer const_pointer;
  2912. typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
  2913. typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
  2914. const_iterator;
  2915. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  2916. typedef std::reverse_iterator<iterator> reverse_iterator;
  2917. protected:
  2918. // type used for positions in insert, erase etc.
  2919. typedef iterator __const_iterator;
  2920. private:
  2921. // _Rep: string representation
  2922. // Invariants:
  2923. // 1. String really contains _M_length + 1 characters: due to 21.3.4
  2924. // must be kept null-terminated.
  2925. // 2. _M_capacity >= _M_length
  2926. // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
  2927. // 3. _M_refcount has three states:
  2928. // -1: leaked, one reference, no ref-copies allowed, non-const.
  2929. // 0: one reference, non-const.
  2930. // n>0: n + 1 references, operations require a lock, const.
  2931. // 4. All fields==0 is an empty string, given the extra storage
  2932. // beyond-the-end for a null terminator; thus, the shared
  2933. // empty string representation needs no constructor.
  2934. struct _Rep_base
  2935. {
  2936. size_type _M_length;
  2937. size_type _M_capacity;
  2938. _Atomic_word _M_refcount;
  2939. };
  2940. struct _Rep : _Rep_base
  2941. {
  2942. // Types:
  2943. typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
  2944. // (Public) Data members:
  2945. // The maximum number of individual char_type elements of an
  2946. // individual string is determined by _S_max_size. This is the
  2947. // value that will be returned by max_size(). (Whereas npos
  2948. // is the maximum number of bytes the allocator can allocate.)
  2949. // If one was to divvy up the theoretical largest size string,
  2950. // with a terminating character and m _CharT elements, it'd
  2951. // look like this:
  2952. // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
  2953. // Solving for m:
  2954. // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
  2955. // In addition, this implementation quarters this amount.
  2956. static const size_type _S_max_size;
  2957. static const _CharT _S_terminal;
  2958. // The following storage is init'd to 0 by the linker, resulting
  2959. // (carefully) in an empty string with one reference.
  2960. static size_type _S_empty_rep_storage[];
  2961. static _Rep&
  2962. _S_empty_rep() _GLIBCXX_NOEXCEPT
  2963. {
  2964. // NB: Mild hack to avoid strict-aliasing warnings. Note that
  2965. // _S_empty_rep_storage is never modified and the punning should
  2966. // be reasonably safe in this case.
  2967. void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
  2968. return *reinterpret_cast<_Rep*>(__p);
  2969. }
  2970. bool
  2971. _M_is_leaked() const _GLIBCXX_NOEXCEPT
  2972. {
  2973. #if defined(__GTHREADS)
  2974. // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
  2975. // so we need to use an atomic load. However, _M_is_leaked
  2976. // predicate does not change concurrently (i.e. the string is either
  2977. // leaked or not), so a relaxed load is enough.
  2978. return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
  2979. #else
  2980. return this->_M_refcount < 0;
  2981. #endif
  2982. }
  2983. bool
  2984. _M_is_shared() const _GLIBCXX_NOEXCEPT
  2985. {
  2986. #if defined(__GTHREADS)
  2987. // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
  2988. // so we need to use an atomic load. Another thread can drop last
  2989. // but one reference concurrently with this check, so we need this
  2990. // load to be acquire to synchronize with release fetch_and_add in
  2991. // _M_dispose.
  2992. return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
  2993. #else
  2994. return this->_M_refcount > 0;
  2995. #endif
  2996. }
  2997. void
  2998. _M_set_leaked() _GLIBCXX_NOEXCEPT
  2999. { this->_M_refcount = -1; }
  3000. void
  3001. _M_set_sharable() _GLIBCXX_NOEXCEPT
  3002. { this->_M_refcount = 0; }
  3003. void
  3004. _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
  3005. {
  3006. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3007. if (__builtin_expect(this != &_S_empty_rep(), false))
  3008. #endif
  3009. {
  3010. this->_M_set_sharable(); // One reference.
  3011. this->_M_length = __n;
  3012. traits_type::assign(this->_M_refdata()[__n], _S_terminal);
  3013. // grrr. (per 21.3.4)
  3014. // You cannot leave those LWG people alone for a second.
  3015. }
  3016. }
  3017. _CharT*
  3018. _M_refdata() throw()
  3019. { return reinterpret_cast<_CharT*>(this + 1); }
  3020. _CharT*
  3021. _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
  3022. {
  3023. return (!_M_is_leaked() && __alloc1 == __alloc2)
  3024. ? _M_refcopy() : _M_clone(__alloc1);
  3025. }
  3026. // Create & Destroy
  3027. static _Rep*
  3028. _S_create(size_type, size_type, const _Alloc&);
  3029. void
  3030. _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
  3031. {
  3032. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3033. if (__builtin_expect(this != &_S_empty_rep(), false))
  3034. #endif
  3035. {
  3036. // Be race-detector-friendly. For more info see bits/c++config.
  3037. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
  3038. // Decrement of _M_refcount is acq_rel, because:
  3039. // - all but last decrements need to release to synchronize with
  3040. // the last decrement that will delete the object.
  3041. // - the last decrement needs to acquire to synchronize with
  3042. // all the previous decrements.
  3043. // - last but one decrement needs to release to synchronize with
  3044. // the acquire load in _M_is_shared that will conclude that
  3045. // the object is not shared anymore.
  3046. if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
  3047. -1) <= 0)
  3048. {
  3049. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
  3050. _M_destroy(__a);
  3051. }
  3052. }
  3053. } // XXX MT
  3054. void
  3055. _M_destroy(const _Alloc&) throw();
  3056. _CharT*
  3057. _M_refcopy() throw()
  3058. {
  3059. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3060. if (__builtin_expect(this != &_S_empty_rep(), false))
  3061. #endif
  3062. __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
  3063. return _M_refdata();
  3064. } // XXX MT
  3065. _CharT*
  3066. _M_clone(const _Alloc&, size_type __res = 0);
  3067. };
  3068. // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
  3069. struct _Alloc_hider : _Alloc
  3070. {
  3071. _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
  3072. : _Alloc(__a), _M_p(__dat) { }
  3073. _CharT* _M_p; // The actual data.
  3074. };
  3075. public:
  3076. // Data Members (public):
  3077. // NB: This is an unsigned type, and thus represents the maximum
  3078. // size that the allocator can hold.
  3079. /// Value returned by various member functions when they fail.
  3080. static const size_type npos = static_cast<size_type>(-1);
  3081. private:
  3082. // Data Members (private):
  3083. mutable _Alloc_hider _M_dataplus;
  3084. _CharT*
  3085. _M_data() const _GLIBCXX_NOEXCEPT
  3086. { return _M_dataplus._M_p; }
  3087. _CharT*
  3088. _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
  3089. { return (_M_dataplus._M_p = __p); }
  3090. _Rep*
  3091. _M_rep() const _GLIBCXX_NOEXCEPT
  3092. { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
  3093. // For the internal use we have functions similar to `begin'/`end'
  3094. // but they do not call _M_leak.
  3095. iterator
  3096. _M_ibegin() const _GLIBCXX_NOEXCEPT
  3097. { return iterator(_M_data()); }
  3098. iterator
  3099. _M_iend() const _GLIBCXX_NOEXCEPT
  3100. { return iterator(_M_data() + this->size()); }
  3101. void
  3102. _M_leak() // for use in begin() & non-const op[]
  3103. {
  3104. if (!_M_rep()->_M_is_leaked())
  3105. _M_leak_hard();
  3106. }
  3107. size_type
  3108. _M_check(size_type __pos, const char* __s) const
  3109. {
  3110. if (__pos > this->size())
  3111. __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
  3112. "this->size() (which is %zu)"),
  3113. __s, __pos, this->size());
  3114. return __pos;
  3115. }
  3116. void
  3117. _M_check_length(size_type __n1, size_type __n2, const char* __s) const
  3118. {
  3119. if (this->max_size() - (this->size() - __n1) < __n2)
  3120. __throw_length_error(__N(__s));
  3121. }
  3122. // NB: _M_limit doesn't check for a bad __pos value.
  3123. size_type
  3124. _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
  3125. {
  3126. const bool __testoff = __off < this->size() - __pos;
  3127. return __testoff ? __off : this->size() - __pos;
  3128. }
  3129. // True if _Rep and source do not overlap.
  3130. bool
  3131. _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  3132. {
  3133. return (less<const _CharT*>()(__s, _M_data())
  3134. || less<const _CharT*>()(_M_data() + this->size(), __s));
  3135. }
  3136. // When __n = 1 way faster than the general multichar
  3137. // traits_type::copy/move/assign.
  3138. static void
  3139. _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
  3140. {
  3141. if (__n == 1)
  3142. traits_type::assign(*__d, *__s);
  3143. else
  3144. traits_type::copy(__d, __s, __n);
  3145. }
  3146. static void
  3147. _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
  3148. {
  3149. if (__n == 1)
  3150. traits_type::assign(*__d, *__s);
  3151. else
  3152. traits_type::move(__d, __s, __n);
  3153. }
  3154. static void
  3155. _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
  3156. {
  3157. if (__n == 1)
  3158. traits_type::assign(*__d, __c);
  3159. else
  3160. traits_type::assign(__d, __n, __c);
  3161. }
  3162. // _S_copy_chars is a separate template to permit specialization
  3163. // to optimize for the common case of pointers as iterators.
  3164. template<class _Iterator>
  3165. static void
  3166. _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
  3167. {
  3168. for (; __k1 != __k2; ++__k1, (void)++__p)
  3169. traits_type::assign(*__p, *__k1); // These types are off.
  3170. }
  3171. static void
  3172. _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
  3173. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  3174. static void
  3175. _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
  3176. _GLIBCXX_NOEXCEPT
  3177. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  3178. static void
  3179. _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
  3180. { _M_copy(__p, __k1, __k2 - __k1); }
  3181. static void
  3182. _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
  3183. _GLIBCXX_NOEXCEPT
  3184. { _M_copy(__p, __k1, __k2 - __k1); }
  3185. static int
  3186. _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
  3187. {
  3188. const difference_type __d = difference_type(__n1 - __n2);
  3189. if (__d > __gnu_cxx::__numeric_traits<int>::__max)
  3190. return __gnu_cxx::__numeric_traits<int>::__max;
  3191. else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
  3192. return __gnu_cxx::__numeric_traits<int>::__min;
  3193. else
  3194. return int(__d);
  3195. }
  3196. void
  3197. _M_mutate(size_type __pos, size_type __len1, size_type __len2);
  3198. void
  3199. _M_leak_hard();
  3200. static _Rep&
  3201. _S_empty_rep() _GLIBCXX_NOEXCEPT
  3202. { return _Rep::_S_empty_rep(); }
  3203. #if __cplusplus >= 201703L
  3204. // A helper type for avoiding boiler-plate.
  3205. typedef basic_string_view<_CharT, _Traits> __sv_type;
  3206. template<typename _Tp, typename _Res>
  3207. using _If_sv = enable_if_t<
  3208. __and_<is_convertible<const _Tp&, __sv_type>,
  3209. __not_<is_convertible<const _Tp*, const basic_string*>>,
  3210. __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
  3211. _Res>;
  3212. // Allows an implicit conversion to __sv_type.
  3213. static __sv_type
  3214. _S_to_string_view(__sv_type __svt) noexcept
  3215. { return __svt; }
  3216. // Wraps a string_view by explicit conversion and thus
  3217. // allows to add an internal constructor that does not
  3218. // participate in overload resolution when a string_view
  3219. // is provided.
  3220. struct __sv_wrapper
  3221. {
  3222. explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
  3223. __sv_type _M_sv;
  3224. };
  3225. /**
  3226. * @brief Only internally used: Construct string from a string view
  3227. * wrapper.
  3228. * @param __svw string view wrapper.
  3229. * @param __a Allocator to use.
  3230. */
  3231. explicit
  3232. basic_string(__sv_wrapper __svw, const _Alloc& __a)
  3233. : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
  3234. #endif
  3235. public:
  3236. // Construct/copy/destroy:
  3237. // NB: We overload ctors in some cases instead of using default
  3238. // arguments, per 17.4.4.4 para. 2 item 2.
  3239. /**
  3240. * @brief Default constructor creates an empty string.
  3241. */
  3242. basic_string()
  3243. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3244. _GLIBCXX_NOEXCEPT
  3245. : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
  3246. #else
  3247. : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
  3248. #endif
  3249. { }
  3250. /**
  3251. * @brief Construct an empty string using allocator @a a.
  3252. */
  3253. explicit
  3254. basic_string(const _Alloc& __a);
  3255. // NB: per LWG issue 42, semantics different from IS:
  3256. /**
  3257. * @brief Construct string with copy of value of @a str.
  3258. * @param __str Source string.
  3259. */
  3260. basic_string(const basic_string& __str);
  3261. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3262. // 2583. no way to supply an allocator for basic_string(str, pos)
  3263. /**
  3264. * @brief Construct string as copy of a substring.
  3265. * @param __str Source string.
  3266. * @param __pos Index of first character to copy from.
  3267. * @param __a Allocator to use.
  3268. */
  3269. basic_string(const basic_string& __str, size_type __pos,
  3270. const _Alloc& __a = _Alloc());
  3271. /**
  3272. * @brief Construct string as copy of a substring.
  3273. * @param __str Source string.
  3274. * @param __pos Index of first character to copy from.
  3275. * @param __n Number of characters to copy.
  3276. */
  3277. basic_string(const basic_string& __str, size_type __pos,
  3278. size_type __n);
  3279. /**
  3280. * @brief Construct string as copy of a substring.
  3281. * @param __str Source string.
  3282. * @param __pos Index of first character to copy from.
  3283. * @param __n Number of characters to copy.
  3284. * @param __a Allocator to use.
  3285. */
  3286. basic_string(const basic_string& __str, size_type __pos,
  3287. size_type __n, const _Alloc& __a);
  3288. /**
  3289. * @brief Construct string initialized by a character %array.
  3290. * @param __s Source character %array.
  3291. * @param __n Number of characters to copy.
  3292. * @param __a Allocator to use (default is default allocator).
  3293. *
  3294. * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
  3295. * has no special meaning.
  3296. */
  3297. basic_string(const _CharT* __s, size_type __n,
  3298. const _Alloc& __a = _Alloc());
  3299. /**
  3300. * @brief Construct string as copy of a C string.
  3301. * @param __s Source C string.
  3302. * @param __a Allocator to use (default is default allocator).
  3303. */
  3304. basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
  3305. /**
  3306. * @brief Construct string as multiple characters.
  3307. * @param __n Number of characters.
  3308. * @param __c Character to use.
  3309. * @param __a Allocator to use (default is default allocator).
  3310. */
  3311. basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
  3312. #if __cplusplus >= 201103L
  3313. /**
  3314. * @brief Move construct string.
  3315. * @param __str Source string.
  3316. *
  3317. * The newly-created string contains the exact contents of @a __str.
  3318. * @a __str is a valid, but unspecified string.
  3319. **/
  3320. basic_string(basic_string&& __str)
  3321. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3322. noexcept // FIXME C++11: should always be noexcept.
  3323. #endif
  3324. : _M_dataplus(std::move(__str._M_dataplus))
  3325. {
  3326. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3327. __str._M_data(_S_empty_rep()._M_refdata());
  3328. #else
  3329. __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
  3330. #endif
  3331. }
  3332. /**
  3333. * @brief Construct string from an initializer %list.
  3334. * @param __l std::initializer_list of characters.
  3335. * @param __a Allocator to use (default is default allocator).
  3336. */
  3337. basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
  3338. basic_string(const basic_string& __str, const _Alloc& __a)
  3339. : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a)
  3340. { }
  3341. basic_string(basic_string&& __str, const _Alloc& __a)
  3342. : _M_dataplus(__str._M_data(), __a)
  3343. {
  3344. if (__a == __str.get_allocator())
  3345. {
  3346. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3347. __str._M_data(_S_empty_rep()._M_refdata());
  3348. #else
  3349. __str._M_data(_S_construct(size_type(), _CharT(), __a));
  3350. #endif
  3351. }
  3352. else
  3353. _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
  3354. }
  3355. #endif // C++11
  3356. /**
  3357. * @brief Construct string as copy of a range.
  3358. * @param __beg Start of range.
  3359. * @param __end End of range.
  3360. * @param __a Allocator to use (default is default allocator).
  3361. */
  3362. template<class _InputIterator>
  3363. basic_string(_InputIterator __beg, _InputIterator __end,
  3364. const _Alloc& __a = _Alloc());
  3365. #if __cplusplus >= 201703L
  3366. /**
  3367. * @brief Construct string from a substring of a string_view.
  3368. * @param __t Source object convertible to string view.
  3369. * @param __pos The index of the first character to copy from __t.
  3370. * @param __n The number of characters to copy from __t.
  3371. * @param __a Allocator to use.
  3372. */
  3373. template<typename _Tp, typename = _If_sv<_Tp, void>>
  3374. basic_string(const _Tp& __t, size_type __pos, size_type __n,
  3375. const _Alloc& __a = _Alloc())
  3376. : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
  3377. /**
  3378. * @brief Construct string from a string_view.
  3379. * @param __t Source object convertible to string view.
  3380. * @param __a Allocator to use (default is default allocator).
  3381. */
  3382. template<typename _Tp, typename = _If_sv<_Tp, void>>
  3383. explicit
  3384. basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
  3385. : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
  3386. #endif // C++17
  3387. /**
  3388. * @brief Destroy the string instance.
  3389. */
  3390. ~basic_string() _GLIBCXX_NOEXCEPT
  3391. { _M_rep()->_M_dispose(this->get_allocator()); }
  3392. /**
  3393. * @brief Assign the value of @a str to this string.
  3394. * @param __str Source string.
  3395. */
  3396. basic_string&
  3397. operator=(const basic_string& __str)
  3398. { return this->assign(__str); }
  3399. /**
  3400. * @brief Copy contents of @a s into this string.
  3401. * @param __s Source null-terminated string.
  3402. */
  3403. basic_string&
  3404. operator=(const _CharT* __s)
  3405. { return this->assign(__s); }
  3406. /**
  3407. * @brief Set value to string of length 1.
  3408. * @param __c Source character.
  3409. *
  3410. * Assigning to a character makes this string length 1 and
  3411. * (*this)[0] == @a c.
  3412. */
  3413. basic_string&
  3414. operator=(_CharT __c)
  3415. {
  3416. this->assign(1, __c);
  3417. return *this;
  3418. }
  3419. #if __cplusplus >= 201103L
  3420. /**
  3421. * @brief Move assign the value of @a str to this string.
  3422. * @param __str Source string.
  3423. *
  3424. * The contents of @a str are moved into this string (without copying).
  3425. * @a str is a valid, but unspecified string.
  3426. **/
  3427. basic_string&
  3428. operator=(basic_string&& __str)
  3429. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
  3430. {
  3431. // NB: DR 1204.
  3432. this->swap(__str);
  3433. return *this;
  3434. }
  3435. /**
  3436. * @brief Set value to string constructed from initializer %list.
  3437. * @param __l std::initializer_list.
  3438. */
  3439. basic_string&
  3440. operator=(initializer_list<_CharT> __l)
  3441. {
  3442. this->assign(__l.begin(), __l.size());
  3443. return *this;
  3444. }
  3445. #endif // C++11
  3446. #if __cplusplus >= 201703L
  3447. /**
  3448. * @brief Set value to string constructed from a string_view.
  3449. * @param __svt An object convertible to string_view.
  3450. */
  3451. template<typename _Tp>
  3452. _If_sv<_Tp, basic_string&>
  3453. operator=(const _Tp& __svt)
  3454. { return this->assign(__svt); }
  3455. /**
  3456. * @brief Convert to a string_view.
  3457. * @return A string_view.
  3458. */
  3459. operator __sv_type() const noexcept
  3460. { return __sv_type(data(), size()); }
  3461. #endif // C++17
  3462. // Iterators:
  3463. /**
  3464. * Returns a read/write iterator that points to the first character in
  3465. * the %string. Unshares the string.
  3466. */
  3467. iterator
  3468. begin() // FIXME C++11: should be noexcept.
  3469. {
  3470. _M_leak();
  3471. return iterator(_M_data());
  3472. }
  3473. /**
  3474. * Returns a read-only (constant) iterator that points to the first
  3475. * character in the %string.
  3476. */
  3477. const_iterator
  3478. begin() const _GLIBCXX_NOEXCEPT
  3479. { return const_iterator(_M_data()); }
  3480. /**
  3481. * Returns a read/write iterator that points one past the last
  3482. * character in the %string. Unshares the string.
  3483. */
  3484. iterator
  3485. end() // FIXME C++11: should be noexcept.
  3486. {
  3487. _M_leak();
  3488. return iterator(_M_data() + this->size());
  3489. }
  3490. /**
  3491. * Returns a read-only (constant) iterator that points one past the
  3492. * last character in the %string.
  3493. */
  3494. const_iterator
  3495. end() const _GLIBCXX_NOEXCEPT
  3496. { return const_iterator(_M_data() + this->size()); }
  3497. /**
  3498. * Returns a read/write reverse iterator that points to the last
  3499. * character in the %string. Iteration is done in reverse element
  3500. * order. Unshares the string.
  3501. */
  3502. reverse_iterator
  3503. rbegin() // FIXME C++11: should be noexcept.
  3504. { return reverse_iterator(this->end()); }
  3505. /**
  3506. * Returns a read-only (constant) reverse iterator that points
  3507. * to the last character in the %string. Iteration is done in
  3508. * reverse element order.
  3509. */
  3510. const_reverse_iterator
  3511. rbegin() const _GLIBCXX_NOEXCEPT
  3512. { return const_reverse_iterator(this->end()); }
  3513. /**
  3514. * Returns a read/write reverse iterator that points to one before the
  3515. * first character in the %string. Iteration is done in reverse
  3516. * element order. Unshares the string.
  3517. */
  3518. reverse_iterator
  3519. rend() // FIXME C++11: should be noexcept.
  3520. { return reverse_iterator(this->begin()); }
  3521. /**
  3522. * Returns a read-only (constant) reverse iterator that points
  3523. * to one before the first character in the %string. Iteration
  3524. * is done in reverse element order.
  3525. */
  3526. const_reverse_iterator
  3527. rend() const _GLIBCXX_NOEXCEPT
  3528. { return const_reverse_iterator(this->begin()); }
  3529. #if __cplusplus >= 201103L
  3530. /**
  3531. * Returns a read-only (constant) iterator that points to the first
  3532. * character in the %string.
  3533. */
  3534. const_iterator
  3535. cbegin() const noexcept
  3536. { return const_iterator(this->_M_data()); }
  3537. /**
  3538. * Returns a read-only (constant) iterator that points one past the
  3539. * last character in the %string.
  3540. */
  3541. const_iterator
  3542. cend() const noexcept
  3543. { return const_iterator(this->_M_data() + this->size()); }
  3544. /**
  3545. * Returns a read-only (constant) reverse iterator that points
  3546. * to the last character in the %string. Iteration is done in
  3547. * reverse element order.
  3548. */
  3549. const_reverse_iterator
  3550. crbegin() const noexcept
  3551. { return const_reverse_iterator(this->end()); }
  3552. /**
  3553. * Returns a read-only (constant) reverse iterator that points
  3554. * to one before the first character in the %string. Iteration
  3555. * is done in reverse element order.
  3556. */
  3557. const_reverse_iterator
  3558. crend() const noexcept
  3559. { return const_reverse_iterator(this->begin()); }
  3560. #endif
  3561. public:
  3562. // Capacity:
  3563. /// Returns the number of characters in the string, not including any
  3564. /// null-termination.
  3565. size_type
  3566. size() const _GLIBCXX_NOEXCEPT
  3567. { return _M_rep()->_M_length; }
  3568. /// Returns the number of characters in the string, not including any
  3569. /// null-termination.
  3570. size_type
  3571. length() const _GLIBCXX_NOEXCEPT
  3572. { return _M_rep()->_M_length; }
  3573. /// Returns the size() of the largest possible %string.
  3574. size_type
  3575. max_size() const _GLIBCXX_NOEXCEPT
  3576. { return _Rep::_S_max_size; }
  3577. /**
  3578. * @brief Resizes the %string to the specified number of characters.
  3579. * @param __n Number of characters the %string should contain.
  3580. * @param __c Character to fill any new elements.
  3581. *
  3582. * This function will %resize the %string to the specified
  3583. * number of characters. If the number is smaller than the
  3584. * %string's current size the %string is truncated, otherwise
  3585. * the %string is extended and new elements are %set to @a __c.
  3586. */
  3587. void
  3588. resize(size_type __n, _CharT __c);
  3589. /**
  3590. * @brief Resizes the %string to the specified number of characters.
  3591. * @param __n Number of characters the %string should contain.
  3592. *
  3593. * This function will resize the %string to the specified length. If
  3594. * the new size is smaller than the %string's current size the %string
  3595. * is truncated, otherwise the %string is extended and new characters
  3596. * are default-constructed. For basic types such as char, this means
  3597. * setting them to 0.
  3598. */
  3599. void
  3600. resize(size_type __n)
  3601. { this->resize(__n, _CharT()); }
  3602. #if __cplusplus >= 201103L
  3603. /// A non-binding request to reduce capacity() to size().
  3604. void
  3605. shrink_to_fit() _GLIBCXX_NOEXCEPT
  3606. {
  3607. #if __cpp_exceptions
  3608. if (capacity() > size())
  3609. {
  3610. try
  3611. { reserve(0); }
  3612. catch(...)
  3613. { }
  3614. }
  3615. #endif
  3616. }
  3617. #endif
  3618. /**
  3619. * Returns the total number of characters that the %string can hold
  3620. * before needing to allocate more memory.
  3621. */
  3622. size_type
  3623. capacity() const _GLIBCXX_NOEXCEPT
  3624. { return _M_rep()->_M_capacity; }
  3625. /**
  3626. * @brief Attempt to preallocate enough memory for specified number of
  3627. * characters.
  3628. * @param __res_arg Number of characters required.
  3629. * @throw std::length_error If @a __res_arg exceeds @c max_size().
  3630. *
  3631. * This function attempts to reserve enough memory for the
  3632. * %string to hold the specified number of characters. If the
  3633. * number requested is more than max_size(), length_error is
  3634. * thrown.
  3635. *
  3636. * The advantage of this function is that if optimal code is a
  3637. * necessity and the user can determine the string length that will be
  3638. * required, the user can reserve the memory in %advance, and thus
  3639. * prevent a possible reallocation of memory and copying of %string
  3640. * data.
  3641. */
  3642. void
  3643. reserve(size_type __res_arg = 0);
  3644. /**
  3645. * Erases the string, making it empty.
  3646. */
  3647. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3648. void
  3649. clear() _GLIBCXX_NOEXCEPT
  3650. {
  3651. if (_M_rep()->_M_is_shared())
  3652. {
  3653. _M_rep()->_M_dispose(this->get_allocator());
  3654. _M_data(_S_empty_rep()._M_refdata());
  3655. }
  3656. else
  3657. _M_rep()->_M_set_length_and_sharable(0);
  3658. }
  3659. #else
  3660. // PR 56166: this should not throw.
  3661. void
  3662. clear()
  3663. { _M_mutate(0, this->size(), 0); }
  3664. #endif
  3665. /**
  3666. * Returns true if the %string is empty. Equivalent to
  3667. * <code>*this == ""</code>.
  3668. */
  3669. _GLIBCXX_NODISCARD bool
  3670. empty() const _GLIBCXX_NOEXCEPT
  3671. { return this->size() == 0; }
  3672. // Element access:
  3673. /**
  3674. * @brief Subscript access to the data contained in the %string.
  3675. * @param __pos The index of the character to access.
  3676. * @return Read-only (constant) reference to the character.
  3677. *
  3678. * This operator allows for easy, array-style, data access.
  3679. * Note that data access with this operator is unchecked and
  3680. * out_of_range lookups are not defined. (For checked lookups
  3681. * see at().)
  3682. */
  3683. const_reference
  3684. operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
  3685. {
  3686. __glibcxx_assert(__pos <= size());
  3687. return _M_data()[__pos];
  3688. }
  3689. /**
  3690. * @brief Subscript access to the data contained in the %string.
  3691. * @param __pos The index of the character to access.
  3692. * @return Read/write reference to the character.
  3693. *
  3694. * This operator allows for easy, array-style, data access.
  3695. * Note that data access with this operator is unchecked and
  3696. * out_of_range lookups are not defined. (For checked lookups
  3697. * see at().) Unshares the string.
  3698. */
  3699. reference
  3700. operator[](size_type __pos)
  3701. {
  3702. // Allow pos == size() both in C++98 mode, as v3 extension,
  3703. // and in C++11 mode.
  3704. __glibcxx_assert(__pos <= size());
  3705. // In pedantic mode be strict in C++98 mode.
  3706. _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
  3707. _M_leak();
  3708. return _M_data()[__pos];
  3709. }
  3710. /**
  3711. * @brief Provides access to the data contained in the %string.
  3712. * @param __n The index of the character to access.
  3713. * @return Read-only (const) reference to the character.
  3714. * @throw std::out_of_range If @a n is an invalid index.
  3715. *
  3716. * This function provides for safer data access. The parameter is
  3717. * first checked that it is in the range of the string. The function
  3718. * throws out_of_range if the check fails.
  3719. */
  3720. const_reference
  3721. at(size_type __n) const
  3722. {
  3723. if (__n >= this->size())
  3724. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  3725. "(which is %zu) >= this->size() "
  3726. "(which is %zu)"),
  3727. __n, this->size());
  3728. return _M_data()[__n];
  3729. }
  3730. /**
  3731. * @brief Provides access to the data contained in the %string.
  3732. * @param __n The index of the character to access.
  3733. * @return Read/write reference to the character.
  3734. * @throw std::out_of_range If @a n is an invalid index.
  3735. *
  3736. * This function provides for safer data access. The parameter is
  3737. * first checked that it is in the range of the string. The function
  3738. * throws out_of_range if the check fails. Success results in
  3739. * unsharing the string.
  3740. */
  3741. reference
  3742. at(size_type __n)
  3743. {
  3744. if (__n >= size())
  3745. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  3746. "(which is %zu) >= this->size() "
  3747. "(which is %zu)"),
  3748. __n, this->size());
  3749. _M_leak();
  3750. return _M_data()[__n];
  3751. }
  3752. #if __cplusplus >= 201103L
  3753. /**
  3754. * Returns a read/write reference to the data at the first
  3755. * element of the %string.
  3756. */
  3757. reference
  3758. front()
  3759. {
  3760. __glibcxx_assert(!empty());
  3761. return operator[](0);
  3762. }
  3763. /**
  3764. * Returns a read-only (constant) reference to the data at the first
  3765. * element of the %string.
  3766. */
  3767. const_reference
  3768. front() const noexcept
  3769. {
  3770. __glibcxx_assert(!empty());
  3771. return operator[](0);
  3772. }
  3773. /**
  3774. * Returns a read/write reference to the data at the last
  3775. * element of the %string.
  3776. */
  3777. reference
  3778. back()
  3779. {
  3780. __glibcxx_assert(!empty());
  3781. return operator[](this->size() - 1);
  3782. }
  3783. /**
  3784. * Returns a read-only (constant) reference to the data at the
  3785. * last element of the %string.
  3786. */
  3787. const_reference
  3788. back() const noexcept
  3789. {
  3790. __glibcxx_assert(!empty());
  3791. return operator[](this->size() - 1);
  3792. }
  3793. #endif
  3794. // Modifiers:
  3795. /**
  3796. * @brief Append a string to this string.
  3797. * @param __str The string to append.
  3798. * @return Reference to this string.
  3799. */
  3800. basic_string&
  3801. operator+=(const basic_string& __str)
  3802. { return this->append(__str); }
  3803. /**
  3804. * @brief Append a C string.
  3805. * @param __s The C string to append.
  3806. * @return Reference to this string.
  3807. */
  3808. basic_string&
  3809. operator+=(const _CharT* __s)
  3810. { return this->append(__s); }
  3811. /**
  3812. * @brief Append a character.
  3813. * @param __c The character to append.
  3814. * @return Reference to this string.
  3815. */
  3816. basic_string&
  3817. operator+=(_CharT __c)
  3818. {
  3819. this->push_back(__c);
  3820. return *this;
  3821. }
  3822. #if __cplusplus >= 201103L
  3823. /**
  3824. * @brief Append an initializer_list of characters.
  3825. * @param __l The initializer_list of characters to be appended.
  3826. * @return Reference to this string.
  3827. */
  3828. basic_string&
  3829. operator+=(initializer_list<_CharT> __l)
  3830. { return this->append(__l.begin(), __l.size()); }
  3831. #endif // C++11
  3832. #if __cplusplus >= 201703L
  3833. /**
  3834. * @brief Append a string_view.
  3835. * @param __svt The object convertible to string_view to be appended.
  3836. * @return Reference to this string.
  3837. */
  3838. template<typename _Tp>
  3839. _If_sv<_Tp, basic_string&>
  3840. operator+=(const _Tp& __svt)
  3841. { return this->append(__svt); }
  3842. #endif // C++17
  3843. /**
  3844. * @brief Append a string to this string.
  3845. * @param __str The string to append.
  3846. * @return Reference to this string.
  3847. */
  3848. basic_string&
  3849. append(const basic_string& __str);
  3850. /**
  3851. * @brief Append a substring.
  3852. * @param __str The string to append.
  3853. * @param __pos Index of the first character of str to append.
  3854. * @param __n The number of characters to append.
  3855. * @return Reference to this string.
  3856. * @throw std::out_of_range if @a __pos is not a valid index.
  3857. *
  3858. * This function appends @a __n characters from @a __str
  3859. * starting at @a __pos to this string. If @a __n is is larger
  3860. * than the number of available characters in @a __str, the
  3861. * remainder of @a __str is appended.
  3862. */
  3863. basic_string&
  3864. append(const basic_string& __str, size_type __pos, size_type __n = npos);
  3865. /**
  3866. * @brief Append a C substring.
  3867. * @param __s The C string to append.
  3868. * @param __n The number of characters to append.
  3869. * @return Reference to this string.
  3870. */
  3871. basic_string&
  3872. append(const _CharT* __s, size_type __n);
  3873. /**
  3874. * @brief Append a C string.
  3875. * @param __s The C string to append.
  3876. * @return Reference to this string.
  3877. */
  3878. basic_string&
  3879. append(const _CharT* __s)
  3880. {
  3881. __glibcxx_requires_string(__s);
  3882. return this->append(__s, traits_type::length(__s));
  3883. }
  3884. /**
  3885. * @brief Append multiple characters.
  3886. * @param __n The number of characters to append.
  3887. * @param __c The character to use.
  3888. * @return Reference to this string.
  3889. *
  3890. * Appends __n copies of __c to this string.
  3891. */
  3892. basic_string&
  3893. append(size_type __n, _CharT __c);
  3894. #if __cplusplus >= 201103L
  3895. /**
  3896. * @brief Append an initializer_list of characters.
  3897. * @param __l The initializer_list of characters to append.
  3898. * @return Reference to this string.
  3899. */
  3900. basic_string&
  3901. append(initializer_list<_CharT> __l)
  3902. { return this->append(__l.begin(), __l.size()); }
  3903. #endif // C++11
  3904. /**
  3905. * @brief Append a range of characters.
  3906. * @param __first Iterator referencing the first character to append.
  3907. * @param __last Iterator marking the end of the range.
  3908. * @return Reference to this string.
  3909. *
  3910. * Appends characters in the range [__first,__last) to this string.
  3911. */
  3912. template<class _InputIterator>
  3913. basic_string&
  3914. append(_InputIterator __first, _InputIterator __last)
  3915. { return this->replace(_M_iend(), _M_iend(), __first, __last); }
  3916. #if __cplusplus >= 201703L
  3917. /**
  3918. * @brief Append a string_view.
  3919. * @param __svt The object convertible to string_view to be appended.
  3920. * @return Reference to this string.
  3921. */
  3922. template<typename _Tp>
  3923. _If_sv<_Tp, basic_string&>
  3924. append(const _Tp& __svt)
  3925. {
  3926. __sv_type __sv = __svt;
  3927. return this->append(__sv.data(), __sv.size());
  3928. }
  3929. /**
  3930. * @brief Append a range of characters from a string_view.
  3931. * @param __svt The object convertible to string_view to be appended
  3932. * from.
  3933. * @param __pos The position in the string_view to append from.
  3934. * @param __n The number of characters to append from the string_view.
  3935. * @return Reference to this string.
  3936. */
  3937. template<typename _Tp>
  3938. _If_sv<_Tp, basic_string&>
  3939. append(const _Tp& __svt, size_type __pos, size_type __n = npos)
  3940. {
  3941. __sv_type __sv = __svt;
  3942. return append(__sv.data()
  3943. + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
  3944. std::__sv_limit(__sv.size(), __pos, __n));
  3945. }
  3946. #endif // C++17
  3947. /**
  3948. * @brief Append a single character.
  3949. * @param __c Character to append.
  3950. */
  3951. void
  3952. push_back(_CharT __c)
  3953. {
  3954. const size_type __len = 1 + this->size();
  3955. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  3956. this->reserve(__len);
  3957. traits_type::assign(_M_data()[this->size()], __c);
  3958. _M_rep()->_M_set_length_and_sharable(__len);
  3959. }
  3960. /**
  3961. * @brief Set value to contents of another string.
  3962. * @param __str Source string to use.
  3963. * @return Reference to this string.
  3964. */
  3965. basic_string&
  3966. assign(const basic_string& __str);
  3967. #if __cplusplus >= 201103L
  3968. /**
  3969. * @brief Set value to contents of another string.
  3970. * @param __str Source string to use.
  3971. * @return Reference to this string.
  3972. *
  3973. * This function sets this string to the exact contents of @a __str.
  3974. * @a __str is a valid, but unspecified string.
  3975. */
  3976. basic_string&
  3977. assign(basic_string&& __str)
  3978. noexcept(allocator_traits<_Alloc>::is_always_equal::value)
  3979. {
  3980. this->swap(__str);
  3981. return *this;
  3982. }
  3983. #endif // C++11
  3984. /**
  3985. * @brief Set value to a substring of a string.
  3986. * @param __str The string to use.
  3987. * @param __pos Index of the first character of str.
  3988. * @param __n Number of characters to use.
  3989. * @return Reference to this string.
  3990. * @throw std::out_of_range if @a pos is not a valid index.
  3991. *
  3992. * This function sets this string to the substring of @a __str
  3993. * consisting of @a __n characters at @a __pos. If @a __n is
  3994. * is larger than the number of available characters in @a
  3995. * __str, the remainder of @a __str is used.
  3996. */
  3997. basic_string&
  3998. assign(const basic_string& __str, size_type __pos, size_type __n = npos)
  3999. { return this->assign(__str._M_data()
  4000. + __str._M_check(__pos, "basic_string::assign"),
  4001. __str._M_limit(__pos, __n)); }
  4002. /**
  4003. * @brief Set value to a C substring.
  4004. * @param __s The C string to use.
  4005. * @param __n Number of characters to use.
  4006. * @return Reference to this string.
  4007. *
  4008. * This function sets the value of this string to the first @a __n
  4009. * characters of @a __s. If @a __n is is larger than the number of
  4010. * available characters in @a __s, the remainder of @a __s is used.
  4011. */
  4012. basic_string&
  4013. assign(const _CharT* __s, size_type __n);
  4014. /**
  4015. * @brief Set value to contents of a C string.
  4016. * @param __s The C string to use.
  4017. * @return Reference to this string.
  4018. *
  4019. * This function sets the value of this string to the value of @a __s.
  4020. * The data is copied, so there is no dependence on @a __s once the
  4021. * function returns.
  4022. */
  4023. basic_string&
  4024. assign(const _CharT* __s)
  4025. {
  4026. __glibcxx_requires_string(__s);
  4027. return this->assign(__s, traits_type::length(__s));
  4028. }
  4029. /**
  4030. * @brief Set value to multiple characters.
  4031. * @param __n Length of the resulting string.
  4032. * @param __c The character to use.
  4033. * @return Reference to this string.
  4034. *
  4035. * This function sets the value of this string to @a __n copies of
  4036. * character @a __c.
  4037. */
  4038. basic_string&
  4039. assign(size_type __n, _CharT __c)
  4040. { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
  4041. /**
  4042. * @brief Set value to a range of characters.
  4043. * @param __first Iterator referencing the first character to append.
  4044. * @param __last Iterator marking the end of the range.
  4045. * @return Reference to this string.
  4046. *
  4047. * Sets value of string to characters in the range [__first,__last).
  4048. */
  4049. template<class _InputIterator>
  4050. basic_string&
  4051. assign(_InputIterator __first, _InputIterator __last)
  4052. { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
  4053. #if __cplusplus >= 201103L
  4054. /**
  4055. * @brief Set value to an initializer_list of characters.
  4056. * @param __l The initializer_list of characters to assign.
  4057. * @return Reference to this string.
  4058. */
  4059. basic_string&
  4060. assign(initializer_list<_CharT> __l)
  4061. { return this->assign(__l.begin(), __l.size()); }
  4062. #endif // C++11
  4063. #if __cplusplus >= 201703L
  4064. /**
  4065. * @brief Set value from a string_view.
  4066. * @param __svt The source object convertible to string_view.
  4067. * @return Reference to this string.
  4068. */
  4069. template<typename _Tp>
  4070. _If_sv<_Tp, basic_string&>
  4071. assign(const _Tp& __svt)
  4072. {
  4073. __sv_type __sv = __svt;
  4074. return this->assign(__sv.data(), __sv.size());
  4075. }
  4076. /**
  4077. * @brief Set value from a range of characters in a string_view.
  4078. * @param __svt The source object convertible to string_view.
  4079. * @param __pos The position in the string_view to assign from.
  4080. * @param __n The number of characters to assign.
  4081. * @return Reference to this string.
  4082. */
  4083. template<typename _Tp>
  4084. _If_sv<_Tp, basic_string&>
  4085. assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
  4086. {
  4087. __sv_type __sv = __svt;
  4088. return assign(__sv.data()
  4089. + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
  4090. std::__sv_limit(__sv.size(), __pos, __n));
  4091. }
  4092. #endif // C++17
  4093. /**
  4094. * @brief Insert multiple characters.
  4095. * @param __p Iterator referencing location in string to insert at.
  4096. * @param __n Number of characters to insert
  4097. * @param __c The character to insert.
  4098. * @throw std::length_error If new length exceeds @c max_size().
  4099. *
  4100. * Inserts @a __n copies of character @a __c starting at the
  4101. * position referenced by iterator @a __p. If adding
  4102. * characters causes the length to exceed max_size(),
  4103. * length_error is thrown. The value of the string doesn't
  4104. * change if an error is thrown.
  4105. */
  4106. void
  4107. insert(iterator __p, size_type __n, _CharT __c)
  4108. { this->replace(__p, __p, __n, __c); }
  4109. /**
  4110. * @brief Insert a range of characters.
  4111. * @param __p Iterator referencing location in string to insert at.
  4112. * @param __beg Start of range.
  4113. * @param __end End of range.
  4114. * @throw std::length_error If new length exceeds @c max_size().
  4115. *
  4116. * Inserts characters in range [__beg,__end). If adding
  4117. * characters causes the length to exceed max_size(),
  4118. * length_error is thrown. The value of the string doesn't
  4119. * change if an error is thrown.
  4120. */
  4121. template<class _InputIterator>
  4122. void
  4123. insert(iterator __p, _InputIterator __beg, _InputIterator __end)
  4124. { this->replace(__p, __p, __beg, __end); }
  4125. #if __cplusplus >= 201103L
  4126. /**
  4127. * @brief Insert an initializer_list of characters.
  4128. * @param __p Iterator referencing location in string to insert at.
  4129. * @param __l The initializer_list of characters to insert.
  4130. * @throw std::length_error If new length exceeds @c max_size().
  4131. */
  4132. void
  4133. insert(iterator __p, initializer_list<_CharT> __l)
  4134. {
  4135. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  4136. this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
  4137. }
  4138. #endif // C++11
  4139. /**
  4140. * @brief Insert value of a string.
  4141. * @param __pos1 Iterator referencing location in string to insert at.
  4142. * @param __str The string to insert.
  4143. * @return Reference to this string.
  4144. * @throw std::length_error If new length exceeds @c max_size().
  4145. *
  4146. * Inserts value of @a __str starting at @a __pos1. If adding
  4147. * characters causes the length to exceed max_size(),
  4148. * length_error is thrown. The value of the string doesn't
  4149. * change if an error is thrown.
  4150. */
  4151. basic_string&
  4152. insert(size_type __pos1, const basic_string& __str)
  4153. { return this->insert(__pos1, __str, size_type(0), __str.size()); }
  4154. /**
  4155. * @brief Insert a substring.
  4156. * @param __pos1 Iterator referencing location in string to insert at.
  4157. * @param __str The string to insert.
  4158. * @param __pos2 Start of characters in str to insert.
  4159. * @param __n Number of characters to insert.
  4160. * @return Reference to this string.
  4161. * @throw std::length_error If new length exceeds @c max_size().
  4162. * @throw std::out_of_range If @a pos1 > size() or
  4163. * @a __pos2 > @a str.size().
  4164. *
  4165. * Starting at @a pos1, insert @a __n character of @a __str
  4166. * beginning with @a __pos2. If adding characters causes the
  4167. * length to exceed max_size(), length_error is thrown. If @a
  4168. * __pos1 is beyond the end of this string or @a __pos2 is
  4169. * beyond the end of @a __str, out_of_range is thrown. The
  4170. * value of the string doesn't change if an error is thrown.
  4171. */
  4172. basic_string&
  4173. insert(size_type __pos1, const basic_string& __str,
  4174. size_type __pos2, size_type __n = npos)
  4175. { return this->insert(__pos1, __str._M_data()
  4176. + __str._M_check(__pos2, "basic_string::insert"),
  4177. __str._M_limit(__pos2, __n)); }
  4178. /**
  4179. * @brief Insert a C substring.
  4180. * @param __pos Iterator referencing location in string to insert at.
  4181. * @param __s The C string to insert.
  4182. * @param __n The number of characters to insert.
  4183. * @return Reference to this string.
  4184. * @throw std::length_error If new length exceeds @c max_size().
  4185. * @throw std::out_of_range If @a __pos is beyond the end of this
  4186. * string.
  4187. *
  4188. * Inserts the first @a __n characters of @a __s starting at @a
  4189. * __pos. If adding characters causes the length to exceed
  4190. * max_size(), length_error is thrown. If @a __pos is beyond
  4191. * end(), out_of_range is thrown. The value of the string
  4192. * doesn't change if an error is thrown.
  4193. */
  4194. basic_string&
  4195. insert(size_type __pos, const _CharT* __s, size_type __n);
  4196. /**
  4197. * @brief Insert a C string.
  4198. * @param __pos Iterator referencing location in string to insert at.
  4199. * @param __s The C string to insert.
  4200. * @return Reference to this string.
  4201. * @throw std::length_error If new length exceeds @c max_size().
  4202. * @throw std::out_of_range If @a pos is beyond the end of this
  4203. * string.
  4204. *
  4205. * Inserts the first @a n characters of @a __s starting at @a __pos. If
  4206. * adding characters causes the length to exceed max_size(),
  4207. * length_error is thrown. If @a __pos is beyond end(), out_of_range is
  4208. * thrown. The value of the string doesn't change if an error is
  4209. * thrown.
  4210. */
  4211. basic_string&
  4212. insert(size_type __pos, const _CharT* __s)
  4213. {
  4214. __glibcxx_requires_string(__s);
  4215. return this->insert(__pos, __s, traits_type::length(__s));
  4216. }
  4217. /**
  4218. * @brief Insert multiple characters.
  4219. * @param __pos Index in string to insert at.
  4220. * @param __n Number of characters to insert
  4221. * @param __c The character to insert.
  4222. * @return Reference to this string.
  4223. * @throw std::length_error If new length exceeds @c max_size().
  4224. * @throw std::out_of_range If @a __pos is beyond the end of this
  4225. * string.
  4226. *
  4227. * Inserts @a __n copies of character @a __c starting at index
  4228. * @a __pos. If adding characters causes the length to exceed
  4229. * max_size(), length_error is thrown. If @a __pos > length(),
  4230. * out_of_range is thrown. The value of the string doesn't
  4231. * change if an error is thrown.
  4232. */
  4233. basic_string&
  4234. insert(size_type __pos, size_type __n, _CharT __c)
  4235. { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
  4236. size_type(0), __n, __c); }
  4237. /**
  4238. * @brief Insert one character.
  4239. * @param __p Iterator referencing position in string to insert at.
  4240. * @param __c The character to insert.
  4241. * @return Iterator referencing newly inserted char.
  4242. * @throw std::length_error If new length exceeds @c max_size().
  4243. *
  4244. * Inserts character @a __c at position referenced by @a __p.
  4245. * If adding character causes the length to exceed max_size(),
  4246. * length_error is thrown. If @a __p is beyond end of string,
  4247. * out_of_range is thrown. The value of the string doesn't
  4248. * change if an error is thrown.
  4249. */
  4250. iterator
  4251. insert(iterator __p, _CharT __c)
  4252. {
  4253. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  4254. const size_type __pos = __p - _M_ibegin();
  4255. _M_replace_aux(__pos, size_type(0), size_type(1), __c);
  4256. _M_rep()->_M_set_leaked();
  4257. return iterator(_M_data() + __pos);
  4258. }
  4259. #if __cplusplus >= 201703L
  4260. /**
  4261. * @brief Insert a string_view.
  4262. * @param __pos Iterator referencing position in string to insert at.
  4263. * @param __svt The object convertible to string_view to insert.
  4264. * @return Reference to this string.
  4265. */
  4266. template<typename _Tp>
  4267. _If_sv<_Tp, basic_string&>
  4268. insert(size_type __pos, const _Tp& __svt)
  4269. {
  4270. __sv_type __sv = __svt;
  4271. return this->insert(__pos, __sv.data(), __sv.size());
  4272. }
  4273. /**
  4274. * @brief Insert a string_view.
  4275. * @param __pos Iterator referencing position in string to insert at.
  4276. * @param __svt The object convertible to string_view to insert from.
  4277. * @param __pos Iterator referencing position in string_view to insert
  4278. * from.
  4279. * @param __n The number of characters to insert.
  4280. * @return Reference to this string.
  4281. */
  4282. template<typename _Tp>
  4283. _If_sv<_Tp, basic_string&>
  4284. insert(size_type __pos1, const _Tp& __svt,
  4285. size_type __pos2, size_type __n = npos)
  4286. {
  4287. __sv_type __sv = __svt;
  4288. return this->replace(__pos1, size_type(0), __sv.data()
  4289. + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
  4290. std::__sv_limit(__sv.size(), __pos2, __n));
  4291. }
  4292. #endif // C++17
  4293. /**
  4294. * @brief Remove characters.
  4295. * @param __pos Index of first character to remove (default 0).
  4296. * @param __n Number of characters to remove (default remainder).
  4297. * @return Reference to this string.
  4298. * @throw std::out_of_range If @a pos is beyond the end of this
  4299. * string.
  4300. *
  4301. * Removes @a __n characters from this string starting at @a
  4302. * __pos. The length of the string is reduced by @a __n. If
  4303. * there are < @a __n characters to remove, the remainder of
  4304. * the string is truncated. If @a __p is beyond end of string,
  4305. * out_of_range is thrown. The value of the string doesn't
  4306. * change if an error is thrown.
  4307. */
  4308. basic_string&
  4309. erase(size_type __pos = 0, size_type __n = npos)
  4310. {
  4311. _M_mutate(_M_check(__pos, "basic_string::erase"),
  4312. _M_limit(__pos, __n), size_type(0));
  4313. return *this;
  4314. }
  4315. /**
  4316. * @brief Remove one character.
  4317. * @param __position Iterator referencing the character to remove.
  4318. * @return iterator referencing same location after removal.
  4319. *
  4320. * Removes the character at @a __position from this string. The value
  4321. * of the string doesn't change if an error is thrown.
  4322. */
  4323. iterator
  4324. erase(iterator __position)
  4325. {
  4326. _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
  4327. && __position < _M_iend());
  4328. const size_type __pos = __position - _M_ibegin();
  4329. _M_mutate(__pos, size_type(1), size_type(0));
  4330. _M_rep()->_M_set_leaked();
  4331. return iterator(_M_data() + __pos);
  4332. }
  4333. /**
  4334. * @brief Remove a range of characters.
  4335. * @param __first Iterator referencing the first character to remove.
  4336. * @param __last Iterator referencing the end of the range.
  4337. * @return Iterator referencing location of first after removal.
  4338. *
  4339. * Removes the characters in the range [first,last) from this string.
  4340. * The value of the string doesn't change if an error is thrown.
  4341. */
  4342. iterator
  4343. erase(iterator __first, iterator __last);
  4344. #if __cplusplus >= 201103L
  4345. /**
  4346. * @brief Remove the last character.
  4347. *
  4348. * The string must be non-empty.
  4349. */
  4350. void
  4351. pop_back() // FIXME C++11: should be noexcept.
  4352. {
  4353. __glibcxx_assert(!empty());
  4354. erase(size() - 1, 1);
  4355. }
  4356. #endif // C++11
  4357. /**
  4358. * @brief Replace characters with value from another string.
  4359. * @param __pos Index of first character to replace.
  4360. * @param __n Number of characters to be replaced.
  4361. * @param __str String to insert.
  4362. * @return Reference to this string.
  4363. * @throw std::out_of_range If @a pos is beyond the end of this
  4364. * string.
  4365. * @throw std::length_error If new length exceeds @c max_size().
  4366. *
  4367. * Removes the characters in the range [__pos,__pos+__n) from
  4368. * this string. In place, the value of @a __str is inserted.
  4369. * If @a __pos is beyond end of string, out_of_range is thrown.
  4370. * If the length of the result exceeds max_size(), length_error
  4371. * is thrown. The value of the string doesn't change if an
  4372. * error is thrown.
  4373. */
  4374. basic_string&
  4375. replace(size_type __pos, size_type __n, const basic_string& __str)
  4376. { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
  4377. /**
  4378. * @brief Replace characters with value from another string.
  4379. * @param __pos1 Index of first character to replace.
  4380. * @param __n1 Number of characters to be replaced.
  4381. * @param __str String to insert.
  4382. * @param __pos2 Index of first character of str to use.
  4383. * @param __n2 Number of characters from str to use.
  4384. * @return Reference to this string.
  4385. * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
  4386. * __str.size().
  4387. * @throw std::length_error If new length exceeds @c max_size().
  4388. *
  4389. * Removes the characters in the range [__pos1,__pos1 + n) from this
  4390. * string. In place, the value of @a __str is inserted. If @a __pos is
  4391. * beyond end of string, out_of_range is thrown. If the length of the
  4392. * result exceeds max_size(), length_error is thrown. The value of the
  4393. * string doesn't change if an error is thrown.
  4394. */
  4395. basic_string&
  4396. replace(size_type __pos1, size_type __n1, const basic_string& __str,
  4397. size_type __pos2, size_type __n2 = npos)
  4398. { return this->replace(__pos1, __n1, __str._M_data()
  4399. + __str._M_check(__pos2, "basic_string::replace"),
  4400. __str._M_limit(__pos2, __n2)); }
  4401. /**
  4402. * @brief Replace characters with value of a C substring.
  4403. * @param __pos Index of first character to replace.
  4404. * @param __n1 Number of characters to be replaced.
  4405. * @param __s C string to insert.
  4406. * @param __n2 Number of characters from @a s to use.
  4407. * @return Reference to this string.
  4408. * @throw std::out_of_range If @a pos1 > size().
  4409. * @throw std::length_error If new length exceeds @c max_size().
  4410. *
  4411. * Removes the characters in the range [__pos,__pos + __n1)
  4412. * from this string. In place, the first @a __n2 characters of
  4413. * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
  4414. * @a __pos is beyond end of string, out_of_range is thrown. If
  4415. * the length of result exceeds max_size(), length_error is
  4416. * thrown. The value of the string doesn't change if an error
  4417. * is thrown.
  4418. */
  4419. basic_string&
  4420. replace(size_type __pos, size_type __n1, const _CharT* __s,
  4421. size_type __n2);
  4422. /**
  4423. * @brief Replace characters with value of a C string.
  4424. * @param __pos Index of first character to replace.
  4425. * @param __n1 Number of characters to be replaced.
  4426. * @param __s C string to insert.
  4427. * @return Reference to this string.
  4428. * @throw std::out_of_range If @a pos > size().
  4429. * @throw std::length_error If new length exceeds @c max_size().
  4430. *
  4431. * Removes the characters in the range [__pos,__pos + __n1)
  4432. * from this string. In place, the characters of @a __s are
  4433. * inserted. If @a __pos is beyond end of string, out_of_range
  4434. * is thrown. If the length of result exceeds max_size(),
  4435. * length_error is thrown. The value of the string doesn't
  4436. * change if an error is thrown.
  4437. */
  4438. basic_string&
  4439. replace(size_type __pos, size_type __n1, const _CharT* __s)
  4440. {
  4441. __glibcxx_requires_string(__s);
  4442. return this->replace(__pos, __n1, __s, traits_type::length(__s));
  4443. }
  4444. /**
  4445. * @brief Replace characters with multiple characters.
  4446. * @param __pos Index of first character to replace.
  4447. * @param __n1 Number of characters to be replaced.
  4448. * @param __n2 Number of characters to insert.
  4449. * @param __c Character to insert.
  4450. * @return Reference to this string.
  4451. * @throw std::out_of_range If @a __pos > size().
  4452. * @throw std::length_error If new length exceeds @c max_size().
  4453. *
  4454. * Removes the characters in the range [pos,pos + n1) from this
  4455. * string. In place, @a __n2 copies of @a __c are inserted.
  4456. * If @a __pos is beyond end of string, out_of_range is thrown.
  4457. * If the length of result exceeds max_size(), length_error is
  4458. * thrown. The value of the string doesn't change if an error
  4459. * is thrown.
  4460. */
  4461. basic_string&
  4462. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  4463. { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
  4464. _M_limit(__pos, __n1), __n2, __c); }
  4465. /**
  4466. * @brief Replace range of characters with string.
  4467. * @param __i1 Iterator referencing start of range to replace.
  4468. * @param __i2 Iterator referencing end of range to replace.
  4469. * @param __str String value to insert.
  4470. * @return Reference to this string.
  4471. * @throw std::length_error If new length exceeds @c max_size().
  4472. *
  4473. * Removes the characters in the range [__i1,__i2). In place,
  4474. * the value of @a __str is inserted. If the length of result
  4475. * exceeds max_size(), length_error is thrown. The value of
  4476. * the string doesn't change if an error is thrown.
  4477. */
  4478. basic_string&
  4479. replace(iterator __i1, iterator __i2, const basic_string& __str)
  4480. { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
  4481. /**
  4482. * @brief Replace range of characters with C substring.
  4483. * @param __i1 Iterator referencing start of range to replace.
  4484. * @param __i2 Iterator referencing end of range to replace.
  4485. * @param __s C string value to insert.
  4486. * @param __n Number of characters from s to insert.
  4487. * @return Reference to this string.
  4488. * @throw std::length_error If new length exceeds @c max_size().
  4489. *
  4490. * Removes the characters in the range [__i1,__i2). In place,
  4491. * the first @a __n characters of @a __s are inserted. If the
  4492. * length of result exceeds max_size(), length_error is thrown.
  4493. * The value of the string doesn't change if an error is
  4494. * thrown.
  4495. */
  4496. basic_string&
  4497. replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
  4498. {
  4499. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4500. && __i2 <= _M_iend());
  4501. return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
  4502. }
  4503. /**
  4504. * @brief Replace range of characters with C string.
  4505. * @param __i1 Iterator referencing start of range to replace.
  4506. * @param __i2 Iterator referencing end of range to replace.
  4507. * @param __s C string value to insert.
  4508. * @return Reference to this string.
  4509. * @throw std::length_error If new length exceeds @c max_size().
  4510. *
  4511. * Removes the characters in the range [__i1,__i2). In place,
  4512. * the characters of @a __s are inserted. If the length of
  4513. * result exceeds max_size(), length_error is thrown. The
  4514. * value of the string doesn't change if an error is thrown.
  4515. */
  4516. basic_string&
  4517. replace(iterator __i1, iterator __i2, const _CharT* __s)
  4518. {
  4519. __glibcxx_requires_string(__s);
  4520. return this->replace(__i1, __i2, __s, traits_type::length(__s));
  4521. }
  4522. /**
  4523. * @brief Replace range of characters with multiple characters
  4524. * @param __i1 Iterator referencing start of range to replace.
  4525. * @param __i2 Iterator referencing end of range to replace.
  4526. * @param __n Number of characters to insert.
  4527. * @param __c Character to insert.
  4528. * @return Reference to this string.
  4529. * @throw std::length_error If new length exceeds @c max_size().
  4530. *
  4531. * Removes the characters in the range [__i1,__i2). In place,
  4532. * @a __n copies of @a __c are inserted. If the length of
  4533. * result exceeds max_size(), length_error is thrown. The
  4534. * value of the string doesn't change if an error is thrown.
  4535. */
  4536. basic_string&
  4537. replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
  4538. {
  4539. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4540. && __i2 <= _M_iend());
  4541. return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
  4542. }
  4543. /**
  4544. * @brief Replace range of characters with range.
  4545. * @param __i1 Iterator referencing start of range to replace.
  4546. * @param __i2 Iterator referencing end of range to replace.
  4547. * @param __k1 Iterator referencing start of range to insert.
  4548. * @param __k2 Iterator referencing end of range to insert.
  4549. * @return Reference to this string.
  4550. * @throw std::length_error If new length exceeds @c max_size().
  4551. *
  4552. * Removes the characters in the range [__i1,__i2). In place,
  4553. * characters in the range [__k1,__k2) are inserted. If the
  4554. * length of result exceeds max_size(), length_error is thrown.
  4555. * The value of the string doesn't change if an error is
  4556. * thrown.
  4557. */
  4558. template<class _InputIterator>
  4559. basic_string&
  4560. replace(iterator __i1, iterator __i2,
  4561. _InputIterator __k1, _InputIterator __k2)
  4562. {
  4563. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4564. && __i2 <= _M_iend());
  4565. __glibcxx_requires_valid_range(__k1, __k2);
  4566. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  4567. return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
  4568. }
  4569. // Specializations for the common case of pointer and iterator:
  4570. // useful to avoid the overhead of temporary buffering in _M_replace.
  4571. basic_string&
  4572. replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
  4573. {
  4574. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4575. && __i2 <= _M_iend());
  4576. __glibcxx_requires_valid_range(__k1, __k2);
  4577. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  4578. __k1, __k2 - __k1);
  4579. }
  4580. basic_string&
  4581. replace(iterator __i1, iterator __i2,
  4582. const _CharT* __k1, const _CharT* __k2)
  4583. {
  4584. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4585. && __i2 <= _M_iend());
  4586. __glibcxx_requires_valid_range(__k1, __k2);
  4587. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  4588. __k1, __k2 - __k1);
  4589. }
  4590. basic_string&
  4591. replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
  4592. {
  4593. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4594. && __i2 <= _M_iend());
  4595. __glibcxx_requires_valid_range(__k1, __k2);
  4596. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  4597. __k1.base(), __k2 - __k1);
  4598. }
  4599. basic_string&
  4600. replace(iterator __i1, iterator __i2,
  4601. const_iterator __k1, const_iterator __k2)
  4602. {
  4603. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4604. && __i2 <= _M_iend());
  4605. __glibcxx_requires_valid_range(__k1, __k2);
  4606. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  4607. __k1.base(), __k2 - __k1);
  4608. }
  4609. #if __cplusplus >= 201103L
  4610. /**
  4611. * @brief Replace range of characters with initializer_list.
  4612. * @param __i1 Iterator referencing start of range to replace.
  4613. * @param __i2 Iterator referencing end of range to replace.
  4614. * @param __l The initializer_list of characters to insert.
  4615. * @return Reference to this string.
  4616. * @throw std::length_error If new length exceeds @c max_size().
  4617. *
  4618. * Removes the characters in the range [__i1,__i2). In place,
  4619. * characters in the range [__k1,__k2) are inserted. If the
  4620. * length of result exceeds max_size(), length_error is thrown.
  4621. * The value of the string doesn't change if an error is
  4622. * thrown.
  4623. */
  4624. basic_string& replace(iterator __i1, iterator __i2,
  4625. initializer_list<_CharT> __l)
  4626. { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
  4627. #endif // C++11
  4628. #if __cplusplus >= 201703L
  4629. /**
  4630. * @brief Replace range of characters with string_view.
  4631. * @param __pos The position to replace at.
  4632. * @param __n The number of characters to replace.
  4633. * @param __svt The object convertible to string_view to insert.
  4634. * @return Reference to this string.
  4635. */
  4636. template<typename _Tp>
  4637. _If_sv<_Tp, basic_string&>
  4638. replace(size_type __pos, size_type __n, const _Tp& __svt)
  4639. {
  4640. __sv_type __sv = __svt;
  4641. return this->replace(__pos, __n, __sv.data(), __sv.size());
  4642. }
  4643. /**
  4644. * @brief Replace range of characters with string_view.
  4645. * @param __pos1 The position to replace at.
  4646. * @param __n1 The number of characters to replace.
  4647. * @param __svt The object convertible to string_view to insert from.
  4648. * @param __pos2 The position in the string_view to insert from.
  4649. * @param __n2 The number of characters to insert.
  4650. * @return Reference to this string.
  4651. */
  4652. template<typename _Tp>
  4653. _If_sv<_Tp, basic_string&>
  4654. replace(size_type __pos1, size_type __n1, const _Tp& __svt,
  4655. size_type __pos2, size_type __n2 = npos)
  4656. {
  4657. __sv_type __sv = __svt;
  4658. return this->replace(__pos1, __n1,
  4659. __sv.data()
  4660. + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
  4661. std::__sv_limit(__sv.size(), __pos2, __n2));
  4662. }
  4663. /**
  4664. * @brief Replace range of characters with string_view.
  4665. * @param __i1 An iterator referencing the start position
  4666. to replace at.
  4667. * @param __i2 An iterator referencing the end position
  4668. for the replace.
  4669. * @param __svt The object convertible to string_view to insert from.
  4670. * @return Reference to this string.
  4671. */
  4672. template<typename _Tp>
  4673. _If_sv<_Tp, basic_string&>
  4674. replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
  4675. {
  4676. __sv_type __sv = __svt;
  4677. return this->replace(__i1 - begin(), __i2 - __i1, __sv);
  4678. }
  4679. #endif // C++17
  4680. private:
  4681. template<class _Integer>
  4682. basic_string&
  4683. _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
  4684. _Integer __val, __true_type)
  4685. { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
  4686. template<class _InputIterator>
  4687. basic_string&
  4688. _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
  4689. _InputIterator __k2, __false_type);
  4690. basic_string&
  4691. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  4692. _CharT __c);
  4693. basic_string&
  4694. _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
  4695. size_type __n2);
  4696. // _S_construct_aux is used to implement the 21.3.1 para 15 which
  4697. // requires special behaviour if _InIter is an integral type
  4698. template<class _InIterator>
  4699. static _CharT*
  4700. _S_construct_aux(_InIterator __beg, _InIterator __end,
  4701. const _Alloc& __a, __false_type)
  4702. {
  4703. typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
  4704. return _S_construct(__beg, __end, __a, _Tag());
  4705. }
  4706. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  4707. // 438. Ambiguity in the "do the right thing" clause
  4708. template<class _Integer>
  4709. static _CharT*
  4710. _S_construct_aux(_Integer __beg, _Integer __end,
  4711. const _Alloc& __a, __true_type)
  4712. { return _S_construct_aux_2(static_cast<size_type>(__beg),
  4713. __end, __a); }
  4714. static _CharT*
  4715. _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
  4716. { return _S_construct(__req, __c, __a); }
  4717. template<class _InIterator>
  4718. static _CharT*
  4719. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
  4720. {
  4721. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  4722. return _S_construct_aux(__beg, __end, __a, _Integral());
  4723. }
  4724. // For Input Iterators, used in istreambuf_iterators, etc.
  4725. template<class _InIterator>
  4726. static _CharT*
  4727. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  4728. input_iterator_tag);
  4729. // For forward_iterators up to random_access_iterators, used for
  4730. // string::iterator, _CharT*, etc.
  4731. template<class _FwdIterator>
  4732. static _CharT*
  4733. _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
  4734. forward_iterator_tag);
  4735. static _CharT*
  4736. _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
  4737. public:
  4738. /**
  4739. * @brief Copy substring into C string.
  4740. * @param __s C string to copy value into.
  4741. * @param __n Number of characters to copy.
  4742. * @param __pos Index of first character to copy.
  4743. * @return Number of characters actually copied
  4744. * @throw std::out_of_range If __pos > size().
  4745. *
  4746. * Copies up to @a __n characters starting at @a __pos into the
  4747. * C string @a __s. If @a __pos is %greater than size(),
  4748. * out_of_range is thrown.
  4749. */
  4750. size_type
  4751. copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
  4752. /**
  4753. * @brief Swap contents with another string.
  4754. * @param __s String to swap with.
  4755. *
  4756. * Exchanges the contents of this string with that of @a __s in constant
  4757. * time.
  4758. */
  4759. void
  4760. swap(basic_string& __s)
  4761. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
  4762. // String operations:
  4763. /**
  4764. * @brief Return const pointer to null-terminated contents.
  4765. *
  4766. * This is a handle to internal data. Do not modify or dire things may
  4767. * happen.
  4768. */
  4769. const _CharT*
  4770. c_str() const _GLIBCXX_NOEXCEPT
  4771. { return _M_data(); }
  4772. /**
  4773. * @brief Return const pointer to contents.
  4774. *
  4775. * This is a pointer to internal data. It is undefined to modify
  4776. * the contents through the returned pointer. To get a pointer that
  4777. * allows modifying the contents use @c &str[0] instead,
  4778. * (or in C++17 the non-const @c str.data() overload).
  4779. */
  4780. const _CharT*
  4781. data() const _GLIBCXX_NOEXCEPT
  4782. { return _M_data(); }
  4783. #if __cplusplus >= 201703L
  4784. /**
  4785. * @brief Return non-const pointer to contents.
  4786. *
  4787. * This is a pointer to the character sequence held by the string.
  4788. * Modifying the characters in the sequence is allowed.
  4789. */
  4790. _CharT*
  4791. data() noexcept
  4792. {
  4793. _M_leak();
  4794. return _M_data();
  4795. }
  4796. #endif
  4797. /**
  4798. * @brief Return copy of allocator used to construct this string.
  4799. */
  4800. allocator_type
  4801. get_allocator() const _GLIBCXX_NOEXCEPT
  4802. { return _M_dataplus; }
  4803. /**
  4804. * @brief Find position of a C substring.
  4805. * @param __s C string to locate.
  4806. * @param __pos Index of character to search from.
  4807. * @param __n Number of characters from @a s to search for.
  4808. * @return Index of start of first occurrence.
  4809. *
  4810. * Starting from @a __pos, searches forward for the first @a
  4811. * __n characters in @a __s within this string. If found,
  4812. * returns the index where it begins. If not found, returns
  4813. * npos.
  4814. */
  4815. size_type
  4816. find(const _CharT* __s, size_type __pos, size_type __n) const
  4817. _GLIBCXX_NOEXCEPT;
  4818. /**
  4819. * @brief Find position of a string.
  4820. * @param __str String to locate.
  4821. * @param __pos Index of character to search from (default 0).
  4822. * @return Index of start of first occurrence.
  4823. *
  4824. * Starting from @a __pos, searches forward for value of @a __str within
  4825. * this string. If found, returns the index where it begins. If not
  4826. * found, returns npos.
  4827. */
  4828. size_type
  4829. find(const basic_string& __str, size_type __pos = 0) const
  4830. _GLIBCXX_NOEXCEPT
  4831. { return this->find(__str.data(), __pos, __str.size()); }
  4832. /**
  4833. * @brief Find position of a C string.
  4834. * @param __s C string to locate.
  4835. * @param __pos Index of character to search from (default 0).
  4836. * @return Index of start of first occurrence.
  4837. *
  4838. * Starting from @a __pos, searches forward for the value of @a
  4839. * __s within this string. If found, returns the index where
  4840. * it begins. If not found, returns npos.
  4841. */
  4842. size_type
  4843. find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  4844. {
  4845. __glibcxx_requires_string(__s);
  4846. return this->find(__s, __pos, traits_type::length(__s));
  4847. }
  4848. /**
  4849. * @brief Find position of a character.
  4850. * @param __c Character to locate.
  4851. * @param __pos Index of character to search from (default 0).
  4852. * @return Index of first occurrence.
  4853. *
  4854. * Starting from @a __pos, searches forward for @a __c within
  4855. * this string. If found, returns the index where it was
  4856. * found. If not found, returns npos.
  4857. */
  4858. size_type
  4859. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
  4860. #if __cplusplus >= 201703L
  4861. /**
  4862. * @brief Find position of a string_view.
  4863. * @param __svt The object convertible to string_view to locate.
  4864. * @param __pos Index of character to search from (default 0).
  4865. * @return Index of start of first occurrence.
  4866. */
  4867. template<typename _Tp>
  4868. _If_sv<_Tp, size_type>
  4869. find(const _Tp& __svt, size_type __pos = 0) const
  4870. noexcept(is_same<_Tp, __sv_type>::value)
  4871. {
  4872. __sv_type __sv = __svt;
  4873. return this->find(__sv.data(), __pos, __sv.size());
  4874. }
  4875. #endif // C++17
  4876. /**
  4877. * @brief Find last position of a string.
  4878. * @param __str String to locate.
  4879. * @param __pos Index of character to search back from (default end).
  4880. * @return Index of start of last occurrence.
  4881. *
  4882. * Starting from @a __pos, searches backward for value of @a
  4883. * __str within this string. If found, returns the index where
  4884. * it begins. If not found, returns npos.
  4885. */
  4886. size_type
  4887. rfind(const basic_string& __str, size_type __pos = npos) const
  4888. _GLIBCXX_NOEXCEPT
  4889. { return this->rfind(__str.data(), __pos, __str.size()); }
  4890. /**
  4891. * @brief Find last position of a C substring.
  4892. * @param __s C string to locate.
  4893. * @param __pos Index of character to search back from.
  4894. * @param __n Number of characters from s to search for.
  4895. * @return Index of start of last occurrence.
  4896. *
  4897. * Starting from @a __pos, searches backward for the first @a
  4898. * __n characters in @a __s within this string. If found,
  4899. * returns the index where it begins. If not found, returns
  4900. * npos.
  4901. */
  4902. size_type
  4903. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  4904. _GLIBCXX_NOEXCEPT;
  4905. /**
  4906. * @brief Find last position of a C string.
  4907. * @param __s C string to locate.
  4908. * @param __pos Index of character to start search at (default end).
  4909. * @return Index of start of last occurrence.
  4910. *
  4911. * Starting from @a __pos, searches backward for the value of
  4912. * @a __s within this string. If found, returns the index
  4913. * where it begins. If not found, returns npos.
  4914. */
  4915. size_type
  4916. rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  4917. {
  4918. __glibcxx_requires_string(__s);
  4919. return this->rfind(__s, __pos, traits_type::length(__s));
  4920. }
  4921. /**
  4922. * @brief Find last position of a character.
  4923. * @param __c Character to locate.
  4924. * @param __pos Index of character to search back from (default end).
  4925. * @return Index of last occurrence.
  4926. *
  4927. * Starting from @a __pos, searches backward for @a __c within
  4928. * this string. If found, returns the index where it was
  4929. * found. If not found, returns npos.
  4930. */
  4931. size_type
  4932. rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
  4933. #if __cplusplus >= 201703L
  4934. /**
  4935. * @brief Find last position of a string_view.
  4936. * @param __svt The object convertible to string_view to locate.
  4937. * @param __pos Index of character to search back from (default end).
  4938. * @return Index of start of last occurrence.
  4939. */
  4940. template<typename _Tp>
  4941. _If_sv<_Tp, size_type>
  4942. rfind(const _Tp& __svt, size_type __pos = npos) const
  4943. noexcept(is_same<_Tp, __sv_type>::value)
  4944. {
  4945. __sv_type __sv = __svt;
  4946. return this->rfind(__sv.data(), __pos, __sv.size());
  4947. }
  4948. #endif // C++17
  4949. /**
  4950. * @brief Find position of a character of string.
  4951. * @param __str String containing characters to locate.
  4952. * @param __pos Index of character to search from (default 0).
  4953. * @return Index of first occurrence.
  4954. *
  4955. * Starting from @a __pos, searches forward for one of the
  4956. * characters of @a __str within this string. If found,
  4957. * returns the index where it was found. If not found, returns
  4958. * npos.
  4959. */
  4960. size_type
  4961. find_first_of(const basic_string& __str, size_type __pos = 0) const
  4962. _GLIBCXX_NOEXCEPT
  4963. { return this->find_first_of(__str.data(), __pos, __str.size()); }
  4964. /**
  4965. * @brief Find position of a character of C substring.
  4966. * @param __s String containing characters to locate.
  4967. * @param __pos Index of character to search from.
  4968. * @param __n Number of characters from s to search for.
  4969. * @return Index of first occurrence.
  4970. *
  4971. * Starting from @a __pos, searches forward for one of the
  4972. * first @a __n characters of @a __s within this string. If
  4973. * found, returns the index where it was found. If not found,
  4974. * returns npos.
  4975. */
  4976. size_type
  4977. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  4978. _GLIBCXX_NOEXCEPT;
  4979. /**
  4980. * @brief Find position of a character of C string.
  4981. * @param __s String containing characters to locate.
  4982. * @param __pos Index of character to search from (default 0).
  4983. * @return Index of first occurrence.
  4984. *
  4985. * Starting from @a __pos, searches forward for one of the
  4986. * characters of @a __s within this string. If found, returns
  4987. * the index where it was found. If not found, returns npos.
  4988. */
  4989. size_type
  4990. find_first_of(const _CharT* __s, size_type __pos = 0) const
  4991. _GLIBCXX_NOEXCEPT
  4992. {
  4993. __glibcxx_requires_string(__s);
  4994. return this->find_first_of(__s, __pos, traits_type::length(__s));
  4995. }
  4996. /**
  4997. * @brief Find position of a character.
  4998. * @param __c Character to locate.
  4999. * @param __pos Index of character to search from (default 0).
  5000. * @return Index of first occurrence.
  5001. *
  5002. * Starting from @a __pos, searches forward for the character
  5003. * @a __c within this string. If found, returns the index
  5004. * where it was found. If not found, returns npos.
  5005. *
  5006. * Note: equivalent to find(__c, __pos).
  5007. */
  5008. size_type
  5009. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  5010. { return this->find(__c, __pos); }
  5011. #if __cplusplus >= 201703L
  5012. /**
  5013. * @brief Find position of a character of a string_view.
  5014. * @param __svt An object convertible to string_view containing
  5015. * characters to locate.
  5016. * @param __pos Index of character to search from (default 0).
  5017. * @return Index of first occurrence.
  5018. */
  5019. template<typename _Tp>
  5020. _If_sv<_Tp, size_type>
  5021. find_first_of(const _Tp& __svt, size_type __pos = 0) const
  5022. noexcept(is_same<_Tp, __sv_type>::value)
  5023. {
  5024. __sv_type __sv = __svt;
  5025. return this->find_first_of(__sv.data(), __pos, __sv.size());
  5026. }
  5027. #endif // C++17
  5028. /**
  5029. * @brief Find last position of a character of string.
  5030. * @param __str String containing characters to locate.
  5031. * @param __pos Index of character to search back from (default end).
  5032. * @return Index of last occurrence.
  5033. *
  5034. * Starting from @a __pos, searches backward for one of the
  5035. * characters of @a __str within this string. If found,
  5036. * returns the index where it was found. If not found, returns
  5037. * npos.
  5038. */
  5039. size_type
  5040. find_last_of(const basic_string& __str, size_type __pos = npos) const
  5041. _GLIBCXX_NOEXCEPT
  5042. { return this->find_last_of(__str.data(), __pos, __str.size()); }
  5043. /**
  5044. * @brief Find last position of a character of C substring.
  5045. * @param __s C string containing characters to locate.
  5046. * @param __pos Index of character to search back from.
  5047. * @param __n Number of characters from s to search for.
  5048. * @return Index of last occurrence.
  5049. *
  5050. * Starting from @a __pos, searches backward for one of the
  5051. * first @a __n characters of @a __s within this string. If
  5052. * found, returns the index where it was found. If not found,
  5053. * returns npos.
  5054. */
  5055. size_type
  5056. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  5057. _GLIBCXX_NOEXCEPT;
  5058. /**
  5059. * @brief Find last position of a character of C string.
  5060. * @param __s C string containing characters to locate.
  5061. * @param __pos Index of character to search back from (default end).
  5062. * @return Index of last occurrence.
  5063. *
  5064. * Starting from @a __pos, searches backward for one of the
  5065. * characters of @a __s within this string. If found, returns
  5066. * the index where it was found. If not found, returns npos.
  5067. */
  5068. size_type
  5069. find_last_of(const _CharT* __s, size_type __pos = npos) const
  5070. _GLIBCXX_NOEXCEPT
  5071. {
  5072. __glibcxx_requires_string(__s);
  5073. return this->find_last_of(__s, __pos, traits_type::length(__s));
  5074. }
  5075. /**
  5076. * @brief Find last position of a character.
  5077. * @param __c Character to locate.
  5078. * @param __pos Index of character to search back from (default end).
  5079. * @return Index of last occurrence.
  5080. *
  5081. * Starting from @a __pos, searches backward for @a __c within
  5082. * this string. If found, returns the index where it was
  5083. * found. If not found, returns npos.
  5084. *
  5085. * Note: equivalent to rfind(__c, __pos).
  5086. */
  5087. size_type
  5088. find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  5089. { return this->rfind(__c, __pos); }
  5090. #if __cplusplus >= 201703L
  5091. /**
  5092. * @brief Find last position of a character of string.
  5093. * @param __svt An object convertible to string_view containing
  5094. * characters to locate.
  5095. * @param __pos Index of character to search back from (default end).
  5096. * @return Index of last occurrence.
  5097. */
  5098. template<typename _Tp>
  5099. _If_sv<_Tp, size_type>
  5100. find_last_of(const _Tp& __svt, size_type __pos = npos) const
  5101. noexcept(is_same<_Tp, __sv_type>::value)
  5102. {
  5103. __sv_type __sv = __svt;
  5104. return this->find_last_of(__sv.data(), __pos, __sv.size());
  5105. }
  5106. #endif // C++17
  5107. /**
  5108. * @brief Find position of a character not in string.
  5109. * @param __str String containing characters to avoid.
  5110. * @param __pos Index of character to search from (default 0).
  5111. * @return Index of first occurrence.
  5112. *
  5113. * Starting from @a __pos, searches forward for a character not contained
  5114. * in @a __str within this string. If found, returns the index where it
  5115. * was found. If not found, returns npos.
  5116. */
  5117. size_type
  5118. find_first_not_of(const basic_string& __str, size_type __pos = 0) const
  5119. _GLIBCXX_NOEXCEPT
  5120. { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
  5121. /**
  5122. * @brief Find position of a character not in C substring.
  5123. * @param __s C string containing characters to avoid.
  5124. * @param __pos Index of character to search from.
  5125. * @param __n Number of characters from __s to consider.
  5126. * @return Index of first occurrence.
  5127. *
  5128. * Starting from @a __pos, searches forward for a character not
  5129. * contained in the first @a __n characters of @a __s within
  5130. * this string. If found, returns the index where it was
  5131. * found. If not found, returns npos.
  5132. */
  5133. size_type
  5134. find_first_not_of(const _CharT* __s, size_type __pos,
  5135. size_type __n) const _GLIBCXX_NOEXCEPT;
  5136. /**
  5137. * @brief Find position of a character not in C string.
  5138. * @param __s C string containing characters to avoid.
  5139. * @param __pos Index of character to search from (default 0).
  5140. * @return Index of first occurrence.
  5141. *
  5142. * Starting from @a __pos, searches forward for a character not
  5143. * contained in @a __s within this string. If found, returns
  5144. * the index where it was found. If not found, returns npos.
  5145. */
  5146. size_type
  5147. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  5148. _GLIBCXX_NOEXCEPT
  5149. {
  5150. __glibcxx_requires_string(__s);
  5151. return this->find_first_not_of(__s, __pos, traits_type::length(__s));
  5152. }
  5153. /**
  5154. * @brief Find position of a different character.
  5155. * @param __c Character to avoid.
  5156. * @param __pos Index of character to search from (default 0).
  5157. * @return Index of first occurrence.
  5158. *
  5159. * Starting from @a __pos, searches forward for a character
  5160. * other than @a __c within this string. If found, returns the
  5161. * index where it was found. If not found, returns npos.
  5162. */
  5163. size_type
  5164. find_first_not_of(_CharT __c, size_type __pos = 0) const
  5165. _GLIBCXX_NOEXCEPT;
  5166. #if __cplusplus >= 201703L
  5167. /**
  5168. * @brief Find position of a character not in a string_view.
  5169. * @param __svt An object convertible to string_view containing
  5170. * characters to avoid.
  5171. * @param __pos Index of character to search from (default 0).
  5172. * @return Index of first occurrence.
  5173. */
  5174. template<typename _Tp>
  5175. _If_sv<_Tp, size_type>
  5176. find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
  5177. noexcept(is_same<_Tp, __sv_type>::value)
  5178. {
  5179. __sv_type __sv = __svt;
  5180. return this->find_first_not_of(__sv.data(), __pos, __sv.size());
  5181. }
  5182. #endif // C++17
  5183. /**
  5184. * @brief Find last position of a character not in string.
  5185. * @param __str String containing characters to avoid.
  5186. * @param __pos Index of character to search back from (default end).
  5187. * @return Index of last occurrence.
  5188. *
  5189. * Starting from @a __pos, searches backward for a character
  5190. * not contained in @a __str within this string. If found,
  5191. * returns the index where it was found. If not found, returns
  5192. * npos.
  5193. */
  5194. size_type
  5195. find_last_not_of(const basic_string& __str, size_type __pos = npos) const
  5196. _GLIBCXX_NOEXCEPT
  5197. { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
  5198. /**
  5199. * @brief Find last position of a character not in C substring.
  5200. * @param __s C string containing characters to avoid.
  5201. * @param __pos Index of character to search back from.
  5202. * @param __n Number of characters from s to consider.
  5203. * @return Index of last occurrence.
  5204. *
  5205. * Starting from @a __pos, searches backward for a character not
  5206. * contained in the first @a __n characters of @a __s within this string.
  5207. * If found, returns the index where it was found. If not found,
  5208. * returns npos.
  5209. */
  5210. size_type
  5211. find_last_not_of(const _CharT* __s, size_type __pos,
  5212. size_type __n) const _GLIBCXX_NOEXCEPT;
  5213. /**
  5214. * @brief Find last position of a character not in C string.
  5215. * @param __s C string containing characters to avoid.
  5216. * @param __pos Index of character to search back from (default end).
  5217. * @return Index of last occurrence.
  5218. *
  5219. * Starting from @a __pos, searches backward for a character
  5220. * not contained in @a __s within this string. If found,
  5221. * returns the index where it was found. If not found, returns
  5222. * npos.
  5223. */
  5224. size_type
  5225. find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  5226. _GLIBCXX_NOEXCEPT
  5227. {
  5228. __glibcxx_requires_string(__s);
  5229. return this->find_last_not_of(__s, __pos, traits_type::length(__s));
  5230. }
  5231. /**
  5232. * @brief Find last position of a different character.
  5233. * @param __c Character to avoid.
  5234. * @param __pos Index of character to search back from (default end).
  5235. * @return Index of last occurrence.
  5236. *
  5237. * Starting from @a __pos, searches backward for a character other than
  5238. * @a __c within this string. If found, returns the index where it was
  5239. * found. If not found, returns npos.
  5240. */
  5241. size_type
  5242. find_last_not_of(_CharT __c, size_type __pos = npos) const
  5243. _GLIBCXX_NOEXCEPT;
  5244. #if __cplusplus >= 201703L
  5245. /**
  5246. * @brief Find last position of a character not in a string_view.
  5247. * @param __svt An object convertible to string_view containing
  5248. * characters to avoid.
  5249. * @param __pos Index of character to search back from (default end).
  5250. * @return Index of last occurrence.
  5251. */
  5252. template<typename _Tp>
  5253. _If_sv<_Tp, size_type>
  5254. find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
  5255. noexcept(is_same<_Tp, __sv_type>::value)
  5256. {
  5257. __sv_type __sv = __svt;
  5258. return this->find_last_not_of(__sv.data(), __pos, __sv.size());
  5259. }
  5260. #endif // C++17
  5261. /**
  5262. * @brief Get a substring.
  5263. * @param __pos Index of first character (default 0).
  5264. * @param __n Number of characters in substring (default remainder).
  5265. * @return The new string.
  5266. * @throw std::out_of_range If __pos > size().
  5267. *
  5268. * Construct and return a new string using the @a __n
  5269. * characters starting at @a __pos. If the string is too
  5270. * short, use the remainder of the characters. If @a __pos is
  5271. * beyond the end of the string, out_of_range is thrown.
  5272. */
  5273. basic_string
  5274. substr(size_type __pos = 0, size_type __n = npos) const
  5275. { return basic_string(*this,
  5276. _M_check(__pos, "basic_string::substr"), __n); }
  5277. /**
  5278. * @brief Compare to a string.
  5279. * @param __str String to compare against.
  5280. * @return Integer < 0, 0, or > 0.
  5281. *
  5282. * Returns an integer < 0 if this string is ordered before @a
  5283. * __str, 0 if their values are equivalent, or > 0 if this
  5284. * string is ordered after @a __str. Determines the effective
  5285. * length rlen of the strings to compare as the smallest of
  5286. * size() and str.size(). The function then compares the two
  5287. * strings by calling traits::compare(data(), str.data(),rlen).
  5288. * If the result of the comparison is nonzero returns it,
  5289. * otherwise the shorter one is ordered first.
  5290. */
  5291. int
  5292. compare(const basic_string& __str) const
  5293. {
  5294. const size_type __size = this->size();
  5295. const size_type __osize = __str.size();
  5296. const size_type __len = std::min(__size, __osize);
  5297. int __r = traits_type::compare(_M_data(), __str.data(), __len);
  5298. if (!__r)
  5299. __r = _S_compare(__size, __osize);
  5300. return __r;
  5301. }
  5302. #if __cplusplus >= 201703L
  5303. /**
  5304. * @brief Compare to a string_view.
  5305. * @param __svt An object convertible to string_view to compare against.
  5306. * @return Integer < 0, 0, or > 0.
  5307. */
  5308. template<typename _Tp>
  5309. _If_sv<_Tp, int>
  5310. compare(const _Tp& __svt) const
  5311. noexcept(is_same<_Tp, __sv_type>::value)
  5312. {
  5313. __sv_type __sv = __svt;
  5314. const size_type __size = this->size();
  5315. const size_type __osize = __sv.size();
  5316. const size_type __len = std::min(__size, __osize);
  5317. int __r = traits_type::compare(_M_data(), __sv.data(), __len);
  5318. if (!__r)
  5319. __r = _S_compare(__size, __osize);
  5320. return __r;
  5321. }
  5322. /**
  5323. * @brief Compare to a string_view.
  5324. * @param __pos A position in the string to start comparing from.
  5325. * @param __n The number of characters to compare.
  5326. * @param __svt An object convertible to string_view to compare
  5327. * against.
  5328. * @return Integer < 0, 0, or > 0.
  5329. */
  5330. template<typename _Tp>
  5331. _If_sv<_Tp, int>
  5332. compare(size_type __pos, size_type __n, const _Tp& __svt) const
  5333. noexcept(is_same<_Tp, __sv_type>::value)
  5334. {
  5335. __sv_type __sv = __svt;
  5336. return __sv_type(*this).substr(__pos, __n).compare(__sv);
  5337. }
  5338. /**
  5339. * @brief Compare to a string_view.
  5340. * @param __pos1 A position in the string to start comparing from.
  5341. * @param __n1 The number of characters to compare.
  5342. * @param __svt An object convertible to string_view to compare
  5343. * against.
  5344. * @param __pos2 A position in the string_view to start comparing from.
  5345. * @param __n2 The number of characters to compare.
  5346. * @return Integer < 0, 0, or > 0.
  5347. */
  5348. template<typename _Tp>
  5349. _If_sv<_Tp, int>
  5350. compare(size_type __pos1, size_type __n1, const _Tp& __svt,
  5351. size_type __pos2, size_type __n2 = npos) const
  5352. noexcept(is_same<_Tp, __sv_type>::value)
  5353. {
  5354. __sv_type __sv = __svt;
  5355. return __sv_type(*this)
  5356. .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  5357. }
  5358. #endif // C++17
  5359. /**
  5360. * @brief Compare substring to a string.
  5361. * @param __pos Index of first character of substring.
  5362. * @param __n Number of characters in substring.
  5363. * @param __str String to compare against.
  5364. * @return Integer < 0, 0, or > 0.
  5365. *
  5366. * Form the substring of this string from the @a __n characters
  5367. * starting at @a __pos. Returns an integer < 0 if the
  5368. * substring is ordered before @a __str, 0 if their values are
  5369. * equivalent, or > 0 if the substring is ordered after @a
  5370. * __str. Determines the effective length rlen of the strings
  5371. * to compare as the smallest of the length of the substring
  5372. * and @a __str.size(). The function then compares the two
  5373. * strings by calling
  5374. * traits::compare(substring.data(),str.data(),rlen). If the
  5375. * result of the comparison is nonzero returns it, otherwise
  5376. * the shorter one is ordered first.
  5377. */
  5378. int
  5379. compare(size_type __pos, size_type __n, const basic_string& __str) const;
  5380. /**
  5381. * @brief Compare substring to a substring.
  5382. * @param __pos1 Index of first character of substring.
  5383. * @param __n1 Number of characters in substring.
  5384. * @param __str String to compare against.
  5385. * @param __pos2 Index of first character of substring of str.
  5386. * @param __n2 Number of characters in substring of str.
  5387. * @return Integer < 0, 0, or > 0.
  5388. *
  5389. * Form the substring of this string from the @a __n1
  5390. * characters starting at @a __pos1. Form the substring of @a
  5391. * __str from the @a __n2 characters starting at @a __pos2.
  5392. * Returns an integer < 0 if this substring is ordered before
  5393. * the substring of @a __str, 0 if their values are equivalent,
  5394. * or > 0 if this substring is ordered after the substring of
  5395. * @a __str. Determines the effective length rlen of the
  5396. * strings to compare as the smallest of the lengths of the
  5397. * substrings. The function then compares the two strings by
  5398. * calling
  5399. * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
  5400. * If the result of the comparison is nonzero returns it,
  5401. * otherwise the shorter one is ordered first.
  5402. */
  5403. int
  5404. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  5405. size_type __pos2, size_type __n2 = npos) const;
  5406. /**
  5407. * @brief Compare to a C string.
  5408. * @param __s C string to compare against.
  5409. * @return Integer < 0, 0, or > 0.
  5410. *
  5411. * Returns an integer < 0 if this string is ordered before @a __s, 0 if
  5412. * their values are equivalent, or > 0 if this string is ordered after
  5413. * @a __s. Determines the effective length rlen of the strings to
  5414. * compare as the smallest of size() and the length of a string
  5415. * constructed from @a __s. The function then compares the two strings
  5416. * by calling traits::compare(data(),s,rlen). If the result of the
  5417. * comparison is nonzero returns it, otherwise the shorter one is
  5418. * ordered first.
  5419. */
  5420. int
  5421. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
  5422. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  5423. // 5 String::compare specification questionable
  5424. /**
  5425. * @brief Compare substring to a C string.
  5426. * @param __pos Index of first character of substring.
  5427. * @param __n1 Number of characters in substring.
  5428. * @param __s C string to compare against.
  5429. * @return Integer < 0, 0, or > 0.
  5430. *
  5431. * Form the substring of this string from the @a __n1
  5432. * characters starting at @a pos. Returns an integer < 0 if
  5433. * the substring is ordered before @a __s, 0 if their values
  5434. * are equivalent, or > 0 if the substring is ordered after @a
  5435. * __s. Determines the effective length rlen of the strings to
  5436. * compare as the smallest of the length of the substring and
  5437. * the length of a string constructed from @a __s. The
  5438. * function then compares the two string by calling
  5439. * traits::compare(substring.data(),__s,rlen). If the result of
  5440. * the comparison is nonzero returns it, otherwise the shorter
  5441. * one is ordered first.
  5442. */
  5443. int
  5444. compare(size_type __pos, size_type __n1, const _CharT* __s) const;
  5445. /**
  5446. * @brief Compare substring against a character %array.
  5447. * @param __pos Index of first character of substring.
  5448. * @param __n1 Number of characters in substring.
  5449. * @param __s character %array to compare against.
  5450. * @param __n2 Number of characters of s.
  5451. * @return Integer < 0, 0, or > 0.
  5452. *
  5453. * Form the substring of this string from the @a __n1
  5454. * characters starting at @a __pos. Form a string from the
  5455. * first @a __n2 characters of @a __s. Returns an integer < 0
  5456. * if this substring is ordered before the string from @a __s,
  5457. * 0 if their values are equivalent, or > 0 if this substring
  5458. * is ordered after the string from @a __s. Determines the
  5459. * effective length rlen of the strings to compare as the
  5460. * smallest of the length of the substring and @a __n2. The
  5461. * function then compares the two strings by calling
  5462. * traits::compare(substring.data(),s,rlen). If the result of
  5463. * the comparison is nonzero returns it, otherwise the shorter
  5464. * one is ordered first.
  5465. *
  5466. * NB: s must have at least n2 characters, &apos;\\0&apos; has
  5467. * no special meaning.
  5468. */
  5469. int
  5470. compare(size_type __pos, size_type __n1, const _CharT* __s,
  5471. size_type __n2) const;
  5472. #if __cplusplus > 201703L
  5473. bool
  5474. starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  5475. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  5476. bool
  5477. starts_with(_CharT __x) const noexcept
  5478. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  5479. bool
  5480. starts_with(const _CharT* __x) const noexcept
  5481. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  5482. bool
  5483. ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  5484. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  5485. bool
  5486. ends_with(_CharT __x) const noexcept
  5487. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  5488. bool
  5489. ends_with(const _CharT* __x) const noexcept
  5490. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  5491. #endif // C++20
  5492. # ifdef _GLIBCXX_TM_TS_INTERNAL
  5493. friend void
  5494. ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
  5495. void* exc);
  5496. friend const char*
  5497. ::_txnal_cow_string_c_str(const void *that);
  5498. friend void
  5499. ::_txnal_cow_string_D1(void *that);
  5500. friend void
  5501. ::_txnal_cow_string_D1_commit(void *that);
  5502. # endif
  5503. };
  5504. #endif // !_GLIBCXX_USE_CXX11_ABI
  5505. #if __cpp_deduction_guides >= 201606
  5506. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  5507. template<typename _InputIterator, typename _CharT
  5508. = typename iterator_traits<_InputIterator>::value_type,
  5509. typename _Allocator = allocator<_CharT>,
  5510. typename = _RequireInputIter<_InputIterator>,
  5511. typename = _RequireAllocator<_Allocator>>
  5512. basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
  5513. -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
  5514. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  5515. // 3075. basic_string needs deduction guides from basic_string_view
  5516. template<typename _CharT, typename _Traits,
  5517. typename _Allocator = allocator<_CharT>,
  5518. typename = _RequireAllocator<_Allocator>>
  5519. basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
  5520. -> basic_string<_CharT, _Traits, _Allocator>;
  5521. template<typename _CharT, typename _Traits,
  5522. typename _Allocator = allocator<_CharT>,
  5523. typename = _RequireAllocator<_Allocator>>
  5524. basic_string(basic_string_view<_CharT, _Traits>,
  5525. typename basic_string<_CharT, _Traits, _Allocator>::size_type,
  5526. typename basic_string<_CharT, _Traits, _Allocator>::size_type,
  5527. const _Allocator& = _Allocator())
  5528. -> basic_string<_CharT, _Traits, _Allocator>;
  5529. _GLIBCXX_END_NAMESPACE_CXX11
  5530. #endif
  5531. // operator+
  5532. /**
  5533. * @brief Concatenate two strings.
  5534. * @param __lhs First string.
  5535. * @param __rhs Last string.
  5536. * @return New string with value of @a __lhs followed by @a __rhs.
  5537. */
  5538. template<typename _CharT, typename _Traits, typename _Alloc>
  5539. basic_string<_CharT, _Traits, _Alloc>
  5540. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5541. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5542. {
  5543. basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
  5544. __str.append(__rhs);
  5545. return __str;
  5546. }
  5547. /**
  5548. * @brief Concatenate C string and string.
  5549. * @param __lhs First string.
  5550. * @param __rhs Last string.
  5551. * @return New string with value of @a __lhs followed by @a __rhs.
  5552. */
  5553. template<typename _CharT, typename _Traits, typename _Alloc>
  5554. basic_string<_CharT,_Traits,_Alloc>
  5555. operator+(const _CharT* __lhs,
  5556. const basic_string<_CharT,_Traits,_Alloc>& __rhs);
  5557. /**
  5558. * @brief Concatenate character and string.
  5559. * @param __lhs First string.
  5560. * @param __rhs Last string.
  5561. * @return New string with @a __lhs followed by @a __rhs.
  5562. */
  5563. template<typename _CharT, typename _Traits, typename _Alloc>
  5564. basic_string<_CharT,_Traits,_Alloc>
  5565. operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
  5566. /**
  5567. * @brief Concatenate string and C string.
  5568. * @param __lhs First string.
  5569. * @param __rhs Last string.
  5570. * @return New string with @a __lhs followed by @a __rhs.
  5571. */
  5572. template<typename _CharT, typename _Traits, typename _Alloc>
  5573. inline basic_string<_CharT, _Traits, _Alloc>
  5574. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5575. const _CharT* __rhs)
  5576. {
  5577. basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
  5578. __str.append(__rhs);
  5579. return __str;
  5580. }
  5581. /**
  5582. * @brief Concatenate string and character.
  5583. * @param __lhs First string.
  5584. * @param __rhs Last string.
  5585. * @return New string with @a __lhs followed by @a __rhs.
  5586. */
  5587. template<typename _CharT, typename _Traits, typename _Alloc>
  5588. inline basic_string<_CharT, _Traits, _Alloc>
  5589. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
  5590. {
  5591. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  5592. typedef typename __string_type::size_type __size_type;
  5593. __string_type __str(__lhs);
  5594. __str.append(__size_type(1), __rhs);
  5595. return __str;
  5596. }
  5597. #if __cplusplus >= 201103L
  5598. template<typename _CharT, typename _Traits, typename _Alloc>
  5599. inline basic_string<_CharT, _Traits, _Alloc>
  5600. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  5601. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5602. { return std::move(__lhs.append(__rhs)); }
  5603. template<typename _CharT, typename _Traits, typename _Alloc>
  5604. inline basic_string<_CharT, _Traits, _Alloc>
  5605. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5606. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  5607. { return std::move(__rhs.insert(0, __lhs)); }
  5608. template<typename _CharT, typename _Traits, typename _Alloc>
  5609. inline basic_string<_CharT, _Traits, _Alloc>
  5610. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  5611. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  5612. {
  5613. const auto __size = __lhs.size() + __rhs.size();
  5614. const bool __cond = (__size > __lhs.capacity()
  5615. && __size <= __rhs.capacity());
  5616. return __cond ? std::move(__rhs.insert(0, __lhs))
  5617. : std::move(__lhs.append(__rhs));
  5618. }
  5619. template<typename _CharT, typename _Traits, typename _Alloc>
  5620. inline basic_string<_CharT, _Traits, _Alloc>
  5621. operator+(const _CharT* __lhs,
  5622. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  5623. { return std::move(__rhs.insert(0, __lhs)); }
  5624. template<typename _CharT, typename _Traits, typename _Alloc>
  5625. inline basic_string<_CharT, _Traits, _Alloc>
  5626. operator+(_CharT __lhs,
  5627. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  5628. { return std::move(__rhs.insert(0, 1, __lhs)); }
  5629. template<typename _CharT, typename _Traits, typename _Alloc>
  5630. inline basic_string<_CharT, _Traits, _Alloc>
  5631. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  5632. const _CharT* __rhs)
  5633. { return std::move(__lhs.append(__rhs)); }
  5634. template<typename _CharT, typename _Traits, typename _Alloc>
  5635. inline basic_string<_CharT, _Traits, _Alloc>
  5636. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  5637. _CharT __rhs)
  5638. { return std::move(__lhs.append(1, __rhs)); }
  5639. #endif
  5640. // operator ==
  5641. /**
  5642. * @brief Test equivalence of two strings.
  5643. * @param __lhs First string.
  5644. * @param __rhs Second string.
  5645. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  5646. */
  5647. template<typename _CharT, typename _Traits, typename _Alloc>
  5648. inline bool
  5649. operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5650. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5651. _GLIBCXX_NOEXCEPT
  5652. { return __lhs.compare(__rhs) == 0; }
  5653. template<typename _CharT>
  5654. inline
  5655. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
  5656. operator==(const basic_string<_CharT>& __lhs,
  5657. const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
  5658. { return (__lhs.size() == __rhs.size()
  5659. && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
  5660. __lhs.size())); }
  5661. /**
  5662. * @brief Test equivalence of C string and string.
  5663. * @param __lhs C string.
  5664. * @param __rhs String.
  5665. * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
  5666. */
  5667. template<typename _CharT, typename _Traits, typename _Alloc>
  5668. inline bool
  5669. operator==(const _CharT* __lhs,
  5670. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5671. { return __rhs.compare(__lhs) == 0; }
  5672. /**
  5673. * @brief Test equivalence of string and C string.
  5674. * @param __lhs String.
  5675. * @param __rhs C string.
  5676. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  5677. */
  5678. template<typename _CharT, typename _Traits, typename _Alloc>
  5679. inline bool
  5680. operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5681. const _CharT* __rhs)
  5682. { return __lhs.compare(__rhs) == 0; }
  5683. // operator !=
  5684. /**
  5685. * @brief Test difference of two strings.
  5686. * @param __lhs First string.
  5687. * @param __rhs Second string.
  5688. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  5689. */
  5690. template<typename _CharT, typename _Traits, typename _Alloc>
  5691. inline bool
  5692. operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5693. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5694. _GLIBCXX_NOEXCEPT
  5695. { return !(__lhs == __rhs); }
  5696. /**
  5697. * @brief Test difference of C string and string.
  5698. * @param __lhs C string.
  5699. * @param __rhs String.
  5700. * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
  5701. */
  5702. template<typename _CharT, typename _Traits, typename _Alloc>
  5703. inline bool
  5704. operator!=(const _CharT* __lhs,
  5705. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5706. { return !(__lhs == __rhs); }
  5707. /**
  5708. * @brief Test difference of string and C string.
  5709. * @param __lhs String.
  5710. * @param __rhs C string.
  5711. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  5712. */
  5713. template<typename _CharT, typename _Traits, typename _Alloc>
  5714. inline bool
  5715. operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5716. const _CharT* __rhs)
  5717. { return !(__lhs == __rhs); }
  5718. // operator <
  5719. /**
  5720. * @brief Test if string precedes string.
  5721. * @param __lhs First string.
  5722. * @param __rhs Second string.
  5723. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  5724. */
  5725. template<typename _CharT, typename _Traits, typename _Alloc>
  5726. inline bool
  5727. operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5728. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5729. _GLIBCXX_NOEXCEPT
  5730. { return __lhs.compare(__rhs) < 0; }
  5731. /**
  5732. * @brief Test if string precedes C string.
  5733. * @param __lhs String.
  5734. * @param __rhs C string.
  5735. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  5736. */
  5737. template<typename _CharT, typename _Traits, typename _Alloc>
  5738. inline bool
  5739. operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5740. const _CharT* __rhs)
  5741. { return __lhs.compare(__rhs) < 0; }
  5742. /**
  5743. * @brief Test if C string precedes string.
  5744. * @param __lhs C string.
  5745. * @param __rhs String.
  5746. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  5747. */
  5748. template<typename _CharT, typename _Traits, typename _Alloc>
  5749. inline bool
  5750. operator<(const _CharT* __lhs,
  5751. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5752. { return __rhs.compare(__lhs) > 0; }
  5753. // operator >
  5754. /**
  5755. * @brief Test if string follows string.
  5756. * @param __lhs First string.
  5757. * @param __rhs Second string.
  5758. * @return True if @a __lhs follows @a __rhs. False otherwise.
  5759. */
  5760. template<typename _CharT, typename _Traits, typename _Alloc>
  5761. inline bool
  5762. operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5763. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5764. _GLIBCXX_NOEXCEPT
  5765. { return __lhs.compare(__rhs) > 0; }
  5766. /**
  5767. * @brief Test if string follows C string.
  5768. * @param __lhs String.
  5769. * @param __rhs C string.
  5770. * @return True if @a __lhs follows @a __rhs. False otherwise.
  5771. */
  5772. template<typename _CharT, typename _Traits, typename _Alloc>
  5773. inline bool
  5774. operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5775. const _CharT* __rhs)
  5776. { return __lhs.compare(__rhs) > 0; }
  5777. /**
  5778. * @brief Test if C string follows string.
  5779. * @param __lhs C string.
  5780. * @param __rhs String.
  5781. * @return True if @a __lhs follows @a __rhs. False otherwise.
  5782. */
  5783. template<typename _CharT, typename _Traits, typename _Alloc>
  5784. inline bool
  5785. operator>(const _CharT* __lhs,
  5786. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5787. { return __rhs.compare(__lhs) < 0; }
  5788. // operator <=
  5789. /**
  5790. * @brief Test if string doesn't follow string.
  5791. * @param __lhs First string.
  5792. * @param __rhs Second string.
  5793. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  5794. */
  5795. template<typename _CharT, typename _Traits, typename _Alloc>
  5796. inline bool
  5797. operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5798. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5799. _GLIBCXX_NOEXCEPT
  5800. { return __lhs.compare(__rhs) <= 0; }
  5801. /**
  5802. * @brief Test if string doesn't follow C string.
  5803. * @param __lhs String.
  5804. * @param __rhs C string.
  5805. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  5806. */
  5807. template<typename _CharT, typename _Traits, typename _Alloc>
  5808. inline bool
  5809. operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5810. const _CharT* __rhs)
  5811. { return __lhs.compare(__rhs) <= 0; }
  5812. /**
  5813. * @brief Test if C string doesn't follow string.
  5814. * @param __lhs C string.
  5815. * @param __rhs String.
  5816. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  5817. */
  5818. template<typename _CharT, typename _Traits, typename _Alloc>
  5819. inline bool
  5820. operator<=(const _CharT* __lhs,
  5821. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5822. { return __rhs.compare(__lhs) >= 0; }
  5823. // operator >=
  5824. /**
  5825. * @brief Test if string doesn't precede string.
  5826. * @param __lhs First string.
  5827. * @param __rhs Second string.
  5828. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  5829. */
  5830. template<typename _CharT, typename _Traits, typename _Alloc>
  5831. inline bool
  5832. operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5833. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5834. _GLIBCXX_NOEXCEPT
  5835. { return __lhs.compare(__rhs) >= 0; }
  5836. /**
  5837. * @brief Test if string doesn't precede C string.
  5838. * @param __lhs String.
  5839. * @param __rhs C string.
  5840. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  5841. */
  5842. template<typename _CharT, typename _Traits, typename _Alloc>
  5843. inline bool
  5844. operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5845. const _CharT* __rhs)
  5846. { return __lhs.compare(__rhs) >= 0; }
  5847. /**
  5848. * @brief Test if C string doesn't precede string.
  5849. * @param __lhs C string.
  5850. * @param __rhs String.
  5851. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  5852. */
  5853. template<typename _CharT, typename _Traits, typename _Alloc>
  5854. inline bool
  5855. operator>=(const _CharT* __lhs,
  5856. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5857. { return __rhs.compare(__lhs) <= 0; }
  5858. /**
  5859. * @brief Swap contents of two strings.
  5860. * @param __lhs First string.
  5861. * @param __rhs Second string.
  5862. *
  5863. * Exchanges the contents of @a __lhs and @a __rhs in constant time.
  5864. */
  5865. template<typename _CharT, typename _Traits, typename _Alloc>
  5866. inline void
  5867. swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5868. basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5869. _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
  5870. { __lhs.swap(__rhs); }
  5871. /**
  5872. * @brief Read stream into a string.
  5873. * @param __is Input stream.
  5874. * @param __str Buffer to store into.
  5875. * @return Reference to the input stream.
  5876. *
  5877. * Stores characters from @a __is into @a __str until whitespace is
  5878. * found, the end of the stream is encountered, or str.max_size()
  5879. * is reached. If is.width() is non-zero, that is the limit on the
  5880. * number of characters stored into @a __str. Any previous
  5881. * contents of @a __str are erased.
  5882. */
  5883. template<typename _CharT, typename _Traits, typename _Alloc>
  5884. basic_istream<_CharT, _Traits>&
  5885. operator>>(basic_istream<_CharT, _Traits>& __is,
  5886. basic_string<_CharT, _Traits, _Alloc>& __str);
  5887. template<>
  5888. basic_istream<char>&
  5889. operator>>(basic_istream<char>& __is, basic_string<char>& __str);
  5890. /**
  5891. * @brief Write string to a stream.
  5892. * @param __os Output stream.
  5893. * @param __str String to write out.
  5894. * @return Reference to the output stream.
  5895. *
  5896. * Output characters of @a __str into os following the same rules as for
  5897. * writing a C string.
  5898. */
  5899. template<typename _CharT, typename _Traits, typename _Alloc>
  5900. inline basic_ostream<_CharT, _Traits>&
  5901. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5902. const basic_string<_CharT, _Traits, _Alloc>& __str)
  5903. {
  5904. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  5905. // 586. string inserter not a formatted function
  5906. return __ostream_insert(__os, __str.data(), __str.size());
  5907. }
  5908. /**
  5909. * @brief Read a line from stream into a string.
  5910. * @param __is Input stream.
  5911. * @param __str Buffer to store into.
  5912. * @param __delim Character marking end of line.
  5913. * @return Reference to the input stream.
  5914. *
  5915. * Stores characters from @a __is into @a __str until @a __delim is
  5916. * found, the end of the stream is encountered, or str.max_size()
  5917. * is reached. Any previous contents of @a __str are erased. If
  5918. * @a __delim is encountered, it is extracted but not stored into
  5919. * @a __str.
  5920. */
  5921. template<typename _CharT, typename _Traits, typename _Alloc>
  5922. basic_istream<_CharT, _Traits>&
  5923. getline(basic_istream<_CharT, _Traits>& __is,
  5924. basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
  5925. /**
  5926. * @brief Read a line from stream into a string.
  5927. * @param __is Input stream.
  5928. * @param __str Buffer to store into.
  5929. * @return Reference to the input stream.
  5930. *
  5931. * Stores characters from is into @a __str until &apos;\n&apos; is
  5932. * found, the end of the stream is encountered, or str.max_size()
  5933. * is reached. Any previous contents of @a __str are erased. If
  5934. * end of line is encountered, it is extracted but not stored into
  5935. * @a __str.
  5936. */
  5937. template<typename _CharT, typename _Traits, typename _Alloc>
  5938. inline basic_istream<_CharT, _Traits>&
  5939. getline(basic_istream<_CharT, _Traits>& __is,
  5940. basic_string<_CharT, _Traits, _Alloc>& __str)
  5941. { return std::getline(__is, __str, __is.widen('\n')); }
  5942. #if __cplusplus >= 201103L
  5943. /// Read a line from an rvalue stream into a string.
  5944. template<typename _CharT, typename _Traits, typename _Alloc>
  5945. inline basic_istream<_CharT, _Traits>&
  5946. getline(basic_istream<_CharT, _Traits>&& __is,
  5947. basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
  5948. { return std::getline(__is, __str, __delim); }
  5949. /// Read a line from an rvalue stream into a string.
  5950. template<typename _CharT, typename _Traits, typename _Alloc>
  5951. inline basic_istream<_CharT, _Traits>&
  5952. getline(basic_istream<_CharT, _Traits>&& __is,
  5953. basic_string<_CharT, _Traits, _Alloc>& __str)
  5954. { return std::getline(__is, __str); }
  5955. #endif
  5956. template<>
  5957. basic_istream<char>&
  5958. getline(basic_istream<char>& __in, basic_string<char>& __str,
  5959. char __delim);
  5960. #ifdef _GLIBCXX_USE_WCHAR_T
  5961. template<>
  5962. basic_istream<wchar_t>&
  5963. getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
  5964. wchar_t __delim);
  5965. #endif
  5966. _GLIBCXX_END_NAMESPACE_VERSION
  5967. } // namespace
  5968. #if __cplusplus >= 201103L
  5969. #include <ext/string_conversions.h>
  5970. namespace std _GLIBCXX_VISIBILITY(default)
  5971. {
  5972. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  5973. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  5974. #if _GLIBCXX_USE_C99_STDLIB
  5975. // 21.4 Numeric Conversions [string.conversions].
  5976. inline int
  5977. stoi(const string& __str, size_t* __idx = 0, int __base = 10)
  5978. { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
  5979. __idx, __base); }
  5980. inline long
  5981. stol(const string& __str, size_t* __idx = 0, int __base = 10)
  5982. { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
  5983. __idx, __base); }
  5984. inline unsigned long
  5985. stoul(const string& __str, size_t* __idx = 0, int __base = 10)
  5986. { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
  5987. __idx, __base); }
  5988. inline long long
  5989. stoll(const string& __str, size_t* __idx = 0, int __base = 10)
  5990. { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
  5991. __idx, __base); }
  5992. inline unsigned long long
  5993. stoull(const string& __str, size_t* __idx = 0, int __base = 10)
  5994. { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
  5995. __idx, __base); }
  5996. // NB: strtof vs strtod.
  5997. inline float
  5998. stof(const string& __str, size_t* __idx = 0)
  5999. { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
  6000. inline double
  6001. stod(const string& __str, size_t* __idx = 0)
  6002. { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
  6003. inline long double
  6004. stold(const string& __str, size_t* __idx = 0)
  6005. { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
  6006. #endif // _GLIBCXX_USE_C99_STDLIB
  6007. #if _GLIBCXX_USE_C99_STDIO
  6008. // NB: (v)snprintf vs sprintf.
  6009. // DR 1261.
  6010. inline string
  6011. to_string(int __val)
  6012. { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
  6013. "%d", __val); }
  6014. inline string
  6015. to_string(unsigned __val)
  6016. { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
  6017. 4 * sizeof(unsigned),
  6018. "%u", __val); }
  6019. inline string
  6020. to_string(long __val)
  6021. { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
  6022. "%ld", __val); }
  6023. inline string
  6024. to_string(unsigned long __val)
  6025. { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
  6026. 4 * sizeof(unsigned long),
  6027. "%lu", __val); }
  6028. inline string
  6029. to_string(long long __val)
  6030. { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
  6031. 4 * sizeof(long long),
  6032. "%lld", __val); }
  6033. inline string
  6034. to_string(unsigned long long __val)
  6035. { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
  6036. 4 * sizeof(unsigned long long),
  6037. "%llu", __val); }
  6038. inline string
  6039. to_string(float __val)
  6040. {
  6041. const int __n =
  6042. __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
  6043. return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
  6044. "%f", __val);
  6045. }
  6046. inline string
  6047. to_string(double __val)
  6048. {
  6049. const int __n =
  6050. __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
  6051. return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
  6052. "%f", __val);
  6053. }
  6054. inline string
  6055. to_string(long double __val)
  6056. {
  6057. const int __n =
  6058. __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
  6059. return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
  6060. "%Lf", __val);
  6061. }
  6062. #endif // _GLIBCXX_USE_C99_STDIO
  6063. #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
  6064. inline int
  6065. stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6066. { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
  6067. __idx, __base); }
  6068. inline long
  6069. stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6070. { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
  6071. __idx, __base); }
  6072. inline unsigned long
  6073. stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6074. { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
  6075. __idx, __base); }
  6076. inline long long
  6077. stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6078. { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
  6079. __idx, __base); }
  6080. inline unsigned long long
  6081. stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6082. { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
  6083. __idx, __base); }
  6084. // NB: wcstof vs wcstod.
  6085. inline float
  6086. stof(const wstring& __str, size_t* __idx = 0)
  6087. { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
  6088. inline double
  6089. stod(const wstring& __str, size_t* __idx = 0)
  6090. { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
  6091. inline long double
  6092. stold(const wstring& __str, size_t* __idx = 0)
  6093. { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
  6094. #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  6095. // DR 1261.
  6096. inline wstring
  6097. to_wstring(int __val)
  6098. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
  6099. L"%d", __val); }
  6100. inline wstring
  6101. to_wstring(unsigned __val)
  6102. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  6103. 4 * sizeof(unsigned),
  6104. L"%u", __val); }
  6105. inline wstring
  6106. to_wstring(long __val)
  6107. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
  6108. L"%ld", __val); }
  6109. inline wstring
  6110. to_wstring(unsigned long __val)
  6111. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  6112. 4 * sizeof(unsigned long),
  6113. L"%lu", __val); }
  6114. inline wstring
  6115. to_wstring(long long __val)
  6116. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  6117. 4 * sizeof(long long),
  6118. L"%lld", __val); }
  6119. inline wstring
  6120. to_wstring(unsigned long long __val)
  6121. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  6122. 4 * sizeof(unsigned long long),
  6123. L"%llu", __val); }
  6124. inline wstring
  6125. to_wstring(float __val)
  6126. {
  6127. const int __n =
  6128. __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
  6129. return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
  6130. L"%f", __val);
  6131. }
  6132. inline wstring
  6133. to_wstring(double __val)
  6134. {
  6135. const int __n =
  6136. __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
  6137. return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
  6138. L"%f", __val);
  6139. }
  6140. inline wstring
  6141. to_wstring(long double __val)
  6142. {
  6143. const int __n =
  6144. __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
  6145. return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
  6146. L"%Lf", __val);
  6147. }
  6148. #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  6149. #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
  6150. _GLIBCXX_END_NAMESPACE_CXX11
  6151. _GLIBCXX_END_NAMESPACE_VERSION
  6152. } // namespace
  6153. #endif /* C++11 */
  6154. #if __cplusplus >= 201103L
  6155. #include <bits/functional_hash.h>
  6156. namespace std _GLIBCXX_VISIBILITY(default)
  6157. {
  6158. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  6159. // DR 1182.
  6160. #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
  6161. /// std::hash specialization for string.
  6162. template<>
  6163. struct hash<string>
  6164. : public __hash_base<size_t, string>
  6165. {
  6166. size_t
  6167. operator()(const string& __s) const noexcept
  6168. { return std::_Hash_impl::hash(__s.data(), __s.length()); }
  6169. };
  6170. template<>
  6171. struct __is_fast_hash<hash<string>> : std::false_type
  6172. { };
  6173. #ifdef _GLIBCXX_USE_WCHAR_T
  6174. /// std::hash specialization for wstring.
  6175. template<>
  6176. struct hash<wstring>
  6177. : public __hash_base<size_t, wstring>
  6178. {
  6179. size_t
  6180. operator()(const wstring& __s) const noexcept
  6181. { return std::_Hash_impl::hash(__s.data(),
  6182. __s.length() * sizeof(wchar_t)); }
  6183. };
  6184. template<>
  6185. struct __is_fast_hash<hash<wstring>> : std::false_type
  6186. { };
  6187. #endif
  6188. #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
  6189. #ifdef _GLIBCXX_USE_CHAR8_T
  6190. /// std::hash specialization for u8string.
  6191. template<>
  6192. struct hash<u8string>
  6193. : public __hash_base<size_t, u8string>
  6194. {
  6195. size_t
  6196. operator()(const u8string& __s) const noexcept
  6197. { return std::_Hash_impl::hash(__s.data(),
  6198. __s.length() * sizeof(char8_t)); }
  6199. };
  6200. template<>
  6201. struct __is_fast_hash<hash<u8string>> : std::false_type
  6202. { };
  6203. #endif
  6204. /// std::hash specialization for u16string.
  6205. template<>
  6206. struct hash<u16string>
  6207. : public __hash_base<size_t, u16string>
  6208. {
  6209. size_t
  6210. operator()(const u16string& __s) const noexcept
  6211. { return std::_Hash_impl::hash(__s.data(),
  6212. __s.length() * sizeof(char16_t)); }
  6213. };
  6214. template<>
  6215. struct __is_fast_hash<hash<u16string>> : std::false_type
  6216. { };
  6217. /// std::hash specialization for u32string.
  6218. template<>
  6219. struct hash<u32string>
  6220. : public __hash_base<size_t, u32string>
  6221. {
  6222. size_t
  6223. operator()(const u32string& __s) const noexcept
  6224. { return std::_Hash_impl::hash(__s.data(),
  6225. __s.length() * sizeof(char32_t)); }
  6226. };
  6227. template<>
  6228. struct __is_fast_hash<hash<u32string>> : std::false_type
  6229. { };
  6230. #if __cplusplus >= 201402L
  6231. #define __cpp_lib_string_udls 201304
  6232. inline namespace literals
  6233. {
  6234. inline namespace string_literals
  6235. {
  6236. #pragma GCC diagnostic push
  6237. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  6238. _GLIBCXX_DEFAULT_ABI_TAG
  6239. inline basic_string<char>
  6240. operator""s(const char* __str, size_t __len)
  6241. { return basic_string<char>{__str, __len}; }
  6242. #ifdef _GLIBCXX_USE_WCHAR_T
  6243. _GLIBCXX_DEFAULT_ABI_TAG
  6244. inline basic_string<wchar_t>
  6245. operator""s(const wchar_t* __str, size_t __len)
  6246. { return basic_string<wchar_t>{__str, __len}; }
  6247. #endif
  6248. #ifdef _GLIBCXX_USE_CHAR8_T
  6249. _GLIBCXX_DEFAULT_ABI_TAG
  6250. inline basic_string<char8_t>
  6251. operator""s(const char8_t* __str, size_t __len)
  6252. { return basic_string<char8_t>{__str, __len}; }
  6253. #endif
  6254. _GLIBCXX_DEFAULT_ABI_TAG
  6255. inline basic_string<char16_t>
  6256. operator""s(const char16_t* __str, size_t __len)
  6257. { return basic_string<char16_t>{__str, __len}; }
  6258. _GLIBCXX_DEFAULT_ABI_TAG
  6259. inline basic_string<char32_t>
  6260. operator""s(const char32_t* __str, size_t __len)
  6261. { return basic_string<char32_t>{__str, __len}; }
  6262. #pragma GCC diagnostic pop
  6263. } // inline namespace string_literals
  6264. } // inline namespace literals
  6265. #if __cplusplus >= 201703L
  6266. namespace __detail::__variant
  6267. {
  6268. template<typename> struct _Never_valueless_alt; // see <variant>
  6269. // Provide the strong exception-safety guarantee when emplacing a
  6270. // basic_string into a variant, but only if moving the string cannot throw.
  6271. template<typename _Tp, typename _Traits, typename _Alloc>
  6272. struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
  6273. : __and_<
  6274. is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
  6275. is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
  6276. >::type
  6277. { };
  6278. } // namespace __detail::__variant
  6279. #endif // C++17
  6280. #endif // C++14
  6281. _GLIBCXX_END_NAMESPACE_VERSION
  6282. } // namespace std
  6283. #endif // C++11
  6284. #endif /* _BASIC_STRING_H */