ustring.cpp 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066
  1. /**************************************************************************/
  2. /* ustring.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "ustring.h"
  31. #include "core/crypto/crypto_core.h"
  32. #include "core/math/color.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/object/object.h"
  35. #include "core/os/memory.h"
  36. #include "core/os/os.h"
  37. #include "core/string/print_string.h"
  38. #include "core/string/string_name.h"
  39. #include "core/string/translation_server.h"
  40. #include "core/string/ucaps.h"
  41. #include "core/variant/variant.h"
  42. #include "core/version_generated.gen.h"
  43. #include "thirdparty/grisu2/grisu2.h"
  44. #ifdef _MSC_VER
  45. #define _CRT_SECURE_NO_WARNINGS // to disable build-time warning which suggested to use strcpy_s instead strcpy
  46. #endif
  47. #if defined(MINGW_ENABLED) || defined(_MSC_VER)
  48. #define snprintf _snprintf_s
  49. #endif
  50. static const int MAX_DECIMALS = 32;
  51. static _FORCE_INLINE_ char32_t lower_case(char32_t c) {
  52. return (is_ascii_upper_case(c) ? (c + ('a' - 'A')) : c);
  53. }
  54. Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path, String &r_fragment) const {
  55. // Splits the URL into scheme, host, port, path, fragment. Strip credentials when present.
  56. String base = *this;
  57. r_scheme = "";
  58. r_host = "";
  59. r_port = 0;
  60. r_path = "";
  61. r_fragment = "";
  62. int pos = base.find("://");
  63. // Scheme
  64. if (pos != -1) {
  65. bool is_scheme_valid = true;
  66. for (int i = 0; i < pos; i++) {
  67. if (!is_ascii_alphanumeric_char(base[i]) && base[i] != '+' && base[i] != '-' && base[i] != '.') {
  68. is_scheme_valid = false;
  69. break;
  70. }
  71. }
  72. if (is_scheme_valid) {
  73. r_scheme = base.substr(0, pos + 3).to_lower();
  74. base = base.substr(pos + 3);
  75. }
  76. }
  77. pos = base.find_char('#');
  78. // Fragment
  79. if (pos != -1) {
  80. r_fragment = base.substr(pos + 1);
  81. base = base.substr(0, pos);
  82. }
  83. pos = base.find_char('/');
  84. // Path
  85. if (pos != -1) {
  86. r_path = base.substr(pos);
  87. base = base.substr(0, pos);
  88. }
  89. // Host
  90. pos = base.find_char('@');
  91. if (pos != -1) {
  92. // Strip credentials
  93. base = base.substr(pos + 1);
  94. }
  95. if (base.begins_with("[")) {
  96. // Literal IPv6
  97. pos = base.rfind_char(']');
  98. if (pos == -1) {
  99. return ERR_INVALID_PARAMETER;
  100. }
  101. r_host = base.substr(1, pos - 1);
  102. base = base.substr(pos + 1);
  103. } else {
  104. // Anything else
  105. if (base.get_slice_count(":") > 2) {
  106. return ERR_INVALID_PARAMETER;
  107. }
  108. pos = base.rfind_char(':');
  109. if (pos == -1) {
  110. r_host = base;
  111. base = "";
  112. } else {
  113. r_host = base.substr(0, pos);
  114. base = base.substr(pos);
  115. }
  116. }
  117. if (r_host.is_empty()) {
  118. return ERR_INVALID_PARAMETER;
  119. }
  120. r_host = r_host.to_lower();
  121. // Port
  122. if (base.begins_with(":")) {
  123. base = base.substr(1);
  124. if (!base.is_valid_int()) {
  125. return ERR_INVALID_PARAMETER;
  126. }
  127. r_port = base.to_int();
  128. if (r_port < 1 || r_port > 65535) {
  129. return ERR_INVALID_PARAMETER;
  130. }
  131. }
  132. return OK;
  133. }
  134. void String::append_latin1(const Span<char> &p_cstr) {
  135. if (p_cstr.is_empty()) {
  136. return;
  137. }
  138. const int prev_length = length();
  139. resize_uninitialized(prev_length + p_cstr.size() + 1); // include 0
  140. const char *src = p_cstr.ptr();
  141. const char *end = src + p_cstr.size();
  142. char32_t *dst = ptrw() + prev_length;
  143. for (; src < end; ++src, ++dst) {
  144. // If char is int8_t, a set sign bit will be reinterpreted as 256 - val implicitly.
  145. if (unlikely(*src == '\0')) {
  146. // NUL in string is allowed by the unicode standard, but unsupported in our implementation right now.
  147. print_unicode_error("Unexpected NUL character", true);
  148. *dst = _replacement_char;
  149. } else {
  150. *dst = static_cast<uint8_t>(*src);
  151. }
  152. }
  153. *dst = 0;
  154. }
  155. Error String::append_utf32(const Span<char32_t> &p_cstr) {
  156. if (p_cstr.is_empty()) {
  157. return OK;
  158. }
  159. Error error = OK;
  160. const int prev_length = length();
  161. resize_uninitialized(prev_length + p_cstr.size() + 1);
  162. const char32_t *src = p_cstr.ptr();
  163. const char32_t *end = p_cstr.ptr() + p_cstr.size();
  164. char32_t *dst = ptrw() + prev_length;
  165. // Copy the string, and check for UTF-32 problems.
  166. for (; src < end; ++src, ++dst) {
  167. const char32_t chr = *src;
  168. if (unlikely(chr == U'\0')) {
  169. // NUL in string is allowed by the unicode standard, but unsupported in our implementation right now.
  170. print_unicode_error("Unexpected NUL character", true);
  171. *dst = _replacement_char;
  172. error = ERR_PARSE_ERROR;
  173. } else if (unlikely((chr & 0xfffff800) == 0xd800)) {
  174. print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)chr), true);
  175. *dst = _replacement_char;
  176. error = ERR_PARSE_ERROR;
  177. } else if (unlikely(chr > 0x10ffff)) {
  178. print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)chr), true);
  179. *dst = _replacement_char;
  180. error = ERR_PARSE_ERROR;
  181. } else {
  182. *dst = chr;
  183. }
  184. }
  185. *dst = 0;
  186. return error;
  187. }
  188. void String::append_utf32_unchecked(const Span<char32_t> &p_span) {
  189. const int prev_length = length();
  190. resize_uninitialized(prev_length + p_span.size() + 1); // + 1 for \0
  191. char32_t *dst = ptrw() + prev_length;
  192. memcpy(dst, p_span.ptr(), p_span.size() * sizeof(char32_t));
  193. *(dst + p_span.size()) = _null;
  194. }
  195. String String::operator+(const String &p_str) const {
  196. String res = *this;
  197. res += p_str;
  198. return res;
  199. }
  200. String String::operator+(const char *p_str) const {
  201. String res = *this;
  202. res += p_str;
  203. return res;
  204. }
  205. String String::operator+(const wchar_t *p_str) const {
  206. String res = *this;
  207. res += p_str;
  208. return res;
  209. }
  210. String String::operator+(const char32_t *p_str) const {
  211. String res = *this;
  212. res += p_str;
  213. return res;
  214. }
  215. String String::operator+(char32_t p_char) const {
  216. String res = *this;
  217. res += p_char;
  218. return res;
  219. }
  220. String operator+(const char *p_chr, const String &p_str) {
  221. String tmp = p_chr;
  222. tmp += p_str;
  223. return tmp;
  224. }
  225. String operator+(const wchar_t *p_chr, const String &p_str) {
  226. #ifdef WINDOWS_ENABLED
  227. // wchar_t is 16-bit
  228. String tmp = String::utf16((const char16_t *)p_chr);
  229. #else
  230. // wchar_t is 32-bit
  231. String tmp = (const char32_t *)p_chr;
  232. #endif
  233. tmp += p_str;
  234. return tmp;
  235. }
  236. String operator+(char32_t p_chr, const String &p_str) {
  237. return (String::chr(p_chr) + p_str);
  238. }
  239. String &String::operator+=(const String &p_str) {
  240. if (is_empty()) {
  241. *this = p_str;
  242. return *this;
  243. }
  244. append_utf32_unchecked(p_str);
  245. return *this;
  246. }
  247. String &String::operator+=(const char *p_str) {
  248. append_latin1(p_str);
  249. return *this;
  250. }
  251. String &String::operator+=(const wchar_t *p_str) {
  252. #ifdef WINDOWS_ENABLED
  253. // wchar_t is 16-bit
  254. *this += String::utf16((const char16_t *)p_str);
  255. #else
  256. // wchar_t is 32-bit
  257. *this += String((const char32_t *)p_str);
  258. #endif
  259. return *this;
  260. }
  261. String &String::operator+=(const char32_t *p_str) {
  262. append_utf32(Span(p_str, strlen(p_str)));
  263. return *this;
  264. }
  265. String &String::operator+=(char32_t p_char) {
  266. append_utf32(Span(&p_char, 1));
  267. return *this;
  268. }
  269. bool String::operator==(const char *p_str) const {
  270. // compare Latin-1 encoded c-string
  271. int len = strlen(p_str);
  272. if (length() != len) {
  273. return false;
  274. }
  275. if (is_empty()) {
  276. return true;
  277. }
  278. int l = length();
  279. const char32_t *dst = get_data();
  280. // Compare char by char
  281. for (int i = 0; i < l; i++) {
  282. if ((char32_t)p_str[i] != dst[i]) {
  283. return false;
  284. }
  285. }
  286. return true;
  287. }
  288. bool String::operator==(const wchar_t *p_str) const {
  289. #ifdef WINDOWS_ENABLED
  290. // wchar_t is 16-bit, parse as UTF-16
  291. return *this == String::utf16((const char16_t *)p_str);
  292. #else
  293. // wchar_t is 32-bit, compare char by char
  294. return *this == (const char32_t *)p_str;
  295. #endif
  296. }
  297. bool String::operator==(const char32_t *p_str) const {
  298. const int len = strlen(p_str);
  299. if (length() != len) {
  300. return false;
  301. }
  302. if (is_empty()) {
  303. return true;
  304. }
  305. return memcmp(ptr(), p_str, len * sizeof(char32_t)) == 0;
  306. }
  307. bool String::operator==(const String &p_str) const {
  308. if (length() != p_str.length()) {
  309. return false;
  310. }
  311. if (is_empty()) {
  312. return true;
  313. }
  314. return memcmp(ptr(), p_str.ptr(), length() * sizeof(char32_t)) == 0;
  315. }
  316. bool String::operator==(const Span<char32_t> &p_str_range) const {
  317. const int len = p_str_range.size();
  318. if (length() != len) {
  319. return false;
  320. }
  321. if (is_empty()) {
  322. return true;
  323. }
  324. return memcmp(ptr(), p_str_range.ptr(), len * sizeof(char32_t)) == 0;
  325. }
  326. bool operator==(const char *p_chr, const String &p_str) {
  327. return p_str == p_chr;
  328. }
  329. bool operator==(const wchar_t *p_chr, const String &p_str) {
  330. #ifdef WINDOWS_ENABLED
  331. // wchar_t is 16-bit
  332. return p_str == String::utf16((const char16_t *)p_chr);
  333. #else
  334. // wchar_t is 32-bi
  335. return p_str == String((const char32_t *)p_chr);
  336. #endif
  337. }
  338. bool operator!=(const char *p_chr, const String &p_str) {
  339. return !(p_str == p_chr);
  340. }
  341. bool operator!=(const wchar_t *p_chr, const String &p_str) {
  342. #ifdef WINDOWS_ENABLED
  343. // wchar_t is 16-bit
  344. return !(p_str == String::utf16((const char16_t *)p_chr));
  345. #else
  346. // wchar_t is 32-bi
  347. return !(p_str == String((const char32_t *)p_chr));
  348. #endif
  349. }
  350. bool String::operator!=(const char *p_str) const {
  351. return (!(*this == p_str));
  352. }
  353. bool String::operator!=(const wchar_t *p_str) const {
  354. return (!(*this == p_str));
  355. }
  356. bool String::operator!=(const char32_t *p_str) const {
  357. return (!(*this == p_str));
  358. }
  359. bool String::operator!=(const String &p_str) const {
  360. return !((*this == p_str));
  361. }
  362. bool String::operator<=(const String &p_str) const {
  363. return !(p_str < *this);
  364. }
  365. bool String::operator>(const String &p_str) const {
  366. return p_str < *this;
  367. }
  368. bool String::operator>=(const String &p_str) const {
  369. return !(*this < p_str);
  370. }
  371. bool String::operator<(const char *p_str) const {
  372. if (is_empty() && p_str[0] == 0) {
  373. return false;
  374. }
  375. if (is_empty()) {
  376. return true;
  377. }
  378. return str_compare(get_data(), p_str) < 0;
  379. }
  380. bool String::operator<(const wchar_t *p_str) const {
  381. if (is_empty() && p_str[0] == 0) {
  382. return false;
  383. }
  384. if (is_empty()) {
  385. return true;
  386. }
  387. #ifdef WINDOWS_ENABLED
  388. // wchar_t is 16-bit
  389. return str_compare(get_data(), String::utf16((const char16_t *)p_str).get_data()) < 0;
  390. #else
  391. // wchar_t is 32-bit
  392. return str_compare(get_data(), (const char32_t *)p_str) < 0;
  393. #endif
  394. }
  395. bool String::operator<(const char32_t *p_str) const {
  396. if (is_empty() && p_str[0] == 0) {
  397. return false;
  398. }
  399. if (is_empty()) {
  400. return true;
  401. }
  402. return str_compare(get_data(), p_str) < 0;
  403. }
  404. bool String::operator<(const String &p_str) const {
  405. return operator<(p_str.get_data());
  406. }
  407. signed char String::nocasecmp_to(const String &p_str) const {
  408. if (is_empty() && p_str.is_empty()) {
  409. return 0;
  410. }
  411. if (is_empty()) {
  412. return -1;
  413. }
  414. if (p_str.is_empty()) {
  415. return 1;
  416. }
  417. const char32_t *that_str = p_str.get_data();
  418. const char32_t *this_str = get_data();
  419. while (true) {
  420. if (*that_str == 0 && *this_str == 0) { // If both strings are at the end, they are equal.
  421. return 0;
  422. } else if (*this_str == 0) { // If at the end of this, and not of other, we are less.
  423. return -1;
  424. } else if (*that_str == 0) { // If at end of other, and not of this, we are greater.
  425. return 1;
  426. } else if (_find_upper(*this_str) < _find_upper(*that_str)) { // If current character in this is less, we are less.
  427. return -1;
  428. } else if (_find_upper(*this_str) > _find_upper(*that_str)) { // If current character in this is greater, we are greater.
  429. return 1;
  430. }
  431. this_str++;
  432. that_str++;
  433. }
  434. }
  435. signed char String::casecmp_to(const String &p_str) const {
  436. if (is_empty() && p_str.is_empty()) {
  437. return 0;
  438. }
  439. if (is_empty()) {
  440. return -1;
  441. }
  442. if (p_str.is_empty()) {
  443. return 1;
  444. }
  445. const char32_t *that_str = p_str.get_data();
  446. const char32_t *this_str = get_data();
  447. while (true) {
  448. if (*that_str == 0 && *this_str == 0) { // If both strings are at the end, they are equal.
  449. return 0;
  450. } else if (*this_str == 0) { // If at the end of this, and not of other, we are less.
  451. return -1;
  452. } else if (*that_str == 0) { // If at end of other, and not of this, we are greater.
  453. return 1;
  454. } else if (*this_str < *that_str) { // If current character in this is less, we are less.
  455. return -1;
  456. } else if (*this_str > *that_str) { // If current character in this is greater, we are greater.
  457. return 1;
  458. }
  459. this_str++;
  460. that_str++;
  461. }
  462. }
  463. static _FORCE_INLINE_ signed char natural_cmp_common(const char32_t *&r_this_str, const char32_t *&r_that_str) {
  464. // Keep ptrs to start of numerical sequences.
  465. const char32_t *this_substr = r_this_str;
  466. const char32_t *that_substr = r_that_str;
  467. // Compare lengths of both numerical sequences, ignoring leading zeros.
  468. while (is_digit(*r_this_str)) {
  469. r_this_str++;
  470. }
  471. while (is_digit(*r_that_str)) {
  472. r_that_str++;
  473. }
  474. while (*this_substr == '0') {
  475. this_substr++;
  476. }
  477. while (*that_substr == '0') {
  478. that_substr++;
  479. }
  480. int this_len = r_this_str - this_substr;
  481. int that_len = r_that_str - that_substr;
  482. if (this_len < that_len) {
  483. return -1;
  484. } else if (this_len > that_len) {
  485. return 1;
  486. }
  487. // If lengths equal, compare lexicographically.
  488. while (this_substr != r_this_str && that_substr != r_that_str) {
  489. if (*this_substr < *that_substr) {
  490. return -1;
  491. } else if (*this_substr > *that_substr) {
  492. return 1;
  493. }
  494. this_substr++;
  495. that_substr++;
  496. }
  497. return 0;
  498. }
  499. static _FORCE_INLINE_ signed char naturalcasecmp_to_base(const char32_t *p_this_str, const char32_t *p_that_str) {
  500. if (p_this_str && p_that_str) {
  501. while (*p_this_str == '.' || *p_that_str == '.') {
  502. if (*p_this_str++ != '.') {
  503. return 1;
  504. }
  505. if (*p_that_str++ != '.') {
  506. return -1;
  507. }
  508. if (!*p_that_str) {
  509. return 1;
  510. }
  511. if (!*p_this_str) {
  512. return -1;
  513. }
  514. }
  515. while (*p_this_str) {
  516. if (!*p_that_str) {
  517. return 1;
  518. } else if (is_digit(*p_this_str)) {
  519. if (!is_digit(*p_that_str)) {
  520. return -1;
  521. }
  522. signed char ret = natural_cmp_common(p_this_str, p_that_str);
  523. if (ret) {
  524. return ret;
  525. }
  526. } else if (is_digit(*p_that_str)) {
  527. return 1;
  528. } else {
  529. if (*p_this_str < *p_that_str) { // If current character in this is less, we are less.
  530. return -1;
  531. } else if (*p_this_str > *p_that_str) { // If current character in this is greater, we are greater.
  532. return 1;
  533. }
  534. p_this_str++;
  535. p_that_str++;
  536. }
  537. }
  538. if (*p_that_str) {
  539. return -1;
  540. }
  541. }
  542. return 0;
  543. }
  544. signed char String::naturalcasecmp_to(const String &p_str) const {
  545. const char32_t *this_str = get_data();
  546. const char32_t *that_str = p_str.get_data();
  547. return naturalcasecmp_to_base(this_str, that_str);
  548. }
  549. static _FORCE_INLINE_ signed char naturalnocasecmp_to_base(const char32_t *p_this_str, const char32_t *p_that_str) {
  550. if (p_this_str && p_that_str) {
  551. while (*p_this_str == '.' || *p_that_str == '.') {
  552. if (*p_this_str++ != '.') {
  553. return 1;
  554. }
  555. if (*p_that_str++ != '.') {
  556. return -1;
  557. }
  558. if (!*p_that_str) {
  559. return 1;
  560. }
  561. if (!*p_this_str) {
  562. return -1;
  563. }
  564. }
  565. while (*p_this_str) {
  566. if (!*p_that_str) {
  567. return 1;
  568. } else if (is_digit(*p_this_str)) {
  569. if (!is_digit(*p_that_str)) {
  570. return -1;
  571. }
  572. signed char ret = natural_cmp_common(p_this_str, p_that_str);
  573. if (ret) {
  574. return ret;
  575. }
  576. } else if (is_digit(*p_that_str)) {
  577. return 1;
  578. } else {
  579. if (_find_upper(*p_this_str) < _find_upper(*p_that_str)) { // If current character in this is less, we are less.
  580. return -1;
  581. } else if (_find_upper(*p_this_str) > _find_upper(*p_that_str)) { // If current character in this is greater, we are greater.
  582. return 1;
  583. }
  584. p_this_str++;
  585. p_that_str++;
  586. }
  587. }
  588. if (*p_that_str) {
  589. return -1;
  590. }
  591. }
  592. return 0;
  593. }
  594. signed char String::naturalnocasecmp_to(const String &p_str) const {
  595. const char32_t *this_str = get_data();
  596. const char32_t *that_str = p_str.get_data();
  597. return naturalnocasecmp_to_base(this_str, that_str);
  598. }
  599. static _FORCE_INLINE_ signed char file_cmp_common(const char32_t *&r_this_str, const char32_t *&r_that_str) {
  600. // Compare leading `_` sequences.
  601. while ((*r_this_str == '_' && *r_that_str) || (*r_this_str && *r_that_str == '_')) {
  602. // Sort `_` lower than everything except `.`
  603. if (*r_this_str != '_') {
  604. return *r_this_str == '.' ? -1 : 1;
  605. } else if (*r_that_str != '_') {
  606. return *r_that_str == '.' ? 1 : -1;
  607. }
  608. r_this_str++;
  609. r_that_str++;
  610. }
  611. return 0;
  612. }
  613. signed char String::filecasecmp_to(const String &p_str) const {
  614. const char32_t *this_str = get_data();
  615. const char32_t *that_str = p_str.get_data();
  616. signed char ret = file_cmp_common(this_str, that_str);
  617. if (ret) {
  618. return ret;
  619. }
  620. return naturalcasecmp_to_base(this_str, that_str);
  621. }
  622. signed char String::filenocasecmp_to(const String &p_str) const {
  623. const char32_t *this_str = get_data();
  624. const char32_t *that_str = p_str.get_data();
  625. signed char ret = file_cmp_common(this_str, that_str);
  626. if (ret) {
  627. return ret;
  628. }
  629. return naturalnocasecmp_to_base(this_str, that_str);
  630. }
  631. String String::_separate_compound_words() const {
  632. if (length() == 0) {
  633. return *this;
  634. }
  635. const char32_t *cstr = get_data();
  636. int start_index = 0;
  637. String new_string;
  638. bool is_prev_upper = is_unicode_upper_case(cstr[0]);
  639. bool is_prev_lower = is_unicode_lower_case(cstr[0]);
  640. bool is_prev_digit = is_digit(cstr[0]);
  641. for (int i = 1; i < length(); i++) {
  642. const bool is_curr_upper = is_unicode_upper_case(cstr[i]);
  643. const bool is_curr_lower = is_unicode_lower_case(cstr[i]);
  644. const bool is_curr_digit = is_digit(cstr[i]);
  645. bool is_next_lower = false;
  646. if (i + 1 < length()) {
  647. is_next_lower = is_unicode_lower_case(cstr[i + 1]);
  648. }
  649. const bool cond_a = is_prev_lower && is_curr_upper; // aA
  650. const bool cond_b = (is_prev_upper || is_prev_digit) && is_curr_upper && is_next_lower; // AAa, 2Aa
  651. const bool cond_c = is_prev_digit && is_curr_lower && is_next_lower; // 2aa
  652. const bool cond_d = (is_prev_upper || is_prev_lower) && is_curr_digit; // A2, a2
  653. if (cond_a || cond_b || cond_c || cond_d) {
  654. new_string += substr(start_index, i - start_index) + " ";
  655. start_index = i;
  656. }
  657. is_prev_upper = is_curr_upper;
  658. is_prev_lower = is_curr_lower;
  659. is_prev_digit = is_curr_digit;
  660. }
  661. new_string += substr(start_index, size() - start_index);
  662. for (int i = 0; i < new_string.size(); i++) {
  663. const bool whitespace = is_whitespace(new_string[i]);
  664. const bool underscore = is_underscore(new_string[i]);
  665. const bool hyphen = is_hyphen(new_string[i]);
  666. if (whitespace || underscore || hyphen) {
  667. new_string[i] = ' ';
  668. }
  669. }
  670. return new_string.to_lower();
  671. }
  672. String String::capitalize() const {
  673. String words = _separate_compound_words().strip_edges();
  674. String ret;
  675. for (int i = 0; i < words.get_slice_count(" "); i++) {
  676. String slice = words.get_slicec(' ', i);
  677. if (slice.length() > 0) {
  678. slice[0] = _find_upper(slice[0]);
  679. if (i > 0) {
  680. ret += " ";
  681. }
  682. ret += slice;
  683. }
  684. }
  685. return ret;
  686. }
  687. String String::to_camel_case() const {
  688. String words = _separate_compound_words().strip_edges();
  689. String ret;
  690. for (int i = 0; i < words.get_slice_count(" "); i++) {
  691. String slice = words.get_slicec(' ', i);
  692. if (slice.length() > 0) {
  693. if (i == 0) {
  694. slice[0] = _find_lower(slice[0]);
  695. } else {
  696. slice[0] = _find_upper(slice[0]);
  697. }
  698. ret += slice;
  699. }
  700. }
  701. return ret;
  702. }
  703. String String::to_pascal_case() const {
  704. String words = _separate_compound_words().strip_edges();
  705. String ret;
  706. for (int i = 0; i < words.get_slice_count(" "); i++) {
  707. String slice = words.get_slicec(' ', i);
  708. if (slice.length() > 0) {
  709. slice[0] = _find_upper(slice[0]);
  710. ret += slice;
  711. }
  712. }
  713. return ret;
  714. }
  715. String String::to_snake_case() const {
  716. return _separate_compound_words().replace_char(' ', '_');
  717. }
  718. String String::to_kebab_case() const {
  719. return _separate_compound_words().replace_char(' ', '-');
  720. }
  721. String String::get_with_code_lines() const {
  722. const Vector<String> lines = split("\n");
  723. String ret;
  724. for (int i = 0; i < lines.size(); i++) {
  725. if (i > 0) {
  726. ret += "\n";
  727. }
  728. ret += vformat("%4d | %s", i + 1, lines[i]);
  729. }
  730. return ret;
  731. }
  732. int String::get_slice_count(const String &p_splitter) const {
  733. if (is_empty()) {
  734. return 0;
  735. }
  736. if (p_splitter.is_empty()) {
  737. return 0;
  738. }
  739. int pos = 0;
  740. int slices = 1;
  741. while ((pos = find(p_splitter, pos)) >= 0) {
  742. slices++;
  743. pos += p_splitter.length();
  744. }
  745. return slices;
  746. }
  747. int String::get_slice_count(const char *p_splitter) const {
  748. if (is_empty()) {
  749. return 0;
  750. }
  751. if (p_splitter == nullptr || *p_splitter == '\0') {
  752. return 0;
  753. }
  754. int pos = 0;
  755. int slices = 1;
  756. int splitter_length = strlen(p_splitter);
  757. while ((pos = find(p_splitter, pos)) >= 0) {
  758. slices++;
  759. pos += splitter_length;
  760. }
  761. return slices;
  762. }
  763. String String::get_slice(const String &p_splitter, int p_slice) const {
  764. if (is_empty() || p_splitter.is_empty()) {
  765. return "";
  766. }
  767. int pos = 0;
  768. int prev_pos = 0;
  769. //int slices=1;
  770. if (p_slice < 0) {
  771. return "";
  772. }
  773. if (find(p_splitter) == -1) {
  774. return *this;
  775. }
  776. int i = 0;
  777. while (true) {
  778. pos = find(p_splitter, pos);
  779. if (pos == -1) {
  780. pos = length(); //reached end
  781. }
  782. int from = prev_pos;
  783. //int to=pos;
  784. if (p_slice == i) {
  785. return substr(from, pos - from);
  786. }
  787. if (pos == length()) { //reached end and no find
  788. break;
  789. }
  790. pos += p_splitter.length();
  791. prev_pos = pos;
  792. i++;
  793. }
  794. return ""; //no find!
  795. }
  796. String String::get_slice(const char *p_splitter, int p_slice) const {
  797. if (is_empty() || p_splitter == nullptr || *p_splitter == '\0') {
  798. return "";
  799. }
  800. int pos = 0;
  801. int prev_pos = 0;
  802. //int slices=1;
  803. if (p_slice < 0) {
  804. return "";
  805. }
  806. if (find(p_splitter) == -1) {
  807. return *this;
  808. }
  809. int i = 0;
  810. const int splitter_length = strlen(p_splitter);
  811. while (true) {
  812. pos = find(p_splitter, pos);
  813. if (pos == -1) {
  814. pos = length(); //reached end
  815. }
  816. int from = prev_pos;
  817. //int to=pos;
  818. if (p_slice == i) {
  819. return substr(from, pos - from);
  820. }
  821. if (pos == length()) { //reached end and no find
  822. break;
  823. }
  824. pos += splitter_length;
  825. prev_pos = pos;
  826. i++;
  827. }
  828. return ""; //no find!
  829. }
  830. String String::get_slicec(char32_t p_splitter, int p_slice) const {
  831. if (is_empty()) {
  832. return String();
  833. }
  834. if (p_slice < 0) {
  835. return String();
  836. }
  837. const char32_t *c = ptr();
  838. int i = 0;
  839. int prev = 0;
  840. int count = 0;
  841. while (true) {
  842. if (c[i] == 0 || c[i] == p_splitter) {
  843. if (p_slice == count) {
  844. return substr(prev, i - prev);
  845. } else if (c[i] == 0) {
  846. return String();
  847. } else {
  848. count++;
  849. prev = i + 1;
  850. }
  851. }
  852. i++;
  853. }
  854. }
  855. Vector<String> String::split_spaces(int p_maxsplit) const {
  856. Vector<String> ret;
  857. int from = 0;
  858. int i = 0;
  859. int len = length();
  860. if (len == 0) {
  861. return ret;
  862. }
  863. bool inside = false;
  864. while (true) {
  865. bool empty = operator[](i) < 33;
  866. if (i == 0) {
  867. inside = !empty;
  868. }
  869. if (!empty && !inside) {
  870. inside = true;
  871. from = i;
  872. }
  873. if (empty && inside) {
  874. if (p_maxsplit > 0 && p_maxsplit == ret.size()) {
  875. // Put rest of the string and leave cycle.
  876. ret.push_back(substr(from));
  877. break;
  878. }
  879. ret.push_back(substr(from, i - from));
  880. inside = false;
  881. }
  882. if (i == len) {
  883. break;
  884. }
  885. i++;
  886. }
  887. return ret;
  888. }
  889. Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
  890. Vector<String> ret;
  891. if (is_empty()) {
  892. if (p_allow_empty) {
  893. ret.push_back("");
  894. }
  895. return ret;
  896. }
  897. int from = 0;
  898. int len = length();
  899. while (true) {
  900. int end;
  901. if (p_splitter.is_empty()) {
  902. end = from + 1;
  903. } else {
  904. end = find(p_splitter, from);
  905. if (end < 0) {
  906. end = len;
  907. }
  908. }
  909. if (p_allow_empty || (end > from)) {
  910. if (p_maxsplit <= 0) {
  911. ret.push_back(substr(from, end - from));
  912. } else {
  913. // Put rest of the string and leave cycle.
  914. if (p_maxsplit == ret.size()) {
  915. ret.push_back(substr(from, len));
  916. break;
  917. }
  918. // Otherwise, push items until positive limit is reached.
  919. ret.push_back(substr(from, end - from));
  920. }
  921. }
  922. if (end == len) {
  923. break;
  924. }
  925. from = end + p_splitter.length();
  926. }
  927. return ret;
  928. }
  929. Vector<String> String::split(const char *p_splitter, bool p_allow_empty, int p_maxsplit) const {
  930. Vector<String> ret;
  931. if (is_empty()) {
  932. if (p_allow_empty) {
  933. ret.push_back("");
  934. }
  935. return ret;
  936. }
  937. int from = 0;
  938. int len = length();
  939. const int splitter_length = strlen(p_splitter);
  940. while (true) {
  941. int end;
  942. if (p_splitter == nullptr || *p_splitter == '\0') {
  943. end = from + 1;
  944. } else {
  945. end = find(p_splitter, from);
  946. if (end < 0) {
  947. end = len;
  948. }
  949. }
  950. if (p_allow_empty || (end > from)) {
  951. if (p_maxsplit <= 0) {
  952. ret.push_back(substr(from, end - from));
  953. } else {
  954. // Put rest of the string and leave cycle.
  955. if (p_maxsplit == ret.size()) {
  956. ret.push_back(substr(from, len));
  957. break;
  958. }
  959. // Otherwise, push items until positive limit is reached.
  960. ret.push_back(substr(from, end - from));
  961. }
  962. }
  963. if (end == len) {
  964. break;
  965. }
  966. from = end + splitter_length;
  967. }
  968. return ret;
  969. }
  970. Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
  971. Vector<String> ret;
  972. const int len = length();
  973. int remaining_len = len;
  974. while (true) {
  975. if (remaining_len < p_splitter.length() || (p_maxsplit > 0 && p_maxsplit == ret.size())) {
  976. // no room for another splitter or hit max splits, push what's left and we're done
  977. if (p_allow_empty || remaining_len > 0) {
  978. ret.push_back(substr(0, remaining_len));
  979. }
  980. break;
  981. }
  982. int left_edge;
  983. if (p_splitter.is_empty()) {
  984. left_edge = remaining_len - 1;
  985. if (left_edge == 0) {
  986. left_edge--; // Skip to the < 0 condition.
  987. }
  988. } else {
  989. left_edge = rfind(p_splitter, remaining_len - p_splitter.length());
  990. }
  991. if (left_edge < 0) {
  992. // no more splitters, we're done
  993. ret.push_back(substr(0, remaining_len));
  994. break;
  995. }
  996. int substr_start = left_edge + p_splitter.length();
  997. if (p_allow_empty || substr_start < remaining_len) {
  998. ret.push_back(substr(substr_start, remaining_len - substr_start));
  999. }
  1000. remaining_len = left_edge;
  1001. }
  1002. ret.reverse();
  1003. return ret;
  1004. }
  1005. Vector<String> String::rsplit(const char *p_splitter, bool p_allow_empty, int p_maxsplit) const {
  1006. Vector<String> ret;
  1007. const int len = length();
  1008. const int splitter_length = strlen(p_splitter);
  1009. int remaining_len = len;
  1010. while (true) {
  1011. if (remaining_len < splitter_length || (p_maxsplit > 0 && p_maxsplit == ret.size())) {
  1012. // no room for another splitter or hit max splits, push what's left and we're done
  1013. if (p_allow_empty || remaining_len > 0) {
  1014. ret.push_back(substr(0, remaining_len));
  1015. }
  1016. break;
  1017. }
  1018. int left_edge;
  1019. if (p_splitter == nullptr || *p_splitter == '\0') {
  1020. left_edge = remaining_len - 1;
  1021. if (left_edge == 0) {
  1022. left_edge--; // Skip to the < 0 condition.
  1023. }
  1024. } else {
  1025. left_edge = rfind(p_splitter, remaining_len - splitter_length);
  1026. }
  1027. if (left_edge < 0) {
  1028. // no more splitters, we're done
  1029. ret.push_back(substr(0, remaining_len));
  1030. break;
  1031. }
  1032. int substr_start = left_edge + splitter_length;
  1033. if (p_allow_empty || substr_start < remaining_len) {
  1034. ret.push_back(substr(substr_start, remaining_len - substr_start));
  1035. }
  1036. remaining_len = left_edge;
  1037. }
  1038. ret.reverse();
  1039. return ret;
  1040. }
  1041. Vector<double> String::split_floats(const String &p_splitter, bool p_allow_empty) const {
  1042. Vector<double> ret;
  1043. int from = 0;
  1044. int len = length();
  1045. String buffer = *this;
  1046. while (true) {
  1047. int end = find(p_splitter, from);
  1048. if (end < 0) {
  1049. end = len;
  1050. }
  1051. if (p_allow_empty || (end > from)) {
  1052. buffer[end] = 0;
  1053. ret.push_back(String::to_float(&buffer.get_data()[from]));
  1054. buffer[end] = _cowdata.get(end);
  1055. }
  1056. if (end == len) {
  1057. break;
  1058. }
  1059. from = end + p_splitter.length();
  1060. }
  1061. return ret;
  1062. }
  1063. Vector<float> String::split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
  1064. Vector<float> ret;
  1065. int from = 0;
  1066. int len = length();
  1067. String buffer = *this;
  1068. while (true) {
  1069. int idx;
  1070. int end = findmk(p_splitters, from, &idx);
  1071. int spl_len = 1;
  1072. if (end < 0) {
  1073. end = len;
  1074. } else {
  1075. spl_len = p_splitters[idx].length();
  1076. }
  1077. if (p_allow_empty || (end > from)) {
  1078. buffer[end] = 0;
  1079. ret.push_back(String::to_float(&buffer.get_data()[from]));
  1080. buffer[end] = _cowdata.get(end);
  1081. }
  1082. if (end == len) {
  1083. break;
  1084. }
  1085. from = end + spl_len;
  1086. }
  1087. return ret;
  1088. }
  1089. Vector<int> String::split_ints(const String &p_splitter, bool p_allow_empty) const {
  1090. Vector<int> ret;
  1091. int from = 0;
  1092. int len = length();
  1093. while (true) {
  1094. int end = find(p_splitter, from);
  1095. if (end < 0) {
  1096. end = len;
  1097. }
  1098. if (p_allow_empty || (end > from)) {
  1099. ret.push_back(String::to_int(&get_data()[from], end - from));
  1100. }
  1101. if (end == len) {
  1102. break;
  1103. }
  1104. from = end + p_splitter.length();
  1105. }
  1106. return ret;
  1107. }
  1108. Vector<int> String::split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
  1109. Vector<int> ret;
  1110. int from = 0;
  1111. int len = length();
  1112. while (true) {
  1113. int idx;
  1114. int end = findmk(p_splitters, from, &idx);
  1115. int spl_len = 1;
  1116. if (end < 0) {
  1117. end = len;
  1118. } else {
  1119. spl_len = p_splitters[idx].length();
  1120. }
  1121. if (p_allow_empty || (end > from)) {
  1122. ret.push_back(String::to_int(&get_data()[from], end - from));
  1123. }
  1124. if (end == len) {
  1125. break;
  1126. }
  1127. from = end + spl_len;
  1128. }
  1129. return ret;
  1130. }
  1131. String String::join(const Vector<String> &parts) const {
  1132. if (parts.is_empty()) {
  1133. return String();
  1134. } else if (parts.size() == 1) {
  1135. return parts[0];
  1136. }
  1137. const int this_length = length();
  1138. int new_size = (parts.size() - 1) * this_length;
  1139. for (const String &part : parts) {
  1140. new_size += part.length();
  1141. }
  1142. new_size += 1;
  1143. String ret;
  1144. ret.resize_uninitialized(new_size);
  1145. char32_t *ret_ptrw = ret.ptrw();
  1146. const char32_t *this_ptr = ptr();
  1147. bool first = true;
  1148. for (const String &part : parts) {
  1149. if (first) {
  1150. first = false;
  1151. } else if (this_length) {
  1152. memcpy(ret_ptrw, this_ptr, this_length * sizeof(char32_t));
  1153. ret_ptrw += this_length;
  1154. }
  1155. const int part_length = part.length();
  1156. if (part_length) {
  1157. memcpy(ret_ptrw, part.ptr(), part_length * sizeof(char32_t));
  1158. ret_ptrw += part_length;
  1159. }
  1160. }
  1161. *ret_ptrw = 0;
  1162. return ret;
  1163. }
  1164. char32_t String::char_uppercase(char32_t p_char) {
  1165. return _find_upper(p_char);
  1166. }
  1167. char32_t String::char_lowercase(char32_t p_char) {
  1168. return _find_lower(p_char);
  1169. }
  1170. String String::to_upper() const {
  1171. if (is_empty()) {
  1172. return *this;
  1173. }
  1174. String upper;
  1175. upper.resize_uninitialized(size());
  1176. const char32_t *old_ptr = ptr();
  1177. char32_t *upper_ptrw = upper.ptrw();
  1178. while (*old_ptr) {
  1179. *upper_ptrw++ = _find_upper(*old_ptr++);
  1180. }
  1181. *upper_ptrw = 0;
  1182. return upper;
  1183. }
  1184. String String::to_lower() const {
  1185. if (is_empty()) {
  1186. return *this;
  1187. }
  1188. String lower;
  1189. lower.resize_uninitialized(size());
  1190. const char32_t *old_ptr = ptr();
  1191. char32_t *lower_ptrw = lower.ptrw();
  1192. while (*old_ptr) {
  1193. *lower_ptrw++ = _find_lower(*old_ptr++);
  1194. }
  1195. *lower_ptrw = 0;
  1196. return lower;
  1197. }
  1198. String String::num(double p_num, int p_decimals) {
  1199. if (Math::is_nan(p_num)) {
  1200. return "nan";
  1201. }
  1202. if (Math::is_inf(p_num)) {
  1203. if (std::signbit(p_num)) {
  1204. return "-inf";
  1205. } else {
  1206. return "inf";
  1207. }
  1208. }
  1209. if (p_decimals < 0) {
  1210. p_decimals = 14;
  1211. const double abs_num = Math::abs(p_num);
  1212. if (abs_num > 10) {
  1213. // We want to align the digits to the above reasonable default, so we only
  1214. // need to subtract log10 for numbers with a positive power of ten.
  1215. p_decimals -= (int)std::floor(std::log10(abs_num));
  1216. }
  1217. }
  1218. if (p_decimals > MAX_DECIMALS) {
  1219. p_decimals = MAX_DECIMALS;
  1220. }
  1221. char fmt[7];
  1222. fmt[0] = '%';
  1223. fmt[1] = '.';
  1224. if (p_decimals < 0) {
  1225. fmt[1] = 'l';
  1226. fmt[2] = 'f';
  1227. fmt[3] = 0;
  1228. } else if (p_decimals < 10) {
  1229. fmt[2] = '0' + p_decimals;
  1230. fmt[3] = 'l';
  1231. fmt[4] = 'f';
  1232. fmt[5] = 0;
  1233. } else {
  1234. fmt[2] = '0' + (p_decimals / 10);
  1235. fmt[3] = '0' + (p_decimals % 10);
  1236. fmt[4] = 'l';
  1237. fmt[5] = 'f';
  1238. fmt[6] = 0;
  1239. }
  1240. // if we want to convert a double with as much decimal places as
  1241. // DBL_MAX or DBL_MIN then we would theoretically need a buffer of at least
  1242. // DBL_MAX_10_EXP + 2 for DBL_MAX and DBL_MAX_10_EXP + 4 for DBL_MIN.
  1243. // BUT those values where still giving me exceptions, so I tested from
  1244. // DBL_MAX_10_EXP + 10 incrementing one by one and DBL_MAX_10_EXP + 17 (325)
  1245. // was the first buffer size not to throw an exception
  1246. char buf[325];
  1247. #if defined(__GNUC__) || defined(_MSC_VER)
  1248. // PLEASE NOTE that, albeit vcrt online reference states that snprintf
  1249. // should safely truncate the output to the given buffer size, we have
  1250. // found a case where this is not true, so we should create a buffer
  1251. // as big as needed
  1252. snprintf(buf, 325, fmt, p_num);
  1253. #else
  1254. sprintf(buf, fmt, p_num);
  1255. #endif
  1256. buf[324] = 0;
  1257. // Destroy trailing zeroes, except one after period.
  1258. {
  1259. bool period = false;
  1260. int z = 0;
  1261. while (buf[z]) {
  1262. if (buf[z] == '.') {
  1263. period = true;
  1264. }
  1265. z++;
  1266. }
  1267. if (period) {
  1268. z--;
  1269. while (z > 0) {
  1270. if (buf[z] == '0') {
  1271. buf[z] = 0;
  1272. } else if (buf[z] == '.') {
  1273. buf[z + 1] = '0';
  1274. break;
  1275. } else {
  1276. break;
  1277. }
  1278. z--;
  1279. }
  1280. }
  1281. }
  1282. return buf;
  1283. }
  1284. String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
  1285. ERR_FAIL_COND_V_MSG(base < 2 || base > 36, "", "Cannot convert to base " + itos(base) + ", since the value is " + (base < 2 ? "less than 2." : "greater than 36."));
  1286. bool sign = p_num < 0;
  1287. int64_t n = p_num;
  1288. int chars = 0;
  1289. do {
  1290. n /= base;
  1291. chars++;
  1292. } while (n);
  1293. if (sign) {
  1294. chars++;
  1295. }
  1296. String s;
  1297. s.resize_uninitialized(chars + 1);
  1298. char32_t *c = s.ptrw();
  1299. c[chars] = 0;
  1300. n = p_num;
  1301. do {
  1302. int mod = Math::abs(n % base);
  1303. if (mod >= 10) {
  1304. char a = (capitalize_hex ? 'A' : 'a');
  1305. c[--chars] = a + (mod - 10);
  1306. } else {
  1307. c[--chars] = '0' + mod;
  1308. }
  1309. n /= base;
  1310. } while (n);
  1311. if (sign) {
  1312. c[0] = '-';
  1313. }
  1314. return s;
  1315. }
  1316. String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
  1317. ERR_FAIL_COND_V_MSG(base < 2 || base > 36, "", "Cannot convert to base " + itos(base) + ", since the value is " + (base < 2 ? "less than 2." : "greater than 36."));
  1318. uint64_t n = p_num;
  1319. int chars = 0;
  1320. do {
  1321. n /= base;
  1322. chars++;
  1323. } while (n);
  1324. String s;
  1325. s.resize_uninitialized(chars + 1);
  1326. char32_t *c = s.ptrw();
  1327. c[chars] = 0;
  1328. n = p_num;
  1329. do {
  1330. int mod = n % base;
  1331. if (mod >= 10) {
  1332. char a = (capitalize_hex ? 'A' : 'a');
  1333. c[--chars] = a + (mod - 10);
  1334. } else {
  1335. c[--chars] = '0' + mod;
  1336. }
  1337. n /= base;
  1338. } while (n);
  1339. return s;
  1340. }
  1341. String String::num_real(double p_num, bool p_trailing) {
  1342. if (Math::is_nan(p_num) || Math::is_inf(p_num)) {
  1343. return num(p_num, 0);
  1344. }
  1345. if (p_num == (double)(int64_t)p_num) {
  1346. if (p_trailing) {
  1347. return num_int64((int64_t)p_num) + ".0";
  1348. } else {
  1349. return num_int64((int64_t)p_num);
  1350. }
  1351. }
  1352. int decimals = 14;
  1353. // We want to align the digits to the above sane default, so we only need
  1354. // to subtract log10 for numbers with a positive power of ten magnitude.
  1355. const double abs_num = Math::abs(p_num);
  1356. if (abs_num > 10) {
  1357. decimals -= (int)std::floor(std::log10(abs_num));
  1358. }
  1359. return num(p_num, decimals);
  1360. }
  1361. String String::num_real(float p_num, bool p_trailing) {
  1362. if (Math::is_nan(p_num) || Math::is_inf(p_num)) {
  1363. return num(p_num, 0);
  1364. }
  1365. if (p_num == (float)(int64_t)p_num) {
  1366. if (p_trailing) {
  1367. return num_int64((int64_t)p_num) + ".0";
  1368. } else {
  1369. return num_int64((int64_t)p_num);
  1370. }
  1371. }
  1372. int decimals = 6;
  1373. // We want to align the digits to the above sane default, so we only need
  1374. // to subtract log10 for numbers with a positive power of ten magnitude.
  1375. const float abs_num = Math::abs(p_num);
  1376. if (abs_num > 10) {
  1377. decimals -= (int)std::floor(std::log10(abs_num));
  1378. }
  1379. return num(p_num, decimals);
  1380. }
  1381. String String::num_scientific(double p_num) {
  1382. if (Math::is_nan(p_num) || Math::is_inf(p_num)) {
  1383. return num(p_num, 0);
  1384. }
  1385. char buffer[256];
  1386. char *last = grisu2::to_chars(buffer, p_num);
  1387. return String::ascii(Span(buffer, last - buffer));
  1388. }
  1389. String String::num_scientific(float p_num) {
  1390. if (Math::is_nan(p_num) || Math::is_inf(p_num)) {
  1391. return num(p_num, 0);
  1392. }
  1393. char buffer[256];
  1394. char *last = grisu2::to_chars(buffer, p_num);
  1395. return String::ascii(Span(buffer, last - buffer));
  1396. }
  1397. String String::md5(const uint8_t *p_md5) {
  1398. return String::hex_encode_buffer(p_md5, 16);
  1399. }
  1400. String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
  1401. static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  1402. String ret;
  1403. ret.resize_uninitialized(p_len * 2 + 1);
  1404. char32_t *ret_ptrw = ret.ptrw();
  1405. for (int i = 0; i < p_len; i++) {
  1406. *ret_ptrw++ = hex[p_buffer[i] >> 4];
  1407. *ret_ptrw++ = hex[p_buffer[i] & 0xF];
  1408. }
  1409. *ret_ptrw = 0;
  1410. return ret;
  1411. }
  1412. Vector<uint8_t> String::hex_decode() const {
  1413. ERR_FAIL_COND_V_MSG(length() % 2 != 0, Vector<uint8_t>(), "Hexadecimal string of uneven length.");
  1414. #define HEX_TO_BYTE(m_output, m_index) \
  1415. uint8_t m_output; \
  1416. c = operator[](m_index); \
  1417. if (is_digit(c)) { \
  1418. m_output = c - '0'; \
  1419. } else if (c >= 'a' && c <= 'f') { \
  1420. m_output = c - 'a' + 10; \
  1421. } else if (c >= 'A' && c <= 'F') { \
  1422. m_output = c - 'A' + 10; \
  1423. } else { \
  1424. ERR_FAIL_V_MSG(Vector<uint8_t>(), "Invalid hexadecimal character \"" + chr(c) + "\" at index " + m_index + "."); \
  1425. }
  1426. Vector<uint8_t> out;
  1427. int len = length() / 2;
  1428. out.resize_uninitialized(len);
  1429. uint8_t *out_ptrw = out.ptrw();
  1430. for (int i = 0; i < len; i++) {
  1431. char32_t c;
  1432. HEX_TO_BYTE(first, i * 2);
  1433. HEX_TO_BYTE(second, i * 2 + 1);
  1434. out_ptrw[i] = first * 16 + second;
  1435. }
  1436. return out;
  1437. #undef HEX_TO_BYTE
  1438. }
  1439. void String::print_unicode_error(const String &p_message, bool p_critical) const {
  1440. if (p_critical) {
  1441. print_error(vformat(U"Unicode parsing error, some characters were replaced with � (U+FFFD): %s", p_message));
  1442. } else {
  1443. print_error(vformat("Unicode parsing error: %s", p_message));
  1444. }
  1445. }
  1446. CharString String::ascii(bool p_allow_extended) const {
  1447. if (!length()) {
  1448. return CharString();
  1449. }
  1450. CharString cs;
  1451. cs.resize_uninitialized(size());
  1452. char *cs_ptrw = cs.ptrw();
  1453. const char32_t *this_ptr = ptr();
  1454. for (int i = 0; i < size(); i++) {
  1455. char32_t c = this_ptr[i];
  1456. if ((c <= 0x7f) || (c <= 0xff && p_allow_extended)) {
  1457. cs_ptrw[i] = char(c);
  1458. } else {
  1459. print_unicode_error(vformat("Invalid unicode codepoint (%x), cannot represent as ASCII/Latin-1", (uint32_t)c));
  1460. cs_ptrw[i] = 0x20; // ASCII doesn't have a replacement character like unicode, 0x1a is sometimes used but is kinda arcane.
  1461. }
  1462. }
  1463. return cs;
  1464. }
  1465. Error String::append_ascii(const Span<char> &p_range) {
  1466. if (p_range.is_empty()) {
  1467. return OK;
  1468. }
  1469. const int prev_length = length();
  1470. resize_uninitialized(prev_length + p_range.size() + 1); // Include \0
  1471. const char *src = p_range.ptr();
  1472. const char *end = src + p_range.size();
  1473. char32_t *dst = ptrw() + prev_length;
  1474. bool decode_failed = false;
  1475. for (; src < end; ++src, ++dst) {
  1476. // If char is int8_t, a set sign bit will be reinterpreted as 256 - val implicitly.
  1477. const uint8_t chr = *src;
  1478. if (unlikely(chr == '\0')) {
  1479. // NUL in string is allowed by the unicode standard, but unsupported in our implementation right now.
  1480. print_unicode_error("Unexpected NUL character", true);
  1481. *dst = _replacement_char;
  1482. } else if (unlikely(chr > 127)) {
  1483. print_unicode_error(vformat("Invalid ASCII codepoint (%x)", (uint32_t)chr), true);
  1484. decode_failed = true;
  1485. *dst = _replacement_char;
  1486. } else {
  1487. *dst = chr;
  1488. }
  1489. }
  1490. *dst = _null;
  1491. return decode_failed ? ERR_INVALID_DATA : OK;
  1492. }
  1493. Error String::append_utf8(const char *p_utf8, int p_len, bool p_skip_cr) {
  1494. if (!p_utf8) {
  1495. return ERR_INVALID_DATA;
  1496. }
  1497. /* HANDLE BOM (Byte Order Mark) */
  1498. if (p_len < 0 || p_len >= 3) {
  1499. bool has_bom = uint8_t(p_utf8[0]) == 0xef && uint8_t(p_utf8[1]) == 0xbb && uint8_t(p_utf8[2]) == 0xbf;
  1500. if (has_bom) {
  1501. //8-bit encoding, byte order has no meaning in UTF-8, just skip it
  1502. if (p_len >= 0) {
  1503. p_len -= 3;
  1504. }
  1505. p_utf8 += 3;
  1506. }
  1507. }
  1508. if (p_len < 0) {
  1509. p_len = strlen(p_utf8);
  1510. }
  1511. const int prev_length = length();
  1512. // If all utf8 characters maps to ASCII, then the max size will be p_len, and we add +1 for the null termination.
  1513. resize_uninitialized(prev_length + p_len + 1);
  1514. char32_t *dst = ptrw() + prev_length;
  1515. Error result = Error::OK;
  1516. const uint8_t *ptrtmp = (uint8_t *)p_utf8;
  1517. const uint8_t *ptr_limit = (uint8_t *)p_utf8 + p_len;
  1518. while (ptrtmp < ptr_limit && *ptrtmp) {
  1519. uint8_t c = *ptrtmp;
  1520. if (p_skip_cr && c == '\r') {
  1521. ++ptrtmp;
  1522. continue;
  1523. }
  1524. uint32_t unicode = _replacement_char;
  1525. uint32_t size = 1;
  1526. if ((c & 0b10000000) == 0) {
  1527. unicode = c;
  1528. if (unicode > 0x7F) {
  1529. unicode = _replacement_char;
  1530. print_unicode_error(vformat("Invalid unicode codepoint (%d)", unicode), true);
  1531. result = Error::ERR_INVALID_DATA;
  1532. }
  1533. } else if ((c & 0b11100000) == 0b11000000) {
  1534. if (ptrtmp + 1 >= ptr_limit) {
  1535. print_unicode_error(vformat("Missing %x UTF-8 continuation byte", c), true);
  1536. result = Error::ERR_INVALID_DATA;
  1537. } else {
  1538. uint8_t c2 = *(ptrtmp + 1);
  1539. if ((c2 & 0b11000000) == 0b10000000) {
  1540. unicode = (uint32_t)((c & 0b00011111) << 6) | (uint32_t)(c2 & 0b00111111);
  1541. if (unicode < 0x80) {
  1542. unicode = _replacement_char;
  1543. print_unicode_error(vformat("Overlong encoding (%x %x)", c, c2));
  1544. result = Error::ERR_INVALID_DATA;
  1545. } else if (unicode > 0x7FF) {
  1546. unicode = _replacement_char;
  1547. print_unicode_error(vformat("Invalid unicode codepoint (%d)", unicode), true);
  1548. result = Error::ERR_INVALID_DATA;
  1549. } else {
  1550. size = 2;
  1551. }
  1552. } else {
  1553. print_unicode_error(vformat("Byte %x is not a correct continuation byte after %x", c2, c));
  1554. result = Error::ERR_INVALID_DATA;
  1555. }
  1556. }
  1557. } else if ((c & 0b11110000) == 0b11100000) {
  1558. uint32_t range_min = (c == 0xE0) ? 0xA0 : 0x80;
  1559. uint32_t range_max = (c == 0xED) ? 0x9F : 0xBF;
  1560. uint8_t c2 = (ptrtmp + 1) < ptr_limit ? *(ptrtmp + 1) : 0;
  1561. uint8_t c3 = (ptrtmp + 2) < ptr_limit ? *(ptrtmp + 2) : 0;
  1562. bool c2_valid = c2 && (c2 >= range_min) && (c2 <= range_max);
  1563. bool c3_valid = c3 && ((c3 & 0b11000000) == 0b10000000);
  1564. if (c2_valid && c3_valid) {
  1565. unicode = (uint32_t)((c & 0b00001111) << 12) | (uint32_t)((c2 & 0b00111111) << 6) | (uint32_t)(c3 & 0b00111111);
  1566. if (unicode < 0x800) {
  1567. unicode = _replacement_char;
  1568. print_unicode_error(vformat("Overlong encoding (%x %x %x)", c, c2, c3));
  1569. result = Error::ERR_INVALID_DATA;
  1570. } else if (unicode > 0xFFFF) {
  1571. unicode = _replacement_char;
  1572. print_unicode_error(vformat("Invalid unicode codepoint (%d)", unicode), true);
  1573. result = Error::ERR_INVALID_DATA;
  1574. } else {
  1575. size = 3;
  1576. }
  1577. } else {
  1578. if (c2 == 0) {
  1579. print_unicode_error(vformat("Missing %x UTF-8 continuation byte", c), true);
  1580. } else if (c2_valid == false) {
  1581. print_unicode_error(vformat("Byte %x is not a correct continuation byte after %x", c2, c));
  1582. } else if (c3 == 0) {
  1583. print_unicode_error(vformat("Missing %x %x UTF-8 continuation byte", c, c2), true);
  1584. } else {
  1585. print_unicode_error(vformat("Byte %x is not a correct continuation byte after %x %x", c3, c, c2));
  1586. // The unicode specification, in paragraphe 3.9 "Unicode Encoding Forms" Conformance
  1587. // state : "Only when a sequence of two or three bytes is a truncated version of a sequence which is
  1588. // otherwise well-formed to that point, is more than one byte replaced with a single U+FFFD"
  1589. // So here we replace the first 2 bytes with one single replacement_char.
  1590. size = 2;
  1591. }
  1592. result = Error::ERR_INVALID_DATA;
  1593. }
  1594. } else if ((c & 0b11111000) == 0b11110000) {
  1595. uint32_t range_min = (c == 0xF0) ? 0x90 : 0x80;
  1596. uint32_t range_max = (c == 0xF4) ? 0x8F : 0xBF;
  1597. uint8_t c2 = ((ptrtmp + 1) < ptr_limit) ? *(ptrtmp + 1) : 0;
  1598. uint8_t c3 = ((ptrtmp + 2) < ptr_limit) ? *(ptrtmp + 2) : 0;
  1599. uint8_t c4 = ((ptrtmp + 3) < ptr_limit) ? *(ptrtmp + 3) : 0;
  1600. bool c2_valid = c2 && (c2 >= range_min) && (c2 <= range_max);
  1601. bool c3_valid = c3 && ((c3 & 0b11000000) == 0b10000000);
  1602. bool c4_valid = c4 && ((c4 & 0b11000000) == 0b10000000);
  1603. if (c2_valid && c3_valid && c4_valid) {
  1604. unicode = (uint32_t)((c & 0b00000111) << 18) | (uint32_t)((c2 & 0b00111111) << 12) | (uint32_t)((c3 & 0b00111111) << 6) | (uint32_t)(c4 & 0b00111111);
  1605. if (unicode < 0x10000) {
  1606. unicode = _replacement_char;
  1607. print_unicode_error(vformat("Overlong encoding (%x %x %x %x)", c, c2, c3, c4));
  1608. result = Error::ERR_INVALID_DATA;
  1609. } else if (unicode > 0x10FFFF) {
  1610. unicode = _replacement_char;
  1611. print_unicode_error(vformat("Invalid unicode codepoint (%d)", unicode), true);
  1612. result = Error::ERR_INVALID_DATA;
  1613. } else {
  1614. size = 4;
  1615. }
  1616. } else {
  1617. if (c2 == 0) {
  1618. print_unicode_error(vformat("Missing %x UTF-8 continuation byte", c), true);
  1619. } else if (c2_valid == false) {
  1620. print_unicode_error(vformat("Byte %x is not a correct continuation byte after %x", c2, c));
  1621. } else if (c3 == 0) {
  1622. print_unicode_error(vformat("Missing %x %x UTF-8 continuation byte", c, c2), true);
  1623. } else if (c3_valid == false) {
  1624. print_unicode_error(vformat("Byte %x is not a correct continuation byte after %x %x", c3, c, c2));
  1625. size = 2;
  1626. } else if (c4 == 0) {
  1627. print_unicode_error(vformat("Missing %x %x %x UTF-8 continuation byte", c, c2, c3), true);
  1628. } else {
  1629. print_unicode_error(vformat("Byte %x is not a correct continuation byte after %x %x %x", c4, c, c2, c3));
  1630. size = 3;
  1631. }
  1632. result = Error::ERR_INVALID_DATA;
  1633. }
  1634. } else {
  1635. print_unicode_error(vformat("Invalid UTF-8 leading byte (%x)", c), true);
  1636. result = Error::ERR_INVALID_DATA;
  1637. }
  1638. (*dst++) = unicode;
  1639. ptrtmp += size;
  1640. }
  1641. (*dst++) = 0;
  1642. resize_uninitialized(dst - ptr());
  1643. return result;
  1644. }
  1645. CharString String::utf8(Vector<uint8_t> *r_ch_length_map) const {
  1646. int l = length();
  1647. if (!l) {
  1648. return CharString();
  1649. }
  1650. uint8_t *map_ptr = nullptr;
  1651. if (r_ch_length_map) {
  1652. r_ch_length_map->resize_uninitialized(l);
  1653. map_ptr = r_ch_length_map->ptrw();
  1654. }
  1655. const char32_t *d = &operator[](0);
  1656. int fl = 0;
  1657. for (int i = 0; i < l; i++) {
  1658. uint32_t c = d[i];
  1659. int ch_w = 1;
  1660. if (c <= 0x7f) { // 7 bits.
  1661. ch_w = 1;
  1662. } else if (c <= 0x7ff) { // 11 bits
  1663. ch_w = 2;
  1664. } else if (c <= 0xffff) { // 16 bits
  1665. ch_w = 3;
  1666. } else if (c <= 0x001fffff) { // 21 bits
  1667. ch_w = 4;
  1668. } else if (c <= 0x03ffffff) { // 26 bits
  1669. ch_w = 5;
  1670. print_unicode_error(vformat("Invalid unicode codepoint (%x)", c));
  1671. } else if (c <= 0x7fffffff) { // 31 bits
  1672. ch_w = 6;
  1673. print_unicode_error(vformat("Invalid unicode codepoint (%x)", c));
  1674. } else {
  1675. ch_w = 1;
  1676. print_unicode_error(vformat("Invalid unicode codepoint (%x), cannot represent as UTF-8", c), true);
  1677. }
  1678. fl += ch_w;
  1679. if (map_ptr) {
  1680. map_ptr[i] = ch_w;
  1681. }
  1682. }
  1683. CharString utf8s;
  1684. if (fl == 0) {
  1685. return utf8s;
  1686. }
  1687. utf8s.resize_uninitialized(fl + 1);
  1688. uint8_t *cdst = (uint8_t *)utf8s.get_data();
  1689. #define APPEND_CHAR(m_c) *(cdst++) = m_c
  1690. for (int i = 0; i < l; i++) {
  1691. uint32_t c = d[i];
  1692. if (c <= 0x7f) { // 7 bits.
  1693. APPEND_CHAR(c);
  1694. } else if (c <= 0x7ff) { // 11 bits
  1695. APPEND_CHAR(uint32_t(0xc0 | ((c >> 6) & 0x1f))); // Top 5 bits.
  1696. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1697. } else if (c <= 0xffff) { // 16 bits
  1698. APPEND_CHAR(uint32_t(0xe0 | ((c >> 12) & 0x0f))); // Top 4 bits.
  1699. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Middle 6 bits.
  1700. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1701. } else if (c <= 0x001fffff) { // 21 bits
  1702. APPEND_CHAR(uint32_t(0xf0 | ((c >> 18) & 0x07))); // Top 3 bits.
  1703. APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper middle 6 bits.
  1704. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
  1705. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1706. } else if (c <= 0x03ffffff) { // 26 bits
  1707. APPEND_CHAR(uint32_t(0xf8 | ((c >> 24) & 0x03))); // Top 2 bits.
  1708. APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Upper middle 6 bits.
  1709. APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // middle 6 bits.
  1710. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
  1711. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1712. } else if (c <= 0x7fffffff) { // 31 bits
  1713. APPEND_CHAR(uint32_t(0xfc | ((c >> 30) & 0x01))); // Top 1 bit.
  1714. APPEND_CHAR(uint32_t(0x80 | ((c >> 24) & 0x3f))); // Upper upper middle 6 bits.
  1715. APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Lower upper middle 6 bits.
  1716. APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper lower middle 6 bits.
  1717. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower lower middle 6 bits.
  1718. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1719. } else {
  1720. // the string is a valid UTF32, so it should never happen ...
  1721. print_unicode_error(vformat("Non scalar value (%x)", c), true);
  1722. APPEND_CHAR(uint32_t(0xe0 | ((_replacement_char >> 12) & 0x0f))); // Top 4 bits.
  1723. APPEND_CHAR(uint32_t(0x80 | ((_replacement_char >> 6) & 0x3f))); // Middle 6 bits.
  1724. APPEND_CHAR(uint32_t(0x80 | (_replacement_char & 0x3f))); // Bottom 6 bits.
  1725. }
  1726. }
  1727. #undef APPEND_CHAR
  1728. *cdst = 0; //trailing zero
  1729. return utf8s;
  1730. }
  1731. Error String::append_utf16(const char16_t *p_utf16, int p_len, bool p_default_little_endian) {
  1732. if (!p_utf16) {
  1733. return ERR_INVALID_DATA;
  1734. }
  1735. String aux;
  1736. int cstr_size = 0;
  1737. int str_size = 0;
  1738. #ifdef BIG_ENDIAN_ENABLED
  1739. bool byteswap = p_default_little_endian;
  1740. #else
  1741. bool byteswap = !p_default_little_endian;
  1742. #endif
  1743. /* HANDLE BOM (Byte Order Mark) */
  1744. if (p_len < 0 || p_len >= 1) {
  1745. bool has_bom = false;
  1746. if (uint16_t(p_utf16[0]) == 0xfeff) { // correct BOM, read as is
  1747. has_bom = true;
  1748. byteswap = false;
  1749. } else if (uint16_t(p_utf16[0]) == 0xfffe) { // backwards BOM, swap bytes
  1750. has_bom = true;
  1751. byteswap = true;
  1752. }
  1753. if (has_bom) {
  1754. if (p_len >= 0) {
  1755. p_len -= 1;
  1756. }
  1757. p_utf16 += 1;
  1758. }
  1759. }
  1760. bool decode_error = false;
  1761. {
  1762. const char16_t *ptrtmp = p_utf16;
  1763. const char16_t *ptrtmp_limit = p_len >= 0 ? &p_utf16[p_len] : nullptr;
  1764. uint32_t c_prev = 0;
  1765. bool skip = false;
  1766. while (ptrtmp != ptrtmp_limit && *ptrtmp) {
  1767. uint32_t c = (byteswap) ? BSWAP16(*ptrtmp) : *ptrtmp;
  1768. if ((c & 0xfffffc00) == 0xd800) { // lead surrogate
  1769. if (skip) {
  1770. print_unicode_error(vformat("Unpaired lead surrogate (%x [trail?] %x)", c_prev, c));
  1771. decode_error = true;
  1772. }
  1773. skip = true;
  1774. } else if ((c & 0xfffffc00) == 0xdc00) { // trail surrogate
  1775. if (skip) {
  1776. str_size--;
  1777. } else {
  1778. print_unicode_error(vformat("Unpaired trail surrogate (%x [lead?] %x)", c_prev, c));
  1779. decode_error = true;
  1780. }
  1781. skip = false;
  1782. } else {
  1783. skip = false;
  1784. }
  1785. c_prev = c;
  1786. str_size++;
  1787. cstr_size++;
  1788. ptrtmp++;
  1789. }
  1790. if (skip) {
  1791. print_unicode_error(vformat("Unpaired lead surrogate (%x [eol])", c_prev));
  1792. decode_error = true;
  1793. }
  1794. }
  1795. if (str_size == 0) {
  1796. clear();
  1797. return OK; // empty string
  1798. }
  1799. const int prev_length = length();
  1800. resize_uninitialized(prev_length + str_size + 1);
  1801. char32_t *dst = ptrw() + prev_length;
  1802. dst[str_size] = 0;
  1803. bool skip = false;
  1804. uint32_t c_prev = 0;
  1805. while (cstr_size) {
  1806. uint32_t c = (byteswap) ? BSWAP16(*p_utf16) : *p_utf16;
  1807. if ((c & 0xfffffc00) == 0xd800) { // lead surrogate
  1808. if (skip) {
  1809. *(dst++) = c_prev; // unpaired, store as is
  1810. }
  1811. skip = true;
  1812. } else if ((c & 0xfffffc00) == 0xdc00) { // trail surrogate
  1813. if (skip) {
  1814. *(dst++) = (c_prev << 10UL) + c - ((0xd800 << 10UL) + 0xdc00 - 0x10000); // decode pair
  1815. } else {
  1816. *(dst++) = c; // unpaired, store as is
  1817. }
  1818. skip = false;
  1819. } else {
  1820. *(dst++) = c;
  1821. skip = false;
  1822. }
  1823. cstr_size--;
  1824. p_utf16++;
  1825. c_prev = c;
  1826. }
  1827. if (skip) {
  1828. *(dst++) = c_prev;
  1829. }
  1830. if (decode_error) {
  1831. return ERR_PARSE_ERROR;
  1832. } else {
  1833. return OK;
  1834. }
  1835. }
  1836. Char16String String::utf16() const {
  1837. int l = length();
  1838. if (!l) {
  1839. return Char16String();
  1840. }
  1841. const char32_t *d = &operator[](0);
  1842. int fl = 0;
  1843. for (int i = 0; i < l; i++) {
  1844. uint32_t c = d[i];
  1845. if (c <= 0xffff) { // 16 bits.
  1846. fl += 1;
  1847. if ((c & 0xfffff800) == 0xd800) {
  1848. print_unicode_error(vformat("Unpaired surrogate (%x)", c));
  1849. }
  1850. } else if (c <= 0x10ffff) { // 32 bits.
  1851. fl += 2;
  1852. } else {
  1853. print_unicode_error(vformat("Invalid unicode codepoint (%x), cannot represent as UTF-16", c), true);
  1854. fl += 1;
  1855. }
  1856. }
  1857. Char16String utf16s;
  1858. if (fl == 0) {
  1859. return utf16s;
  1860. }
  1861. utf16s.resize_uninitialized(fl + 1);
  1862. uint16_t *cdst = (uint16_t *)utf16s.get_data();
  1863. #define APPEND_CHAR(m_c) *(cdst++) = m_c
  1864. for (int i = 0; i < l; i++) {
  1865. uint32_t c = d[i];
  1866. if (c <= 0xffff) { // 16 bits.
  1867. APPEND_CHAR(c);
  1868. } else if (c <= 0x10ffff) { // 32 bits.
  1869. APPEND_CHAR(uint32_t((c >> 10) + 0xd7c0)); // lead surrogate.
  1870. APPEND_CHAR(uint32_t((c & 0x3ff) | 0xdc00)); // trail surrogate.
  1871. } else {
  1872. // the string is a valid UTF32, so it should never happen ...
  1873. APPEND_CHAR(uint32_t((_replacement_char >> 10) + 0xd7c0));
  1874. APPEND_CHAR(uint32_t((_replacement_char & 0x3ff) | 0xdc00));
  1875. }
  1876. }
  1877. #undef APPEND_CHAR
  1878. *cdst = 0; //trailing zero
  1879. return utf16s;
  1880. }
  1881. int64_t String::hex_to_int() const {
  1882. int len = length();
  1883. if (len == 0) {
  1884. return 0;
  1885. }
  1886. const char32_t *s = ptr();
  1887. int64_t sign = s[0] == '-' ? -1 : 1;
  1888. if (sign < 0) {
  1889. s++;
  1890. }
  1891. if (len > 2 && s[0] == '0' && lower_case(s[1]) == 'x') {
  1892. s += 2;
  1893. }
  1894. int64_t hex = 0;
  1895. while (*s) {
  1896. char32_t c = lower_case(*s);
  1897. int64_t n;
  1898. if (is_digit(c)) {
  1899. n = c - '0';
  1900. } else if (c >= 'a' && c <= 'f') {
  1901. n = (c - 'a') + 10;
  1902. } else {
  1903. ERR_FAIL_V_MSG(0, vformat(R"(Invalid hexadecimal notation character "%c" (U+%04X) in string "%s".)", *s, static_cast<int32_t>(*s), *this));
  1904. }
  1905. // Check for overflow/underflow, with special case to ensure INT64_MIN does not result in error
  1906. bool overflow = ((hex > INT64_MAX / 16) && (sign == 1 || (sign == -1 && hex != (INT64_MAX >> 4) + 1))) || (sign == -1 && hex == (INT64_MAX >> 4) + 1 && c > '0');
  1907. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as a 64-bit signed integer, since the value is " + (sign == 1 ? "too large." : "too small."));
  1908. hex *= 16;
  1909. hex += n;
  1910. s++;
  1911. }
  1912. return hex * sign;
  1913. }
  1914. int64_t String::bin_to_int() const {
  1915. int len = length();
  1916. if (len == 0) {
  1917. return 0;
  1918. }
  1919. const char32_t *s = ptr();
  1920. int64_t sign = s[0] == '-' ? -1 : 1;
  1921. if (sign < 0) {
  1922. s++;
  1923. }
  1924. if (len > 2 && s[0] == '0' && lower_case(s[1]) == 'b') {
  1925. s += 2;
  1926. }
  1927. int64_t binary = 0;
  1928. while (*s) {
  1929. char32_t c = lower_case(*s);
  1930. int64_t n;
  1931. if (c == '0' || c == '1') {
  1932. n = c - '0';
  1933. } else {
  1934. return 0;
  1935. }
  1936. // Check for overflow/underflow, with special case to ensure INT64_MIN does not result in error
  1937. bool overflow = ((binary > INT64_MAX / 2) && (sign == 1 || (sign == -1 && binary != (INT64_MAX >> 1) + 1))) || (sign == -1 && binary == (INT64_MAX >> 1) + 1 && c > '0');
  1938. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as a 64-bit signed integer, since the value is " + (sign == 1 ? "too large." : "too small."));
  1939. binary *= 2;
  1940. binary += n;
  1941. s++;
  1942. }
  1943. return binary * sign;
  1944. }
  1945. template <typename C, typename T>
  1946. _ALWAYS_INLINE_ int64_t _to_int(const T &p_in, int to) {
  1947. // Accumulate the total number in an unsigned integer as the range is:
  1948. // +9223372036854775807 to -9223372036854775808 and the smallest negative
  1949. // number does not fit inside an int64_t. So we accumulate the positive
  1950. // number in an unsigned, and then at the very end convert to its signed
  1951. // form.
  1952. uint64_t integer = 0;
  1953. uint8_t digits = 0;
  1954. bool positive = true;
  1955. for (int i = 0; i < to; i++) {
  1956. C c = p_in[i];
  1957. if (is_digit(c)) {
  1958. // No need to do expensive checks unless we're approaching INT64_MAX / INT64_MIN.
  1959. if (unlikely(digits > 18)) {
  1960. bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((positive && c > '7') || (!positive && c > '8')));
  1961. ERR_FAIL_COND_V_MSG(overflow, positive ? INT64_MAX : INT64_MIN, "Cannot represent " + String(p_in) + " as a 64-bit signed integer, since the value is " + (positive ? "too large." : "too small."));
  1962. }
  1963. integer *= 10;
  1964. integer += c - '0';
  1965. ++digits;
  1966. } else if (integer == 0 && c == '-') {
  1967. positive = !positive;
  1968. }
  1969. }
  1970. if (positive) {
  1971. return int64_t(integer);
  1972. } else {
  1973. return int64_t(integer * uint64_t(-1));
  1974. }
  1975. }
  1976. int64_t String::to_int() const {
  1977. if (length() == 0) {
  1978. return 0;
  1979. }
  1980. int to = (find_char('.') >= 0) ? find_char('.') : length();
  1981. return _to_int<char32_t>(*this, to);
  1982. }
  1983. int64_t String::to_int(const char *p_str, int p_len) {
  1984. int to = 0;
  1985. if (p_len >= 0) {
  1986. to = p_len;
  1987. } else {
  1988. while (p_str[to] != 0 && p_str[to] != '.') {
  1989. to++;
  1990. }
  1991. }
  1992. return _to_int<char>(p_str, to);
  1993. }
  1994. int64_t String::to_int(const wchar_t *p_str, int p_len) {
  1995. int to = 0;
  1996. if (p_len >= 0) {
  1997. to = p_len;
  1998. } else {
  1999. while (p_str[to] != 0 && p_str[to] != '.') {
  2000. to++;
  2001. }
  2002. }
  2003. return _to_int<wchar_t>(p_str, to);
  2004. }
  2005. bool String::is_numeric() const {
  2006. if (length() == 0) {
  2007. return false;
  2008. }
  2009. int s = 0;
  2010. if (operator[](0) == '-') {
  2011. ++s;
  2012. }
  2013. bool dot = false;
  2014. for (int i = s; i < length(); i++) {
  2015. char32_t c = operator[](i);
  2016. if (c == '.') {
  2017. if (dot) {
  2018. return false;
  2019. }
  2020. dot = true;
  2021. } else if (!is_digit(c)) {
  2022. return false;
  2023. }
  2024. }
  2025. return true; // TODO: Use the parser below for this instead
  2026. }
  2027. template <typename C>
  2028. static double built_in_strtod(
  2029. /* A decimal ASCII floating-point number,
  2030. * optionally preceded by white space. Must
  2031. * have form "-I.FE-X", where I is the integer
  2032. * part of the mantissa, F is the fractional
  2033. * part of the mantissa, and X is the
  2034. * exponent. Either of the signs may be "+",
  2035. * "-", or omitted. Either I or F may be
  2036. * omitted, or both. The decimal point isn't
  2037. * necessary unless F is present. The "E" may
  2038. * actually be an "e". E and X may both be
  2039. * omitted (but not just one). */
  2040. const C *string,
  2041. /* If non-nullptr, store terminating Cacter's
  2042. * address here. */
  2043. C **endPtr = nullptr) {
  2044. /* Largest possible base 10 exponent. Any
  2045. * exponent larger than this will already
  2046. * produce underflow or overflow, so there's
  2047. * no need to worry about additional digits. */
  2048. static const int maxExponent = 511;
  2049. /* Table giving binary powers of 10. Entry
  2050. * is 10^2^i. Used to convert decimal
  2051. * exponents into floating-point numbers. */
  2052. static const double powersOf10[] = {
  2053. 10.,
  2054. 100.,
  2055. 1.0e4,
  2056. 1.0e8,
  2057. 1.0e16,
  2058. 1.0e32,
  2059. 1.0e64,
  2060. 1.0e128,
  2061. 1.0e256
  2062. };
  2063. bool sign, expSign = false;
  2064. double fraction, dblExp;
  2065. const double *d;
  2066. const C *p;
  2067. int c;
  2068. /* Exponent read from "EX" field. */
  2069. int exp = 0;
  2070. /* Exponent that derives from the fractional
  2071. * part. Under normal circumstances, it is
  2072. * the negative of the number of digits in F.
  2073. * However, if I is very long, the last digits
  2074. * of I get dropped (otherwise a long I with a
  2075. * large negative exponent could cause an
  2076. * unnecessary overflow on I alone). In this
  2077. * case, fracExp is incremented one for each
  2078. * dropped digit. */
  2079. int fracExp = 0;
  2080. /* Number of digits in mantissa. */
  2081. int mantSize;
  2082. /* Number of mantissa digits BEFORE decimal point. */
  2083. int decPt;
  2084. /* Temporarily holds location of exponent in string. */
  2085. const C *pExp;
  2086. /*
  2087. * Strip off leading blanks and check for a sign.
  2088. */
  2089. p = string;
  2090. while (*p == ' ' || *p == '\t' || *p == '\n') {
  2091. p += 1;
  2092. }
  2093. if (*p == '-') {
  2094. sign = true;
  2095. p += 1;
  2096. } else {
  2097. if (*p == '+') {
  2098. p += 1;
  2099. }
  2100. sign = false;
  2101. }
  2102. /*
  2103. * Count the number of digits in the mantissa (including the decimal
  2104. * point), and also locate the decimal point.
  2105. */
  2106. decPt = -1;
  2107. for (mantSize = 0;; mantSize += 1) {
  2108. c = *p;
  2109. if (!is_digit(c)) {
  2110. if ((c != '.') || (decPt >= 0)) {
  2111. break;
  2112. }
  2113. decPt = mantSize;
  2114. }
  2115. p += 1;
  2116. }
  2117. /*
  2118. * Now suck up the digits in the mantissa. Use two integers to collect 9
  2119. * digits each (this is faster than using floating-point). If the mantissa
  2120. * has more than 18 digits, ignore the extras, since they can't affect the
  2121. * value anyway.
  2122. */
  2123. pExp = p;
  2124. p -= mantSize;
  2125. if (decPt < 0) {
  2126. decPt = mantSize;
  2127. } else {
  2128. mantSize -= 1; /* One of the digits was the point. */
  2129. }
  2130. if (mantSize > 18) {
  2131. fracExp = decPt - 18;
  2132. mantSize = 18;
  2133. } else {
  2134. fracExp = decPt - mantSize;
  2135. }
  2136. if (mantSize == 0) {
  2137. fraction = 0.0;
  2138. p = string;
  2139. goto done;
  2140. } else {
  2141. int frac1, frac2;
  2142. frac1 = 0;
  2143. for (; mantSize > 9; mantSize -= 1) {
  2144. c = *p;
  2145. p += 1;
  2146. if (c == '.') {
  2147. c = *p;
  2148. p += 1;
  2149. }
  2150. frac1 = 10 * frac1 + (c - '0');
  2151. }
  2152. frac2 = 0;
  2153. for (; mantSize > 0; mantSize -= 1) {
  2154. c = *p;
  2155. p += 1;
  2156. if (c == '.') {
  2157. c = *p;
  2158. p += 1;
  2159. }
  2160. frac2 = 10 * frac2 + (c - '0');
  2161. }
  2162. fraction = (1.0e9 * frac1) + frac2;
  2163. }
  2164. /*
  2165. * Skim off the exponent.
  2166. */
  2167. p = pExp;
  2168. if ((*p == 'E') || (*p == 'e')) {
  2169. p += 1;
  2170. if (*p == '-') {
  2171. expSign = true;
  2172. p += 1;
  2173. } else {
  2174. if (*p == '+') {
  2175. p += 1;
  2176. }
  2177. expSign = false;
  2178. }
  2179. if (!is_digit(char32_t(*p))) {
  2180. p = pExp;
  2181. goto done;
  2182. }
  2183. while (is_digit(char32_t(*p))) {
  2184. exp = exp * 10 + (*p - '0');
  2185. p += 1;
  2186. }
  2187. }
  2188. if (expSign) {
  2189. exp = fracExp - exp;
  2190. } else {
  2191. exp = fracExp + exp;
  2192. }
  2193. /*
  2194. * Generate a floating-point number that represents the exponent. Do this
  2195. * by processing the exponent one bit at a time to combine many powers of
  2196. * 2 of 10. Then combine the exponent with the fraction.
  2197. */
  2198. if (exp < 0) {
  2199. expSign = true;
  2200. exp = -exp;
  2201. } else {
  2202. expSign = false;
  2203. }
  2204. if (exp > maxExponent) {
  2205. exp = maxExponent;
  2206. WARN_PRINT("Exponent too high");
  2207. }
  2208. dblExp = 1.0;
  2209. for (d = powersOf10; exp != 0; exp >>= 1, ++d) {
  2210. if (exp & 01) {
  2211. dblExp *= *d;
  2212. }
  2213. }
  2214. if (expSign) {
  2215. fraction /= dblExp;
  2216. } else {
  2217. fraction *= dblExp;
  2218. }
  2219. done:
  2220. if (endPtr != nullptr) {
  2221. *endPtr = (C *)p;
  2222. }
  2223. if (sign) {
  2224. return -fraction;
  2225. }
  2226. return fraction;
  2227. }
  2228. #define READING_SIGN 0
  2229. #define READING_INT 1
  2230. #define READING_DEC 2
  2231. #define READING_EXP 3
  2232. #define READING_DONE 4
  2233. double String::to_float(const char *p_str) {
  2234. return built_in_strtod<char>(p_str);
  2235. }
  2236. double String::to_float(const char32_t *p_str, const char32_t **r_end) {
  2237. return built_in_strtod<char32_t>(p_str, (char32_t **)r_end);
  2238. }
  2239. double String::to_float(const wchar_t *p_str, const wchar_t **r_end) {
  2240. return built_in_strtod<wchar_t>(p_str, (wchar_t **)r_end);
  2241. }
  2242. uint32_t String::num_characters(int64_t p_int) {
  2243. int r = 1;
  2244. if (p_int < 0) {
  2245. r += 1;
  2246. if (p_int == INT64_MIN) {
  2247. p_int = INT64_MAX;
  2248. } else {
  2249. p_int = -p_int;
  2250. }
  2251. }
  2252. while (p_int >= 10) {
  2253. p_int /= 10;
  2254. r++;
  2255. }
  2256. return r;
  2257. }
  2258. int64_t String::to_int(const char32_t *p_str, int p_len, bool p_clamp) {
  2259. if (p_len == 0 || !p_str[0]) {
  2260. return 0;
  2261. }
  2262. ///@todo make more exact so saving and loading does not lose precision
  2263. int64_t integer = 0;
  2264. int64_t sign = 1;
  2265. int reading = READING_SIGN;
  2266. const char32_t *str = p_str;
  2267. const char32_t *limit = &p_str[p_len];
  2268. while (*str && reading != READING_DONE && str != limit) {
  2269. char32_t c = *(str++);
  2270. switch (reading) {
  2271. case READING_SIGN: {
  2272. if (is_digit(c)) {
  2273. reading = READING_INT;
  2274. // let it fallthrough
  2275. } else if (c == '-') {
  2276. sign = -1;
  2277. reading = READING_INT;
  2278. break;
  2279. } else if (c == '+') {
  2280. sign = 1;
  2281. reading = READING_INT;
  2282. break;
  2283. } else {
  2284. break;
  2285. }
  2286. [[fallthrough]];
  2287. }
  2288. case READING_INT: {
  2289. if (is_digit(c)) {
  2290. if (integer > INT64_MAX / 10) {
  2291. String number("");
  2292. str = p_str;
  2293. while (*str && str != limit) {
  2294. number += *(str++);
  2295. }
  2296. if (p_clamp) {
  2297. if (sign == 1) {
  2298. return INT64_MAX;
  2299. } else {
  2300. return INT64_MIN;
  2301. }
  2302. } else {
  2303. ERR_FAIL_V_MSG(sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + number + " as a 64-bit signed integer, since the value is " + (sign == 1 ? "too large." : "too small."));
  2304. }
  2305. }
  2306. integer *= 10;
  2307. integer += c - '0';
  2308. } else {
  2309. reading = READING_DONE;
  2310. }
  2311. } break;
  2312. }
  2313. }
  2314. return sign * integer;
  2315. }
  2316. double String::to_float() const {
  2317. if (is_empty()) {
  2318. return 0;
  2319. }
  2320. return built_in_strtod<char32_t>(get_data());
  2321. }
  2322. uint32_t String::hash(const char *p_cstr) {
  2323. // static_cast: avoid negative values on platforms where char is signed.
  2324. uint32_t hashv = 5381;
  2325. uint32_t c = static_cast<uint8_t>(*p_cstr++);
  2326. while (c) {
  2327. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  2328. c = static_cast<uint8_t>(*p_cstr++);
  2329. }
  2330. return hashv;
  2331. }
  2332. uint32_t String::hash(const char *p_cstr, int p_len) {
  2333. uint32_t hashv = 5381;
  2334. for (int i = 0; i < p_len; i++) {
  2335. // static_cast: avoid negative values on platforms where char is signed.
  2336. hashv = ((hashv << 5) + hashv) + static_cast<uint8_t>(p_cstr[i]); /* hash * 33 + c */
  2337. }
  2338. return hashv;
  2339. }
  2340. uint32_t String::hash(const wchar_t *p_cstr, int p_len) {
  2341. // Avoid negative values on platforms where wchar_t is signed. Account for different sizes.
  2342. using wide_unsigned = std::conditional<sizeof(wchar_t) == 2, uint16_t, uint32_t>::type;
  2343. uint32_t hashv = 5381;
  2344. for (int i = 0; i < p_len; i++) {
  2345. hashv = ((hashv << 5) + hashv) + static_cast<wide_unsigned>(p_cstr[i]); /* hash * 33 + c */
  2346. }
  2347. return hashv;
  2348. }
  2349. uint32_t String::hash(const wchar_t *p_cstr) {
  2350. // Avoid negative values on platforms where wchar_t is signed. Account for different sizes.
  2351. using wide_unsigned = std::conditional<sizeof(wchar_t) == 2, uint16_t, uint32_t>::type;
  2352. uint32_t hashv = 5381;
  2353. uint32_t c = static_cast<wide_unsigned>(*p_cstr++);
  2354. while (c) {
  2355. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  2356. c = static_cast<wide_unsigned>(*p_cstr++);
  2357. }
  2358. return hashv;
  2359. }
  2360. uint32_t String::hash(const char32_t *p_cstr, int p_len) {
  2361. uint32_t hashv = 5381;
  2362. for (int i = 0; i < p_len; i++) {
  2363. hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
  2364. }
  2365. return hashv;
  2366. }
  2367. uint32_t String::hash(const char32_t *p_cstr) {
  2368. uint32_t hashv = 5381;
  2369. uint32_t c = *p_cstr++;
  2370. while (c) {
  2371. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  2372. c = *p_cstr++;
  2373. }
  2374. return hashv;
  2375. }
  2376. uint32_t String::hash() const {
  2377. /* simple djb2 hashing */
  2378. const char32_t *chr = get_data();
  2379. uint32_t hashv = 5381;
  2380. uint32_t c = *chr++;
  2381. while (c) {
  2382. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  2383. c = *chr++;
  2384. }
  2385. return hashv;
  2386. }
  2387. uint64_t String::hash64() const {
  2388. /* simple djb2 hashing */
  2389. const char32_t *chr = get_data();
  2390. uint64_t hashv = 5381;
  2391. uint64_t c = *chr++;
  2392. while (c) {
  2393. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  2394. c = *chr++;
  2395. }
  2396. return hashv;
  2397. }
  2398. String String::md5_text() const {
  2399. CharString cs = utf8();
  2400. unsigned char hash[16];
  2401. CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
  2402. return String::hex_encode_buffer(hash, 16);
  2403. }
  2404. String String::sha1_text() const {
  2405. CharString cs = utf8();
  2406. unsigned char hash[20];
  2407. CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
  2408. return String::hex_encode_buffer(hash, 20);
  2409. }
  2410. String String::sha256_text() const {
  2411. CharString cs = utf8();
  2412. unsigned char hash[32];
  2413. CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
  2414. return String::hex_encode_buffer(hash, 32);
  2415. }
  2416. Vector<uint8_t> String::md5_buffer() const {
  2417. CharString cs = utf8();
  2418. unsigned char hash[16];
  2419. CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
  2420. Vector<uint8_t> ret;
  2421. ret.resize_uninitialized(16);
  2422. uint8_t *ret_ptrw = ret.ptrw();
  2423. for (int i = 0; i < 16; i++) {
  2424. ret_ptrw[i] = hash[i];
  2425. }
  2426. return ret;
  2427. }
  2428. Vector<uint8_t> String::sha1_buffer() const {
  2429. CharString cs = utf8();
  2430. unsigned char hash[20];
  2431. CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
  2432. Vector<uint8_t> ret;
  2433. ret.resize_uninitialized(20);
  2434. uint8_t *ret_ptrw = ret.ptrw();
  2435. for (int i = 0; i < 20; i++) {
  2436. ret_ptrw[i] = hash[i];
  2437. }
  2438. return ret;
  2439. }
  2440. Vector<uint8_t> String::sha256_buffer() const {
  2441. CharString cs = utf8();
  2442. unsigned char hash[32];
  2443. CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
  2444. Vector<uint8_t> ret;
  2445. ret.resize_uninitialized(32);
  2446. uint8_t *ret_ptrw = ret.ptrw();
  2447. for (int i = 0; i < 32; i++) {
  2448. ret_ptrw[i] = hash[i];
  2449. }
  2450. return ret;
  2451. }
  2452. String String::insert(int p_at_pos, const String &p_string) const {
  2453. if (p_string.is_empty() || p_at_pos < 0) {
  2454. return *this;
  2455. }
  2456. if (p_at_pos > length()) {
  2457. p_at_pos = length();
  2458. }
  2459. String ret;
  2460. ret.resize_uninitialized(length() + p_string.length() + 1);
  2461. char32_t *ret_ptrw = ret.ptrw();
  2462. const char32_t *this_ptr = ptr();
  2463. if (p_at_pos > 0) {
  2464. memcpy(ret_ptrw, this_ptr, p_at_pos * sizeof(char32_t));
  2465. ret_ptrw += p_at_pos;
  2466. }
  2467. memcpy(ret_ptrw, p_string.ptr(), p_string.length() * sizeof(char32_t));
  2468. ret_ptrw += p_string.length();
  2469. if (p_at_pos < length()) {
  2470. memcpy(ret_ptrw, this_ptr + p_at_pos, (length() - p_at_pos) * sizeof(char32_t));
  2471. ret_ptrw += length() - p_at_pos;
  2472. }
  2473. *ret_ptrw = 0;
  2474. return ret;
  2475. }
  2476. String String::erase(int p_pos, int p_chars) const {
  2477. ERR_FAIL_COND_V_MSG(p_pos < 0, "", vformat("Invalid starting position for `String.erase()`: %d. Starting position must be positive or zero.", p_pos));
  2478. ERR_FAIL_COND_V_MSG(p_chars < 0, "", vformat("Invalid character count for `String.erase()`: %d. Character count must be positive or zero.", p_chars));
  2479. return left(p_pos) + substr(p_pos + p_chars);
  2480. }
  2481. template <class T>
  2482. static bool _contains_char(char32_t p_c, const T *p_chars, int p_chars_len) {
  2483. for (int i = 0; i < p_chars_len; ++i) {
  2484. if (p_c == (char32_t)p_chars[i]) {
  2485. return true;
  2486. }
  2487. }
  2488. return false;
  2489. }
  2490. String String::remove_char(char32_t p_char) const {
  2491. if (p_char == 0) {
  2492. return *this;
  2493. }
  2494. int len = length();
  2495. if (len == 0) {
  2496. return *this;
  2497. }
  2498. int index = 0;
  2499. const char32_t *old_ptr = ptr();
  2500. for (; index < len; ++index) {
  2501. if (old_ptr[index] == p_char) {
  2502. break;
  2503. }
  2504. }
  2505. // If no occurrence of `char` was found, return this.
  2506. if (index == len) {
  2507. return *this;
  2508. }
  2509. // If we found at least one occurrence of `char`, create new string, allocating enough space for the current length minus one.
  2510. String new_string;
  2511. new_string.resize_uninitialized(len);
  2512. char32_t *new_ptr = new_string.ptrw();
  2513. // Copy part of input before `char`.
  2514. memcpy(new_ptr, old_ptr, index * sizeof(char32_t));
  2515. int new_size = index;
  2516. // Copy rest, skipping `char`.
  2517. for (++index; index < len; ++index) {
  2518. const char32_t old_char = old_ptr[index];
  2519. if (old_char != p_char) {
  2520. new_ptr[new_size] = old_char;
  2521. ++new_size;
  2522. }
  2523. }
  2524. new_ptr[new_size] = _null;
  2525. // Shrink new string to fit.
  2526. new_string.resize_uninitialized(new_size + 1);
  2527. return new_string;
  2528. }
  2529. template <class T>
  2530. static String _remove_chars_common(const String &p_this, const T *p_chars, int p_chars_len) {
  2531. // Delegate if p_chars has a single element.
  2532. if (p_chars_len == 1) {
  2533. return p_this.remove_char(*p_chars);
  2534. } else if (p_chars_len == 0) {
  2535. return p_this;
  2536. }
  2537. int len = p_this.length();
  2538. if (len == 0) {
  2539. return p_this;
  2540. }
  2541. int index = 0;
  2542. const char32_t *old_ptr = p_this.ptr();
  2543. for (; index < len; ++index) {
  2544. if (_contains_char(old_ptr[index], p_chars, p_chars_len)) {
  2545. break;
  2546. }
  2547. }
  2548. // If no occurrence of `chars` was found, return this.
  2549. if (index == len) {
  2550. return p_this;
  2551. }
  2552. // If we found at least one occurrence of `chars`, create new string, allocating enough space for the current length minus one.
  2553. String new_string;
  2554. new_string.resize_uninitialized(len);
  2555. char32_t *new_ptr = new_string.ptrw();
  2556. // Copy part of input before `char`.
  2557. memcpy(new_ptr, old_ptr, index * sizeof(char32_t));
  2558. int new_size = index;
  2559. // Copy rest, skipping `chars`.
  2560. for (++index; index < len; ++index) {
  2561. const char32_t old_char = old_ptr[index];
  2562. if (!_contains_char(old_char, p_chars, p_chars_len)) {
  2563. new_ptr[new_size] = old_char;
  2564. ++new_size;
  2565. }
  2566. }
  2567. new_ptr[new_size] = 0;
  2568. // Shrink new string to fit.
  2569. new_string.resize_uninitialized(new_size + 1);
  2570. return new_string;
  2571. }
  2572. String String::remove_chars(const String &p_chars) const {
  2573. return _remove_chars_common(*this, p_chars.ptr(), p_chars.length());
  2574. }
  2575. String String::remove_chars(const char *p_chars) const {
  2576. return _remove_chars_common(*this, p_chars, strlen(p_chars));
  2577. }
  2578. String String::substr(int p_from, int p_chars) const {
  2579. if (p_chars == -1) {
  2580. p_chars = length() - p_from;
  2581. }
  2582. if (is_empty() || p_from < 0 || p_from >= length() || p_chars <= 0) {
  2583. return "";
  2584. }
  2585. if ((p_from + p_chars) > length()) {
  2586. p_chars = length() - p_from;
  2587. }
  2588. if (p_from == 0 && p_chars >= length()) {
  2589. return String(*this);
  2590. }
  2591. String s;
  2592. s.append_utf32_unchecked(Span(ptr() + p_from, p_chars));
  2593. return s;
  2594. }
  2595. int String::find(const String &p_str, int p_from) const {
  2596. if (p_from < 0) {
  2597. return -1;
  2598. }
  2599. const int src_len = p_str.length();
  2600. const int len = length();
  2601. if (src_len == 0 || len == 0) {
  2602. return -1; // won't find anything!
  2603. }
  2604. if (src_len == 1) {
  2605. return find_char(p_str[0], p_from); // Optimize with single-char find.
  2606. }
  2607. const char32_t *src = get_data();
  2608. const char32_t *str = p_str.get_data();
  2609. for (int i = p_from; i <= (len - src_len); i++) {
  2610. bool found = true;
  2611. for (int j = 0; j < src_len; j++) {
  2612. int read_pos = i + j;
  2613. if (read_pos >= len) {
  2614. ERR_PRINT("read_pos>=len");
  2615. return -1;
  2616. }
  2617. if (src[read_pos] != str[j]) {
  2618. found = false;
  2619. break;
  2620. }
  2621. }
  2622. if (found) {
  2623. return i;
  2624. }
  2625. }
  2626. return -1;
  2627. }
  2628. int String::find(const char *p_str, int p_from) const {
  2629. if (p_from < 0 || !p_str) {
  2630. return -1;
  2631. }
  2632. const int src_len = strlen(p_str);
  2633. const int len = length();
  2634. if (len == 0 || src_len == 0) {
  2635. return -1; // won't find anything!
  2636. }
  2637. if (src_len == 1) {
  2638. return find_char(*p_str, p_from); // Optimize with single-char find.
  2639. }
  2640. const char32_t *src = get_data();
  2641. if (src_len == 1) {
  2642. const char32_t needle = p_str[0];
  2643. for (int i = p_from; i < len; i++) {
  2644. if (src[i] == needle) {
  2645. return i;
  2646. }
  2647. }
  2648. } else {
  2649. for (int i = p_from; i <= (len - src_len); i++) {
  2650. bool found = true;
  2651. for (int j = 0; j < src_len; j++) {
  2652. int read_pos = i + j;
  2653. if (read_pos >= len) {
  2654. ERR_PRINT("read_pos>=len");
  2655. return -1;
  2656. }
  2657. if (src[read_pos] != (char32_t)p_str[j]) {
  2658. found = false;
  2659. break;
  2660. }
  2661. }
  2662. if (found) {
  2663. return i;
  2664. }
  2665. }
  2666. }
  2667. return -1;
  2668. }
  2669. int String::find_char(char32_t p_char, int p_from) const {
  2670. if (p_from < 0) {
  2671. p_from = length() + p_from;
  2672. }
  2673. if (p_from < 0 || p_from >= length()) {
  2674. return -1;
  2675. }
  2676. return span().find(p_char, p_from);
  2677. }
  2678. int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
  2679. if (p_from < 0) {
  2680. return -1;
  2681. }
  2682. if (p_keys.is_empty()) {
  2683. return -1;
  2684. }
  2685. //int src_len=p_str.length();
  2686. const String *keys = &p_keys[0];
  2687. int key_count = p_keys.size();
  2688. int len = length();
  2689. if (len == 0) {
  2690. return -1; // won't find anything!
  2691. }
  2692. const char32_t *src = get_data();
  2693. for (int i = p_from; i < len; i++) {
  2694. bool found = true;
  2695. for (int k = 0; k < key_count; k++) {
  2696. found = true;
  2697. if (r_key) {
  2698. *r_key = k;
  2699. }
  2700. const char32_t *cmp = keys[k].get_data();
  2701. int l = keys[k].length();
  2702. for (int j = 0; j < l; j++) {
  2703. int read_pos = i + j;
  2704. if (read_pos >= len) {
  2705. found = false;
  2706. break;
  2707. }
  2708. if (src[read_pos] != cmp[j]) {
  2709. found = false;
  2710. break;
  2711. }
  2712. }
  2713. if (found) {
  2714. break;
  2715. }
  2716. }
  2717. if (found) {
  2718. return i;
  2719. }
  2720. }
  2721. return -1;
  2722. }
  2723. int String::findn(const String &p_str, int p_from) const {
  2724. if (p_from < 0) {
  2725. return -1;
  2726. }
  2727. int src_len = p_str.length();
  2728. if (src_len == 0 || length() == 0) {
  2729. return -1; // won't find anything!
  2730. }
  2731. const char32_t *srcd = get_data();
  2732. for (int i = p_from; i <= (length() - src_len); i++) {
  2733. bool found = true;
  2734. for (int j = 0; j < src_len; j++) {
  2735. int read_pos = i + j;
  2736. if (read_pos >= length()) {
  2737. ERR_PRINT("read_pos>=length()");
  2738. return -1;
  2739. }
  2740. char32_t src = _find_lower(srcd[read_pos]);
  2741. char32_t dst = _find_lower(p_str[j]);
  2742. if (src != dst) {
  2743. found = false;
  2744. break;
  2745. }
  2746. }
  2747. if (found) {
  2748. return i;
  2749. }
  2750. }
  2751. return -1;
  2752. }
  2753. int String::findn(const char *p_str, int p_from) const {
  2754. if (p_from < 0) {
  2755. return -1;
  2756. }
  2757. int src_len = strlen(p_str);
  2758. if (src_len == 0 || length() == 0) {
  2759. return -1; // won't find anything!
  2760. }
  2761. const char32_t *srcd = get_data();
  2762. for (int i = p_from; i <= (length() - src_len); i++) {
  2763. bool found = true;
  2764. for (int j = 0; j < src_len; j++) {
  2765. int read_pos = i + j;
  2766. if (read_pos >= length()) {
  2767. ERR_PRINT("read_pos>=length()");
  2768. return -1;
  2769. }
  2770. char32_t src = _find_lower(srcd[read_pos]);
  2771. char32_t dst = _find_lower(p_str[j]);
  2772. if (src != dst) {
  2773. found = false;
  2774. break;
  2775. }
  2776. }
  2777. if (found) {
  2778. return i;
  2779. }
  2780. }
  2781. return -1;
  2782. }
  2783. int String::rfind(const String &p_str, int p_from) const {
  2784. // establish a limit
  2785. int limit = length() - p_str.length();
  2786. if (limit < 0) {
  2787. return -1;
  2788. }
  2789. // establish a starting point
  2790. if (p_from < 0) {
  2791. p_from = limit;
  2792. } else if (p_from > limit) {
  2793. p_from = limit;
  2794. }
  2795. int src_len = p_str.length();
  2796. int len = length();
  2797. if (src_len == 0 || len == 0) {
  2798. return -1; // won't find anything!
  2799. }
  2800. const char32_t *src = get_data();
  2801. for (int i = p_from; i >= 0; i--) {
  2802. bool found = true;
  2803. for (int j = 0; j < src_len; j++) {
  2804. int read_pos = i + j;
  2805. if (read_pos >= len) {
  2806. ERR_PRINT("read_pos>=len");
  2807. return -1;
  2808. }
  2809. if (src[read_pos] != p_str[j]) {
  2810. found = false;
  2811. break;
  2812. }
  2813. }
  2814. if (found) {
  2815. return i;
  2816. }
  2817. }
  2818. return -1;
  2819. }
  2820. int String::rfind(const char *p_str, int p_from) const {
  2821. const int source_length = length();
  2822. int substring_length = strlen(p_str);
  2823. if (source_length == 0 || substring_length == 0) {
  2824. return -1; // won't find anything!
  2825. }
  2826. // establish a limit
  2827. int limit = length() - substring_length;
  2828. if (limit < 0) {
  2829. return -1;
  2830. }
  2831. // establish a starting point
  2832. int starting_point;
  2833. if (p_from < 0) {
  2834. starting_point = limit;
  2835. } else if (p_from > limit) {
  2836. starting_point = limit;
  2837. } else {
  2838. starting_point = p_from;
  2839. }
  2840. const char32_t *source = get_data();
  2841. for (int i = starting_point; i >= 0; i--) {
  2842. bool found = true;
  2843. for (int j = 0; j < substring_length; j++) {
  2844. int read_pos = i + j;
  2845. if (read_pos >= source_length) {
  2846. ERR_PRINT("read_pos>=source_length");
  2847. return -1;
  2848. }
  2849. const char32_t key_needle = p_str[j];
  2850. if (source[read_pos] != key_needle) {
  2851. found = false;
  2852. break;
  2853. }
  2854. }
  2855. if (found) {
  2856. return i;
  2857. }
  2858. }
  2859. return -1;
  2860. }
  2861. int String::rfind_char(char32_t p_char, int p_from) const {
  2862. if (p_from < 0) {
  2863. p_from = length() + p_from;
  2864. }
  2865. if (p_from < 0 || p_from >= length()) {
  2866. return -1;
  2867. }
  2868. return span().rfind(p_char, p_from);
  2869. }
  2870. int String::rfindn(const String &p_str, int p_from) const {
  2871. // establish a limit
  2872. int limit = length() - p_str.length();
  2873. if (limit < 0) {
  2874. return -1;
  2875. }
  2876. // establish a starting point
  2877. if (p_from < 0) {
  2878. p_from = limit;
  2879. } else if (p_from > limit) {
  2880. p_from = limit;
  2881. }
  2882. int src_len = p_str.length();
  2883. int len = length();
  2884. if (src_len == 0 || len == 0) {
  2885. return -1; // won't find anything!
  2886. }
  2887. const char32_t *src = get_data();
  2888. for (int i = p_from; i >= 0; i--) {
  2889. bool found = true;
  2890. for (int j = 0; j < src_len; j++) {
  2891. int read_pos = i + j;
  2892. if (read_pos >= len) {
  2893. ERR_PRINT("read_pos>=len");
  2894. return -1;
  2895. }
  2896. char32_t srcc = _find_lower(src[read_pos]);
  2897. char32_t dstc = _find_lower(p_str[j]);
  2898. if (srcc != dstc) {
  2899. found = false;
  2900. break;
  2901. }
  2902. }
  2903. if (found) {
  2904. return i;
  2905. }
  2906. }
  2907. return -1;
  2908. }
  2909. int String::rfindn(const char *p_str, int p_from) const {
  2910. const int source_length = length();
  2911. int substring_length = strlen(p_str);
  2912. if (source_length == 0 || substring_length == 0) {
  2913. return -1; // won't find anything!
  2914. }
  2915. // establish a limit
  2916. int limit = length() - substring_length;
  2917. if (limit < 0) {
  2918. return -1;
  2919. }
  2920. // establish a starting point
  2921. int starting_point;
  2922. if (p_from < 0) {
  2923. starting_point = limit;
  2924. } else if (p_from > limit) {
  2925. starting_point = limit;
  2926. } else {
  2927. starting_point = p_from;
  2928. }
  2929. const char32_t *source = get_data();
  2930. for (int i = starting_point; i >= 0; i--) {
  2931. bool found = true;
  2932. for (int j = 0; j < substring_length; j++) {
  2933. int read_pos = i + j;
  2934. if (read_pos >= source_length) {
  2935. ERR_PRINT("read_pos>=source_length");
  2936. return -1;
  2937. }
  2938. const char32_t key_needle = p_str[j];
  2939. int srcc = _find_lower(source[read_pos]);
  2940. int keyc = _find_lower(key_needle);
  2941. if (srcc != keyc) {
  2942. found = false;
  2943. break;
  2944. }
  2945. }
  2946. if (found) {
  2947. return i;
  2948. }
  2949. }
  2950. return -1;
  2951. }
  2952. bool String::ends_with(const String &p_string) const {
  2953. const int l = p_string.length();
  2954. if (l > length()) {
  2955. return false;
  2956. }
  2957. if (l == 0) {
  2958. return true;
  2959. }
  2960. return memcmp(ptr() + (length() - l), p_string.ptr(), l * sizeof(char32_t)) == 0;
  2961. }
  2962. bool String::ends_with(const char *p_string) const {
  2963. if (!p_string) {
  2964. return false;
  2965. }
  2966. int l = strlen(p_string);
  2967. if (l > length()) {
  2968. return false;
  2969. }
  2970. if (l == 0) {
  2971. return true;
  2972. }
  2973. const char32_t *s = &operator[](length() - l);
  2974. for (int i = 0; i < l; i++) {
  2975. if (static_cast<char32_t>(p_string[i]) != s[i]) {
  2976. return false;
  2977. }
  2978. }
  2979. return true;
  2980. }
  2981. bool String::begins_with(const String &p_string) const {
  2982. const int l = p_string.length();
  2983. if (l > length()) {
  2984. return false;
  2985. }
  2986. if (l == 0) {
  2987. return true;
  2988. }
  2989. return memcmp(ptr(), p_string.ptr(), l * sizeof(char32_t)) == 0;
  2990. }
  2991. bool String::begins_with(const char *p_string) const {
  2992. if (!p_string) {
  2993. return false;
  2994. }
  2995. int l = length();
  2996. if (l == 0) {
  2997. return *p_string == 0;
  2998. }
  2999. const char32_t *str = &operator[](0);
  3000. int i = 0;
  3001. while (*p_string && i < l) {
  3002. if ((char32_t)*p_string != str[i]) {
  3003. return false;
  3004. }
  3005. i++;
  3006. p_string++;
  3007. }
  3008. return *p_string == 0;
  3009. }
  3010. bool String::is_enclosed_in(const String &p_string) const {
  3011. return begins_with(p_string) && ends_with(p_string);
  3012. }
  3013. bool String::is_subsequence_of(const String &p_string) const {
  3014. return _base_is_subsequence_of(p_string, false);
  3015. }
  3016. bool String::is_subsequence_ofn(const String &p_string) const {
  3017. return _base_is_subsequence_of(p_string, true);
  3018. }
  3019. bool String::is_quoted() const {
  3020. return is_enclosed_in("\"") || is_enclosed_in("'");
  3021. }
  3022. bool String::is_lowercase() const {
  3023. for (const char32_t *str = &operator[](0); *str; str++) {
  3024. if (is_unicode_upper_case(*str)) {
  3025. return false;
  3026. }
  3027. }
  3028. return true;
  3029. }
  3030. int String::_count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const {
  3031. if (p_string.is_empty()) {
  3032. return 0;
  3033. }
  3034. int len = length();
  3035. int slen = p_string.length();
  3036. if (len < slen) {
  3037. return 0;
  3038. }
  3039. String str;
  3040. if (p_from >= 0 && p_to >= 0) {
  3041. if (p_to == 0) {
  3042. p_to = len;
  3043. } else if (p_from >= p_to) {
  3044. return 0;
  3045. }
  3046. if (p_from == 0 && p_to == len) {
  3047. str = *this;
  3048. } else {
  3049. str = substr(p_from, p_to - p_from);
  3050. }
  3051. } else {
  3052. return 0;
  3053. }
  3054. int c = 0;
  3055. int idx = 0;
  3056. while ((idx = p_case_insensitive ? str.findn(p_string, idx) : str.find(p_string, idx)) != -1) {
  3057. // Skip the occurrence itself.
  3058. idx += slen;
  3059. ++c;
  3060. }
  3061. return c;
  3062. }
  3063. int String::_count(const char *p_string, int p_from, int p_to, bool p_case_insensitive) const {
  3064. int substring_length = strlen(p_string);
  3065. if (substring_length == 0) {
  3066. return 0;
  3067. }
  3068. const int source_length = length();
  3069. if (source_length < substring_length) {
  3070. return 0;
  3071. }
  3072. String str;
  3073. int search_limit = p_to;
  3074. if (p_from >= 0 && p_to >= 0) {
  3075. if (p_to == 0) {
  3076. search_limit = source_length;
  3077. } else if (p_from >= p_to) {
  3078. return 0;
  3079. }
  3080. if (p_from == 0 && search_limit == source_length) {
  3081. str = *this;
  3082. } else {
  3083. str = substr(p_from, search_limit - p_from);
  3084. }
  3085. } else {
  3086. return 0;
  3087. }
  3088. int c = 0;
  3089. int idx = 0;
  3090. while ((idx = p_case_insensitive ? str.findn(p_string, idx) : str.find(p_string, idx)) != -1) {
  3091. // Skip the occurrence itself.
  3092. idx += substring_length;
  3093. ++c;
  3094. }
  3095. return c;
  3096. }
  3097. int String::count(const String &p_string, int p_from, int p_to) const {
  3098. return _count(p_string, p_from, p_to, false);
  3099. }
  3100. int String::count(const char *p_string, int p_from, int p_to) const {
  3101. return _count(p_string, p_from, p_to, false);
  3102. }
  3103. int String::countn(const String &p_string, int p_from, int p_to) const {
  3104. return _count(p_string, p_from, p_to, true);
  3105. }
  3106. int String::countn(const char *p_string, int p_from, int p_to) const {
  3107. return _count(p_string, p_from, p_to, true);
  3108. }
  3109. bool String::_base_is_subsequence_of(const String &p_string, bool case_insensitive) const {
  3110. int len = length();
  3111. if (len == 0) {
  3112. // Technically an empty string is subsequence of any string
  3113. return true;
  3114. }
  3115. if (len > p_string.length()) {
  3116. return false;
  3117. }
  3118. const char32_t *src = &operator[](0);
  3119. const char32_t *tgt = &p_string[0];
  3120. for (; *src && *tgt; tgt++) {
  3121. bool match = false;
  3122. if (case_insensitive) {
  3123. char32_t srcc = _find_lower(*src);
  3124. char32_t tgtc = _find_lower(*tgt);
  3125. match = srcc == tgtc;
  3126. } else {
  3127. match = *src == *tgt;
  3128. }
  3129. if (match) {
  3130. src++;
  3131. if (!*src) {
  3132. return true;
  3133. }
  3134. }
  3135. }
  3136. return false;
  3137. }
  3138. Vector<String> String::bigrams() const {
  3139. int n_pairs = length() - 1;
  3140. Vector<String> b;
  3141. if (n_pairs <= 0) {
  3142. return b;
  3143. }
  3144. b.resize_initialized(n_pairs);
  3145. String *b_ptrw = b.ptrw();
  3146. for (int i = 0; i < n_pairs; i++) {
  3147. b_ptrw[i] = substr(i, 2);
  3148. }
  3149. return b;
  3150. }
  3151. // Similarity according to Sorensen-Dice coefficient
  3152. float String::similarity(const String &p_string) const {
  3153. if (operator==(p_string)) {
  3154. // Equal strings are totally similar
  3155. return 1.0f;
  3156. }
  3157. if (length() < 2 || p_string.length() < 2) {
  3158. // No way to calculate similarity without a single bigram
  3159. return 0.0f;
  3160. }
  3161. const int src_size = length() - 1;
  3162. const int tgt_size = p_string.length() - 1;
  3163. const int sum = src_size + tgt_size;
  3164. int inter = 0;
  3165. for (int i = 0; i < src_size; i++) {
  3166. const char32_t i0 = get(i);
  3167. const char32_t i1 = get(i + 1);
  3168. for (int j = 0; j < tgt_size; j++) {
  3169. if (i0 == p_string.get(j) && i1 == p_string.get(j + 1)) {
  3170. inter++;
  3171. break;
  3172. }
  3173. }
  3174. }
  3175. return (2.0f * inter) / sum;
  3176. }
  3177. static bool _wildcard_match(const char32_t *p_pattern, const char32_t *p_string, bool p_case_sensitive) {
  3178. switch (*p_pattern) {
  3179. case '\0':
  3180. return !*p_string;
  3181. case '*':
  3182. return _wildcard_match(p_pattern + 1, p_string, p_case_sensitive) || (*p_string && _wildcard_match(p_pattern, p_string + 1, p_case_sensitive));
  3183. case '?':
  3184. return *p_string && (*p_string != '.') && _wildcard_match(p_pattern + 1, p_string + 1, p_case_sensitive);
  3185. default:
  3186. return (p_case_sensitive ? (*p_string == *p_pattern) : (_find_upper(*p_string) == _find_upper(*p_pattern))) && _wildcard_match(p_pattern + 1, p_string + 1, p_case_sensitive);
  3187. }
  3188. }
  3189. bool String::match(const String &p_wildcard) const {
  3190. if (!p_wildcard.length() || !length()) {
  3191. return false;
  3192. }
  3193. return _wildcard_match(p_wildcard.get_data(), get_data(), true);
  3194. }
  3195. bool String::matchn(const String &p_wildcard) const {
  3196. if (!p_wildcard.length() || !length()) {
  3197. return false;
  3198. }
  3199. return _wildcard_match(p_wildcard.get_data(), get_data(), false);
  3200. }
  3201. String String::format(const Variant &values, const String &placeholder) const {
  3202. String new_string = *this;
  3203. if (values.get_type() == Variant::ARRAY) {
  3204. Array values_arr = values;
  3205. for (int i = 0; i < values_arr.size(); i++) {
  3206. if (values_arr[i].get_type() == Variant::ARRAY) { //Array in Array structure [["name","RobotGuy"],[0,"godot"],["strength",9000.91]]
  3207. Array value_arr = values_arr[i];
  3208. if (value_arr.size() == 2) {
  3209. String key = value_arr[0];
  3210. String val = value_arr[1];
  3211. new_string = new_string.replace(placeholder.replace("_", key), val);
  3212. } else {
  3213. ERR_PRINT(vformat("Invalid format: the inner Array at index %d needs to contain only 2 elements, as a key-value pair.", i).ascii().get_data());
  3214. }
  3215. } else { //Array structure ["RobotGuy","Logis","rookie"]
  3216. String val = values_arr[i];
  3217. if (placeholder.contains_char('_')) {
  3218. new_string = new_string.replace(placeholder.replace("_", String::num_int64(i)), val);
  3219. } else {
  3220. new_string = new_string.replace_first(placeholder, val);
  3221. }
  3222. }
  3223. }
  3224. } else if (values.get_type() == Variant::DICTIONARY) {
  3225. Dictionary d = values;
  3226. for (const KeyValue<Variant, Variant> &kv : d) {
  3227. new_string = new_string.replace(placeholder.replace("_", kv.key), kv.value);
  3228. }
  3229. } else if (values.get_type() == Variant::OBJECT) {
  3230. Object *obj = values.get_validated_object();
  3231. ERR_FAIL_NULL_V(obj, new_string);
  3232. List<PropertyInfo> props;
  3233. obj->get_property_list(&props);
  3234. for (const PropertyInfo &E : props) {
  3235. new_string = new_string.replace(placeholder.replace("_", E.name), obj->get(E.name));
  3236. }
  3237. } else {
  3238. ERR_PRINT(String("Invalid type: use Array, Dictionary or Object.").ascii().get_data());
  3239. }
  3240. return new_string;
  3241. }
  3242. static String _replace_common(const String &p_this, const String &p_key, const String &p_with, bool p_case_insensitive) {
  3243. if (p_key.is_empty() || p_this.is_empty()) {
  3244. return p_this;
  3245. }
  3246. const size_t key_length = p_key.length();
  3247. int search_from = 0;
  3248. int result = 0;
  3249. LocalVector<int> found;
  3250. while ((result = (p_case_insensitive ? p_this.findn(p_key, search_from) : p_this.find(p_key, search_from))) >= 0) {
  3251. found.push_back(result);
  3252. ERR_FAIL_COND_V_MSG((result + key_length) > INT32_MAX, p_this, "Key length too long");
  3253. search_from = result + key_length;
  3254. }
  3255. if (found.is_empty()) {
  3256. return p_this;
  3257. }
  3258. String new_string;
  3259. const int with_length = p_with.length();
  3260. const int old_length = p_this.length();
  3261. new_string.resize_uninitialized(old_length + int(found.size()) * (with_length - key_length) + 1);
  3262. char32_t *new_ptrw = new_string.ptrw();
  3263. const char32_t *old_ptr = p_this.ptr();
  3264. const char32_t *with_ptr = p_with.ptr();
  3265. int last_pos = 0;
  3266. for (const int &pos : found) {
  3267. if (last_pos != pos) {
  3268. memcpy(new_ptrw, old_ptr + last_pos, (pos - last_pos) * sizeof(char32_t));
  3269. new_ptrw += (pos - last_pos);
  3270. }
  3271. if (with_length) {
  3272. memcpy(new_ptrw, with_ptr, with_length * sizeof(char32_t));
  3273. new_ptrw += with_length;
  3274. }
  3275. last_pos = pos + key_length;
  3276. }
  3277. if (last_pos != old_length) {
  3278. memcpy(new_ptrw, old_ptr + last_pos, (old_length - last_pos) * sizeof(char32_t));
  3279. new_ptrw += old_length - last_pos;
  3280. }
  3281. *new_ptrw = 0;
  3282. return new_string;
  3283. }
  3284. static String _replace_common(const String &p_this, char const *p_key, char const *p_with, bool p_case_insensitive) {
  3285. size_t key_length = strlen(p_key);
  3286. if (key_length == 0 || p_this.is_empty()) {
  3287. return p_this;
  3288. }
  3289. int search_from = 0;
  3290. int result = 0;
  3291. LocalVector<int> found;
  3292. while ((result = (p_case_insensitive ? p_this.findn(p_key, search_from) : p_this.find(p_key, search_from))) >= 0) {
  3293. found.push_back(result);
  3294. ERR_FAIL_COND_V_MSG((result + key_length) > INT32_MAX, p_this, "Key length too long");
  3295. search_from = result + key_length;
  3296. }
  3297. if (found.is_empty()) {
  3298. return p_this;
  3299. }
  3300. String new_string;
  3301. // Create string to speed up copying as we can't do `memcopy` between `char32_t` and `char`.
  3302. const String with_string(p_with);
  3303. const int with_length = with_string.length();
  3304. const int old_length = p_this.length();
  3305. new_string.resize_uninitialized(old_length + int(found.size()) * (with_length - key_length) + 1);
  3306. char32_t *new_ptrw = new_string.ptrw();
  3307. const char32_t *old_ptr = p_this.ptr();
  3308. const char32_t *with_ptr = with_string.ptr();
  3309. int last_pos = 0;
  3310. for (const int &pos : found) {
  3311. if (last_pos != pos) {
  3312. memcpy(new_ptrw, old_ptr + last_pos, (pos - last_pos) * sizeof(char32_t));
  3313. new_ptrw += (pos - last_pos);
  3314. }
  3315. if (with_length) {
  3316. memcpy(new_ptrw, with_ptr, with_length * sizeof(char32_t));
  3317. new_ptrw += with_length;
  3318. }
  3319. last_pos = pos + key_length;
  3320. }
  3321. if (last_pos != old_length) {
  3322. memcpy(new_ptrw, old_ptr + last_pos, (old_length - last_pos) * sizeof(char32_t));
  3323. new_ptrw += old_length - last_pos;
  3324. }
  3325. *new_ptrw = 0;
  3326. return new_string;
  3327. }
  3328. String String::replace(const String &p_key, const String &p_with) const {
  3329. return _replace_common(*this, p_key, p_with, false);
  3330. }
  3331. String String::replace(const char *p_key, const char *p_with) const {
  3332. return _replace_common(*this, p_key, p_with, false);
  3333. }
  3334. String String::replace_first(const String &p_key, const String &p_with) const {
  3335. int pos = find(p_key);
  3336. if (pos >= 0) {
  3337. const int old_length = length();
  3338. const int key_length = p_key.length();
  3339. const int with_length = p_with.length();
  3340. String new_string;
  3341. new_string.resize_uninitialized(old_length + (with_length - key_length) + 1);
  3342. char32_t *new_ptrw = new_string.ptrw();
  3343. const char32_t *old_ptr = ptr();
  3344. const char32_t *with_ptr = p_with.ptr();
  3345. if (pos > 0) {
  3346. memcpy(new_ptrw, old_ptr, pos * sizeof(char32_t));
  3347. new_ptrw += pos;
  3348. }
  3349. if (with_length) {
  3350. memcpy(new_ptrw, with_ptr, with_length * sizeof(char32_t));
  3351. new_ptrw += with_length;
  3352. }
  3353. pos += key_length;
  3354. if (pos != old_length) {
  3355. memcpy(new_ptrw, old_ptr + pos, (old_length - pos) * sizeof(char32_t));
  3356. new_ptrw += (old_length - pos);
  3357. }
  3358. *new_ptrw = 0;
  3359. return new_string;
  3360. }
  3361. return *this;
  3362. }
  3363. String String::replace_first(const char *p_key, const char *p_with) const {
  3364. int pos = find(p_key);
  3365. if (pos >= 0) {
  3366. const int old_length = length();
  3367. const int key_length = strlen(p_key);
  3368. const int with_length = strlen(p_with);
  3369. String new_string;
  3370. new_string.resize_uninitialized(old_length + (with_length - key_length) + 1);
  3371. char32_t *new_ptrw = new_string.ptrw();
  3372. const char32_t *old_ptr = ptr();
  3373. if (pos > 0) {
  3374. memcpy(new_ptrw, old_ptr, pos * sizeof(char32_t));
  3375. new_ptrw += pos;
  3376. }
  3377. for (int i = 0; i < with_length; ++i) {
  3378. *new_ptrw++ = p_with[i];
  3379. }
  3380. pos += key_length;
  3381. if (pos != old_length) {
  3382. memcpy(new_ptrw, old_ptr + pos, (old_length - pos) * sizeof(char32_t));
  3383. new_ptrw += (old_length - pos);
  3384. }
  3385. *new_ptrw = 0;
  3386. return new_string;
  3387. }
  3388. return *this;
  3389. }
  3390. String String::replace_char(char32_t p_key, char32_t p_with) const {
  3391. ERR_FAIL_COND_V_MSG(p_with == 0, *this, "`with` must not be the NUL character.");
  3392. if (p_key == 0) {
  3393. return *this;
  3394. }
  3395. int len = length();
  3396. if (len == 0) {
  3397. return *this;
  3398. }
  3399. int index = 0;
  3400. const char32_t *old_ptr = ptr();
  3401. for (; index < len; ++index) {
  3402. if (old_ptr[index] == p_key) {
  3403. break;
  3404. }
  3405. }
  3406. // If no occurrence of `key` was found, return this.
  3407. if (index == len) {
  3408. return *this;
  3409. }
  3410. // If we found at least one occurrence of `key`, create new string.
  3411. String new_string;
  3412. new_string.resize_uninitialized(len + 1);
  3413. char32_t *new_ptr = new_string.ptrw();
  3414. // Copy part of input before `key`.
  3415. memcpy(new_ptr, old_ptr, index * sizeof(char32_t));
  3416. new_ptr[index] = p_with;
  3417. // Copy or replace rest of input.
  3418. for (++index; index < len; ++index) {
  3419. if (old_ptr[index] == p_key) {
  3420. new_ptr[index] = p_with;
  3421. } else {
  3422. new_ptr[index] = old_ptr[index];
  3423. }
  3424. }
  3425. new_ptr[index] = _null;
  3426. return new_string;
  3427. }
  3428. template <class T>
  3429. static String _replace_chars_common(const String &p_this, const T *p_keys, int p_keys_len, char32_t p_with) {
  3430. ERR_FAIL_COND_V_MSG(p_with == 0, p_this, "`with` must not be the NUL character.");
  3431. // Delegate if p_keys is a single element.
  3432. if (p_keys_len == 1) {
  3433. return p_this.replace_char(*p_keys, p_with);
  3434. } else if (p_keys_len == 0) {
  3435. return p_this;
  3436. }
  3437. int len = p_this.length();
  3438. if (len == 0) {
  3439. return p_this;
  3440. }
  3441. int index = 0;
  3442. const char32_t *old_ptr = p_this.ptr();
  3443. for (; index < len; ++index) {
  3444. if (_contains_char(old_ptr[index], p_keys, p_keys_len)) {
  3445. break;
  3446. }
  3447. }
  3448. // If no occurrence of `keys` was found, return this.
  3449. if (index == len) {
  3450. return p_this;
  3451. }
  3452. // If we found at least one occurrence of `keys`, create new string.
  3453. String new_string;
  3454. new_string.resize_uninitialized(len + 1);
  3455. char32_t *new_ptr = new_string.ptrw();
  3456. // Copy part of input before `key`.
  3457. memcpy(new_ptr, old_ptr, index * sizeof(char32_t));
  3458. new_ptr[index] = p_with;
  3459. // Copy or replace rest of input.
  3460. for (++index; index < len; ++index) {
  3461. const char32_t old_char = old_ptr[index];
  3462. if (_contains_char(old_char, p_keys, p_keys_len)) {
  3463. new_ptr[index] = p_with;
  3464. } else {
  3465. new_ptr[index] = old_char;
  3466. }
  3467. }
  3468. new_ptr[index] = 0;
  3469. return new_string;
  3470. }
  3471. String String::replace_chars(const String &p_keys, char32_t p_with) const {
  3472. return _replace_chars_common(*this, p_keys.ptr(), p_keys.length(), p_with);
  3473. }
  3474. String String::replace_chars(const char *p_keys, char32_t p_with) const {
  3475. return _replace_chars_common(*this, p_keys, strlen(p_keys), p_with);
  3476. }
  3477. String String::replacen(const String &p_key, const String &p_with) const {
  3478. return _replace_common(*this, p_key, p_with, true);
  3479. }
  3480. String String::replacen(const char *p_key, const char *p_with) const {
  3481. return _replace_common(*this, p_key, p_with, true);
  3482. }
  3483. String String::repeat(int p_count) const {
  3484. ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number.");
  3485. if (p_count == 0) {
  3486. return "";
  3487. }
  3488. if (p_count == 1) {
  3489. return *this;
  3490. }
  3491. int len = length();
  3492. String new_string = *this;
  3493. new_string.resize_uninitialized(p_count * len + 1);
  3494. char32_t *dst = new_string.ptrw();
  3495. int offset = 1;
  3496. int stride = 1;
  3497. while (offset < p_count) {
  3498. memcpy(dst + offset * len, dst, stride * len * sizeof(char32_t));
  3499. offset += stride;
  3500. stride = MIN(stride * 2, p_count - offset);
  3501. }
  3502. dst[p_count * len] = _null;
  3503. return new_string;
  3504. }
  3505. String String::reverse() const {
  3506. int len = length();
  3507. if (len <= 1) {
  3508. return *this;
  3509. }
  3510. String new_string;
  3511. new_string.resize_uninitialized(len + 1);
  3512. const char32_t *src = ptr();
  3513. char32_t *dst = new_string.ptrw();
  3514. for (int i = 0; i < len; i++) {
  3515. dst[i] = src[len - i - 1];
  3516. }
  3517. dst[len] = _null;
  3518. return new_string;
  3519. }
  3520. String String::left(int p_len) const {
  3521. if (p_len < 0) {
  3522. p_len = length() + p_len;
  3523. }
  3524. if (p_len <= 0) {
  3525. return "";
  3526. }
  3527. if (p_len >= length()) {
  3528. return *this;
  3529. }
  3530. String s;
  3531. s.append_utf32_unchecked(Span(ptr(), p_len));
  3532. return s;
  3533. }
  3534. String String::right(int p_len) const {
  3535. if (p_len < 0) {
  3536. p_len = length() + p_len;
  3537. }
  3538. if (p_len <= 0) {
  3539. return "";
  3540. }
  3541. if (p_len >= length()) {
  3542. return *this;
  3543. }
  3544. String s;
  3545. s.append_utf32_unchecked(Span(ptr() + length() - p_len, p_len));
  3546. return s;
  3547. }
  3548. char32_t String::unicode_at(int p_idx) const {
  3549. ERR_FAIL_INDEX_V(p_idx, length(), 0);
  3550. return operator[](p_idx);
  3551. }
  3552. String String::indent(const String &p_prefix) const {
  3553. String new_string;
  3554. int line_start = 0;
  3555. for (int i = 0; i < length(); i++) {
  3556. const char32_t c = operator[](i);
  3557. if (c == '\n') {
  3558. if (i == line_start) {
  3559. new_string += c; // Leave empty lines empty.
  3560. } else {
  3561. new_string += p_prefix + substr(line_start, i - line_start + 1);
  3562. }
  3563. line_start = i + 1;
  3564. }
  3565. }
  3566. if (line_start != length()) {
  3567. new_string += p_prefix + substr(line_start);
  3568. }
  3569. return new_string;
  3570. }
  3571. String String::dedent() const {
  3572. String new_string;
  3573. String indent;
  3574. bool has_indent = false;
  3575. bool has_text = false;
  3576. int line_start = 0;
  3577. int indent_stop = -1;
  3578. for (int i = 0; i < length(); i++) {
  3579. char32_t c = operator[](i);
  3580. if (c == '\n') {
  3581. if (has_text) {
  3582. new_string += substr(indent_stop, i - indent_stop);
  3583. }
  3584. new_string += "\n";
  3585. has_text = false;
  3586. line_start = i + 1;
  3587. indent_stop = -1;
  3588. } else if (!has_text) {
  3589. if (c > 32) {
  3590. has_text = true;
  3591. if (!has_indent) {
  3592. has_indent = true;
  3593. indent = substr(line_start, i - line_start);
  3594. indent_stop = i;
  3595. }
  3596. }
  3597. if (has_indent && indent_stop < 0) {
  3598. int j = i - line_start;
  3599. if (j >= indent.length() || c != indent[j]) {
  3600. indent_stop = i;
  3601. }
  3602. }
  3603. }
  3604. }
  3605. if (has_text) {
  3606. new_string += substr(indent_stop, length() - indent_stop);
  3607. }
  3608. return new_string;
  3609. }
  3610. String String::strip_edges(bool left, bool right) const {
  3611. int len = length();
  3612. int beg = 0, end = len;
  3613. if (left) {
  3614. for (int i = 0; i < len; i++) {
  3615. if (operator[](i) <= 32) {
  3616. beg++;
  3617. } else {
  3618. break;
  3619. }
  3620. }
  3621. }
  3622. if (right) {
  3623. for (int i = len - 1; i >= 0; i--) {
  3624. if (operator[](i) <= 32) {
  3625. end--;
  3626. } else {
  3627. break;
  3628. }
  3629. }
  3630. }
  3631. if (beg == 0 && end == len) {
  3632. return *this;
  3633. }
  3634. return substr(beg, end - beg);
  3635. }
  3636. String String::strip_escapes() const {
  3637. String new_string;
  3638. for (int i = 0; i < length(); i++) {
  3639. // Escape characters on first page of the ASCII table, before 32 (Space).
  3640. if (operator[](i) < 32) {
  3641. continue;
  3642. }
  3643. new_string += operator[](i);
  3644. }
  3645. return new_string;
  3646. }
  3647. String String::lstrip(const String &p_chars) const {
  3648. int len = length();
  3649. int beg;
  3650. for (beg = 0; beg < len; beg++) {
  3651. if (p_chars.find_char(get(beg)) == -1) {
  3652. break;
  3653. }
  3654. }
  3655. if (beg == 0) {
  3656. return *this;
  3657. }
  3658. return substr(beg, len - beg);
  3659. }
  3660. String String::rstrip(const String &p_chars) const {
  3661. int len = length();
  3662. int end;
  3663. for (end = len - 1; end >= 0; end--) {
  3664. if (p_chars.find_char(get(end)) == -1) {
  3665. break;
  3666. }
  3667. }
  3668. if (end == len - 1) {
  3669. return *this;
  3670. }
  3671. return substr(0, end + 1);
  3672. }
  3673. bool String::is_network_share_path() const {
  3674. return begins_with("//") || begins_with("\\\\");
  3675. }
  3676. String String::simplify_path() const {
  3677. String s = *this;
  3678. String drive;
  3679. // Check if we have a special path (like res://) or a protocol identifier.
  3680. int p = s.find("://");
  3681. bool found = false;
  3682. if (p > 0) {
  3683. bool only_chars = true;
  3684. for (int i = 0; i < p; i++) {
  3685. if (!is_ascii_alphanumeric_char(s[i])) {
  3686. only_chars = false;
  3687. break;
  3688. }
  3689. }
  3690. if (only_chars) {
  3691. found = true;
  3692. drive = s.substr(0, p + 3);
  3693. s = s.substr(p + 3);
  3694. }
  3695. }
  3696. if (!found) {
  3697. if (is_network_share_path()) {
  3698. // Network path, beginning with // or \\.
  3699. drive = s.substr(0, 2);
  3700. s = s.substr(2);
  3701. } else if (s.begins_with("/") || s.begins_with("\\")) {
  3702. // Absolute path.
  3703. drive = s.substr(0, 1);
  3704. s = s.substr(1);
  3705. } else {
  3706. // Windows-style drive path, like C:/ or C:\.
  3707. p = s.find(":/");
  3708. if (p == -1) {
  3709. p = s.find(":\\");
  3710. }
  3711. if (p != -1 && p < s.find_char('/')) {
  3712. drive = s.substr(0, p + 2);
  3713. s = s.substr(p + 2);
  3714. }
  3715. }
  3716. }
  3717. s = s.replace_char('\\', '/');
  3718. while (true) { // in case of using 2 or more slash
  3719. String compare = s.replace("//", "/");
  3720. if (s == compare) {
  3721. break;
  3722. } else {
  3723. s = compare;
  3724. }
  3725. }
  3726. Vector<String> dirs = s.split("/", false);
  3727. for (int i = 0; i < dirs.size(); i++) {
  3728. String d = dirs[i];
  3729. if (d == ".") {
  3730. dirs.remove_at(i);
  3731. i--;
  3732. } else if (d == "..") {
  3733. if (i != 0 && dirs[i - 1] != "..") {
  3734. dirs.remove_at(i);
  3735. dirs.remove_at(i - 1);
  3736. i -= 2;
  3737. }
  3738. }
  3739. }
  3740. s = "";
  3741. for (int i = 0; i < dirs.size(); i++) {
  3742. if (i > 0) {
  3743. s += "/";
  3744. }
  3745. s += dirs[i];
  3746. }
  3747. return drive + s;
  3748. }
  3749. static int _humanize_digits(int p_num) {
  3750. if (p_num < 100) {
  3751. return 2;
  3752. } else if (p_num < 1024) {
  3753. return 1;
  3754. } else {
  3755. return 0;
  3756. }
  3757. }
  3758. String String::humanize_size(uint64_t p_size) {
  3759. int magnitude = 0;
  3760. uint64_t _div = 1;
  3761. while (p_size > _div * 1024 && magnitude < 6) {
  3762. _div *= 1024;
  3763. magnitude++;
  3764. }
  3765. if (magnitude == 0) {
  3766. return String::num_uint64(p_size) + " " + RTR("B");
  3767. } else {
  3768. String suffix;
  3769. switch (magnitude) {
  3770. case 1:
  3771. suffix = RTR("KiB");
  3772. break;
  3773. case 2:
  3774. suffix = RTR("MiB");
  3775. break;
  3776. case 3:
  3777. suffix = RTR("GiB");
  3778. break;
  3779. case 4:
  3780. suffix = RTR("TiB");
  3781. break;
  3782. case 5:
  3783. suffix = RTR("PiB");
  3784. break;
  3785. case 6:
  3786. suffix = RTR("EiB");
  3787. break;
  3788. }
  3789. const double divisor = _div;
  3790. const int digits = _humanize_digits(p_size / _div);
  3791. return String::num(p_size / divisor).pad_decimals(digits) + " " + suffix;
  3792. }
  3793. }
  3794. bool String::is_absolute_path() const {
  3795. if (length() > 1) {
  3796. return (operator[](0) == '/' || operator[](0) == '\\' || find(":/") != -1 || find(":\\") != -1);
  3797. } else if ((length()) == 1) {
  3798. return (operator[](0) == '/' || operator[](0) == '\\');
  3799. } else {
  3800. return false;
  3801. }
  3802. }
  3803. String String::validate_ascii_identifier() const {
  3804. if (is_empty()) {
  3805. return "_"; // Empty string is not a valid identifier.
  3806. }
  3807. String result;
  3808. if (is_digit(operator[](0))) {
  3809. result = "_" + *this;
  3810. } else {
  3811. result = *this;
  3812. }
  3813. int len = result.length();
  3814. char32_t *buffer = result.ptrw();
  3815. for (int i = 0; i < len; i++) {
  3816. if (!is_ascii_identifier_char(buffer[i])) {
  3817. buffer[i] = '_';
  3818. }
  3819. }
  3820. return result;
  3821. }
  3822. String String::validate_unicode_identifier() const {
  3823. if (is_empty()) {
  3824. return "_"; // Empty string is not a valid identifier.
  3825. }
  3826. String result;
  3827. if (is_unicode_identifier_start(operator[](0))) {
  3828. result = *this;
  3829. } else {
  3830. result = "_" + *this;
  3831. }
  3832. int len = result.length();
  3833. char32_t *buffer = result.ptrw();
  3834. for (int i = 0; i < len; i++) {
  3835. if (!is_unicode_identifier_continue(buffer[i])) {
  3836. buffer[i] = '_';
  3837. }
  3838. }
  3839. return result;
  3840. }
  3841. bool String::is_valid_ascii_identifier() const {
  3842. int len = length();
  3843. if (len == 0) {
  3844. return false;
  3845. }
  3846. if (is_digit(operator[](0))) {
  3847. return false;
  3848. }
  3849. const char32_t *str = &operator[](0);
  3850. for (int i = 0; i < len; i++) {
  3851. if (!is_ascii_identifier_char(str[i])) {
  3852. return false;
  3853. }
  3854. }
  3855. return true;
  3856. }
  3857. bool String::is_valid_unicode_identifier() const {
  3858. const char32_t *str = ptr();
  3859. int len = length();
  3860. if (len == 0) {
  3861. return false; // Empty string.
  3862. }
  3863. if (!is_unicode_identifier_start(str[0])) {
  3864. return false;
  3865. }
  3866. for (int i = 1; i < len; i++) {
  3867. if (!is_unicode_identifier_continue(str[i])) {
  3868. return false;
  3869. }
  3870. }
  3871. return true;
  3872. }
  3873. bool String::is_valid_string() const {
  3874. int l = length();
  3875. const char32_t *src = get_data();
  3876. bool valid = true;
  3877. for (int i = 0; i < l; i++) {
  3878. valid = valid && (src[i] < 0xd800 || (src[i] > 0xdfff && src[i] <= 0x10ffff));
  3879. }
  3880. return valid;
  3881. }
  3882. String String::uri_encode() const {
  3883. const CharString temp = utf8();
  3884. String res;
  3885. for (int i = 0; i < temp.length(); ++i) {
  3886. uint8_t ord = uint8_t(temp[i]);
  3887. if (ord == '.' || ord == '-' || ord == '~' || is_ascii_identifier_char(ord)) {
  3888. res += ord;
  3889. } else {
  3890. char p[4] = { '%', 0, 0, 0 };
  3891. p[1] = hex_char_table_upper[ord >> 4];
  3892. p[2] = hex_char_table_upper[ord & 0xF];
  3893. res += p;
  3894. }
  3895. }
  3896. return res;
  3897. }
  3898. String String::uri_decode() const {
  3899. CharString src = utf8();
  3900. CharString res;
  3901. for (int i = 0; i < src.length(); ++i) {
  3902. if (src[i] == '%' && i + 2 < src.length()) {
  3903. char ord1 = src[i + 1];
  3904. if (is_digit(ord1) || is_ascii_upper_case(ord1)) {
  3905. char ord2 = src[i + 2];
  3906. if (is_digit(ord2) || is_ascii_upper_case(ord2)) {
  3907. char bytes[3] = { (char)ord1, (char)ord2, 0 };
  3908. res += (char)strtol(bytes, nullptr, 16);
  3909. i += 2;
  3910. }
  3911. } else {
  3912. res += src[i];
  3913. }
  3914. } else if (src[i] == '+') {
  3915. res += ' ';
  3916. } else {
  3917. res += src[i];
  3918. }
  3919. }
  3920. return String::utf8(res);
  3921. }
  3922. String String::uri_file_decode() const {
  3923. CharString src = utf8();
  3924. CharString res;
  3925. for (int i = 0; i < src.length(); ++i) {
  3926. if (src[i] == '%' && i + 2 < src.length()) {
  3927. char ord1 = src[i + 1];
  3928. if (is_digit(ord1) || is_ascii_upper_case(ord1)) {
  3929. char ord2 = src[i + 2];
  3930. if (is_digit(ord2) || is_ascii_upper_case(ord2)) {
  3931. char bytes[3] = { (char)ord1, (char)ord2, 0 };
  3932. res += (char)strtol(bytes, nullptr, 16);
  3933. i += 2;
  3934. }
  3935. } else {
  3936. res += src[i];
  3937. }
  3938. } else {
  3939. res += src[i];
  3940. }
  3941. }
  3942. return String::utf8(res);
  3943. }
  3944. String String::c_unescape() const {
  3945. String escaped = *this;
  3946. escaped = escaped.replace("\\a", "\a");
  3947. escaped = escaped.replace("\\b", "\b");
  3948. escaped = escaped.replace("\\f", "\f");
  3949. escaped = escaped.replace("\\n", "\n");
  3950. escaped = escaped.replace("\\r", "\r");
  3951. escaped = escaped.replace("\\t", "\t");
  3952. escaped = escaped.replace("\\v", "\v");
  3953. escaped = escaped.replace("\\'", "\'");
  3954. escaped = escaped.replace("\\\"", "\"");
  3955. escaped = escaped.replace("\\\\", "\\");
  3956. return escaped;
  3957. }
  3958. String String::c_escape() const {
  3959. String escaped = *this;
  3960. escaped = escaped.replace("\\", "\\\\");
  3961. escaped = escaped.replace("\a", "\\a");
  3962. escaped = escaped.replace("\b", "\\b");
  3963. escaped = escaped.replace("\f", "\\f");
  3964. escaped = escaped.replace("\n", "\\n");
  3965. escaped = escaped.replace("\r", "\\r");
  3966. escaped = escaped.replace("\t", "\\t");
  3967. escaped = escaped.replace("\v", "\\v");
  3968. escaped = escaped.replace("\'", "\\'");
  3969. escaped = escaped.replace("\"", "\\\"");
  3970. return escaped;
  3971. }
  3972. String String::c_escape_multiline() const {
  3973. String escaped = *this;
  3974. escaped = escaped.replace("\\", "\\\\");
  3975. escaped = escaped.replace("\"", "\\\"");
  3976. return escaped;
  3977. }
  3978. String String::json_escape() const {
  3979. String escaped = *this;
  3980. escaped = escaped.replace("\\", "\\\\");
  3981. escaped = escaped.replace("\b", "\\b");
  3982. escaped = escaped.replace("\f", "\\f");
  3983. escaped = escaped.replace("\n", "\\n");
  3984. escaped = escaped.replace("\r", "\\r");
  3985. escaped = escaped.replace("\t", "\\t");
  3986. escaped = escaped.replace("\v", "\\v");
  3987. escaped = escaped.replace("\"", "\\\"");
  3988. return escaped;
  3989. }
  3990. String String::xml_escape(bool p_escape_quotes) const {
  3991. String str = *this;
  3992. str = str.replace("&", "&amp;");
  3993. str = str.replace("<", "&lt;");
  3994. str = str.replace(">", "&gt;");
  3995. if (p_escape_quotes) {
  3996. str = str.replace("'", "&apos;");
  3997. str = str.replace("\"", "&quot;");
  3998. }
  3999. /*
  4000. for (int i=1;i<32;i++) {
  4001. char chr[2]={i,0};
  4002. str=str.replace(chr,"&#"+String::num(i)+";");
  4003. }*/
  4004. return str;
  4005. }
  4006. static _FORCE_INLINE_ int _xml_unescape(const char32_t *p_src, int p_src_len, char32_t *p_dst) {
  4007. int len = 0;
  4008. while (p_src_len) {
  4009. if (*p_src == '&') {
  4010. int eat = 0;
  4011. if (p_src_len >= 4 && p_src[1] == '#') {
  4012. char32_t c = 0;
  4013. bool overflow = false;
  4014. if (p_src[2] == 'x') {
  4015. // Hex entity &#x<num>;
  4016. for (int i = 3; i < p_src_len; i++) {
  4017. eat = i + 1;
  4018. char32_t ct = p_src[i];
  4019. if (ct == ';') {
  4020. break;
  4021. } else if (is_digit(ct)) {
  4022. ct = ct - '0';
  4023. } else if (ct >= 'a' && ct <= 'f') {
  4024. ct = (ct - 'a') + 10;
  4025. } else if (ct >= 'A' && ct <= 'F') {
  4026. ct = (ct - 'A') + 10;
  4027. } else {
  4028. break;
  4029. }
  4030. if (c > (UINT32_MAX >> 4)) {
  4031. overflow = true;
  4032. break;
  4033. }
  4034. c <<= 4;
  4035. c |= ct;
  4036. }
  4037. } else {
  4038. // Decimal entity &#<num>;
  4039. for (int i = 2; i < p_src_len; i++) {
  4040. eat = i + 1;
  4041. char32_t ct = p_src[i];
  4042. if (ct == ';' || !is_digit(ct)) {
  4043. break;
  4044. }
  4045. }
  4046. if (p_src[eat - 1] == ';') {
  4047. int64_t val = String::to_int(p_src + 2, eat - 3);
  4048. if (val > 0 && val <= UINT32_MAX) {
  4049. c = (char32_t)val;
  4050. } else {
  4051. overflow = true;
  4052. }
  4053. }
  4054. }
  4055. // Value must be non-zero, in the range of char32_t,
  4056. // actually end with ';'. If invalid, leave the entity as-is
  4057. if (c == '\0' || overflow || p_src[eat - 1] != ';') {
  4058. eat = 1;
  4059. c = *p_src;
  4060. }
  4061. if (p_dst) {
  4062. *p_dst = c;
  4063. }
  4064. } else if (p_src_len >= 4 && p_src[1] == 'g' && p_src[2] == 't' && p_src[3] == ';') {
  4065. if (p_dst) {
  4066. *p_dst = '>';
  4067. }
  4068. eat = 4;
  4069. } else if (p_src_len >= 4 && p_src[1] == 'l' && p_src[2] == 't' && p_src[3] == ';') {
  4070. if (p_dst) {
  4071. *p_dst = '<';
  4072. }
  4073. eat = 4;
  4074. } else if (p_src_len >= 5 && p_src[1] == 'a' && p_src[2] == 'm' && p_src[3] == 'p' && p_src[4] == ';') {
  4075. if (p_dst) {
  4076. *p_dst = '&';
  4077. }
  4078. eat = 5;
  4079. } else if (p_src_len >= 6 && p_src[1] == 'q' && p_src[2] == 'u' && p_src[3] == 'o' && p_src[4] == 't' && p_src[5] == ';') {
  4080. if (p_dst) {
  4081. *p_dst = '"';
  4082. }
  4083. eat = 6;
  4084. } else if (p_src_len >= 6 && p_src[1] == 'a' && p_src[2] == 'p' && p_src[3] == 'o' && p_src[4] == 's' && p_src[5] == ';') {
  4085. if (p_dst) {
  4086. *p_dst = '\'';
  4087. }
  4088. eat = 6;
  4089. } else {
  4090. if (p_dst) {
  4091. *p_dst = *p_src;
  4092. }
  4093. eat = 1;
  4094. }
  4095. if (p_dst) {
  4096. p_dst++;
  4097. }
  4098. len++;
  4099. p_src += eat;
  4100. p_src_len -= eat;
  4101. } else {
  4102. if (p_dst) {
  4103. *p_dst = *p_src;
  4104. p_dst++;
  4105. }
  4106. len++;
  4107. p_src++;
  4108. p_src_len--;
  4109. }
  4110. }
  4111. return len;
  4112. }
  4113. String String::xml_unescape() const {
  4114. String str;
  4115. int l = length();
  4116. int len = _xml_unescape(get_data(), l, nullptr);
  4117. if (len == 0) {
  4118. return String();
  4119. }
  4120. str.resize_uninitialized(len + 1);
  4121. char32_t *str_ptrw = str.ptrw();
  4122. _xml_unescape(get_data(), l, str_ptrw);
  4123. str_ptrw[len] = 0;
  4124. return str;
  4125. }
  4126. String String::pad_decimals(int p_digits) const {
  4127. String s = *this;
  4128. int c = s.find_char('.');
  4129. if (c == -1) {
  4130. if (p_digits <= 0) {
  4131. return s;
  4132. }
  4133. s += ".";
  4134. c = s.length() - 1;
  4135. } else {
  4136. if (p_digits <= 0) {
  4137. return s.substr(0, c);
  4138. }
  4139. }
  4140. if (s.length() - (c + 1) > p_digits) {
  4141. return s.substr(0, c + p_digits + 1);
  4142. } else {
  4143. int zeros_to_add = p_digits - s.length() + (c + 1);
  4144. return s + String("0").repeat(zeros_to_add);
  4145. }
  4146. }
  4147. String String::pad_zeros(int p_digits) const {
  4148. String s = *this;
  4149. int end = s.find_char('.');
  4150. if (end == -1) {
  4151. end = s.length();
  4152. }
  4153. if (end == 0) {
  4154. return s;
  4155. }
  4156. int begin = 0;
  4157. while (begin < end && !is_digit(s[begin])) {
  4158. begin++;
  4159. }
  4160. int zeros_to_add = p_digits - (end - begin);
  4161. if (zeros_to_add <= 0) {
  4162. return s;
  4163. } else {
  4164. return s.insert(begin, String("0").repeat(zeros_to_add));
  4165. }
  4166. }
  4167. String String::trim_prefix(const String &p_prefix) const {
  4168. String s = *this;
  4169. if (s.begins_with(p_prefix)) {
  4170. return s.substr(p_prefix.length());
  4171. }
  4172. return s;
  4173. }
  4174. String String::trim_prefix(const char *p_prefix) const {
  4175. String s = *this;
  4176. if (s.begins_with(p_prefix)) {
  4177. int prefix_length = strlen(p_prefix);
  4178. return s.substr(prefix_length);
  4179. }
  4180. return s;
  4181. }
  4182. String String::trim_suffix(const String &p_suffix) const {
  4183. String s = *this;
  4184. if (s.ends_with(p_suffix)) {
  4185. return s.substr(0, s.length() - p_suffix.length());
  4186. }
  4187. return s;
  4188. }
  4189. String String::trim_suffix(const char *p_suffix) const {
  4190. String s = *this;
  4191. if (s.ends_with(p_suffix)) {
  4192. return s.substr(0, s.length() - strlen(p_suffix));
  4193. }
  4194. return s;
  4195. }
  4196. bool String::is_valid_int() const {
  4197. int len = length();
  4198. if (len == 0) {
  4199. return false;
  4200. }
  4201. int from = 0;
  4202. if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
  4203. from++;
  4204. }
  4205. for (int i = from; i < len; i++) {
  4206. if (!is_digit(operator[](i))) {
  4207. return false; // no start with number plz
  4208. }
  4209. }
  4210. return true;
  4211. }
  4212. bool String::is_valid_hex_number(bool p_with_prefix) const {
  4213. int len = length();
  4214. if (len == 0) {
  4215. return false;
  4216. }
  4217. int from = 0;
  4218. if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
  4219. from++;
  4220. }
  4221. if (p_with_prefix) {
  4222. if (len < 3) {
  4223. return false;
  4224. }
  4225. if (operator[](from) != '0' || operator[](from + 1) != 'x') {
  4226. return false;
  4227. }
  4228. from += 2;
  4229. }
  4230. if (from == len) {
  4231. return false;
  4232. }
  4233. for (int i = from; i < len; i++) {
  4234. char32_t c = operator[](i);
  4235. if (is_hex_digit(c)) {
  4236. continue;
  4237. }
  4238. return false;
  4239. }
  4240. return true;
  4241. }
  4242. bool String::is_valid_float() const {
  4243. int len = length();
  4244. if (len == 0) {
  4245. return false;
  4246. }
  4247. int from = 0;
  4248. if (operator[](0) == '+' || operator[](0) == '-') {
  4249. from++;
  4250. }
  4251. bool exponent_found = false;
  4252. bool period_found = false;
  4253. bool sign_found = false;
  4254. bool exponent_values_found = false;
  4255. bool numbers_found = false;
  4256. for (int i = from; i < len; i++) {
  4257. const char32_t c = operator[](i);
  4258. if (is_digit(c)) {
  4259. if (exponent_found) {
  4260. exponent_values_found = true;
  4261. } else {
  4262. numbers_found = true;
  4263. }
  4264. } else if (numbers_found && !exponent_found && (c == 'e' || c == 'E')) {
  4265. exponent_found = true;
  4266. } else if (!period_found && !exponent_found && c == '.') {
  4267. period_found = true;
  4268. } else if ((c == '-' || c == '+') && exponent_found && !exponent_values_found && !sign_found) {
  4269. sign_found = true;
  4270. } else {
  4271. return false; // no start with number plz
  4272. }
  4273. }
  4274. return numbers_found;
  4275. }
  4276. String String::path_to_file(const String &p_path) const {
  4277. // Don't get base dir for src, this is expected to be a dir already.
  4278. String src = replace_char('\\', '/');
  4279. String dst = p_path.replace_char('\\', '/').get_base_dir();
  4280. String rel = src.path_to(dst);
  4281. if (rel == dst) { // failed
  4282. return p_path;
  4283. } else {
  4284. return rel + p_path.get_file();
  4285. }
  4286. }
  4287. String String::path_to(const String &p_path) const {
  4288. String src = replace_char('\\', '/');
  4289. String dst = p_path.replace_char('\\', '/');
  4290. if (!src.ends_with("/")) {
  4291. src += "/";
  4292. }
  4293. if (!dst.ends_with("/")) {
  4294. dst += "/";
  4295. }
  4296. if (src.begins_with("res://") && dst.begins_with("res://")) {
  4297. src = src.replace("res://", "/");
  4298. dst = dst.replace("res://", "/");
  4299. } else if (src.begins_with("user://") && dst.begins_with("user://")) {
  4300. src = src.replace("user://", "/");
  4301. dst = dst.replace("user://", "/");
  4302. } else if (src.begins_with("/") && dst.begins_with("/")) {
  4303. //nothing
  4304. } else {
  4305. //dos style
  4306. String src_begin = src.get_slicec('/', 0);
  4307. String dst_begin = dst.get_slicec('/', 0);
  4308. if (src_begin != dst_begin) {
  4309. return p_path; //impossible to do this
  4310. }
  4311. src = src.substr(src_begin.length());
  4312. dst = dst.substr(dst_begin.length());
  4313. }
  4314. //remove leading and trailing slash and split
  4315. Vector<String> src_dirs = src.substr(1, src.length() - 2).split("/");
  4316. Vector<String> dst_dirs = dst.substr(1, dst.length() - 2).split("/");
  4317. //find common parent
  4318. int common_parent = 0;
  4319. while (true) {
  4320. if (src_dirs.size() == common_parent) {
  4321. break;
  4322. }
  4323. if (dst_dirs.size() == common_parent) {
  4324. break;
  4325. }
  4326. if (src_dirs[common_parent] != dst_dirs[common_parent]) {
  4327. break;
  4328. }
  4329. common_parent++;
  4330. }
  4331. common_parent--;
  4332. int dirs_to_backtrack = (src_dirs.size() - 1) - common_parent;
  4333. String dir = String("../").repeat(dirs_to_backtrack);
  4334. for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
  4335. dir += dst_dirs[i] + "/";
  4336. }
  4337. if (dir.length() == 0) {
  4338. dir = "./";
  4339. }
  4340. return dir;
  4341. }
  4342. bool String::is_valid_html_color() const {
  4343. return Color::html_is_valid(*this);
  4344. }
  4345. // Changes made to the set of invalid filename characters must also be reflected in the String documentation for is_valid_filename.
  4346. static const char *invalid_filename_characters[] = { ":", "/", "\\", "?", "*", "\"", "|", "%", "<", ">" };
  4347. bool String::is_valid_filename() const {
  4348. String stripped = strip_edges();
  4349. if (*this != stripped) {
  4350. return false;
  4351. }
  4352. if (stripped.is_empty()) {
  4353. return false;
  4354. }
  4355. for (const char *ch : invalid_filename_characters) {
  4356. if (contains(ch)) {
  4357. return false;
  4358. }
  4359. }
  4360. return true;
  4361. }
  4362. String String::validate_filename() const {
  4363. String name = strip_edges();
  4364. for (const char *ch : invalid_filename_characters) {
  4365. name = name.replace(ch, "_");
  4366. }
  4367. return name;
  4368. }
  4369. bool String::is_valid_ip_address() const {
  4370. if (find_char(':') >= 0) {
  4371. Vector<String> ip = split(":");
  4372. for (int i = 0; i < ip.size(); i++) {
  4373. const String &n = ip[i];
  4374. if (n.is_empty()) {
  4375. continue;
  4376. }
  4377. if (n.is_valid_hex_number(false)) {
  4378. int64_t nint = n.hex_to_int();
  4379. if (nint < 0 || nint > 0xffff) {
  4380. return false;
  4381. }
  4382. continue;
  4383. }
  4384. if (!n.is_valid_ip_address()) {
  4385. return false;
  4386. }
  4387. }
  4388. } else {
  4389. Vector<String> ip = split(".");
  4390. if (ip.size() != 4) {
  4391. return false;
  4392. }
  4393. for (int i = 0; i < ip.size(); i++) {
  4394. const String &n = ip[i];
  4395. if (!n.is_valid_int()) {
  4396. return false;
  4397. }
  4398. int val = n.to_int();
  4399. if (val < 0 || val > 255) {
  4400. return false;
  4401. }
  4402. }
  4403. }
  4404. return true;
  4405. }
  4406. bool String::is_resource_file() const {
  4407. return begins_with("res://") && find("::") == -1;
  4408. }
  4409. bool String::is_relative_path() const {
  4410. return !is_absolute_path();
  4411. }
  4412. String String::get_base_dir() const {
  4413. int end = 0;
  4414. // URL scheme style base.
  4415. int basepos = find("://");
  4416. if (basepos != -1) {
  4417. end = basepos + 3;
  4418. }
  4419. // Windows top level directory base.
  4420. if (end == 0) {
  4421. basepos = find(":/");
  4422. if (basepos == -1) {
  4423. basepos = find(":\\");
  4424. }
  4425. if (basepos != -1) {
  4426. end = basepos + 2;
  4427. }
  4428. }
  4429. // Windows UNC network share path.
  4430. if (end == 0) {
  4431. if (is_network_share_path()) {
  4432. basepos = find_char('/', 2);
  4433. if (basepos == -1) {
  4434. basepos = find_char('\\', 2);
  4435. }
  4436. int servpos = find_char('/', basepos + 1);
  4437. if (servpos == -1) {
  4438. servpos = find_char('\\', basepos + 1);
  4439. }
  4440. if (servpos != -1) {
  4441. end = servpos + 1;
  4442. }
  4443. }
  4444. }
  4445. // Unix root directory base.
  4446. if (end == 0) {
  4447. if (begins_with("/")) {
  4448. end = 1;
  4449. }
  4450. }
  4451. String rs;
  4452. String base;
  4453. if (end != 0) {
  4454. rs = substr(end, length());
  4455. base = substr(0, end);
  4456. } else {
  4457. rs = *this;
  4458. }
  4459. int sep = MAX(rs.rfind_char('/'), rs.rfind_char('\\'));
  4460. if (sep == -1) {
  4461. return base;
  4462. }
  4463. return base + rs.substr(0, sep);
  4464. }
  4465. String String::get_file() const {
  4466. int sep = MAX(rfind_char('/'), rfind_char('\\'));
  4467. if (sep == -1) {
  4468. return *this;
  4469. }
  4470. return substr(sep + 1, length());
  4471. }
  4472. String String::get_extension() const {
  4473. int pos = rfind_char('.');
  4474. if (pos < 0 || pos < MAX(rfind_char('/'), rfind_char('\\'))) {
  4475. return "";
  4476. }
  4477. return substr(pos + 1, length());
  4478. }
  4479. String String::path_join(const String &p_file) const {
  4480. if (is_empty()) {
  4481. return p_file;
  4482. }
  4483. if (operator[](length() - 1) == '/' || (p_file.size() > 0 && p_file.operator[](0) == '/')) {
  4484. return *this + p_file;
  4485. }
  4486. return *this + "/" + p_file;
  4487. }
  4488. String String::property_name_encode() const {
  4489. // Escape and quote strings with extended ASCII or further Unicode characters
  4490. // as well as '"', '=' or ' ' (32)
  4491. const char32_t *cstr = get_data();
  4492. for (int i = 0; cstr[i]; i++) {
  4493. if (cstr[i] == '=' || cstr[i] == '"' || cstr[i] == ';' || cstr[i] == '[' || cstr[i] == ']' || cstr[i] < 33 || cstr[i] > 126) {
  4494. return "\"" + c_escape_multiline() + "\"";
  4495. }
  4496. }
  4497. // Keep as is
  4498. return *this;
  4499. }
  4500. // Changes made to the set of invalid characters must also be reflected in the String documentation.
  4501. static const char32_t invalid_node_name_characters[] = { '.', ':', '@', '/', '\"', UNIQUE_NODE_PREFIX[0], 0 };
  4502. String String::get_invalid_node_name_characters(bool p_allow_internal) {
  4503. // Do not use this function for critical validation.
  4504. String r;
  4505. const char32_t *c = invalid_node_name_characters;
  4506. while (*c) {
  4507. if (p_allow_internal && *c == '@') {
  4508. c++;
  4509. continue;
  4510. }
  4511. if (c != invalid_node_name_characters) {
  4512. r += " ";
  4513. }
  4514. r += String::chr(*c);
  4515. c++;
  4516. }
  4517. return r;
  4518. }
  4519. String String::validate_node_name() const {
  4520. // This is a critical validation in node addition, so it must be optimized.
  4521. const char32_t *cn = ptr();
  4522. if (cn == nullptr) {
  4523. return String();
  4524. }
  4525. bool valid = true;
  4526. uint32_t idx = 0;
  4527. while (cn[idx]) {
  4528. const char32_t *c = invalid_node_name_characters;
  4529. while (*c) {
  4530. if (cn[idx] == *c) {
  4531. valid = false;
  4532. break;
  4533. }
  4534. c++;
  4535. }
  4536. if (!valid) {
  4537. break;
  4538. }
  4539. idx++;
  4540. }
  4541. if (valid) {
  4542. return *this;
  4543. }
  4544. String validated = *this;
  4545. char32_t *nn = validated.ptrw();
  4546. while (nn[idx]) {
  4547. const char32_t *c = invalid_node_name_characters;
  4548. while (*c) {
  4549. if (nn[idx] == *c) {
  4550. nn[idx] = '_';
  4551. break;
  4552. }
  4553. c++;
  4554. }
  4555. idx++;
  4556. }
  4557. return validated;
  4558. }
  4559. String String::get_basename() const {
  4560. int pos = rfind_char('.');
  4561. if (pos < 0 || pos < MAX(rfind_char('/'), rfind_char('\\'))) {
  4562. return *this;
  4563. }
  4564. return substr(0, pos);
  4565. }
  4566. String itos(int64_t p_val) {
  4567. return String::num_int64(p_val);
  4568. }
  4569. String uitos(uint64_t p_val) {
  4570. return String::num_uint64(p_val);
  4571. }
  4572. String rtos(double p_val) {
  4573. return String::num(p_val);
  4574. }
  4575. String rtoss(double p_val) {
  4576. return String::num_scientific(p_val);
  4577. }
  4578. // Right-pad with a character.
  4579. String String::rpad(int min_length, const String &character) const {
  4580. String s = *this;
  4581. int padding = min_length - s.length();
  4582. if (padding > 0) {
  4583. s += character.repeat(padding);
  4584. }
  4585. return s;
  4586. }
  4587. // Left-pad with a character.
  4588. String String::lpad(int min_length, const String &character) const {
  4589. String s = *this;
  4590. int padding = min_length - s.length();
  4591. if (padding > 0) {
  4592. s = character.repeat(padding) + s;
  4593. }
  4594. return s;
  4595. }
  4596. // sprintf is implemented in GDScript via:
  4597. // "fish %s pie" % "frog"
  4598. // "fish %s %d pie" % ["frog", 12]
  4599. // In case of an error, the string returned is the error description and "error" is true.
  4600. String String::sprintf(const Array &values, bool *error) const {
  4601. static const String ZERO("0");
  4602. static const String SPACE(" ");
  4603. static const String MINUS("-");
  4604. static const String PLUS("+");
  4605. String formatted;
  4606. char32_t *self = (char32_t *)get_data();
  4607. bool in_format = false;
  4608. int value_index = 0;
  4609. int min_chars = 0;
  4610. int min_decimals = 0;
  4611. bool in_decimals = false;
  4612. bool pad_with_zeros = false;
  4613. bool left_justified = false;
  4614. bool show_sign = false;
  4615. bool as_unsigned = false;
  4616. if (error) {
  4617. *error = true;
  4618. }
  4619. for (; *self; self++) {
  4620. const char32_t c = *self;
  4621. if (in_format) { // We have % - let's see what else we get.
  4622. switch (c) {
  4623. case '%': { // Replace %% with %
  4624. formatted += c;
  4625. in_format = false;
  4626. break;
  4627. }
  4628. case 'd': // Integer (signed)
  4629. case 'o': // Octal
  4630. case 'x': // Hexadecimal (lowercase)
  4631. case 'X': { // Hexadecimal (uppercase)
  4632. if (value_index >= values.size()) {
  4633. return "not enough arguments for format string";
  4634. }
  4635. if (!values[value_index].is_num()) {
  4636. return "a number is required";
  4637. }
  4638. int64_t value = values[value_index];
  4639. int base = 16;
  4640. bool capitalize = false;
  4641. switch (c) {
  4642. case 'd':
  4643. base = 10;
  4644. break;
  4645. case 'o':
  4646. base = 8;
  4647. break;
  4648. case 'x':
  4649. break;
  4650. case 'X':
  4651. capitalize = true;
  4652. break;
  4653. }
  4654. // Get basic number.
  4655. String str;
  4656. if (!as_unsigned) {
  4657. str = String::num_int64(Math::abs(value), base, capitalize);
  4658. } else {
  4659. uint64_t uvalue = *((uint64_t *)&value);
  4660. // In unsigned hex, if the value fits in 32 bits, trim it down to that.
  4661. if (base == 16 && value < 0 && value >= INT32_MIN) {
  4662. uvalue &= 0xffffffff;
  4663. }
  4664. str = String::num_uint64(uvalue, base, capitalize);
  4665. }
  4666. int number_len = str.length();
  4667. bool negative = value < 0 && !as_unsigned;
  4668. // Padding.
  4669. int pad_chars_count = (negative || show_sign) ? min_chars - 1 : min_chars;
  4670. const String &pad_char = pad_with_zeros ? ZERO : SPACE;
  4671. if (left_justified) {
  4672. str = str.rpad(pad_chars_count, pad_char);
  4673. } else {
  4674. str = str.lpad(pad_chars_count, pad_char);
  4675. }
  4676. // Sign.
  4677. if (show_sign || negative) {
  4678. const String &sign_char = negative ? MINUS : PLUS;
  4679. if (left_justified) {
  4680. str = str.insert(0, sign_char);
  4681. } else {
  4682. str = str.insert(pad_with_zeros ? 0 : str.length() - number_len, sign_char);
  4683. }
  4684. }
  4685. formatted += str;
  4686. ++value_index;
  4687. in_format = false;
  4688. break;
  4689. }
  4690. case 'f': { // Float
  4691. if (value_index >= values.size()) {
  4692. return "not enough arguments for format string";
  4693. }
  4694. if (!values[value_index].is_num()) {
  4695. return "a number is required";
  4696. }
  4697. double value = values[value_index];
  4698. bool is_negative = std::signbit(value);
  4699. String str = String::num(Math::abs(value), min_decimals);
  4700. const bool is_finite = Math::is_finite(value);
  4701. // Pad decimals out.
  4702. if (is_finite) {
  4703. str = str.pad_decimals(min_decimals);
  4704. }
  4705. int initial_len = str.length();
  4706. // Padding. Leave room for sign later if required.
  4707. int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars;
  4708. const String &pad_char = (pad_with_zeros && is_finite) ? ZERO : SPACE; // Never pad NaN or inf with zeros
  4709. if (left_justified) {
  4710. str = str.rpad(pad_chars_count, pad_char);
  4711. } else {
  4712. str = str.lpad(pad_chars_count, pad_char);
  4713. }
  4714. // Add sign if needed.
  4715. if (show_sign || is_negative) {
  4716. const String &sign_char = is_negative ? MINUS : PLUS;
  4717. if (left_justified) {
  4718. str = str.insert(0, sign_char);
  4719. } else {
  4720. str = str.insert(pad_with_zeros ? 0 : str.length() - initial_len, sign_char);
  4721. }
  4722. }
  4723. formatted += str;
  4724. ++value_index;
  4725. in_format = false;
  4726. break;
  4727. }
  4728. case 'v': { // Vector2/3/4/2i/3i/4i
  4729. if (value_index >= values.size()) {
  4730. return "not enough arguments for format string";
  4731. }
  4732. int count;
  4733. switch (values[value_index].get_type()) {
  4734. case Variant::VECTOR2:
  4735. case Variant::VECTOR2I: {
  4736. count = 2;
  4737. } break;
  4738. case Variant::VECTOR3:
  4739. case Variant::VECTOR3I: {
  4740. count = 3;
  4741. } break;
  4742. case Variant::VECTOR4:
  4743. case Variant::VECTOR4I: {
  4744. count = 4;
  4745. } break;
  4746. default: {
  4747. return "%v requires a vector type (Vector2/3/4/2i/3i/4i)";
  4748. }
  4749. }
  4750. Vector4 vec = values[value_index];
  4751. String str = "(";
  4752. for (int i = 0; i < count; i++) {
  4753. double val = vec[i];
  4754. String number_str = String::num(Math::abs(val), min_decimals);
  4755. const bool is_finite = Math::is_finite(val);
  4756. // Pad decimals out.
  4757. if (is_finite) {
  4758. number_str = number_str.pad_decimals(min_decimals);
  4759. }
  4760. int initial_len = number_str.length();
  4761. // Padding. Leave room for sign later if required.
  4762. int pad_chars_count = val < 0 ? min_chars - 1 : min_chars;
  4763. const String &pad_char = (pad_with_zeros && is_finite) ? ZERO : SPACE; // Never pad NaN or inf with zeros
  4764. if (left_justified) {
  4765. number_str = number_str.rpad(pad_chars_count, pad_char);
  4766. } else {
  4767. number_str = number_str.lpad(pad_chars_count, pad_char);
  4768. }
  4769. // Add sign if needed.
  4770. if (val < 0) {
  4771. if (left_justified) {
  4772. number_str = number_str.insert(0, MINUS);
  4773. } else {
  4774. number_str = number_str.insert(pad_with_zeros ? 0 : number_str.length() - initial_len, MINUS);
  4775. }
  4776. }
  4777. // Add number to combined string
  4778. str += number_str;
  4779. if (i < count - 1) {
  4780. str += ", ";
  4781. }
  4782. }
  4783. str += ")";
  4784. formatted += str;
  4785. ++value_index;
  4786. in_format = false;
  4787. break;
  4788. }
  4789. case 's': { // String
  4790. if (value_index >= values.size()) {
  4791. return "not enough arguments for format string";
  4792. }
  4793. String str = values[value_index];
  4794. // Padding.
  4795. if (left_justified) {
  4796. str = str.rpad(min_chars);
  4797. } else {
  4798. str = str.lpad(min_chars);
  4799. }
  4800. formatted += str;
  4801. ++value_index;
  4802. in_format = false;
  4803. break;
  4804. }
  4805. case 'c': {
  4806. if (value_index >= values.size()) {
  4807. return "not enough arguments for format string";
  4808. }
  4809. // Convert to character.
  4810. String str;
  4811. if (values[value_index].is_num()) {
  4812. int value = values[value_index];
  4813. if (value < 0) {
  4814. return "unsigned integer is lower than minimum";
  4815. } else if (value >= 0xd800 && value <= 0xdfff) {
  4816. return "unsigned integer is invalid Unicode character";
  4817. } else if (value > 0x10ffff) {
  4818. return "unsigned integer is greater than maximum";
  4819. }
  4820. str = chr(values[value_index]);
  4821. } else if (values[value_index].get_type() == Variant::STRING) {
  4822. str = values[value_index];
  4823. if (str.length() != 1) {
  4824. return "%c requires number or single-character string";
  4825. }
  4826. } else {
  4827. return "%c requires number or single-character string";
  4828. }
  4829. // Padding.
  4830. if (left_justified) {
  4831. str = str.rpad(min_chars);
  4832. } else {
  4833. str = str.lpad(min_chars);
  4834. }
  4835. formatted += str;
  4836. ++value_index;
  4837. in_format = false;
  4838. break;
  4839. }
  4840. case '-': { // Left justify
  4841. left_justified = true;
  4842. break;
  4843. }
  4844. case '+': { // Show + if positive.
  4845. show_sign = true;
  4846. break;
  4847. }
  4848. case 'u': { // Treat as unsigned (for int/hex).
  4849. as_unsigned = true;
  4850. break;
  4851. }
  4852. case '0':
  4853. case '1':
  4854. case '2':
  4855. case '3':
  4856. case '4':
  4857. case '5':
  4858. case '6':
  4859. case '7':
  4860. case '8':
  4861. case '9': {
  4862. int n = c - '0';
  4863. if (in_decimals) {
  4864. min_decimals *= 10;
  4865. min_decimals += n;
  4866. } else {
  4867. if (c == '0' && min_chars == 0) {
  4868. if (left_justified) {
  4869. WARN_PRINT("'0' flag ignored with '-' flag in string format");
  4870. } else {
  4871. pad_with_zeros = true;
  4872. }
  4873. } else {
  4874. min_chars *= 10;
  4875. min_chars += n;
  4876. }
  4877. }
  4878. break;
  4879. }
  4880. case '.': { // Float/Vector separator.
  4881. if (in_decimals) {
  4882. return "too many decimal points in format";
  4883. }
  4884. in_decimals = true;
  4885. min_decimals = 0; // We want to add the value manually.
  4886. break;
  4887. }
  4888. case '*': { // Dynamic width, based on value.
  4889. if (value_index >= values.size()) {
  4890. return "not enough arguments for format string";
  4891. }
  4892. Variant::Type value_type = values[value_index].get_type();
  4893. if (!values[value_index].is_num() &&
  4894. value_type != Variant::VECTOR2 && value_type != Variant::VECTOR2I &&
  4895. value_type != Variant::VECTOR3 && value_type != Variant::VECTOR3I &&
  4896. value_type != Variant::VECTOR4 && value_type != Variant::VECTOR4I) {
  4897. return "* wants number or vector";
  4898. }
  4899. int size = values[value_index];
  4900. if (in_decimals) {
  4901. min_decimals = size;
  4902. } else {
  4903. min_chars = size;
  4904. }
  4905. ++value_index;
  4906. break;
  4907. }
  4908. default: {
  4909. return "unsupported format character";
  4910. }
  4911. }
  4912. } else { // Not in format string.
  4913. switch (c) {
  4914. case '%':
  4915. in_format = true;
  4916. // Back to defaults:
  4917. min_chars = 0;
  4918. min_decimals = 6;
  4919. pad_with_zeros = false;
  4920. left_justified = false;
  4921. show_sign = false;
  4922. in_decimals = false;
  4923. break;
  4924. default:
  4925. formatted += c;
  4926. }
  4927. }
  4928. }
  4929. if (in_format) {
  4930. return "incomplete format";
  4931. }
  4932. if (value_index != values.size()) {
  4933. return "not all arguments converted during string formatting";
  4934. }
  4935. if (error) {
  4936. *error = false;
  4937. }
  4938. return formatted;
  4939. }
  4940. String String::quote(const String &quotechar) const {
  4941. return quotechar + *this + quotechar;
  4942. }
  4943. String String::unquote() const {
  4944. if (!is_quoted()) {
  4945. return *this;
  4946. }
  4947. return substr(1, length() - 2);
  4948. }
  4949. Vector<uint8_t> String::to_ascii_buffer() const {
  4950. const String *s = this;
  4951. if (s->is_empty()) {
  4952. return Vector<uint8_t>();
  4953. }
  4954. CharString charstr = s->ascii();
  4955. Vector<uint8_t> retval;
  4956. size_t len = charstr.length();
  4957. retval.resize_uninitialized(len);
  4958. uint8_t *w = retval.ptrw();
  4959. memcpy(w, charstr.ptr(), len);
  4960. return retval;
  4961. }
  4962. Vector<uint8_t> String::to_utf8_buffer() const {
  4963. const String *s = this;
  4964. if (s->is_empty()) {
  4965. return Vector<uint8_t>();
  4966. }
  4967. CharString charstr = s->utf8();
  4968. Vector<uint8_t> retval;
  4969. size_t len = charstr.length();
  4970. retval.resize_uninitialized(len);
  4971. uint8_t *w = retval.ptrw();
  4972. memcpy(w, charstr.ptr(), len);
  4973. return retval;
  4974. }
  4975. Vector<uint8_t> String::to_utf16_buffer() const {
  4976. const String *s = this;
  4977. if (s->is_empty()) {
  4978. return Vector<uint8_t>();
  4979. }
  4980. Char16String charstr = s->utf16();
  4981. Vector<uint8_t> retval;
  4982. size_t len = charstr.length() * sizeof(char16_t);
  4983. retval.resize_uninitialized(len);
  4984. uint8_t *w = retval.ptrw();
  4985. memcpy(w, (const void *)charstr.ptr(), len);
  4986. return retval;
  4987. }
  4988. Vector<uint8_t> String::to_utf32_buffer() const {
  4989. const String *s = this;
  4990. if (s->is_empty()) {
  4991. return Vector<uint8_t>();
  4992. }
  4993. Vector<uint8_t> retval;
  4994. size_t len = s->length() * sizeof(char32_t);
  4995. retval.resize_uninitialized(len);
  4996. uint8_t *w = retval.ptrw();
  4997. memcpy(w, (const void *)s->ptr(), len);
  4998. return retval;
  4999. }
  5000. Vector<uint8_t> String::to_wchar_buffer() const {
  5001. #ifdef WINDOWS_ENABLED
  5002. return to_utf16_buffer();
  5003. #else
  5004. return to_utf32_buffer();
  5005. #endif
  5006. }
  5007. Vector<uint8_t> String::to_multibyte_char_buffer(const String &p_encoding) const {
  5008. return OS::get_singleton()->string_to_multibyte(p_encoding, *this);
  5009. }
  5010. #ifdef TOOLS_ENABLED
  5011. /**
  5012. * "Tools TRanslate". Performs string replacement for internationalization
  5013. * within the editor. A translation context can optionally be specified to
  5014. * disambiguate between identical source strings in translations. When
  5015. * placeholders are desired, use `vformat(TTR("Example: %s"), some_string)`.
  5016. * If a string mentions a quantity (and may therefore need a dynamic plural form),
  5017. * use `TTRN()` instead of `TTR()`.
  5018. *
  5019. * NOTE: Only use `TTR()` in editor-only code (typically within the `editor/` folder).
  5020. * For translations that can be supplied by exported projects, use `RTR()` instead.
  5021. */
  5022. String TTR(const String &p_text, const String &p_context) {
  5023. if (TranslationServer::get_singleton()) {
  5024. return TranslationServer::get_singleton()->tool_translate(p_text, p_context);
  5025. }
  5026. return p_text;
  5027. }
  5028. /**
  5029. * "Tools TRanslate for N items". Performs string replacement for
  5030. * internationalization within the editor. A translation context can optionally
  5031. * be specified to disambiguate between identical source strings in
  5032. * translations. Use `TTR()` if the string doesn't need dynamic plural form.
  5033. * When placeholders are desired, use
  5034. * `vformat(TTRN("%d item", "%d items", some_integer), some_integer)`.
  5035. * The placeholder must be present in both strings to avoid run-time warnings in `vformat()`.
  5036. *
  5037. * NOTE: Only use `TTRN()` in editor-only code (typically within the `editor/` folder).
  5038. * For translations that can be supplied by exported projects, use `RTRN()` instead.
  5039. */
  5040. String TTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
  5041. if (TranslationServer::get_singleton()) {
  5042. return TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
  5043. }
  5044. // Return message based on English plural rule if translation is not possible.
  5045. if (p_n == 1) {
  5046. return p_text;
  5047. }
  5048. return p_text_plural;
  5049. }
  5050. /**
  5051. * "Docs TRanslate". Used for the editor class reference documentation,
  5052. * handling descriptions extracted from the XML.
  5053. * It also replaces `$DOCS_URL` with the actual URL to the documentation's branch,
  5054. * to allow dehardcoding it in the XML and doing proper substitutions everywhere.
  5055. */
  5056. String DTR(const String &p_text, const String &p_context) {
  5057. // Comes straight from the XML, so remove indentation and any trailing whitespace.
  5058. const String text = p_text.dedent().strip_edges();
  5059. if (TranslationServer::get_singleton()) {
  5060. return String(TranslationServer::get_singleton()->doc_translate(text, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
  5061. }
  5062. return text.replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
  5063. }
  5064. /**
  5065. * "Docs TRanslate for N items". Used for the editor class reference documentation
  5066. * (with support for plurals), handling descriptions extracted from the XML.
  5067. * It also replaces `$DOCS_URL` with the actual URL to the documentation's branch,
  5068. * to allow dehardcoding it in the XML and doing proper substitutions everywhere.
  5069. */
  5070. String DTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
  5071. const String text = p_text.dedent().strip_edges();
  5072. const String text_plural = p_text_plural.dedent().strip_edges();
  5073. if (TranslationServer::get_singleton()) {
  5074. return String(TranslationServer::get_singleton()->doc_translate_plural(text, text_plural, p_n, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
  5075. }
  5076. // Return message based on English plural rule if translation is not possible.
  5077. if (p_n == 1) {
  5078. return text.replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
  5079. }
  5080. return text_plural.replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
  5081. }
  5082. #endif
  5083. /**
  5084. * "Run-time TRanslate". Performs string replacement for internationalization
  5085. * without the editor. A translation context can optionally be specified to
  5086. * disambiguate between identical source strings in translations. When
  5087. * placeholders are desired, use `vformat(RTR("Example: %s"), some_string)`.
  5088. * If a string mentions a quantity (and may therefore need a dynamic plural form),
  5089. * use `RTRN()` instead of `RTR()`.
  5090. *
  5091. * NOTE: Do not use `RTR()` in editor-only code (typically within the `editor/`
  5092. * folder). For editor translations, use `TTR()` instead.
  5093. */
  5094. String RTR(const String &p_text, const String &p_context) {
  5095. if (TranslationServer::get_singleton()) {
  5096. String rtr = TranslationServer::get_singleton()->tool_translate(p_text, p_context);
  5097. if (rtr.is_empty() || rtr == p_text) {
  5098. return TranslationServer::get_singleton()->translate(p_text, p_context);
  5099. }
  5100. return rtr;
  5101. }
  5102. return p_text;
  5103. }
  5104. /**
  5105. * "Run-time TRanslate for N items". Performs string replacement for
  5106. * internationalization without the editor. A translation context can optionally
  5107. * be specified to disambiguate between identical source strings in translations.
  5108. * Use `RTR()` if the string doesn't need dynamic plural form. When placeholders
  5109. * are desired, use `vformat(RTRN("%d item", "%d items", some_integer), some_integer)`.
  5110. * The placeholder must be present in both strings to avoid run-time warnings in `vformat()`.
  5111. *
  5112. * NOTE: Do not use `RTRN()` in editor-only code (typically within the `editor/`
  5113. * folder). For editor translations, use `TTRN()` instead.
  5114. */
  5115. String RTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
  5116. if (TranslationServer::get_singleton()) {
  5117. String rtr = TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
  5118. if (rtr.is_empty() || rtr == p_text || rtr == p_text_plural) {
  5119. return TranslationServer::get_singleton()->translate_plural(p_text, p_text_plural, p_n, p_context);
  5120. }
  5121. return rtr;
  5122. }
  5123. // Return message based on English plural rule if translation is not possible.
  5124. if (p_n == 1) {
  5125. return p_text;
  5126. }
  5127. return p_text_plural;
  5128. }