basic_string.h 246 KB

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