basic_string.h 243 KB

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