ustring.cpp 137 KB

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