ustring.cpp 139 KB

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