ustring.cpp 139 KB

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