basic_string.h 237 KB

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