ustring.cpp 139 KB

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