basic_string.h 235 KB

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