12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915 |
- /**************************************************************************/
- /* ustring.cpp */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #include "ustring.h"
- #include "core/crypto/crypto_core.h"
- #include "core/math/color.h"
- #include "core/math/math_funcs.h"
- #include "core/os/memory.h"
- #include "core/string/print_string.h"
- #include "core/string/string_name.h"
- #include "core/string/translation.h"
- #include "core/string/ucaps.h"
- #include "core/variant/variant.h"
- #include "core/version_generated.gen.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <cstdint>
- #ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS // to disable build-time warning which suggested to use strcpy_s instead strcpy
- #endif
- #if defined(MINGW_ENABLED) || defined(_MSC_VER)
- #define snprintf _snprintf_s
- #endif
- static const int MAX_DECIMALS = 32;
- static _FORCE_INLINE_ char32_t lower_case(char32_t c) {
- return (is_ascii_upper_case(c) ? (c + ('a' - 'A')) : c);
- }
- const char CharString::_null = 0;
- const char16_t Char16String::_null = 0;
- const char32_t String::_null = 0;
- const char32_t String::_replacement_char = 0xfffd;
- bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end) {
- const String &s = p_s;
- int beg = CLAMP(p_col, 0, s.length());
- int end = beg;
- if (s[beg] > 32 || beg == s.length()) {
- bool symbol = beg < s.length() && is_symbol(s[beg]);
- while (beg > 0 && s[beg - 1] > 32 && (symbol == is_symbol(s[beg - 1]))) {
- beg--;
- }
- while (end < s.length() && s[end + 1] > 32 && (symbol == is_symbol(s[end + 1]))) {
- end++;
- }
- if (end < s.length()) {
- end += 1;
- }
- r_beg = beg;
- r_end = end;
- return true;
- } else {
- return false;
- }
- }
- /*************************************************************************/
- /* Char16String */
- /*************************************************************************/
- bool Char16String::operator<(const Char16String &p_right) const {
- if (length() == 0) {
- return p_right.length() != 0;
- }
- return is_str_less(get_data(), p_right.get_data());
- }
- Char16String &Char16String::operator+=(char16_t p_char) {
- const int lhs_len = length();
- resize(lhs_len + 2);
- char16_t *dst = ptrw();
- dst[lhs_len] = p_char;
- dst[lhs_len + 1] = 0;
- return *this;
- }
- void Char16String::operator=(const char16_t *p_cstr) {
- copy_from(p_cstr);
- }
- const char16_t *Char16String::get_data() const {
- if (size()) {
- return &operator[](0);
- } else {
- return u"";
- }
- }
- void Char16String::copy_from(const char16_t *p_cstr) {
- if (!p_cstr) {
- resize(0);
- return;
- }
- const char16_t *s = p_cstr;
- for (; *s; s++) {
- }
- size_t len = s - p_cstr;
- if (len == 0) {
- resize(0);
- return;
- }
- Error err = resize(++len); // include terminating null char
- ERR_FAIL_COND_MSG(err != OK, "Failed to copy char16_t string.");
- memcpy(ptrw(), p_cstr, len * sizeof(char16_t));
- }
- /*************************************************************************/
- /* CharString */
- /*************************************************************************/
- bool CharString::operator<(const CharString &p_right) const {
- if (length() == 0) {
- return p_right.length() != 0;
- }
- return is_str_less(get_data(), p_right.get_data());
- }
- bool CharString::operator==(const CharString &p_right) const {
- if (length() == 0) {
- // True if both have length 0, false if only p_right has a length
- return p_right.length() == 0;
- } else if (p_right.length() == 0) {
- // False due to unequal length
- return false;
- }
- return strcmp(ptr(), p_right.ptr()) == 0;
- }
- CharString &CharString::operator+=(char p_char) {
- const int lhs_len = length();
- resize(lhs_len + 2);
- char *dst = ptrw();
- dst[lhs_len] = p_char;
- dst[lhs_len + 1] = 0;
- return *this;
- }
- void CharString::operator=(const char *p_cstr) {
- copy_from(p_cstr);
- }
- const char *CharString::get_data() const {
- if (size()) {
- return &operator[](0);
- } else {
- return "";
- }
- }
- void CharString::copy_from(const char *p_cstr) {
- if (!p_cstr) {
- resize(0);
- return;
- }
- size_t len = strlen(p_cstr);
- if (len == 0) {
- resize(0);
- return;
- }
- Error err = resize(++len); // include terminating null char
- ERR_FAIL_COND_MSG(err != OK, "Failed to copy C-string.");
- memcpy(ptrw(), p_cstr, len);
- }
- /*************************************************************************/
- /* String */
- /*************************************************************************/
- Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const {
- // Splits the URL into scheme, host, port, path. Strip credentials when present.
- String base = *this;
- r_scheme = "";
- r_host = "";
- r_port = 0;
- r_path = "";
- int pos = base.find("://");
- // Scheme
- if (pos != -1) {
- r_scheme = base.substr(0, pos + 3).to_lower();
- base = base.substr(pos + 3, base.length() - pos - 3);
- }
- pos = base.find("/");
- // Path
- if (pos != -1) {
- r_path = base.substr(pos, base.length() - pos);
- base = base.substr(0, pos);
- }
- // Host
- pos = base.find("@");
- if (pos != -1) {
- // Strip credentials
- base = base.substr(pos + 1, base.length() - pos - 1);
- }
- if (base.begins_with("[")) {
- // Literal IPv6
- pos = base.rfind("]");
- if (pos == -1) {
- return ERR_INVALID_PARAMETER;
- }
- r_host = base.substr(1, pos - 1);
- base = base.substr(pos + 1, base.length() - pos - 1);
- } else {
- // Anything else
- if (base.get_slice_count(":") > 2) {
- return ERR_INVALID_PARAMETER;
- }
- pos = base.rfind(":");
- if (pos == -1) {
- r_host = base;
- base = "";
- } else {
- r_host = base.substr(0, pos);
- base = base.substr(pos, base.length() - pos);
- }
- }
- if (r_host.is_empty()) {
- return ERR_INVALID_PARAMETER;
- }
- r_host = r_host.to_lower();
- // Port
- if (base.begins_with(":")) {
- base = base.substr(1, base.length() - 1);
- if (!base.is_valid_int()) {
- return ERR_INVALID_PARAMETER;
- }
- r_port = base.to_int();
- if (r_port < 1 || r_port > 65535) {
- return ERR_INVALID_PARAMETER;
- }
- }
- return OK;
- }
- void String::copy_from(const char *p_cstr) {
- // copy Latin-1 encoded c-string directly
- if (!p_cstr) {
- resize(0);
- return;
- }
- const size_t len = strlen(p_cstr);
- if (len == 0) {
- resize(0);
- return;
- }
- resize(len + 1); // include 0
- char32_t *dst = ptrw();
- for (size_t i = 0; i <= len; i++) {
- #if CHAR_MIN == 0
- uint8_t c = p_cstr[i];
- #else
- uint8_t c = p_cstr[i] >= 0 ? p_cstr[i] : uint8_t(256 + p_cstr[i]);
- #endif
- if (c == 0 && i < len) {
- print_unicode_error("NUL character", true);
- dst[i] = _replacement_char;
- } else {
- dst[i] = c;
- }
- }
- }
- void String::copy_from(const char *p_cstr, const int p_clip_to) {
- // copy Latin-1 encoded c-string directly
- if (!p_cstr) {
- resize(0);
- return;
- }
- int len = 0;
- const char *ptr = p_cstr;
- while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0) {
- len++;
- }
- if (len == 0) {
- resize(0);
- return;
- }
- resize(len + 1); // include 0
- char32_t *dst = ptrw();
- for (int i = 0; i < len; i++) {
- #if CHAR_MIN == 0
- uint8_t c = p_cstr[i];
- #else
- uint8_t c = p_cstr[i] >= 0 ? p_cstr[i] : uint8_t(256 + p_cstr[i]);
- #endif
- if (c == 0) {
- print_unicode_error("NUL character", true);
- dst[i] = _replacement_char;
- } else {
- dst[i] = c;
- }
- }
- dst[len] = 0;
- }
- void String::copy_from(const wchar_t *p_cstr) {
- #ifdef WINDOWS_ENABLED
- // wchar_t is 16-bit, parse as UTF-16
- parse_utf16((const char16_t *)p_cstr);
- #else
- // wchar_t is 32-bit, copy directly
- copy_from((const char32_t *)p_cstr);
- #endif
- }
- void String::copy_from(const wchar_t *p_cstr, const int p_clip_to) {
- #ifdef WINDOWS_ENABLED
- // wchar_t is 16-bit, parse as UTF-16
- parse_utf16((const char16_t *)p_cstr, p_clip_to);
- #else
- // wchar_t is 32-bit, copy directly
- copy_from((const char32_t *)p_cstr, p_clip_to);
- #endif
- }
- void String::copy_from(const char32_t &p_char) {
- if (p_char == 0) {
- print_unicode_error("NUL character", true);
- return;
- }
- resize(2);
- char32_t *dst = ptrw();
- if ((p_char & 0xfffff800) == 0xd800) {
- print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)p_char));
- dst[0] = _replacement_char;
- } else if (p_char > 0x10ffff) {
- print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)p_char));
- dst[0] = _replacement_char;
- } else {
- dst[0] = p_char;
- }
- dst[1] = 0;
- }
- void String::copy_from(const char32_t *p_cstr) {
- if (!p_cstr) {
- resize(0);
- return;
- }
- int len = 0;
- const char32_t *ptr = p_cstr;
- while (*(ptr++) != 0) {
- len++;
- }
- if (len == 0) {
- resize(0);
- return;
- }
- copy_from_unchecked(p_cstr, len);
- }
- void String::copy_from(const char32_t *p_cstr, const int p_clip_to) {
- if (!p_cstr) {
- resize(0);
- return;
- }
- int len = 0;
- const char32_t *ptr = p_cstr;
- while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0) {
- len++;
- }
- if (len == 0) {
- resize(0);
- return;
- }
- copy_from_unchecked(p_cstr, len);
- }
- // assumes the following have already been validated:
- // p_char != nullptr
- // p_length > 0
- // p_length <= p_char strlen
- void String::copy_from_unchecked(const char32_t *p_char, const int p_length) {
- resize(p_length + 1);
- char32_t *dst = ptrw();
- dst[p_length] = 0;
- for (int i = 0; i < p_length; i++) {
- if (p_char[i] == 0) {
- print_unicode_error("NUL character", true);
- dst[i] = _replacement_char;
- continue;
- }
- if ((p_char[i] & 0xfffff800) == 0xd800) {
- print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)p_char[i]));
- dst[i] = _replacement_char;
- continue;
- }
- if (p_char[i] > 0x10ffff) {
- print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)p_char[i]));
- dst[i] = _replacement_char;
- continue;
- }
- dst[i] = p_char[i];
- }
- }
- void String::operator=(const char *p_str) {
- copy_from(p_str);
- }
- void String::operator=(const char32_t *p_str) {
- copy_from(p_str);
- }
- void String::operator=(const wchar_t *p_str) {
- copy_from(p_str);
- }
- String String::operator+(const String &p_str) const {
- String res = *this;
- res += p_str;
- return res;
- }
- String String::operator+(char32_t p_char) const {
- String res = *this;
- res += p_char;
- return res;
- }
- String operator+(const char *p_chr, const String &p_str) {
- String tmp = p_chr;
- tmp += p_str;
- return tmp;
- }
- String operator+(const wchar_t *p_chr, const String &p_str) {
- #ifdef WINDOWS_ENABLED
- // wchar_t is 16-bit
- String tmp = String::utf16((const char16_t *)p_chr);
- #else
- // wchar_t is 32-bit
- String tmp = (const char32_t *)p_chr;
- #endif
- tmp += p_str;
- return tmp;
- }
- String operator+(char32_t p_chr, const String &p_str) {
- return (String::chr(p_chr) + p_str);
- }
- String &String::operator+=(const String &p_str) {
- const int lhs_len = length();
- if (lhs_len == 0) {
- *this = p_str;
- return *this;
- }
- const int rhs_len = p_str.length();
- if (rhs_len == 0) {
- return *this;
- }
- resize(lhs_len + rhs_len + 1);
- const char32_t *src = p_str.ptr();
- char32_t *dst = ptrw() + lhs_len;
- // Don't copy the terminating null with `memcpy` to avoid undefined behavior when string is being added to itself (it would overlap the destination).
- memcpy(dst, src, rhs_len * sizeof(char32_t));
- *(dst + rhs_len) = _null;
- return *this;
- }
- String &String::operator+=(const char *p_str) {
- if (!p_str || p_str[0] == 0) {
- return *this;
- }
- const int lhs_len = length();
- const size_t rhs_len = strlen(p_str);
- resize(lhs_len + rhs_len + 1);
- char32_t *dst = ptrw() + lhs_len;
- for (size_t i = 0; i <= rhs_len; i++) {
- #if CHAR_MIN == 0
- uint8_t c = p_str[i];
- #else
- uint8_t c = p_str[i] >= 0 ? p_str[i] : uint8_t(256 + p_str[i]);
- #endif
- if (c == 0 && i < rhs_len) {
- print_unicode_error("NUL character", true);
- dst[i] = _replacement_char;
- } else {
- dst[i] = c;
- }
- }
- return *this;
- }
- String &String::operator+=(const wchar_t *p_str) {
- #ifdef WINDOWS_ENABLED
- // wchar_t is 16-bit
- *this += String::utf16((const char16_t *)p_str);
- #else
- // wchar_t is 32-bit
- *this += String((const char32_t *)p_str);
- #endif
- return *this;
- }
- String &String::operator+=(const char32_t *p_str) {
- *this += String(p_str);
- return *this;
- }
- String &String::operator+=(char32_t p_char) {
- if (p_char == 0) {
- print_unicode_error("NUL character", true);
- return *this;
- }
- const int lhs_len = length();
- resize(lhs_len + 2);
- char32_t *dst = ptrw();
- if ((p_char & 0xfffff800) == 0xd800) {
- print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)p_char));
- dst[lhs_len] = _replacement_char;
- } else if (p_char > 0x10ffff) {
- print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)p_char));
- dst[lhs_len] = _replacement_char;
- } else {
- dst[lhs_len] = p_char;
- }
- dst[lhs_len + 1] = 0;
- return *this;
- }
- bool String::operator==(const char *p_str) const {
- // compare Latin-1 encoded c-string
- int len = 0;
- const char *aux = p_str;
- while (*(aux++) != 0) {
- len++;
- }
- if (length() != len) {
- return false;
- }
- if (is_empty()) {
- return true;
- }
- int l = length();
- const char32_t *dst = get_data();
- // Compare char by char
- for (int i = 0; i < l; i++) {
- if ((char32_t)p_str[i] != dst[i]) {
- return false;
- }
- }
- return true;
- }
- bool String::operator==(const wchar_t *p_str) const {
- #ifdef WINDOWS_ENABLED
- // wchar_t is 16-bit, parse as UTF-16
- return *this == String::utf16((const char16_t *)p_str);
- #else
- // wchar_t is 32-bit, compare char by char
- return *this == (const char32_t *)p_str;
- #endif
- }
- bool String::operator==(const char32_t *p_str) const {
- int len = 0;
- const char32_t *aux = p_str;
- while (*(aux++) != 0) {
- len++;
- }
- if (length() != len) {
- return false;
- }
- if (is_empty()) {
- return true;
- }
- int l = length();
- const char32_t *dst = get_data();
- /* Compare char by char */
- for (int i = 0; i < l; i++) {
- if (p_str[i] != dst[i]) {
- return false;
- }
- }
- return true;
- }
- bool String::operator==(const String &p_str) const {
- if (length() != p_str.length()) {
- return false;
- }
- if (is_empty()) {
- return true;
- }
- int l = length();
- const char32_t *src = get_data();
- const char32_t *dst = p_str.get_data();
- /* Compare char by char */
- for (int i = 0; i < l; i++) {
- if (src[i] != dst[i]) {
- return false;
- }
- }
- return true;
- }
- bool String::operator==(const StrRange &p_str_range) const {
- int len = p_str_range.len;
- if (length() != len) {
- return false;
- }
- if (is_empty()) {
- return true;
- }
- const char32_t *c_str = p_str_range.c_str;
- const char32_t *dst = &operator[](0);
- /* Compare char by char */
- for (int i = 0; i < len; i++) {
- if (c_str[i] != dst[i]) {
- return false;
- }
- }
- return true;
- }
- bool operator==(const char *p_chr, const String &p_str) {
- return p_str == p_chr;
- }
- bool operator==(const wchar_t *p_chr, const String &p_str) {
- #ifdef WINDOWS_ENABLED
- // wchar_t is 16-bit
- return p_str == String::utf16((const char16_t *)p_chr);
- #else
- // wchar_t is 32-bi
- return p_str == String((const char32_t *)p_chr);
- #endif
- }
- bool operator!=(const char *p_chr, const String &p_str) {
- return !(p_str == p_chr);
- }
- bool operator!=(const wchar_t *p_chr, const String &p_str) {
- #ifdef WINDOWS_ENABLED
- // wchar_t is 16-bit
- return !(p_str == String::utf16((const char16_t *)p_chr));
- #else
- // wchar_t is 32-bi
- return !(p_str == String((const char32_t *)p_chr));
- #endif
- }
- bool String::operator!=(const char *p_str) const {
- return (!(*this == p_str));
- }
- bool String::operator!=(const wchar_t *p_str) const {
- return (!(*this == p_str));
- }
- bool String::operator!=(const char32_t *p_str) const {
- return (!(*this == p_str));
- }
- bool String::operator!=(const String &p_str) const {
- return !((*this == p_str));
- }
- bool String::operator<=(const String &p_str) const {
- return !(p_str < *this);
- }
- bool String::operator>(const String &p_str) const {
- return p_str < *this;
- }
- bool String::operator>=(const String &p_str) const {
- return !(*this < p_str);
- }
- bool String::operator<(const char *p_str) const {
- if (is_empty() && p_str[0] == 0) {
- return false;
- }
- if (is_empty()) {
- return true;
- }
- return is_str_less(get_data(), p_str);
- }
- bool String::operator<(const wchar_t *p_str) const {
- if (is_empty() && p_str[0] == 0) {
- return false;
- }
- if (is_empty()) {
- return true;
- }
- #ifdef WINDOWS_ENABLED
- // wchar_t is 16-bit
- return is_str_less(get_data(), String::utf16((const char16_t *)p_str).get_data());
- #else
- // wchar_t is 32-bit
- return is_str_less(get_data(), (const char32_t *)p_str);
- #endif
- }
- bool String::operator<(const char32_t *p_str) const {
- if (is_empty() && p_str[0] == 0) {
- return false;
- }
- if (is_empty()) {
- return true;
- }
- return is_str_less(get_data(), p_str);
- }
- bool String::operator<(const String &p_str) const {
- return operator<(p_str.get_data());
- }
- signed char String::nocasecmp_to(const String &p_str) const {
- if (is_empty() && p_str.is_empty()) {
- return 0;
- }
- if (is_empty()) {
- return -1;
- }
- if (p_str.is_empty()) {
- return 1;
- }
- const char32_t *that_str = p_str.get_data();
- const char32_t *this_str = get_data();
- while (true) {
- if (*that_str == 0 && *this_str == 0) { // If both strings are at the end, they are equal.
- return 0;
- } else if (*this_str == 0) { // If at the end of this, and not of other, we are less.
- return -1;
- } else if (*that_str == 0) { // If at end of other, and not of this, we are greater.
- return 1;
- } else if (_find_upper(*this_str) < _find_upper(*that_str)) { // If current character in this is less, we are less.
- return -1;
- } else if (_find_upper(*this_str) > _find_upper(*that_str)) { // If current character in this is greater, we are greater.
- return 1;
- }
- this_str++;
- that_str++;
- }
- }
- signed char String::casecmp_to(const String &p_str) const {
- if (is_empty() && p_str.is_empty()) {
- return 0;
- }
- if (is_empty()) {
- return -1;
- }
- if (p_str.is_empty()) {
- return 1;
- }
- const char32_t *that_str = p_str.get_data();
- const char32_t *this_str = get_data();
- while (true) {
- if (*that_str == 0 && *this_str == 0) { // If both strings are at the end, they are equal.
- return 0;
- } else if (*this_str == 0) { // If at the end of this, and not of other, we are less.
- return -1;
- } else if (*that_str == 0) { // If at end of other, and not of this, we are greater.
- return 1;
- } else if (*this_str < *that_str) { // If current character in this is less, we are less.
- return -1;
- } else if (*this_str > *that_str) { // If current character in this is greater, we are greater.
- return 1;
- }
- this_str++;
- that_str++;
- }
- }
- static _FORCE_INLINE_ signed char natural_cmp_common(const char32_t *&r_this_str, const char32_t *&r_that_str) {
- // Keep ptrs to start of numerical sequences.
- const char32_t *this_substr = r_this_str;
- const char32_t *that_substr = r_that_str;
- // Compare lengths of both numerical sequences, ignoring leading zeros.
- while (is_digit(*r_this_str)) {
- r_this_str++;
- }
- while (is_digit(*r_that_str)) {
- r_that_str++;
- }
- while (*this_substr == '0') {
- this_substr++;
- }
- while (*that_substr == '0') {
- that_substr++;
- }
- int this_len = r_this_str - this_substr;
- int that_len = r_that_str - that_substr;
- if (this_len < that_len) {
- return -1;
- } else if (this_len > that_len) {
- return 1;
- }
- // If lengths equal, compare lexicographically.
- while (this_substr != r_this_str && that_substr != r_that_str) {
- if (*this_substr < *that_substr) {
- return -1;
- } else if (*this_substr > *that_substr) {
- return 1;
- }
- this_substr++;
- that_substr++;
- }
- return 0;
- }
- static _FORCE_INLINE_ signed char naturalcasecmp_to_base(const char32_t *p_this_str, const char32_t *p_that_str) {
- if (p_this_str && p_that_str) {
- while (*p_this_str == '.' || *p_that_str == '.') {
- if (*p_this_str++ != '.') {
- return 1;
- }
- if (*p_that_str++ != '.') {
- return -1;
- }
- if (!*p_that_str) {
- return 1;
- }
- if (!*p_this_str) {
- return -1;
- }
- }
- while (*p_this_str) {
- if (!*p_that_str) {
- return 1;
- } else if (is_digit(*p_this_str)) {
- if (!is_digit(*p_that_str)) {
- return -1;
- }
- signed char ret = natural_cmp_common(p_this_str, p_that_str);
- if (ret) {
- return ret;
- }
- } else if (is_digit(*p_that_str)) {
- return 1;
- } else {
- if (*p_this_str < *p_that_str) { // If current character in this is less, we are less.
- return -1;
- } else if (*p_this_str > *p_that_str) { // If current character in this is greater, we are greater.
- return 1;
- }
- p_this_str++;
- p_that_str++;
- }
- }
- if (*p_that_str) {
- return -1;
- }
- }
- return 0;
- }
- signed char String::naturalcasecmp_to(const String &p_str) const {
- const char32_t *this_str = get_data();
- const char32_t *that_str = p_str.get_data();
- return naturalcasecmp_to_base(this_str, that_str);
- }
- static _FORCE_INLINE_ signed char naturalnocasecmp_to_base(const char32_t *p_this_str, const char32_t *p_that_str) {
- if (p_this_str && p_that_str) {
- while (*p_this_str == '.' || *p_that_str == '.') {
- if (*p_this_str++ != '.') {
- return 1;
- }
- if (*p_that_str++ != '.') {
- return -1;
- }
- if (!*p_that_str) {
- return 1;
- }
- if (!*p_this_str) {
- return -1;
- }
- }
- while (*p_this_str) {
- if (!*p_that_str) {
- return 1;
- } else if (is_digit(*p_this_str)) {
- if (!is_digit(*p_that_str)) {
- return -1;
- }
- signed char ret = natural_cmp_common(p_this_str, p_that_str);
- if (ret) {
- return ret;
- }
- } else if (is_digit(*p_that_str)) {
- return 1;
- } else {
- if (_find_upper(*p_this_str) < _find_upper(*p_that_str)) { // If current character in this is less, we are less.
- return -1;
- } else if (_find_upper(*p_this_str) > _find_upper(*p_that_str)) { // If current character in this is greater, we are greater.
- return 1;
- }
- p_this_str++;
- p_that_str++;
- }
- }
- if (*p_that_str) {
- return -1;
- }
- }
- return 0;
- }
- signed char String::naturalnocasecmp_to(const String &p_str) const {
- const char32_t *this_str = get_data();
- const char32_t *that_str = p_str.get_data();
- return naturalnocasecmp_to_base(this_str, that_str);
- }
- static _FORCE_INLINE_ signed char file_cmp_common(const char32_t *&r_this_str, const char32_t *&r_that_str) {
- // Compare leading `_` sequences.
- while ((*r_this_str == '_' && *r_that_str) || (*r_this_str && *r_that_str == '_')) {
- // Sort `_` lower than everything except `.`
- if (*r_this_str != '_') {
- return *r_this_str == '.' ? -1 : 1;
- } else if (*r_that_str != '_') {
- return *r_that_str == '.' ? 1 : -1;
- }
- r_this_str++;
- r_that_str++;
- }
- return 0;
- }
- signed char String::filecasecmp_to(const String &p_str) const {
- const char32_t *this_str = get_data();
- const char32_t *that_str = p_str.get_data();
- signed char ret = file_cmp_common(this_str, that_str);
- if (ret) {
- return ret;
- }
- return naturalcasecmp_to_base(this_str, that_str);
- }
- signed char String::filenocasecmp_to(const String &p_str) const {
- const char32_t *this_str = get_data();
- const char32_t *that_str = p_str.get_data();
- signed char ret = file_cmp_common(this_str, that_str);
- if (ret) {
- return ret;
- }
- return naturalnocasecmp_to_base(this_str, that_str);
- }
- const char32_t *String::get_data() const {
- static const char32_t zero = 0;
- return size() ? &operator[](0) : &zero;
- }
- String String::_camelcase_to_underscore() const {
- const char32_t *cstr = get_data();
- String new_string;
- int start_index = 0;
- for (int i = 1; i < size(); i++) {
- bool is_prev_upper = is_unicode_upper_case(cstr[i - 1]);
- bool is_prev_lower = is_unicode_lower_case(cstr[i - 1]);
- bool is_prev_digit = is_digit(cstr[i - 1]);
- bool is_curr_upper = is_unicode_upper_case(cstr[i]);
- bool is_curr_lower = is_unicode_lower_case(cstr[i]);
- bool is_curr_digit = is_digit(cstr[i]);
- bool is_next_lower = false;
- if (i + 1 < size()) {
- is_next_lower = is_unicode_lower_case(cstr[i + 1]);
- }
- const bool cond_a = is_prev_lower && is_curr_upper; // aA
- const bool cond_b = (is_prev_upper || is_prev_digit) && is_curr_upper && is_next_lower; // AAa, 2Aa
- const bool cond_c = is_prev_digit && is_curr_lower && is_next_lower; // 2aa
- const bool cond_d = (is_prev_upper || is_prev_lower) && is_curr_digit; // A2, a2
- if (cond_a || cond_b || cond_c || cond_d) {
- new_string += substr(start_index, i - start_index) + "_";
- start_index = i;
- }
- }
- new_string += substr(start_index, size() - start_index);
- return new_string.to_lower();
- }
- String String::capitalize() const {
- String aux = _camelcase_to_underscore().replace("_", " ").strip_edges();
- String cap;
- for (int i = 0; i < aux.get_slice_count(" "); i++) {
- String slice = aux.get_slicec(' ', i);
- if (slice.length() > 0) {
- slice[0] = _find_upper(slice[0]);
- if (i > 0) {
- cap += " ";
- }
- cap += slice;
- }
- }
- return cap;
- }
- String String::to_camel_case() const {
- String s = to_pascal_case();
- if (!s.is_empty()) {
- s[0] = _find_lower(s[0]);
- }
- return s;
- }
- String String::to_pascal_case() const {
- return capitalize().replace(" ", "");
- }
- String String::to_snake_case() const {
- return _camelcase_to_underscore().replace(" ", "_").strip_edges();
- }
- String String::get_with_code_lines() const {
- const Vector<String> lines = split("\n");
- String ret;
- for (int i = 0; i < lines.size(); i++) {
- if (i > 0) {
- ret += "\n";
- }
- ret += vformat("%4d | %s", i + 1, lines[i]);
- }
- return ret;
- }
- int String::get_slice_count(const String &p_splitter) const {
- if (is_empty()) {
- return 0;
- }
- if (p_splitter.is_empty()) {
- return 0;
- }
- int pos = 0;
- int slices = 1;
- while ((pos = find(p_splitter, pos)) >= 0) {
- slices++;
- pos += p_splitter.length();
- }
- return slices;
- }
- int String::get_slice_count(const char *p_splitter) const {
- if (is_empty()) {
- return 0;
- }
- if (p_splitter == nullptr || *p_splitter == '\0') {
- return 0;
- }
- int pos = 0;
- int slices = 1;
- int splitter_length = strlen(p_splitter);
- while ((pos = find(p_splitter, pos)) >= 0) {
- slices++;
- pos += splitter_length;
- }
- return slices;
- }
- String String::get_slice(const String &p_splitter, int p_slice) const {
- if (is_empty() || p_splitter.is_empty()) {
- return "";
- }
- int pos = 0;
- int prev_pos = 0;
- //int slices=1;
- if (p_slice < 0) {
- return "";
- }
- if (find(p_splitter) == -1) {
- return *this;
- }
- int i = 0;
- while (true) {
- pos = find(p_splitter, pos);
- if (pos == -1) {
- pos = length(); //reached end
- }
- int from = prev_pos;
- //int to=pos;
- if (p_slice == i) {
- return substr(from, pos - from);
- }
- if (pos == length()) { //reached end and no find
- break;
- }
- pos += p_splitter.length();
- prev_pos = pos;
- i++;
- }
- return ""; //no find!
- }
- String String::get_slice(const char *p_splitter, int p_slice) const {
- if (is_empty() || p_splitter == nullptr || *p_splitter == '\0') {
- return "";
- }
- int pos = 0;
- int prev_pos = 0;
- //int slices=1;
- if (p_slice < 0) {
- return "";
- }
- if (find(p_splitter) == -1) {
- return *this;
- }
- int i = 0;
- int splitter_length = strlen(p_splitter);
- while (true) {
- pos = find(p_splitter, pos);
- if (pos == -1) {
- pos = length(); //reached end
- }
- int from = prev_pos;
- //int to=pos;
- if (p_slice == i) {
- return substr(from, pos - from);
- }
- if (pos == length()) { //reached end and no find
- break;
- }
- pos += splitter_length;
- prev_pos = pos;
- i++;
- }
- return ""; //no find!
- }
- String String::get_slicec(char32_t p_splitter, int p_slice) const {
- if (is_empty()) {
- return String();
- }
- if (p_slice < 0) {
- return String();
- }
- const char32_t *c = ptr();
- int i = 0;
- int prev = 0;
- int count = 0;
- while (true) {
- if (c[i] == 0 || c[i] == p_splitter) {
- if (p_slice == count) {
- return substr(prev, i - prev);
- } else if (c[i] == 0) {
- return String();
- } else {
- count++;
- prev = i + 1;
- }
- }
- i++;
- }
- }
- Vector<String> String::split_spaces() const {
- Vector<String> ret;
- int from = 0;
- int i = 0;
- int len = length();
- if (len == 0) {
- return ret;
- }
- bool inside = false;
- while (true) {
- bool empty = operator[](i) < 33;
- if (i == 0) {
- inside = !empty;
- }
- if (!empty && !inside) {
- inside = true;
- from = i;
- }
- if (empty && inside) {
- ret.push_back(substr(from, i - from));
- inside = false;
- }
- if (i == len) {
- break;
- }
- i++;
- }
- return ret;
- }
- Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
- Vector<String> ret;
- if (is_empty()) {
- if (p_allow_empty) {
- ret.push_back("");
- }
- return ret;
- }
- int from = 0;
- int len = length();
- while (true) {
- int end;
- if (p_splitter.is_empty()) {
- end = from + 1;
- } else {
- end = find(p_splitter, from);
- if (end < 0) {
- end = len;
- }
- }
- if (p_allow_empty || (end > from)) {
- if (p_maxsplit <= 0) {
- ret.push_back(substr(from, end - from));
- } else {
- // Put rest of the string and leave cycle.
- if (p_maxsplit == ret.size()) {
- ret.push_back(substr(from, len));
- break;
- }
- // Otherwise, push items until positive limit is reached.
- ret.push_back(substr(from, end - from));
- }
- }
- if (end == len) {
- break;
- }
- from = end + p_splitter.length();
- }
- return ret;
- }
- Vector<String> String::split(const char *p_splitter, bool p_allow_empty, int p_maxsplit) const {
- Vector<String> ret;
- if (is_empty()) {
- if (p_allow_empty) {
- ret.push_back("");
- }
- return ret;
- }
- int from = 0;
- int len = length();
- while (true) {
- int end;
- if (p_splitter == nullptr || *p_splitter == '\0') {
- end = from + 1;
- } else {
- end = find(p_splitter, from);
- if (end < 0) {
- end = len;
- }
- }
- if (p_allow_empty || (end > from)) {
- if (p_maxsplit <= 0) {
- ret.push_back(substr(from, end - from));
- } else {
- // Put rest of the string and leave cycle.
- if (p_maxsplit == ret.size()) {
- ret.push_back(substr(from, len));
- break;
- }
- // Otherwise, push items until positive limit is reached.
- ret.push_back(substr(from, end - from));
- }
- }
- if (end == len) {
- break;
- }
- from = end + strlen(p_splitter);
- }
- return ret;
- }
- Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
- Vector<String> ret;
- const int len = length();
- int remaining_len = len;
- while (true) {
- if (remaining_len < p_splitter.length() || (p_maxsplit > 0 && p_maxsplit == ret.size())) {
- // no room for another splitter or hit max splits, push what's left and we're done
- if (p_allow_empty || remaining_len > 0) {
- ret.push_back(substr(0, remaining_len));
- }
- break;
- }
- int left_edge;
- if (p_splitter.is_empty()) {
- left_edge = remaining_len - 1;
- if (left_edge == 0) {
- left_edge--; // Skip to the < 0 condition.
- }
- } else {
- left_edge = rfind(p_splitter, remaining_len - p_splitter.length());
- }
- if (left_edge < 0) {
- // no more splitters, we're done
- ret.push_back(substr(0, remaining_len));
- break;
- }
- int substr_start = left_edge + p_splitter.length();
- if (p_allow_empty || substr_start < remaining_len) {
- ret.push_back(substr(substr_start, remaining_len - substr_start));
- }
- remaining_len = left_edge;
- }
- ret.reverse();
- return ret;
- }
- Vector<String> String::rsplit(const char *p_splitter, bool p_allow_empty, int p_maxsplit) const {
- Vector<String> ret;
- const int len = length();
- const int splitter_length = strlen(p_splitter);
- int remaining_len = len;
- while (true) {
- if (remaining_len < splitter_length || (p_maxsplit > 0 && p_maxsplit == ret.size())) {
- // no room for another splitter or hit max splits, push what's left and we're done
- if (p_allow_empty || remaining_len > 0) {
- ret.push_back(substr(0, remaining_len));
- }
- break;
- }
- int left_edge;
- if (p_splitter == nullptr || *p_splitter == '\0') {
- left_edge = remaining_len - 1;
- if (left_edge == 0) {
- left_edge--; // Skip to the < 0 condition.
- }
- } else {
- left_edge = rfind(p_splitter, remaining_len - splitter_length);
- }
- if (left_edge < 0) {
- // no more splitters, we're done
- ret.push_back(substr(0, remaining_len));
- break;
- }
- int substr_start = left_edge + splitter_length;
- if (p_allow_empty || substr_start < remaining_len) {
- ret.push_back(substr(substr_start, remaining_len - substr_start));
- }
- remaining_len = left_edge;
- }
- ret.reverse();
- return ret;
- }
- Vector<double> String::split_floats(const String &p_splitter, bool p_allow_empty) const {
- Vector<double> ret;
- int from = 0;
- int len = length();
- while (true) {
- int end = find(p_splitter, from);
- if (end < 0) {
- end = len;
- }
- if (p_allow_empty || (end > from)) {
- ret.push_back(String::to_float(&get_data()[from]));
- }
- if (end == len) {
- break;
- }
- from = end + p_splitter.length();
- }
- return ret;
- }
- Vector<float> String::split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
- Vector<float> ret;
- int from = 0;
- int len = length();
- while (true) {
- int idx;
- int end = findmk(p_splitters, from, &idx);
- int spl_len = 1;
- if (end < 0) {
- end = len;
- } else {
- spl_len = p_splitters[idx].length();
- }
- if (p_allow_empty || (end > from)) {
- ret.push_back(String::to_float(&get_data()[from]));
- }
- if (end == len) {
- break;
- }
- from = end + spl_len;
- }
- return ret;
- }
- Vector<int> String::split_ints(const String &p_splitter, bool p_allow_empty) const {
- Vector<int> ret;
- int from = 0;
- int len = length();
- while (true) {
- int end = find(p_splitter, from);
- if (end < 0) {
- end = len;
- }
- if (p_allow_empty || (end > from)) {
- ret.push_back(String::to_int(&get_data()[from], end - from));
- }
- if (end == len) {
- break;
- }
- from = end + p_splitter.length();
- }
- return ret;
- }
- Vector<int> String::split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
- Vector<int> ret;
- int from = 0;
- int len = length();
- while (true) {
- int idx;
- int end = findmk(p_splitters, from, &idx);
- int spl_len = 1;
- if (end < 0) {
- end = len;
- } else {
- spl_len = p_splitters[idx].length();
- }
- if (p_allow_empty || (end > from)) {
- ret.push_back(String::to_int(&get_data()[from], end - from));
- }
- if (end == len) {
- break;
- }
- from = end + spl_len;
- }
- return ret;
- }
- String String::join(const Vector<String> &parts) const {
- String ret;
- for (int i = 0; i < parts.size(); ++i) {
- if (i > 0) {
- ret += *this;
- }
- ret += parts[i];
- }
- return ret;
- }
- char32_t String::char_uppercase(char32_t p_char) {
- return _find_upper(p_char);
- }
- char32_t String::char_lowercase(char32_t p_char) {
- return _find_lower(p_char);
- }
- String String::to_upper() const {
- String upper = *this;
- for (int i = 0; i < upper.size(); i++) {
- const char32_t s = upper[i];
- const char32_t t = _find_upper(s);
- if (s != t) { // avoid copy on write
- upper[i] = t;
- }
- }
- return upper;
- }
- String String::to_lower() const {
- String lower = *this;
- for (int i = 0; i < lower.size(); i++) {
- const char32_t s = lower[i];
- const char32_t t = _find_lower(s);
- if (s != t) { // avoid copy on write
- lower[i] = t;
- }
- }
- return lower;
- }
- String String::chr(char32_t p_char) {
- char32_t c[2] = { p_char, 0 };
- return String(c);
- }
- String String::num(double p_num, int p_decimals) {
- if (Math::is_nan(p_num)) {
- return "nan";
- }
- if (Math::is_inf(p_num)) {
- if (signbit(p_num)) {
- return "-inf";
- } else {
- return "inf";
- }
- }
- if (p_decimals < 0) {
- p_decimals = 14;
- const double abs_num = Math::abs(p_num);
- if (abs_num > 10) {
- // We want to align the digits to the above reasonable default, so we only
- // need to subtract log10 for numbers with a positive power of ten.
- p_decimals -= (int)floor(log10(abs_num));
- }
- }
- if (p_decimals > MAX_DECIMALS) {
- p_decimals = MAX_DECIMALS;
- }
- char fmt[7];
- fmt[0] = '%';
- fmt[1] = '.';
- if (p_decimals < 0) {
- fmt[1] = 'l';
- fmt[2] = 'f';
- fmt[3] = 0;
- } else if (p_decimals < 10) {
- fmt[2] = '0' + p_decimals;
- fmt[3] = 'l';
- fmt[4] = 'f';
- fmt[5] = 0;
- } else {
- fmt[2] = '0' + (p_decimals / 10);
- fmt[3] = '0' + (p_decimals % 10);
- fmt[4] = 'l';
- fmt[5] = 'f';
- fmt[6] = 0;
- }
- // if we want to convert a double with as much decimal places as
- // DBL_MAX or DBL_MIN then we would theoretically need a buffer of at least
- // DBL_MAX_10_EXP + 2 for DBL_MAX and DBL_MAX_10_EXP + 4 for DBL_MIN.
- // BUT those values where still giving me exceptions, so I tested from
- // DBL_MAX_10_EXP + 10 incrementing one by one and DBL_MAX_10_EXP + 17 (325)
- // was the first buffer size not to throw an exception
- char buf[325];
- #if defined(__GNUC__) || defined(_MSC_VER)
- // PLEASE NOTE that, albeit vcrt online reference states that snprintf
- // should safely truncate the output to the given buffer size, we have
- // found a case where this is not true, so we should create a buffer
- // as big as needed
- snprintf(buf, 325, fmt, p_num);
- #else
- sprintf(buf, fmt, p_num);
- #endif
- buf[324] = 0;
- //destroy trailing zeroes
- {
- bool period = false;
- int z = 0;
- while (buf[z]) {
- if (buf[z] == '.') {
- period = true;
- }
- z++;
- }
- if (period) {
- z--;
- while (z > 0) {
- if (buf[z] == '0') {
- buf[z] = 0;
- } else if (buf[z] == '.') {
- buf[z] = 0;
- break;
- } else {
- break;
- }
- z--;
- }
- }
- }
- return buf;
- }
- String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
- bool sign = p_num < 0;
- int64_t n = p_num;
- int chars = 0;
- do {
- n /= base;
- chars++;
- } while (n);
- if (sign) {
- chars++;
- }
- String s;
- s.resize(chars + 1);
- char32_t *c = s.ptrw();
- c[chars] = 0;
- n = p_num;
- do {
- int mod = ABS(n % base);
- if (mod >= 10) {
- char a = (capitalize_hex ? 'A' : 'a');
- c[--chars] = a + (mod - 10);
- } else {
- c[--chars] = '0' + mod;
- }
- n /= base;
- } while (n);
- if (sign) {
- c[0] = '-';
- }
- return s;
- }
- String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
- uint64_t n = p_num;
- int chars = 0;
- do {
- n /= base;
- chars++;
- } while (n);
- String s;
- s.resize(chars + 1);
- char32_t *c = s.ptrw();
- c[chars] = 0;
- n = p_num;
- do {
- int mod = n % base;
- if (mod >= 10) {
- char a = (capitalize_hex ? 'A' : 'a');
- c[--chars] = a + (mod - 10);
- } else {
- c[--chars] = '0' + mod;
- }
- n /= base;
- } while (n);
- return s;
- }
- String String::num_real(double p_num, bool p_trailing) {
- if (p_num == (double)(int64_t)p_num) {
- if (p_trailing) {
- return num_int64((int64_t)p_num) + ".0";
- } else {
- return num_int64((int64_t)p_num);
- }
- }
- #ifdef REAL_T_IS_DOUBLE
- int decimals = 14;
- #else
- int decimals = 6;
- #endif
- // We want to align the digits to the above sane default, so we only need
- // to subtract log10 for numbers with a positive power of ten magnitude.
- double abs_num = Math::abs(p_num);
- if (abs_num > 10) {
- decimals -= (int)floor(log10(abs_num));
- }
- return num(p_num, decimals);
- }
- String String::num_scientific(double p_num) {
- if (Math::is_nan(p_num)) {
- return "nan";
- }
- if (Math::is_inf(p_num)) {
- if (signbit(p_num)) {
- return "-inf";
- } else {
- return "inf";
- }
- }
- char buf[256];
- #if defined(__GNUC__) || defined(_MSC_VER)
- #if defined(__MINGW32__) && defined(_TWO_DIGIT_EXPONENT) && !defined(_UCRT)
- // MinGW requires _set_output_format() to conform to C99 output for printf
- unsigned int old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
- #endif
- snprintf(buf, 256, "%lg", p_num);
- #if defined(__MINGW32__) && defined(_TWO_DIGIT_EXPONENT) && !defined(_UCRT)
- _set_output_format(old_exponent_format);
- #endif
- #else
- sprintf(buf, "%.16lg", p_num);
- #endif
- buf[255] = 0;
- return buf;
- }
- String String::md5(const uint8_t *p_md5) {
- return String::hex_encode_buffer(p_md5, 16);
- }
- String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
- static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
- String ret;
- char v[2] = { 0, 0 };
- for (int i = 0; i < p_len; i++) {
- v[0] = hex[p_buffer[i] >> 4];
- ret += v;
- v[0] = hex[p_buffer[i] & 0xF];
- ret += v;
- }
- return ret;
- }
- Vector<uint8_t> String::hex_decode() const {
- ERR_FAIL_COND_V_MSG(length() % 2 != 0, Vector<uint8_t>(), "Hexadecimal string of uneven length.");
- #define HEX_TO_BYTE(m_output, m_index) \
- uint8_t m_output; \
- c = operator[](m_index); \
- if (is_digit(c)) { \
- m_output = c - '0'; \
- } else if (c >= 'a' && c <= 'f') { \
- m_output = c - 'a' + 10; \
- } else if (c >= 'A' && c <= 'F') { \
- m_output = c - 'A' + 10; \
- } else { \
- ERR_FAIL_V_MSG(Vector<uint8_t>(), "Invalid hexadecimal character \"" + chr(c) + "\" at index " + m_index + "."); \
- }
- Vector<uint8_t> out;
- int len = length() / 2;
- out.resize(len);
- for (int i = 0; i < len; i++) {
- char32_t c;
- HEX_TO_BYTE(first, i * 2);
- HEX_TO_BYTE(second, i * 2 + 1);
- out.write[i] = first * 16 + second;
- }
- return out;
- #undef HEX_TO_BYTE
- }
- void String::print_unicode_error(const String &p_message, bool p_critical) const {
- if (p_critical) {
- print_error(vformat(U"Unicode parsing error, some characters were replaced with � (U+FFFD): %s", p_message));
- } else {
- print_error(vformat("Unicode parsing error: %s", p_message));
- }
- }
- CharString String::ascii(bool p_allow_extended) const {
- if (!length()) {
- return CharString();
- }
- CharString cs;
- cs.resize(size());
- for (int i = 0; i < size(); i++) {
- char32_t c = operator[](i);
- if ((c <= 0x7f) || (c <= 0xff && p_allow_extended)) {
- cs[i] = c;
- } else {
- print_unicode_error(vformat("Invalid unicode codepoint (%x), cannot represent as ASCII/Latin-1", (uint32_t)c));
- cs[i] = 0x20; // ascii doesn't have a replacement character like unicode, 0x1a is sometimes used but is kinda arcane
- }
- }
- return cs;
- }
- String String::utf8(const char *p_utf8, int p_len) {
- String ret;
- ret.parse_utf8(p_utf8, p_len);
- return ret;
- }
- Error String::parse_utf8(const char *p_utf8, int p_len, bool p_skip_cr) {
- if (!p_utf8) {
- return ERR_INVALID_DATA;
- }
- String aux;
- int cstr_size = 0;
- int str_size = 0;
- /* HANDLE BOM (Byte Order Mark) */
- if (p_len < 0 || p_len >= 3) {
- bool has_bom = uint8_t(p_utf8[0]) == 0xef && uint8_t(p_utf8[1]) == 0xbb && uint8_t(p_utf8[2]) == 0xbf;
- if (has_bom) {
- //8-bit encoding, byte order has no meaning in UTF-8, just skip it
- if (p_len >= 0) {
- p_len -= 3;
- }
- p_utf8 += 3;
- }
- }
- bool decode_error = false;
- bool decode_failed = false;
- {
- const char *ptrtmp = p_utf8;
- const char *ptrtmp_limit = p_len >= 0 ? &p_utf8[p_len] : nullptr;
- int skip = 0;
- uint8_t c_start = 0;
- while (ptrtmp != ptrtmp_limit && *ptrtmp) {
- #if CHAR_MIN == 0
- uint8_t c = *ptrtmp;
- #else
- uint8_t c = *ptrtmp >= 0 ? *ptrtmp : uint8_t(256 + *ptrtmp);
- #endif
- if (skip == 0) {
- if (p_skip_cr && c == '\r') {
- ptrtmp++;
- continue;
- }
- /* Determine the number of characters in sequence */
- if ((c & 0x80) == 0) {
- skip = 0;
- } else if ((c & 0xe0) == 0xc0) {
- skip = 1;
- } else if ((c & 0xf0) == 0xe0) {
- skip = 2;
- } else if ((c & 0xf8) == 0xf0) {
- skip = 3;
- } else if ((c & 0xfc) == 0xf8) {
- skip = 4;
- } else if ((c & 0xfe) == 0xfc) {
- skip = 5;
- } else {
- skip = 0;
- print_unicode_error(vformat("Invalid UTF-8 leading byte (%x)", c), true);
- decode_failed = true;
- }
- c_start = c;
- if (skip == 1 && (c & 0x1e) == 0) {
- print_unicode_error(vformat("Overlong encoding (%x ...)", c));
- decode_error = true;
- }
- str_size++;
- } else {
- if ((c_start == 0xe0 && skip == 2 && c < 0xa0) || (c_start == 0xf0 && skip == 3 && c < 0x90) || (c_start == 0xf8 && skip == 4 && c < 0x88) || (c_start == 0xfc && skip == 5 && c < 0x84)) {
- print_unicode_error(vformat("Overlong encoding (%x %x ...)", c_start, c));
- decode_error = true;
- }
- if (c < 0x80 || c > 0xbf) {
- print_unicode_error(vformat("Invalid UTF-8 continuation byte (%x ... %x ...)", c_start, c), true);
- decode_failed = true;
- skip = 0;
- } else {
- --skip;
- }
- }
- cstr_size++;
- ptrtmp++;
- }
- if (skip) {
- print_unicode_error(vformat("Missing %d UTF-8 continuation byte(s)", skip), true);
- decode_failed = true;
- }
- }
- if (str_size == 0) {
- clear();
- return OK; // empty string
- }
- resize(str_size + 1);
- char32_t *dst = ptrw();
- dst[str_size] = 0;
- int skip = 0;
- uint32_t unichar = 0;
- while (cstr_size) {
- #if CHAR_MIN == 0
- uint8_t c = *p_utf8;
- #else
- uint8_t c = *p_utf8 >= 0 ? *p_utf8 : uint8_t(256 + *p_utf8);
- #endif
- if (skip == 0) {
- if (p_skip_cr && c == '\r') {
- p_utf8++;
- continue;
- }
- /* Determine the number of characters in sequence */
- if ((c & 0x80) == 0) {
- *(dst++) = c;
- unichar = 0;
- skip = 0;
- } else if ((c & 0xe0) == 0xc0) {
- unichar = (0xff >> 3) & c;
- skip = 1;
- } else if ((c & 0xf0) == 0xe0) {
- unichar = (0xff >> 4) & c;
- skip = 2;
- } else if ((c & 0xf8) == 0xf0) {
- unichar = (0xff >> 5) & c;
- skip = 3;
- } else if ((c & 0xfc) == 0xf8) {
- unichar = (0xff >> 6) & c;
- skip = 4;
- } else if ((c & 0xfe) == 0xfc) {
- unichar = (0xff >> 7) & c;
- skip = 5;
- } else {
- *(dst++) = _replacement_char;
- unichar = 0;
- skip = 0;
- }
- } else {
- if (c < 0x80 || c > 0xbf) {
- *(dst++) = _replacement_char;
- skip = 0;
- } else {
- unichar = (unichar << 6) | (c & 0x3f);
- --skip;
- if (skip == 0) {
- if (unichar == 0) {
- print_unicode_error("NUL character", true);
- decode_failed = true;
- unichar = _replacement_char;
- } else if ((unichar & 0xfffff800) == 0xd800) {
- print_unicode_error(vformat("Unpaired surrogate (%x)", unichar), true);
- decode_failed = true;
- unichar = _replacement_char;
- } else if (unichar > 0x10ffff) {
- print_unicode_error(vformat("Invalid unicode codepoint (%x)", unichar), true);
- decode_failed = true;
- unichar = _replacement_char;
- }
- *(dst++) = unichar;
- }
- }
- }
- cstr_size--;
- p_utf8++;
- }
- if (skip) {
- *(dst++) = 0x20;
- }
- if (decode_failed) {
- return ERR_INVALID_DATA;
- } else if (decode_error) {
- return ERR_PARSE_ERROR;
- } else {
- return OK;
- }
- }
- CharString String::utf8() const {
- int l = length();
- if (!l) {
- return CharString();
- }
- const char32_t *d = &operator[](0);
- int fl = 0;
- for (int i = 0; i < l; i++) {
- uint32_t c = d[i];
- if (c <= 0x7f) { // 7 bits.
- fl += 1;
- } else if (c <= 0x7ff) { // 11 bits
- fl += 2;
- } else if (c <= 0xffff) { // 16 bits
- fl += 3;
- } else if (c <= 0x001fffff) { // 21 bits
- fl += 4;
- } else if (c <= 0x03ffffff) { // 26 bits
- fl += 5;
- print_unicode_error(vformat("Invalid unicode codepoint (%x)", c));
- } else if (c <= 0x7fffffff) { // 31 bits
- fl += 6;
- print_unicode_error(vformat("Invalid unicode codepoint (%x)", c));
- } else {
- fl += 1;
- print_unicode_error(vformat("Invalid unicode codepoint (%x), cannot represent as UTF-8", c), true);
- }
- }
- CharString utf8s;
- if (fl == 0) {
- return utf8s;
- }
- utf8s.resize(fl + 1);
- uint8_t *cdst = (uint8_t *)utf8s.get_data();
- #define APPEND_CHAR(m_c) *(cdst++) = m_c
- for (int i = 0; i < l; i++) {
- uint32_t c = d[i];
- if (c <= 0x7f) { // 7 bits.
- APPEND_CHAR(c);
- } else if (c <= 0x7ff) { // 11 bits
- APPEND_CHAR(uint32_t(0xc0 | ((c >> 6) & 0x1f))); // Top 5 bits.
- APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
- } else if (c <= 0xffff) { // 16 bits
- APPEND_CHAR(uint32_t(0xe0 | ((c >> 12) & 0x0f))); // Top 4 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
- } else if (c <= 0x001fffff) { // 21 bits
- APPEND_CHAR(uint32_t(0xf0 | ((c >> 18) & 0x07))); // Top 3 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
- } else if (c <= 0x03ffffff) { // 26 bits
- APPEND_CHAR(uint32_t(0xf8 | ((c >> 24) & 0x03))); // Top 2 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Upper middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
- } else if (c <= 0x7fffffff) { // 31 bits
- APPEND_CHAR(uint32_t(0xfc | ((c >> 30) & 0x01))); // Top 1 bit.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 24) & 0x3f))); // Upper upper middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Lower upper middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper lower middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower lower middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
- } else {
- // the string is a valid UTF32, so it should never happen ...
- print_unicode_error(vformat("Non scalar value (%x)", c), true);
- APPEND_CHAR(uint32_t(0xe0 | ((_replacement_char >> 12) & 0x0f))); // Top 4 bits.
- APPEND_CHAR(uint32_t(0x80 | ((_replacement_char >> 6) & 0x3f))); // Middle 6 bits.
- APPEND_CHAR(uint32_t(0x80 | (_replacement_char & 0x3f))); // Bottom 6 bits.
- }
- }
- #undef APPEND_CHAR
- *cdst = 0; //trailing zero
- return utf8s;
- }
- String String::utf16(const char16_t *p_utf16, int p_len) {
- String ret;
- ret.parse_utf16(p_utf16, p_len, true);
- return ret;
- }
- Error String::parse_utf16(const char16_t *p_utf16, int p_len, bool p_default_little_endian) {
- if (!p_utf16) {
- return ERR_INVALID_DATA;
- }
- String aux;
- int cstr_size = 0;
- int str_size = 0;
- #ifdef BIG_ENDIAN_ENABLED
- bool byteswap = p_default_little_endian;
- #else
- bool byteswap = !p_default_little_endian;
- #endif
- /* HANDLE BOM (Byte Order Mark) */
- if (p_len < 0 || p_len >= 1) {
- bool has_bom = false;
- if (uint16_t(p_utf16[0]) == 0xfeff) { // correct BOM, read as is
- has_bom = true;
- byteswap = false;
- } else if (uint16_t(p_utf16[0]) == 0xfffe) { // backwards BOM, swap bytes
- has_bom = true;
- byteswap = true;
- }
- if (has_bom) {
- if (p_len >= 0) {
- p_len -= 1;
- }
- p_utf16 += 1;
- }
- }
- bool decode_error = false;
- {
- const char16_t *ptrtmp = p_utf16;
- const char16_t *ptrtmp_limit = p_len >= 0 ? &p_utf16[p_len] : nullptr;
- uint32_t c_prev = 0;
- bool skip = false;
- while (ptrtmp != ptrtmp_limit && *ptrtmp) {
- uint32_t c = (byteswap) ? BSWAP16(*ptrtmp) : *ptrtmp;
- if ((c & 0xfffffc00) == 0xd800) { // lead surrogate
- if (skip) {
- print_unicode_error(vformat("Unpaired lead surrogate (%x [trail?] %x)", c_prev, c));
- decode_error = true;
- }
- skip = true;
- } else if ((c & 0xfffffc00) == 0xdc00) { // trail surrogate
- if (skip) {
- str_size--;
- } else {
- print_unicode_error(vformat("Unpaired trail surrogate (%x [lead?] %x)", c_prev, c));
- decode_error = true;
- }
- skip = false;
- } else {
- skip = false;
- }
- c_prev = c;
- str_size++;
- cstr_size++;
- ptrtmp++;
- }
- if (skip) {
- print_unicode_error(vformat("Unpaired lead surrogate (%x [eol])", c_prev));
- decode_error = true;
- }
- }
- if (str_size == 0) {
- clear();
- return OK; // empty string
- }
- resize(str_size + 1);
- char32_t *dst = ptrw();
- dst[str_size] = 0;
- bool skip = false;
- uint32_t c_prev = 0;
- while (cstr_size) {
- uint32_t c = (byteswap) ? BSWAP16(*p_utf16) : *p_utf16;
- if ((c & 0xfffffc00) == 0xd800) { // lead surrogate
- if (skip) {
- *(dst++) = c_prev; // unpaired, store as is
- }
- skip = true;
- } else if ((c & 0xfffffc00) == 0xdc00) { // trail surrogate
- if (skip) {
- *(dst++) = (c_prev << 10UL) + c - ((0xd800 << 10UL) + 0xdc00 - 0x10000); // decode pair
- } else {
- *(dst++) = c; // unpaired, store as is
- }
- skip = false;
- } else {
- *(dst++) = c;
- skip = false;
- }
- cstr_size--;
- p_utf16++;
- c_prev = c;
- }
- if (skip) {
- *(dst++) = c_prev;
- }
- if (decode_error) {
- return ERR_PARSE_ERROR;
- } else {
- return OK;
- }
- }
- Char16String String::utf16() const {
- int l = length();
- if (!l) {
- return Char16String();
- }
- const char32_t *d = &operator[](0);
- int fl = 0;
- for (int i = 0; i < l; i++) {
- uint32_t c = d[i];
- if (c <= 0xffff) { // 16 bits.
- fl += 1;
- if ((c & 0xfffff800) == 0xd800) {
- print_unicode_error(vformat("Unpaired surrogate (%x)", c));
- }
- } else if (c <= 0x10ffff) { // 32 bits.
- fl += 2;
- } else {
- print_unicode_error(vformat("Invalid unicode codepoint (%x), cannot represent as UTF-16", c), true);
- fl += 1;
- }
- }
- Char16String utf16s;
- if (fl == 0) {
- return utf16s;
- }
- utf16s.resize(fl + 1);
- uint16_t *cdst = (uint16_t *)utf16s.get_data();
- #define APPEND_CHAR(m_c) *(cdst++) = m_c
- for (int i = 0; i < l; i++) {
- uint32_t c = d[i];
- if (c <= 0xffff) { // 16 bits.
- APPEND_CHAR(c);
- } else if (c <= 0x10ffff) { // 32 bits.
- APPEND_CHAR(uint32_t((c >> 10) + 0xd7c0)); // lead surrogate.
- APPEND_CHAR(uint32_t((c & 0x3ff) | 0xdc00)); // trail surrogate.
- } else {
- // the string is a valid UTF32, so it should never happen ...
- APPEND_CHAR(uint32_t((_replacement_char >> 10) + 0xd7c0));
- APPEND_CHAR(uint32_t((_replacement_char & 0x3ff) | 0xdc00));
- }
- }
- #undef APPEND_CHAR
- *cdst = 0; //trailing zero
- return utf16s;
- }
- String::String(const char *p_str) {
- copy_from(p_str);
- }
- String::String(const wchar_t *p_str) {
- copy_from(p_str);
- }
- String::String(const char32_t *p_str) {
- copy_from(p_str);
- }
- String::String(const char *p_str, int p_clip_to_len) {
- copy_from(p_str, p_clip_to_len);
- }
- String::String(const wchar_t *p_str, int p_clip_to_len) {
- copy_from(p_str, p_clip_to_len);
- }
- String::String(const char32_t *p_str, int p_clip_to_len) {
- copy_from(p_str, p_clip_to_len);
- }
- String::String(const StrRange &p_range) {
- if (!p_range.c_str) {
- return;
- }
- copy_from(p_range.c_str, p_range.len);
- }
- int64_t String::hex_to_int() const {
- int len = length();
- if (len == 0) {
- return 0;
- }
- const char32_t *s = ptr();
- int64_t sign = s[0] == '-' ? -1 : 1;
- if (sign < 0) {
- s++;
- }
- if (len > 2 && s[0] == '0' && lower_case(s[1]) == 'x') {
- s += 2;
- }
- int64_t hex = 0;
- while (*s) {
- char32_t c = lower_case(*s);
- int64_t n;
- if (is_digit(c)) {
- n = c - '0';
- } else if (c >= 'a' && c <= 'f') {
- n = (c - 'a') + 10;
- } else {
- ERR_FAIL_V_MSG(0, vformat(R"(Invalid hexadecimal notation character "%c" (U+%04X) in string "%s".)", *s, static_cast<int32_t>(*s), *this));
- }
- // Check for overflow/underflow, with special case to ensure INT64_MIN does not result in error
- bool overflow = ((hex > INT64_MAX / 16) && (sign == 1 || (sign == -1 && hex != (INT64_MAX >> 4) + 1))) || (sign == -1 && hex == (INT64_MAX >> 4) + 1 && c > '0');
- 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."));
- hex *= 16;
- hex += n;
- s++;
- }
- return hex * sign;
- }
- int64_t String::bin_to_int() const {
- int len = length();
- if (len == 0) {
- return 0;
- }
- const char32_t *s = ptr();
- int64_t sign = s[0] == '-' ? -1 : 1;
- if (sign < 0) {
- s++;
- }
- if (len > 2 && s[0] == '0' && lower_case(s[1]) == 'b') {
- s += 2;
- }
- int64_t binary = 0;
- while (*s) {
- char32_t c = lower_case(*s);
- int64_t n;
- if (c == '0' || c == '1') {
- n = c - '0';
- } else {
- return 0;
- }
- // Check for overflow/underflow, with special case to ensure INT64_MIN does not result in error
- bool overflow = ((binary > INT64_MAX / 2) && (sign == 1 || (sign == -1 && binary != (INT64_MAX >> 1) + 1))) || (sign == -1 && binary == (INT64_MAX >> 1) + 1 && c > '0');
- 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."));
- binary *= 2;
- binary += n;
- s++;
- }
- return binary * sign;
- }
- int64_t String::to_int() const {
- if (length() == 0) {
- return 0;
- }
- int to = (find(".") >= 0) ? find(".") : length();
- int64_t integer = 0;
- int64_t sign = 1;
- for (int i = 0; i < to; i++) {
- char32_t c = operator[](i);
- if (is_digit(c)) {
- bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
- 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."));
- integer *= 10;
- integer += c - '0';
- } else if (integer == 0 && c == '-') {
- sign = -sign;
- }
- }
- return integer * sign;
- }
- int64_t String::to_int(const char *p_str, int p_len) {
- int to = 0;
- if (p_len >= 0) {
- to = p_len;
- } else {
- while (p_str[to] != 0 && p_str[to] != '.') {
- to++;
- }
- }
- int64_t integer = 0;
- int64_t sign = 1;
- for (int i = 0; i < to; i++) {
- char c = p_str[i];
- if (is_digit(c)) {
- bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
- ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + String(p_str).substr(0, to) + " as a 64-bit signed integer, since the value is " + (sign == 1 ? "too large." : "too small."));
- integer *= 10;
- integer += c - '0';
- } else if (c == '-' && integer == 0) {
- sign = -sign;
- } else if (c != ' ') {
- break;
- }
- }
- return integer * sign;
- }
- int64_t String::to_int(const wchar_t *p_str, int p_len) {
- int to = 0;
- if (p_len >= 0) {
- to = p_len;
- } else {
- while (p_str[to] != 0 && p_str[to] != '.') {
- to++;
- }
- }
- int64_t integer = 0;
- int64_t sign = 1;
- for (int i = 0; i < to; i++) {
- wchar_t c = p_str[i];
- if (is_digit(c)) {
- bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
- ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + String(p_str).substr(0, to) + " as a 64-bit signed integer, since the value is " + (sign == 1 ? "too large." : "too small."));
- integer *= 10;
- integer += c - '0';
- } else if (c == '-' && integer == 0) {
- sign = -sign;
- } else if (c != ' ') {
- break;
- }
- }
- return integer * sign;
- }
- bool String::is_numeric() const {
- if (length() == 0) {
- return false;
- }
- int s = 0;
- if (operator[](0) == '-') {
- ++s;
- }
- bool dot = false;
- for (int i = s; i < length(); i++) {
- char32_t c = operator[](i);
- if (c == '.') {
- if (dot) {
- return false;
- }
- dot = true;
- } else if (!is_digit(c)) {
- return false;
- }
- }
- return true; // TODO: Use the parser below for this instead
- }
- template <typename C>
- static double built_in_strtod(
- /* A decimal ASCII floating-point number,
- * optionally preceded by white space. Must
- * have form "-I.FE-X", where I is the integer
- * part of the mantissa, F is the fractional
- * part of the mantissa, and X is the
- * exponent. Either of the signs may be "+",
- * "-", or omitted. Either I or F may be
- * omitted, or both. The decimal point isn't
- * necessary unless F is present. The "E" may
- * actually be an "e". E and X may both be
- * omitted (but not just one). */
- const C *string,
- /* If non-nullptr, store terminating Cacter's
- * address here. */
- C **endPtr = nullptr) {
- /* Largest possible base 10 exponent. Any
- * exponent larger than this will already
- * produce underflow or overflow, so there's
- * no need to worry about additional digits. */
- static const int maxExponent = 511;
- /* Table giving binary powers of 10. Entry
- * is 10^2^i. Used to convert decimal
- * exponents into floating-point numbers. */
- static const double powersOf10[] = {
- 10.,
- 100.,
- 1.0e4,
- 1.0e8,
- 1.0e16,
- 1.0e32,
- 1.0e64,
- 1.0e128,
- 1.0e256
- };
- bool sign, expSign = false;
- double fraction, dblExp;
- const double *d;
- const C *p;
- int c;
- /* Exponent read from "EX" field. */
- int exp = 0;
- /* Exponent that derives from the fractional
- * part. Under normal circumstances, it is
- * the negative of the number of digits in F.
- * However, if I is very long, the last digits
- * of I get dropped (otherwise a long I with a
- * large negative exponent could cause an
- * unnecessary overflow on I alone). In this
- * case, fracExp is incremented one for each
- * dropped digit. */
- int fracExp = 0;
- /* Number of digits in mantissa. */
- int mantSize;
- /* Number of mantissa digits BEFORE decimal point. */
- int decPt;
- /* Temporarily holds location of exponent in string. */
- const C *pExp;
- /*
- * Strip off leading blanks and check for a sign.
- */
- p = string;
- while (*p == ' ' || *p == '\t' || *p == '\n') {
- p += 1;
- }
- if (*p == '-') {
- sign = true;
- p += 1;
- } else {
- if (*p == '+') {
- p += 1;
- }
- sign = false;
- }
- /*
- * Count the number of digits in the mantissa (including the decimal
- * point), and also locate the decimal point.
- */
- decPt = -1;
- for (mantSize = 0;; mantSize += 1) {
- c = *p;
- if (!is_digit(c)) {
- if ((c != '.') || (decPt >= 0)) {
- break;
- }
- decPt = mantSize;
- }
- p += 1;
- }
- /*
- * Now suck up the digits in the mantissa. Use two integers to collect 9
- * digits each (this is faster than using floating-point). If the mantissa
- * has more than 18 digits, ignore the extras, since they can't affect the
- * value anyway.
- */
- pExp = p;
- p -= mantSize;
- if (decPt < 0) {
- decPt = mantSize;
- } else {
- mantSize -= 1; /* One of the digits was the point. */
- }
- if (mantSize > 18) {
- fracExp = decPt - 18;
- mantSize = 18;
- } else {
- fracExp = decPt - mantSize;
- }
- if (mantSize == 0) {
- fraction = 0.0;
- p = string;
- goto done;
- } else {
- int frac1, frac2;
- frac1 = 0;
- for (; mantSize > 9; mantSize -= 1) {
- c = *p;
- p += 1;
- if (c == '.') {
- c = *p;
- p += 1;
- }
- frac1 = 10 * frac1 + (c - '0');
- }
- frac2 = 0;
- for (; mantSize > 0; mantSize -= 1) {
- c = *p;
- p += 1;
- if (c == '.') {
- c = *p;
- p += 1;
- }
- frac2 = 10 * frac2 + (c - '0');
- }
- fraction = (1.0e9 * frac1) + frac2;
- }
- /*
- * Skim off the exponent.
- */
- p = pExp;
- if ((*p == 'E') || (*p == 'e')) {
- p += 1;
- if (*p == '-') {
- expSign = true;
- p += 1;
- } else {
- if (*p == '+') {
- p += 1;
- }
- expSign = false;
- }
- if (!is_digit(char32_t(*p))) {
- p = pExp;
- goto done;
- }
- while (is_digit(char32_t(*p))) {
- exp = exp * 10 + (*p - '0');
- p += 1;
- }
- }
- if (expSign) {
- exp = fracExp - exp;
- } else {
- exp = fracExp + exp;
- }
- /*
- * Generate a floating-point number that represents the exponent. Do this
- * by processing the exponent one bit at a time to combine many powers of
- * 2 of 10. Then combine the exponent with the fraction.
- */
- if (exp < 0) {
- expSign = true;
- exp = -exp;
- } else {
- expSign = false;
- }
- if (exp > maxExponent) {
- exp = maxExponent;
- WARN_PRINT("Exponent too high");
- }
- dblExp = 1.0;
- for (d = powersOf10; exp != 0; exp >>= 1, ++d) {
- if (exp & 01) {
- dblExp *= *d;
- }
- }
- if (expSign) {
- fraction /= dblExp;
- } else {
- fraction *= dblExp;
- }
- done:
- if (endPtr != nullptr) {
- *endPtr = (C *)p;
- }
- if (sign) {
- return -fraction;
- }
- return fraction;
- }
- #define READING_SIGN 0
- #define READING_INT 1
- #define READING_DEC 2
- #define READING_EXP 3
- #define READING_DONE 4
- double String::to_float(const char *p_str) {
- return built_in_strtod<char>(p_str);
- }
- double String::to_float(const char32_t *p_str, const char32_t **r_end) {
- return built_in_strtod<char32_t>(p_str, (char32_t **)r_end);
- }
- double String::to_float(const wchar_t *p_str, const wchar_t **r_end) {
- return built_in_strtod<wchar_t>(p_str, (wchar_t **)r_end);
- }
- uint32_t String::num_characters(int64_t p_int) {
- int r = 1;
- if (p_int < 0) {
- r += 1;
- if (p_int == INT64_MIN) {
- p_int = INT64_MAX;
- } else {
- p_int = -p_int;
- }
- }
- while (p_int >= 10) {
- p_int /= 10;
- r++;
- }
- return r;
- }
- int64_t String::to_int(const char32_t *p_str, int p_len, bool p_clamp) {
- if (p_len == 0 || !p_str[0]) {
- return 0;
- }
- ///@todo make more exact so saving and loading does not lose precision
- int64_t integer = 0;
- int64_t sign = 1;
- int reading = READING_SIGN;
- const char32_t *str = p_str;
- const char32_t *limit = &p_str[p_len];
- while (*str && reading != READING_DONE && str != limit) {
- char32_t c = *(str++);
- switch (reading) {
- case READING_SIGN: {
- if (is_digit(c)) {
- reading = READING_INT;
- // let it fallthrough
- } else if (c == '-') {
- sign = -1;
- reading = READING_INT;
- break;
- } else if (c == '+') {
- sign = 1;
- reading = READING_INT;
- break;
- } else {
- break;
- }
- [[fallthrough]];
- }
- case READING_INT: {
- if (is_digit(c)) {
- if (integer > INT64_MAX / 10) {
- String number("");
- str = p_str;
- while (*str && str != limit) {
- number += *(str++);
- }
- if (p_clamp) {
- if (sign == 1) {
- return INT64_MAX;
- } else {
- return INT64_MIN;
- }
- } else {
- 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."));
- }
- }
- integer *= 10;
- integer += c - '0';
- } else {
- reading = READING_DONE;
- }
- } break;
- }
- }
- return sign * integer;
- }
- double String::to_float() const {
- if (is_empty()) {
- return 0;
- }
- return built_in_strtod<char32_t>(get_data());
- }
- uint32_t String::hash(const char *p_cstr) {
- // static_cast: avoid negative values on platforms where char is signed.
- uint32_t hashv = 5381;
- uint32_t c = static_cast<uint8_t>(*p_cstr++);
- while (c) {
- hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
- c = static_cast<uint8_t>(*p_cstr++);
- }
- return hashv;
- }
- uint32_t String::hash(const char *p_cstr, int p_len) {
- uint32_t hashv = 5381;
- for (int i = 0; i < p_len; i++) {
- // static_cast: avoid negative values on platforms where char is signed.
- hashv = ((hashv << 5) + hashv) + static_cast<uint8_t>(p_cstr[i]); /* hash * 33 + c */
- }
- return hashv;
- }
- uint32_t String::hash(const wchar_t *p_cstr, int p_len) {
- // Avoid negative values on platforms where wchar_t is signed. Account for different sizes.
- using wide_unsigned = std::conditional<sizeof(wchar_t) == 2, uint16_t, uint32_t>::type;
- uint32_t hashv = 5381;
- for (int i = 0; i < p_len; i++) {
- hashv = ((hashv << 5) + hashv) + static_cast<wide_unsigned>(p_cstr[i]); /* hash * 33 + c */
- }
- return hashv;
- }
- uint32_t String::hash(const wchar_t *p_cstr) {
- // Avoid negative values on platforms where wchar_t is signed. Account for different sizes.
- using wide_unsigned = std::conditional<sizeof(wchar_t) == 2, uint16_t, uint32_t>::type;
- uint32_t hashv = 5381;
- uint32_t c = static_cast<wide_unsigned>(*p_cstr++);
- while (c) {
- hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
- c = static_cast<wide_unsigned>(*p_cstr++);
- }
- return hashv;
- }
- uint32_t String::hash(const char32_t *p_cstr, int p_len) {
- uint32_t hashv = 5381;
- for (int i = 0; i < p_len; i++) {
- hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
- }
- return hashv;
- }
- uint32_t String::hash(const char32_t *p_cstr) {
- uint32_t hashv = 5381;
- uint32_t c = *p_cstr++;
- while (c) {
- hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
- c = *p_cstr++;
- }
- return hashv;
- }
- uint32_t String::hash() const {
- /* simple djb2 hashing */
- const char32_t *chr = get_data();
- uint32_t hashv = 5381;
- uint32_t c = *chr++;
- while (c) {
- hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
- c = *chr++;
- }
- return hashv;
- }
- uint64_t String::hash64() const {
- /* simple djb2 hashing */
- const char32_t *chr = get_data();
- uint64_t hashv = 5381;
- uint64_t c = *chr++;
- while (c) {
- hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
- c = *chr++;
- }
- return hashv;
- }
- String String::md5_text() const {
- CharString cs = utf8();
- unsigned char hash[16];
- CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
- return String::hex_encode_buffer(hash, 16);
- }
- String String::sha1_text() const {
- CharString cs = utf8();
- unsigned char hash[20];
- CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
- return String::hex_encode_buffer(hash, 20);
- }
- String String::sha256_text() const {
- CharString cs = utf8();
- unsigned char hash[32];
- CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
- return String::hex_encode_buffer(hash, 32);
- }
- Vector<uint8_t> String::md5_buffer() const {
- CharString cs = utf8();
- unsigned char hash[16];
- CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
- Vector<uint8_t> ret;
- ret.resize(16);
- for (int i = 0; i < 16; i++) {
- ret.write[i] = hash[i];
- }
- return ret;
- }
- Vector<uint8_t> String::sha1_buffer() const {
- CharString cs = utf8();
- unsigned char hash[20];
- CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
- Vector<uint8_t> ret;
- ret.resize(20);
- for (int i = 0; i < 20; i++) {
- ret.write[i] = hash[i];
- }
- return ret;
- }
- Vector<uint8_t> String::sha256_buffer() const {
- CharString cs = utf8();
- unsigned char hash[32];
- CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
- Vector<uint8_t> ret;
- ret.resize(32);
- for (int i = 0; i < 32; i++) {
- ret.write[i] = hash[i];
- }
- return ret;
- }
- String String::insert(int p_at_pos, const String &p_string) const {
- if (p_at_pos < 0) {
- return *this;
- }
- if (p_at_pos > length()) {
- p_at_pos = length();
- }
- String pre;
- if (p_at_pos > 0) {
- pre = substr(0, p_at_pos);
- }
- String post;
- if (p_at_pos < length()) {
- post = substr(p_at_pos, length() - p_at_pos);
- }
- return pre + p_string + post;
- }
- String String::erase(int p_pos, int p_chars) const {
- 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));
- 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));
- return left(p_pos) + substr(p_pos + p_chars);
- }
- String String::substr(int p_from, int p_chars) const {
- if (p_chars == -1) {
- p_chars = length() - p_from;
- }
- if (is_empty() || p_from < 0 || p_from >= length() || p_chars <= 0) {
- return "";
- }
- if ((p_from + p_chars) > length()) {
- p_chars = length() - p_from;
- }
- if (p_from == 0 && p_chars >= length()) {
- return String(*this);
- }
- String s;
- s.copy_from_unchecked(&get_data()[p_from], p_chars);
- return s;
- }
- int String::find(const String &p_str, int p_from) const {
- if (p_from < 0) {
- return -1;
- }
- const int src_len = p_str.length();
- const int len = length();
- if (src_len == 0 || len == 0) {
- return -1; // won't find anything!
- }
- const char32_t *src = get_data();
- const char32_t *str = p_str.get_data();
- for (int i = p_from; i <= (len - src_len); i++) {
- bool found = true;
- for (int j = 0; j < src_len; j++) {
- int read_pos = i + j;
- if (read_pos >= len) {
- ERR_PRINT("read_pos>=len");
- return -1;
- }
- if (src[read_pos] != str[j]) {
- found = false;
- break;
- }
- }
- if (found) {
- return i;
- }
- }
- return -1;
- }
- int String::find(const char *p_str, int p_from) const {
- if (p_from < 0 || !p_str) {
- return -1;
- }
- const int src_len = strlen(p_str);
- const int len = length();
- if (len == 0 || src_len == 0) {
- return -1; // won't find anything!
- }
- const char32_t *src = get_data();
- if (src_len == 1) {
- const char32_t needle = p_str[0];
- for (int i = p_from; i < len; i++) {
- if (src[i] == needle) {
- return i;
- }
- }
- } else {
- for (int i = p_from; i <= (len - src_len); i++) {
- bool found = true;
- for (int j = 0; j < src_len; j++) {
- int read_pos = i + j;
- if (read_pos >= len) {
- ERR_PRINT("read_pos>=len");
- return -1;
- }
- if (src[read_pos] != (char32_t)p_str[j]) {
- found = false;
- break;
- }
- }
- if (found) {
- return i;
- }
- }
- }
- return -1;
- }
- int String::find_char(const char32_t &p_char, int p_from) const {
- return _cowdata.find(p_char, p_from);
- }
- int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
- if (p_from < 0) {
- return -1;
- }
- if (p_keys.size() == 0) {
- return -1;
- }
- //int src_len=p_str.length();
- const String *keys = &p_keys[0];
- int key_count = p_keys.size();
- int len = length();
- if (len == 0) {
- return -1; // won't find anything!
- }
- const char32_t *src = get_data();
- for (int i = p_from; i < len; i++) {
- bool found = true;
- for (int k = 0; k < key_count; k++) {
- found = true;
- if (r_key) {
- *r_key = k;
- }
- const char32_t *cmp = keys[k].get_data();
- int l = keys[k].length();
- for (int j = 0; j < l; j++) {
- int read_pos = i + j;
- if (read_pos >= len) {
- found = false;
- break;
- }
- if (src[read_pos] != cmp[j]) {
- found = false;
- break;
- }
- }
- if (found) {
- break;
- }
- }
- if (found) {
- return i;
- }
- }
- return -1;
- }
- int String::findn(const String &p_str, int p_from) const {
- if (p_from < 0) {
- return -1;
- }
- int src_len = p_str.length();
- if (src_len == 0 || length() == 0) {
- return -1; // won't find anything!
- }
- const char32_t *srcd = get_data();
- for (int i = p_from; i <= (length() - src_len); i++) {
- bool found = true;
- for (int j = 0; j < src_len; j++) {
- int read_pos = i + j;
- if (read_pos >= length()) {
- ERR_PRINT("read_pos>=length()");
- return -1;
- }
- char32_t src = _find_lower(srcd[read_pos]);
- char32_t dst = _find_lower(p_str[j]);
- if (src != dst) {
- found = false;
- break;
- }
- }
- if (found) {
- return i;
- }
- }
- return -1;
- }
- int String::findn(const char *p_str, int p_from) const {
- if (p_from < 0) {
- return -1;
- }
- int src_len = strlen(p_str);
- if (src_len == 0 || length() == 0) {
- return -1; // won't find anything!
- }
- const char32_t *srcd = get_data();
- for (int i = p_from; i <= (length() - src_len); i++) {
- bool found = true;
- for (int j = 0; j < src_len; j++) {
- int read_pos = i + j;
- if (read_pos >= length()) {
- ERR_PRINT("read_pos>=length()");
- return -1;
- }
- char32_t src = _find_lower(srcd[read_pos]);
- char32_t dst = _find_lower(p_str[j]);
- if (src != dst) {
- found = false;
- break;
- }
- }
- if (found) {
- return i;
- }
- }
- return -1;
- }
- int String::rfind(const String &p_str, int p_from) const {
- // establish a limit
- int limit = length() - p_str.length();
- if (limit < 0) {
- return -1;
- }
- // establish a starting point
- if (p_from < 0) {
- p_from = limit;
- } else if (p_from > limit) {
- p_from = limit;
- }
- int src_len = p_str.length();
- int len = length();
- if (src_len == 0 || len == 0) {
- return -1; // won't find anything!
- }
- const char32_t *src = get_data();
- for (int i = p_from; i >= 0; i--) {
- bool found = true;
- for (int j = 0; j < src_len; j++) {
- int read_pos = i + j;
- if (read_pos >= len) {
- ERR_PRINT("read_pos>=len");
- return -1;
- }
- if (src[read_pos] != p_str[j]) {
- found = false;
- break;
- }
- }
- if (found) {
- return i;
- }
- }
- return -1;
- }
- int String::rfind(const char *p_str, int p_from) const {
- const int source_length = length();
- int substring_length = strlen(p_str);
- if (source_length == 0 || substring_length == 0) {
- return -1; // won't find anything!
- }
- // establish a limit
- int limit = length() - substring_length;
- if (limit < 0) {
- return -1;
- }
- // establish a starting point
- int starting_point;
- if (p_from < 0) {
- starting_point = limit;
- } else if (p_from > limit) {
- starting_point = limit;
- } else {
- starting_point = p_from;
- }
- const char32_t *source = get_data();
- for (int i = starting_point; i >= 0; i--) {
- bool found = true;
- for (int j = 0; j < substring_length; j++) {
- int read_pos = i + j;
- if (read_pos >= source_length) {
- ERR_PRINT("read_pos>=source_length");
- return -1;
- }
- const char32_t key_needle = p_str[j];
- if (source[read_pos] != key_needle) {
- found = false;
- break;
- }
- }
- if (found) {
- return i;
- }
- }
- return -1;
- }
- int String::rfindn(const String &p_str, int p_from) const {
- // establish a limit
- int limit = length() - p_str.length();
- if (limit < 0) {
- return -1;
- }
- // establish a starting point
- if (p_from < 0) {
- p_from = limit;
- } else if (p_from > limit) {
- p_from = limit;
- }
- int src_len = p_str.length();
- int len = length();
- if (src_len == 0 || len == 0) {
- return -1; // won't find anything!
- }
- const char32_t *src = get_data();
- for (int i = p_from; i >= 0; i--) {
- bool found = true;
- for (int j = 0; j < src_len; j++) {
- int read_pos = i + j;
- if (read_pos >= len) {
- ERR_PRINT("read_pos>=len");
- return -1;
- }
- char32_t srcc = _find_lower(src[read_pos]);
- char32_t dstc = _find_lower(p_str[j]);
- if (srcc != dstc) {
- found = false;
- break;
- }
- }
- if (found) {
- return i;
- }
- }
- return -1;
- }
- int String::rfindn(const char *p_str, int p_from) const {
- const int source_length = length();
- int substring_length = strlen(p_str);
- if (source_length == 0 || substring_length == 0) {
- return -1; // won't find anything!
- }
- // establish a limit
- int limit = length() - substring_length;
- if (limit < 0) {
- return -1;
- }
- // establish a starting point
- int starting_point;
- if (p_from < 0) {
- starting_point = limit;
- } else if (p_from > limit) {
- starting_point = limit;
- } else {
- starting_point = p_from;
- }
- const char32_t *source = get_data();
- for (int i = starting_point; i >= 0; i--) {
- bool found = true;
- for (int j = 0; j < substring_length; j++) {
- int read_pos = i + j;
- if (read_pos >= source_length) {
- ERR_PRINT("read_pos>=source_length");
- return -1;
- }
- const char32_t key_needle = p_str[j];
- int srcc = _find_lower(source[read_pos]);
- int keyc = _find_lower(key_needle);
- if (srcc != keyc) {
- found = false;
- break;
- }
- }
- if (found) {
- return i;
- }
- }
- return -1;
- }
- bool String::ends_with(const String &p_string) const {
- int l = p_string.length();
- if (l > length()) {
- return false;
- }
- if (l == 0) {
- return true;
- }
- const char32_t *p = &p_string[0];
- const char32_t *s = &operator[](length() - l);
- for (int i = 0; i < l; i++) {
- if (p[i] != s[i]) {
- return false;
- }
- }
- return true;
- }
- bool String::ends_with(const char *p_string) const {
- if (!p_string) {
- return false;
- }
- int l = strlen(p_string);
- if (l > length()) {
- return false;
- }
- if (l == 0) {
- return true;
- }
- const char32_t *s = &operator[](length() - l);
- for (int i = 0; i < l; i++) {
- if (static_cast<char32_t>(p_string[i]) != s[i]) {
- return false;
- }
- }
- return true;
- }
- bool String::begins_with(const String &p_string) const {
- int l = p_string.length();
- if (l > length()) {
- return false;
- }
- if (l == 0) {
- return true;
- }
- const char32_t *p = &p_string[0];
- const char32_t *s = &operator[](0);
- for (int i = 0; i < l; i++) {
- if (p[i] != s[i]) {
- return false;
- }
- }
- return true;
- }
- bool String::begins_with(const char *p_string) const {
- if (!p_string) {
- return false;
- }
- int l = length();
- if (l == 0) {
- return *p_string == 0;
- }
- const char32_t *str = &operator[](0);
- int i = 0;
- while (*p_string && i < l) {
- if ((char32_t)*p_string != str[i]) {
- return false;
- }
- i++;
- p_string++;
- }
- return *p_string == 0;
- }
- bool String::is_enclosed_in(const String &p_string) const {
- return begins_with(p_string) && ends_with(p_string);
- }
- bool String::is_subsequence_of(const String &p_string) const {
- return _base_is_subsequence_of(p_string, false);
- }
- bool String::is_subsequence_ofn(const String &p_string) const {
- return _base_is_subsequence_of(p_string, true);
- }
- bool String::is_quoted() const {
- return is_enclosed_in("\"") || is_enclosed_in("'");
- }
- int String::_count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const {
- if (p_string.is_empty()) {
- return 0;
- }
- int len = length();
- int slen = p_string.length();
- if (len < slen) {
- return 0;
- }
- String str;
- if (p_from >= 0 && p_to >= 0) {
- if (p_to == 0) {
- p_to = len;
- } else if (p_from >= p_to) {
- return 0;
- }
- if (p_from == 0 && p_to == len) {
- str = String();
- str.copy_from_unchecked(&get_data()[0], len);
- } else {
- str = substr(p_from, p_to - p_from);
- }
- } else {
- return 0;
- }
- int c = 0;
- int idx = -1;
- do {
- idx = p_case_insensitive ? str.findn(p_string) : str.find(p_string);
- if (idx != -1) {
- str = str.substr(idx + slen, str.length() - slen);
- ++c;
- }
- } while (idx != -1);
- return c;
- }
- int String::_count(const char *p_string, int p_from, int p_to, bool p_case_insensitive) const {
- int substring_length = strlen(p_string);
- if (substring_length == 0) {
- return 0;
- }
- const int source_length = length();
- if (source_length < substring_length) {
- return 0;
- }
- String str;
- int search_limit = p_to;
- if (p_from >= 0 && p_to >= 0) {
- if (p_to == 0) {
- search_limit = source_length;
- } else if (p_from >= p_to) {
- return 0;
- }
- if (p_from == 0 && search_limit == source_length) {
- str = String();
- str.copy_from_unchecked(&get_data()[0], source_length);
- } else {
- str = substr(p_from, search_limit - p_from);
- }
- } else {
- return 0;
- }
- int c = 0;
- int idx = -1;
- do {
- idx = p_case_insensitive ? str.findn(p_string) : str.find(p_string);
- if (idx != -1) {
- str = str.substr(idx + substring_length, str.length() - substring_length);
- ++c;
- }
- } while (idx != -1);
- return c;
- }
- int String::count(const String &p_string, int p_from, int p_to) const {
- return _count(p_string, p_from, p_to, false);
- }
- int String::count(const char *p_string, int p_from, int p_to) const {
- return _count(p_string, p_from, p_to, false);
- }
- int String::countn(const String &p_string, int p_from, int p_to) const {
- return _count(p_string, p_from, p_to, true);
- }
- int String::countn(const char *p_string, int p_from, int p_to) const {
- return _count(p_string, p_from, p_to, true);
- }
- bool String::_base_is_subsequence_of(const String &p_string, bool case_insensitive) const {
- int len = length();
- if (len == 0) {
- // Technically an empty string is subsequence of any string
- return true;
- }
- if (len > p_string.length()) {
- return false;
- }
- const char32_t *src = &operator[](0);
- const char32_t *tgt = &p_string[0];
- for (; *src && *tgt; tgt++) {
- bool match = false;
- if (case_insensitive) {
- char32_t srcc = _find_lower(*src);
- char32_t tgtc = _find_lower(*tgt);
- match = srcc == tgtc;
- } else {
- match = *src == *tgt;
- }
- if (match) {
- src++;
- if (!*src) {
- return true;
- }
- }
- }
- return false;
- }
- Vector<String> String::bigrams() const {
- int n_pairs = length() - 1;
- Vector<String> b;
- if (n_pairs <= 0) {
- return b;
- }
- b.resize(n_pairs);
- for (int i = 0; i < n_pairs; i++) {
- b.write[i] = substr(i, 2);
- }
- return b;
- }
- // Similarity according to Sorensen-Dice coefficient
- float String::similarity(const String &p_string) const {
- if (operator==(p_string)) {
- // Equal strings are totally similar
- return 1.0f;
- }
- if (length() < 2 || p_string.length() < 2) {
- // No way to calculate similarity without a single bigram
- return 0.0f;
- }
- Vector<String> src_bigrams = bigrams();
- Vector<String> tgt_bigrams = p_string.bigrams();
- int src_size = src_bigrams.size();
- int tgt_size = tgt_bigrams.size();
- int sum = src_size + tgt_size;
- int inter = 0;
- for (int i = 0; i < src_size; i++) {
- for (int j = 0; j < tgt_size; j++) {
- if (src_bigrams[i] == tgt_bigrams[j]) {
- inter++;
- break;
- }
- }
- }
- return (2.0f * inter) / sum;
- }
- static bool _wildcard_match(const char32_t *p_pattern, const char32_t *p_string, bool p_case_sensitive) {
- switch (*p_pattern) {
- case '\0':
- return !*p_string;
- case '*':
- return _wildcard_match(p_pattern + 1, p_string, p_case_sensitive) || (*p_string && _wildcard_match(p_pattern, p_string + 1, p_case_sensitive));
- case '?':
- return *p_string && (*p_string != '.') && _wildcard_match(p_pattern + 1, p_string + 1, p_case_sensitive);
- default:
- 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);
- }
- }
- bool String::match(const String &p_wildcard) const {
- if (!p_wildcard.length() || !length()) {
- return false;
- }
- return _wildcard_match(p_wildcard.get_data(), get_data(), true);
- }
- bool String::matchn(const String &p_wildcard) const {
- if (!p_wildcard.length() || !length()) {
- return false;
- }
- return _wildcard_match(p_wildcard.get_data(), get_data(), false);
- }
- String String::format(const Variant &values, const String &placeholder) const {
- String new_string = String(ptr());
- if (values.get_type() == Variant::ARRAY) {
- Array values_arr = values;
- for (int i = 0; i < values_arr.size(); i++) {
- String i_as_str = String::num_int64(i);
- if (values_arr[i].get_type() == Variant::ARRAY) { //Array in Array structure [["name","RobotGuy"],[0,"godot"],["strength",9000.91]]
- Array value_arr = values_arr[i];
- if (value_arr.size() == 2) {
- Variant v_key = value_arr[0];
- String key = v_key;
- Variant v_val = value_arr[1];
- String val = v_val;
- new_string = new_string.replace(placeholder.replace("_", key), val);
- } else {
- ERR_PRINT(String("STRING.format Inner Array size != 2 ").ascii().get_data());
- }
- } else { //Array structure ["RobotGuy","Logis","rookie"]
- Variant v_val = values_arr[i];
- String val = v_val;
- if (placeholder.contains("_")) {
- new_string = new_string.replace(placeholder.replace("_", i_as_str), val);
- } else {
- new_string = new_string.replace_first(placeholder, val);
- }
- }
- }
- } else if (values.get_type() == Variant::DICTIONARY) {
- Dictionary d = values;
- List<Variant> keys;
- d.get_key_list(&keys);
- for (const Variant &key : keys) {
- new_string = new_string.replace(placeholder.replace("_", key), d[key]);
- }
- } else {
- ERR_PRINT(String("Invalid type: use Array or Dictionary.").ascii().get_data());
- }
- return new_string;
- }
- String String::replace(const String &p_key, const String &p_with) const {
- String new_string;
- int search_from = 0;
- int result = 0;
- while ((result = find(p_key, search_from)) >= 0) {
- new_string += substr(search_from, result - search_from);
- new_string += p_with;
- search_from = result + p_key.length();
- }
- if (search_from == 0) {
- return *this;
- }
- new_string += substr(search_from, length() - search_from);
- return new_string;
- }
- String String::replace(const char *p_key, const char *p_with) const {
- String new_string;
- int search_from = 0;
- int result = 0;
- while ((result = find(p_key, search_from)) >= 0) {
- new_string += substr(search_from, result - search_from);
- new_string += p_with;
- int k = 0;
- while (p_key[k] != '\0') {
- k++;
- }
- search_from = result + k;
- }
- if (search_from == 0) {
- return *this;
- }
- new_string += substr(search_from, length() - search_from);
- return new_string;
- }
- String String::replace_first(const String &p_key, const String &p_with) const {
- int pos = find(p_key);
- if (pos >= 0) {
- return substr(0, pos) + p_with + substr(pos + p_key.length(), length());
- }
- return *this;
- }
- String String::replace_first(const char *p_key, const char *p_with) const {
- int pos = find(p_key);
- if (pos >= 0) {
- int substring_length = strlen(p_key);
- return substr(0, pos) + p_with + substr(pos + substring_length, length());
- }
- return *this;
- }
- String String::replacen(const String &p_key, const String &p_with) const {
- String new_string;
- int search_from = 0;
- int result = 0;
- while ((result = findn(p_key, search_from)) >= 0) {
- new_string += substr(search_from, result - search_from);
- new_string += p_with;
- search_from = result + p_key.length();
- }
- if (search_from == 0) {
- return *this;
- }
- new_string += substr(search_from, length() - search_from);
- return new_string;
- }
- String String::replacen(const char *p_key, const char *p_with) const {
- String new_string;
- int search_from = 0;
- int result = 0;
- int substring_length = strlen(p_key);
- if (substring_length == 0) {
- return *this; // there's nothing to match or substitute
- }
- while ((result = findn(p_key, search_from)) >= 0) {
- new_string += substr(search_from, result - search_from);
- new_string += p_with;
- search_from = result + substring_length;
- }
- if (search_from == 0) {
- return *this;
- }
- new_string += substr(search_from, length() - search_from);
- return new_string;
- }
- String String::repeat(int p_count) const {
- ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number.");
- if (p_count == 0) {
- return "";
- }
- if (p_count == 1) {
- return *this;
- }
- int len = length();
- String new_string = *this;
- new_string.resize(p_count * len + 1);
- char32_t *dst = new_string.ptrw();
- int offset = 1;
- int stride = 1;
- while (offset < p_count) {
- memcpy(dst + offset * len, dst, stride * len * sizeof(char32_t));
- offset += stride;
- stride = MIN(stride * 2, p_count - offset);
- }
- dst[p_count * len] = _null;
- return new_string;
- }
- String String::reverse() const {
- int len = length();
- if (len <= 1) {
- return *this;
- }
- String new_string;
- new_string.resize(len + 1);
- const char32_t *src = ptr();
- char32_t *dst = new_string.ptrw();
- for (int i = 0; i < len; i++) {
- dst[i] = src[len - i - 1];
- }
- dst[len] = _null;
- return new_string;
- }
- String String::left(int p_len) const {
- if (p_len < 0) {
- p_len = length() + p_len;
- }
- if (p_len <= 0) {
- return "";
- }
- if (p_len >= length()) {
- return *this;
- }
- String s;
- s.copy_from_unchecked(&get_data()[0], p_len);
- return s;
- }
- String String::right(int p_len) const {
- if (p_len < 0) {
- p_len = length() + p_len;
- }
- if (p_len <= 0) {
- return "";
- }
- if (p_len >= length()) {
- return *this;
- }
- String s;
- s.copy_from_unchecked(&get_data()[length() - p_len], p_len);
- return s;
- }
- char32_t String::unicode_at(int p_idx) const {
- ERR_FAIL_INDEX_V(p_idx, length(), 0);
- return operator[](p_idx);
- }
- String String::indent(const String &p_prefix) const {
- String new_string;
- int line_start = 0;
- for (int i = 0; i < length(); i++) {
- const char32_t c = operator[](i);
- if (c == '\n') {
- if (i == line_start) {
- new_string += c; // Leave empty lines empty.
- } else {
- new_string += p_prefix + substr(line_start, i - line_start + 1);
- }
- line_start = i + 1;
- }
- }
- if (line_start != length()) {
- new_string += p_prefix + substr(line_start);
- }
- return new_string;
- }
- String String::dedent() const {
- String new_string;
- String indent;
- bool has_indent = false;
- bool has_text = false;
- int line_start = 0;
- int indent_stop = -1;
- for (int i = 0; i < length(); i++) {
- char32_t c = operator[](i);
- if (c == '\n') {
- if (has_text) {
- new_string += substr(indent_stop, i - indent_stop);
- }
- new_string += "\n";
- has_text = false;
- line_start = i + 1;
- indent_stop = -1;
- } else if (!has_text) {
- if (c > 32) {
- has_text = true;
- if (!has_indent) {
- has_indent = true;
- indent = substr(line_start, i - line_start);
- indent_stop = i;
- }
- }
- if (has_indent && indent_stop < 0) {
- int j = i - line_start;
- if (j >= indent.length() || c != indent[j]) {
- indent_stop = i;
- }
- }
- }
- }
- if (has_text) {
- new_string += substr(indent_stop, length() - indent_stop);
- }
- return new_string;
- }
- String String::strip_edges(bool left, bool right) const {
- int len = length();
- int beg = 0, end = len;
- if (left) {
- for (int i = 0; i < len; i++) {
- if (operator[](i) <= 32) {
- beg++;
- } else {
- break;
- }
- }
- }
- if (right) {
- for (int i = len - 1; i >= 0; i--) {
- if (operator[](i) <= 32) {
- end--;
- } else {
- break;
- }
- }
- }
- if (beg == 0 && end == len) {
- return *this;
- }
- return substr(beg, end - beg);
- }
- String String::strip_escapes() const {
- String new_string;
- for (int i = 0; i < length(); i++) {
- // Escape characters on first page of the ASCII table, before 32 (Space).
- if (operator[](i) < 32) {
- continue;
- }
- new_string += operator[](i);
- }
- return new_string;
- }
- String String::lstrip(const String &p_chars) const {
- int len = length();
- int beg;
- for (beg = 0; beg < len; beg++) {
- if (p_chars.find_char(get(beg)) == -1) {
- break;
- }
- }
- if (beg == 0) {
- return *this;
- }
- return substr(beg, len - beg);
- }
- String String::rstrip(const String &p_chars) const {
- int len = length();
- int end;
- for (end = len - 1; end >= 0; end--) {
- if (p_chars.find_char(get(end)) == -1) {
- break;
- }
- }
- if (end == len - 1) {
- return *this;
- }
- return substr(0, end + 1);
- }
- bool String::is_network_share_path() const {
- return begins_with("//") || begins_with("\\\\");
- }
- String String::simplify_path() const {
- String s = *this;
- String drive;
- // Check if we have a special path (like res://) or a protocol identifier.
- int p = s.find("://");
- bool found = false;
- if (p > 0) {
- bool only_chars = true;
- for (int i = 0; i < p; i++) {
- if (!is_ascii_alphanumeric_char(s[i])) {
- only_chars = false;
- break;
- }
- }
- if (only_chars) {
- found = true;
- drive = s.substr(0, p + 3);
- s = s.substr(p + 3);
- }
- }
- if (!found) {
- if (is_network_share_path()) {
- // Network path, beginning with // or \\.
- drive = s.substr(0, 2);
- s = s.substr(2);
- } else if (s.begins_with("/") || s.begins_with("\\")) {
- // Absolute path.
- drive = s.substr(0, 1);
- s = s.substr(1);
- } else {
- // Windows-style drive path, like C:/ or C:\.
- p = s.find(":/");
- if (p == -1) {
- p = s.find(":\\");
- }
- if (p != -1 && p < s.find("/")) {
- drive = s.substr(0, p + 2);
- s = s.substr(p + 2);
- }
- }
- }
- s = s.replace("\\", "/");
- while (true) { // in case of using 2 or more slash
- String compare = s.replace("//", "/");
- if (s == compare) {
- break;
- } else {
- s = compare;
- }
- }
- Vector<String> dirs = s.split("/", false);
- for (int i = 0; i < dirs.size(); i++) {
- String d = dirs[i];
- if (d == ".") {
- dirs.remove_at(i);
- i--;
- } else if (d == "..") {
- if (i == 0) {
- dirs.remove_at(i);
- i--;
- } else {
- dirs.remove_at(i);
- dirs.remove_at(i - 1);
- i -= 2;
- }
- }
- }
- s = "";
- for (int i = 0; i < dirs.size(); i++) {
- if (i > 0) {
- s += "/";
- }
- s += dirs[i];
- }
- return drive + s;
- }
- static int _humanize_digits(int p_num) {
- if (p_num < 100) {
- return 2;
- } else if (p_num < 1024) {
- return 1;
- } else {
- return 0;
- }
- }
- String String::humanize_size(uint64_t p_size) {
- int magnitude = 0;
- uint64_t _div = 1;
- while (p_size > _div * 1024 && magnitude < 6) {
- _div *= 1024;
- magnitude++;
- }
- if (magnitude == 0) {
- return String::num(p_size) + " " + RTR("B");
- } else {
- String suffix;
- switch (magnitude) {
- case 1:
- suffix = RTR("KiB");
- break;
- case 2:
- suffix = RTR("MiB");
- break;
- case 3:
- suffix = RTR("GiB");
- break;
- case 4:
- suffix = RTR("TiB");
- break;
- case 5:
- suffix = RTR("PiB");
- break;
- case 6:
- suffix = RTR("EiB");
- break;
- }
- const double divisor = _div;
- const int digits = _humanize_digits(p_size / _div);
- return String::num(p_size / divisor).pad_decimals(digits) + " " + suffix;
- }
- }
- bool String::is_absolute_path() const {
- if (length() > 1) {
- return (operator[](0) == '/' || operator[](0) == '\\' || find(":/") != -1 || find(":\\") != -1);
- } else if ((length()) == 1) {
- return (operator[](0) == '/' || operator[](0) == '\\');
- } else {
- return false;
- }
- }
- String String::validate_identifier() const {
- if (is_empty()) {
- return "_"; // Empty string is not a valid identifier;
- }
- String result;
- if (is_digit(operator[](0))) {
- result = "_" + *this;
- } else {
- result = *this;
- }
- int len = result.length();
- char32_t *buffer = result.ptrw();
- for (int i = 0; i < len; i++) {
- if (!is_ascii_identifier_char(buffer[i])) {
- buffer[i] = '_';
- }
- }
- return result;
- }
- bool String::is_valid_identifier() const {
- int len = length();
- if (len == 0) {
- return false;
- }
- if (is_digit(operator[](0))) {
- return false;
- }
- const char32_t *str = &operator[](0);
- for (int i = 0; i < len; i++) {
- if (!is_ascii_identifier_char(str[i])) {
- return false;
- }
- }
- return true;
- }
- bool String::is_valid_string() const {
- int l = length();
- const char32_t *src = get_data();
- bool valid = true;
- for (int i = 0; i < l; i++) {
- valid = valid && (src[i] < 0xd800 || (src[i] > 0xdfff && src[i] <= 0x10ffff));
- }
- return valid;
- }
- String String::uri_encode() const {
- const CharString temp = utf8();
- String res;
- for (int i = 0; i < temp.length(); ++i) {
- uint8_t ord = temp[i];
- if (ord == '.' || ord == '-' || ord == '~' || is_ascii_identifier_char(ord)) {
- res += ord;
- } else {
- char p[4] = { '%', 0, 0, 0 };
- static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
- p[1] = hex[ord >> 4];
- p[2] = hex[ord & 0xF];
- res += p;
- }
- }
- return res;
- }
- String String::uri_decode() const {
- CharString src = utf8();
- CharString res;
- for (int i = 0; i < src.length(); ++i) {
- if (src[i] == '%' && i + 2 < src.length()) {
- char ord1 = src[i + 1];
- if (is_digit(ord1) || is_ascii_upper_case(ord1)) {
- char ord2 = src[i + 2];
- if (is_digit(ord2) || is_ascii_upper_case(ord2)) {
- char bytes[3] = { (char)ord1, (char)ord2, 0 };
- res += (char)strtol(bytes, nullptr, 16);
- i += 2;
- }
- } else {
- res += src[i];
- }
- } else if (src[i] == '+') {
- res += ' ';
- } else {
- res += src[i];
- }
- }
- return String::utf8(res);
- }
- String String::c_unescape() const {
- String escaped = *this;
- escaped = escaped.replace("\\a", "\a");
- escaped = escaped.replace("\\b", "\b");
- escaped = escaped.replace("\\f", "\f");
- escaped = escaped.replace("\\n", "\n");
- escaped = escaped.replace("\\r", "\r");
- escaped = escaped.replace("\\t", "\t");
- escaped = escaped.replace("\\v", "\v");
- escaped = escaped.replace("\\'", "\'");
- escaped = escaped.replace("\\\"", "\"");
- escaped = escaped.replace("\\\\", "\\");
- return escaped;
- }
- String String::c_escape() const {
- String escaped = *this;
- escaped = escaped.replace("\\", "\\\\");
- escaped = escaped.replace("\a", "\\a");
- escaped = escaped.replace("\b", "\\b");
- escaped = escaped.replace("\f", "\\f");
- escaped = escaped.replace("\n", "\\n");
- escaped = escaped.replace("\r", "\\r");
- escaped = escaped.replace("\t", "\\t");
- escaped = escaped.replace("\v", "\\v");
- escaped = escaped.replace("\'", "\\'");
- escaped = escaped.replace("\"", "\\\"");
- return escaped;
- }
- String String::c_escape_multiline() const {
- String escaped = *this;
- escaped = escaped.replace("\\", "\\\\");
- escaped = escaped.replace("\"", "\\\"");
- return escaped;
- }
- String String::json_escape() const {
- String escaped = *this;
- escaped = escaped.replace("\\", "\\\\");
- escaped = escaped.replace("\b", "\\b");
- escaped = escaped.replace("\f", "\\f");
- escaped = escaped.replace("\n", "\\n");
- escaped = escaped.replace("\r", "\\r");
- escaped = escaped.replace("\t", "\\t");
- escaped = escaped.replace("\v", "\\v");
- escaped = escaped.replace("\"", "\\\"");
- return escaped;
- }
- String String::xml_escape(bool p_escape_quotes) const {
- String str = *this;
- str = str.replace("&", "&");
- str = str.replace("<", "<");
- str = str.replace(">", ">");
- if (p_escape_quotes) {
- str = str.replace("'", "'");
- str = str.replace("\"", """);
- }
- /*
- for (int i=1;i<32;i++) {
- char chr[2]={i,0};
- str=str.replace(chr,"&#"+String::num(i)+";");
- }*/
- return str;
- }
- static _FORCE_INLINE_ int _xml_unescape(const char32_t *p_src, int p_src_len, char32_t *p_dst) {
- int len = 0;
- while (p_src_len) {
- if (*p_src == '&') {
- int eat = 0;
- if (p_src_len >= 4 && p_src[1] == '#') {
- char32_t c = 0;
- bool overflow = false;
- if (p_src[2] == 'x') {
- // Hex entity &#x<num>;
- for (int i = 3; i < p_src_len; i++) {
- eat = i + 1;
- char32_t ct = p_src[i];
- if (ct == ';') {
- break;
- } else if (is_digit(ct)) {
- ct = ct - '0';
- } else if (ct >= 'a' && ct <= 'f') {
- ct = (ct - 'a') + 10;
- } else if (ct >= 'A' && ct <= 'F') {
- ct = (ct - 'A') + 10;
- } else {
- break;
- }
- if (c > (UINT32_MAX >> 4)) {
- overflow = true;
- break;
- }
- c <<= 4;
- c |= ct;
- }
- } else {
- // Decimal entity &#<num>;
- for (int i = 2; i < p_src_len; i++) {
- eat = i + 1;
- char32_t ct = p_src[i];
- if (ct == ';' || !is_digit(ct)) {
- break;
- }
- }
- if (p_src[eat - 1] == ';') {
- int64_t val = String::to_int(p_src + 2, eat - 3);
- if (val > 0 && val <= UINT32_MAX) {
- c = (char32_t)val;
- } else {
- overflow = true;
- }
- }
- }
- // Value must be non-zero, in the range of char32_t,
- // actually end with ';'. If invalid, leave the entity as-is
- if (c == '\0' || overflow || p_src[eat - 1] != ';') {
- eat = 1;
- c = *p_src;
- }
- if (p_dst) {
- *p_dst = c;
- }
- } else if (p_src_len >= 4 && p_src[1] == 'g' && p_src[2] == 't' && p_src[3] == ';') {
- if (p_dst) {
- *p_dst = '>';
- }
- eat = 4;
- } else if (p_src_len >= 4 && p_src[1] == 'l' && p_src[2] == 't' && p_src[3] == ';') {
- if (p_dst) {
- *p_dst = '<';
- }
- eat = 4;
- } else if (p_src_len >= 5 && p_src[1] == 'a' && p_src[2] == 'm' && p_src[3] == 'p' && p_src[4] == ';') {
- if (p_dst) {
- *p_dst = '&';
- }
- eat = 5;
- } 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] == ';') {
- if (p_dst) {
- *p_dst = '"';
- }
- eat = 6;
- } 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] == ';') {
- if (p_dst) {
- *p_dst = '\'';
- }
- eat = 6;
- } else {
- if (p_dst) {
- *p_dst = *p_src;
- }
- eat = 1;
- }
- if (p_dst) {
- p_dst++;
- }
- len++;
- p_src += eat;
- p_src_len -= eat;
- } else {
- if (p_dst) {
- *p_dst = *p_src;
- p_dst++;
- }
- len++;
- p_src++;
- p_src_len--;
- }
- }
- return len;
- }
- String String::xml_unescape() const {
- String str;
- int l = length();
- int len = _xml_unescape(get_data(), l, nullptr);
- if (len == 0) {
- return String();
- }
- str.resize(len + 1);
- _xml_unescape(get_data(), l, str.ptrw());
- str[len] = 0;
- return str;
- }
- String String::pad_decimals(int p_digits) const {
- String s = *this;
- int c = s.find(".");
- if (c == -1) {
- if (p_digits <= 0) {
- return s;
- }
- s += ".";
- c = s.length() - 1;
- } else {
- if (p_digits <= 0) {
- return s.substr(0, c);
- }
- }
- if (s.length() - (c + 1) > p_digits) {
- return s.substr(0, c + p_digits + 1);
- } else {
- int zeros_to_add = p_digits - s.length() + (c + 1);
- return s + String("0").repeat(zeros_to_add);
- }
- }
- String String::pad_zeros(int p_digits) const {
- String s = *this;
- int end = s.find(".");
- if (end == -1) {
- end = s.length();
- }
- if (end == 0) {
- return s;
- }
- int begin = 0;
- while (begin < end && !is_digit(s[begin])) {
- begin++;
- }
- int zeros_to_add = p_digits - (end - begin);
- if (zeros_to_add <= 0) {
- return s;
- } else {
- return s.insert(begin, String("0").repeat(zeros_to_add));
- }
- }
- String String::trim_prefix(const String &p_prefix) const {
- String s = *this;
- if (s.begins_with(p_prefix)) {
- return s.substr(p_prefix.length(), s.length() - p_prefix.length());
- }
- return s;
- }
- String String::trim_prefix(const char *p_prefix) const {
- String s = *this;
- if (s.begins_with(p_prefix)) {
- int prefix_length = strlen(p_prefix);
- return s.substr(prefix_length, s.length() - prefix_length);
- }
- return s;
- }
- String String::trim_suffix(const String &p_suffix) const {
- String s = *this;
- if (s.ends_with(p_suffix)) {
- return s.substr(0, s.length() - p_suffix.length());
- }
- return s;
- }
- String String::trim_suffix(const char *p_suffix) const {
- String s = *this;
- if (s.ends_with(p_suffix)) {
- return s.substr(0, s.length() - strlen(p_suffix));
- }
- return s;
- }
- bool String::is_valid_int() const {
- int len = length();
- if (len == 0) {
- return false;
- }
- int from = 0;
- if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
- from++;
- }
- for (int i = from; i < len; i++) {
- if (!is_digit(operator[](i))) {
- return false; // no start with number plz
- }
- }
- return true;
- }
- bool String::is_valid_hex_number(bool p_with_prefix) const {
- int len = length();
- if (len == 0) {
- return false;
- }
- int from = 0;
- if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
- from++;
- }
- if (p_with_prefix) {
- if (len < 3) {
- return false;
- }
- if (operator[](from) != '0' || operator[](from + 1) != 'x') {
- return false;
- }
- from += 2;
- }
- for (int i = from; i < len; i++) {
- char32_t c = operator[](i);
- if (is_hex_digit(c)) {
- continue;
- }
- return false;
- }
- return true;
- }
- bool String::is_valid_float() const {
- int len = length();
- if (len == 0) {
- return false;
- }
- int from = 0;
- if (operator[](0) == '+' || operator[](0) == '-') {
- from++;
- }
- bool exponent_found = false;
- bool period_found = false;
- bool sign_found = false;
- bool exponent_values_found = false;
- bool numbers_found = false;
- for (int i = from; i < len; i++) {
- if (is_digit(operator[](i))) {
- if (exponent_found) {
- exponent_values_found = true;
- } else {
- numbers_found = true;
- }
- } else if (numbers_found && !exponent_found && operator[](i) == 'e') {
- exponent_found = true;
- } else if (!period_found && !exponent_found && operator[](i) == '.') {
- period_found = true;
- } else if ((operator[](i) == '-' || operator[](i) == '+') && exponent_found && !exponent_values_found && !sign_found) {
- sign_found = true;
- } else {
- return false; // no start with number plz
- }
- }
- return numbers_found;
- }
- String String::path_to_file(const String &p_path) const {
- // Don't get base dir for src, this is expected to be a dir already.
- String src = replace("\\", "/");
- String dst = p_path.replace("\\", "/").get_base_dir();
- String rel = src.path_to(dst);
- if (rel == dst) { // failed
- return p_path;
- } else {
- return rel + p_path.get_file();
- }
- }
- String String::path_to(const String &p_path) const {
- String src = replace("\\", "/");
- String dst = p_path.replace("\\", "/");
- if (!src.ends_with("/")) {
- src += "/";
- }
- if (!dst.ends_with("/")) {
- dst += "/";
- }
- if (src.begins_with("res://") && dst.begins_with("res://")) {
- src = src.replace("res://", "/");
- dst = dst.replace("res://", "/");
- } else if (src.begins_with("user://") && dst.begins_with("user://")) {
- src = src.replace("user://", "/");
- dst = dst.replace("user://", "/");
- } else if (src.begins_with("/") && dst.begins_with("/")) {
- //nothing
- } else {
- //dos style
- String src_begin = src.get_slicec('/', 0);
- String dst_begin = dst.get_slicec('/', 0);
- if (src_begin != dst_begin) {
- return p_path; //impossible to do this
- }
- src = src.substr(src_begin.length(), src.length());
- dst = dst.substr(dst_begin.length(), dst.length());
- }
- //remove leading and trailing slash and split
- Vector<String> src_dirs = src.substr(1, src.length() - 2).split("/");
- Vector<String> dst_dirs = dst.substr(1, dst.length() - 2).split("/");
- //find common parent
- int common_parent = 0;
- while (true) {
- if (src_dirs.size() == common_parent) {
- break;
- }
- if (dst_dirs.size() == common_parent) {
- break;
- }
- if (src_dirs[common_parent] != dst_dirs[common_parent]) {
- break;
- }
- common_parent++;
- }
- common_parent--;
- int dirs_to_backtrack = (src_dirs.size() - 1) - common_parent;
- String dir = String("../").repeat(dirs_to_backtrack);
- for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
- dir += dst_dirs[i] + "/";
- }
- if (dir.length() == 0) {
- dir = "./";
- }
- return dir;
- }
- bool String::is_valid_html_color() const {
- return Color::html_is_valid(*this);
- }
- // Changes made to the set of invalid filename characters must also be reflected in the String documentation for is_valid_filename.
- static const char *invalid_filename_characters = ": / \\ ? * \" | % < >";
- bool String::is_valid_filename() const {
- String stripped = strip_edges();
- if (*this != stripped) {
- return false;
- }
- if (stripped.is_empty()) {
- return false;
- }
- Vector<String> chars = String(invalid_filename_characters).split(" ");
- for (const String &ch : chars) {
- if (contains(ch)) {
- return false;
- }
- }
- return true;
- }
- String String::validate_filename() const {
- Vector<String> chars = String(invalid_filename_characters).split(" ");
- String name = strip_edges();
- for (int i = 0; i < chars.size(); i++) {
- name = name.replace(chars[i], "_");
- }
- return name;
- }
- bool String::is_valid_ip_address() const {
- if (find(":") >= 0) {
- Vector<String> ip = split(":");
- for (int i = 0; i < ip.size(); i++) {
- const String &n = ip[i];
- if (n.is_empty()) {
- continue;
- }
- if (n.is_valid_hex_number(false)) {
- int64_t nint = n.hex_to_int();
- if (nint < 0 || nint > 0xffff) {
- return false;
- }
- continue;
- }
- if (!n.is_valid_ip_address()) {
- return false;
- }
- }
- } else {
- Vector<String> ip = split(".");
- if (ip.size() != 4) {
- return false;
- }
- for (int i = 0; i < ip.size(); i++) {
- const String &n = ip[i];
- if (!n.is_valid_int()) {
- return false;
- }
- int val = n.to_int();
- if (val < 0 || val > 255) {
- return false;
- }
- }
- }
- return true;
- }
- bool String::is_resource_file() const {
- return begins_with("res://") && find("::") == -1;
- }
- bool String::is_relative_path() const {
- return !is_absolute_path();
- }
- String String::get_base_dir() const {
- int end = 0;
- // URL scheme style base.
- int basepos = find("://");
- if (basepos != -1) {
- end = basepos + 3;
- }
- // Windows top level directory base.
- if (end == 0) {
- basepos = find(":/");
- if (basepos == -1) {
- basepos = find(":\\");
- }
- if (basepos != -1) {
- end = basepos + 2;
- }
- }
- // Windows UNC network share path.
- if (end == 0) {
- if (is_network_share_path()) {
- basepos = find("/", 2);
- if (basepos == -1) {
- basepos = find("\\", 2);
- }
- int servpos = find("/", basepos + 1);
- if (servpos == -1) {
- servpos = find("\\", basepos + 1);
- }
- if (servpos != -1) {
- end = servpos + 1;
- }
- }
- }
- // Unix root directory base.
- if (end == 0) {
- if (begins_with("/")) {
- end = 1;
- }
- }
- String rs;
- String base;
- if (end != 0) {
- rs = substr(end, length());
- base = substr(0, end);
- } else {
- rs = *this;
- }
- int sep = MAX(rs.rfind("/"), rs.rfind("\\"));
- if (sep == -1) {
- return base;
- }
- return base + rs.substr(0, sep);
- }
- String String::get_file() const {
- int sep = MAX(rfind("/"), rfind("\\"));
- if (sep == -1) {
- return *this;
- }
- return substr(sep + 1, length());
- }
- String String::get_extension() const {
- int pos = rfind(".");
- if (pos < 0 || pos < MAX(rfind("/"), rfind("\\"))) {
- return "";
- }
- return substr(pos + 1, length());
- }
- String String::path_join(const String &p_file) const {
- if (is_empty()) {
- return p_file;
- }
- if (operator[](length() - 1) == '/' || (p_file.size() > 0 && p_file.operator[](0) == '/')) {
- return *this + p_file;
- }
- return *this + "/" + p_file;
- }
- String String::property_name_encode() const {
- // Escape and quote strings with extended ASCII or further Unicode characters
- // as well as '"', '=' or ' ' (32)
- const char32_t *cstr = get_data();
- for (int i = 0; cstr[i]; i++) {
- if (cstr[i] == '=' || cstr[i] == '"' || cstr[i] == ';' || cstr[i] == '[' || cstr[i] == ']' || cstr[i] < 33 || cstr[i] > 126) {
- return "\"" + c_escape_multiline() + "\"";
- }
- }
- // Keep as is
- return *this;
- }
- // Changes made to the set of invalid characters must also be reflected in the String documentation.
- static const char32_t invalid_node_name_characters[] = { '.', ':', '@', '/', '\"', UNIQUE_NODE_PREFIX[0], 0 };
- String String::get_invalid_node_name_characters(bool p_allow_internal) {
- // Do not use this function for critical validation.
- String r;
- const char32_t *c = invalid_node_name_characters;
- while (*c) {
- if (p_allow_internal && *c == '@') {
- c++;
- continue;
- }
- if (c != invalid_node_name_characters) {
- r += " ";
- }
- r += String::chr(*c);
- c++;
- }
- return r;
- }
- String String::validate_node_name() const {
- // This is a critical validation in node addition, so it must be optimized.
- const char32_t *cn = ptr();
- if (cn == nullptr) {
- return String();
- }
- bool valid = true;
- uint32_t idx = 0;
- while (cn[idx]) {
- const char32_t *c = invalid_node_name_characters;
- while (*c) {
- if (cn[idx] == *c) {
- valid = false;
- break;
- }
- c++;
- }
- if (!valid) {
- break;
- }
- idx++;
- }
- if (valid) {
- return *this;
- }
- String validated = *this;
- char32_t *nn = validated.ptrw();
- while (nn[idx]) {
- const char32_t *c = invalid_node_name_characters;
- while (*c) {
- if (nn[idx] == *c) {
- nn[idx] = '_';
- break;
- }
- c++;
- }
- idx++;
- }
- return validated;
- }
- String String::get_basename() const {
- int pos = rfind(".");
- if (pos < 0 || pos < MAX(rfind("/"), rfind("\\"))) {
- return *this;
- }
- return substr(0, pos);
- }
- String itos(int64_t p_val) {
- return String::num_int64(p_val);
- }
- String uitos(uint64_t p_val) {
- return String::num_uint64(p_val);
- }
- String rtos(double p_val) {
- return String::num(p_val);
- }
- String rtoss(double p_val) {
- return String::num_scientific(p_val);
- }
- // Right-pad with a character.
- String String::rpad(int min_length, const String &character) const {
- String s = *this;
- int padding = min_length - s.length();
- if (padding > 0) {
- s += character.repeat(padding);
- }
- return s;
- }
- // Left-pad with a character.
- String String::lpad(int min_length, const String &character) const {
- String s = *this;
- int padding = min_length - s.length();
- if (padding > 0) {
- s = character.repeat(padding) + s;
- }
- return s;
- }
- // sprintf is implemented in GDScript via:
- // "fish %s pie" % "frog"
- // "fish %s %d pie" % ["frog", 12]
- // In case of an error, the string returned is the error description and "error" is true.
- String String::sprintf(const Array &values, bool *error) const {
- String formatted;
- char32_t *self = (char32_t *)get_data();
- bool in_format = false;
- int value_index = 0;
- int min_chars = 0;
- int min_decimals = 0;
- bool in_decimals = false;
- bool pad_with_zeros = false;
- bool left_justified = false;
- bool show_sign = false;
- bool as_unsigned = false;
- if (error) {
- *error = true;
- }
- for (; *self; self++) {
- const char32_t c = *self;
- if (in_format) { // We have % - let's see what else we get.
- switch (c) {
- case '%': { // Replace %% with %
- formatted += chr(c);
- in_format = false;
- break;
- }
- case 'd': // Integer (signed)
- case 'o': // Octal
- case 'x': // Hexadecimal (lowercase)
- case 'X': { // Hexadecimal (uppercase)
- if (value_index >= values.size()) {
- return "not enough arguments for format string";
- }
- if (!values[value_index].is_num()) {
- return "a number is required";
- }
- int64_t value = values[value_index];
- int base = 16;
- bool capitalize = false;
- switch (c) {
- case 'd':
- base = 10;
- break;
- case 'o':
- base = 8;
- break;
- case 'x':
- break;
- case 'X':
- capitalize = true;
- break;
- }
- // Get basic number.
- String str;
- if (!as_unsigned) {
- str = String::num_int64(ABS(value), base, capitalize);
- } else {
- uint64_t uvalue = *((uint64_t *)&value);
- // In unsigned hex, if the value fits in 32 bits, trim it down to that.
- if (base == 16 && value < 0 && value >= INT32_MIN) {
- uvalue &= 0xffffffff;
- }
- str = String::num_uint64(uvalue, base, capitalize);
- }
- int number_len = str.length();
- bool negative = value < 0 && !as_unsigned;
- // Padding.
- int pad_chars_count = (negative || show_sign) ? min_chars - 1 : min_chars;
- String pad_char = pad_with_zeros ? String("0") : String(" ");
- if (left_justified) {
- str = str.rpad(pad_chars_count, pad_char);
- } else {
- str = str.lpad(pad_chars_count, pad_char);
- }
- // Sign.
- if (show_sign || negative) {
- String sign_char = negative ? "-" : "+";
- if (left_justified) {
- str = str.insert(0, sign_char);
- } else {
- str = str.insert(pad_with_zeros ? 0 : str.length() - number_len, sign_char);
- }
- }
- formatted += str;
- ++value_index;
- in_format = false;
- break;
- }
- case 'f': { // Float
- if (value_index >= values.size()) {
- return "not enough arguments for format string";
- }
- if (!values[value_index].is_num()) {
- return "a number is required";
- }
- double value = values[value_index];
- bool is_negative = signbit(value);
- String str = String::num(Math::abs(value), min_decimals);
- const bool is_finite = Math::is_finite(value);
- // Pad decimals out.
- if (is_finite) {
- str = str.pad_decimals(min_decimals);
- }
- int initial_len = str.length();
- // Padding. Leave room for sign later if required.
- int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars;
- String pad_char = (pad_with_zeros && is_finite) ? String("0") : String(" "); // Never pad NaN or inf with zeros
- if (left_justified) {
- str = str.rpad(pad_chars_count, pad_char);
- } else {
- str = str.lpad(pad_chars_count, pad_char);
- }
- // Add sign if needed.
- if (show_sign || is_negative) {
- String sign_char = is_negative ? "-" : "+";
- if (left_justified) {
- str = str.insert(0, sign_char);
- } else {
- str = str.insert(pad_with_zeros ? 0 : str.length() - initial_len, sign_char);
- }
- }
- formatted += str;
- ++value_index;
- in_format = false;
- break;
- }
- case 'v': { // Vector2/3/4/2i/3i/4i
- if (value_index >= values.size()) {
- return "not enough arguments for format string";
- }
- int count;
- switch (values[value_index].get_type()) {
- case Variant::VECTOR2:
- case Variant::VECTOR2I: {
- count = 2;
- } break;
- case Variant::VECTOR3:
- case Variant::VECTOR3I: {
- count = 3;
- } break;
- case Variant::VECTOR4:
- case Variant::VECTOR4I: {
- count = 4;
- } break;
- default: {
- return "%v requires a vector type (Vector2/3/4/2i/3i/4i)";
- }
- }
- Vector4 vec = values[value_index];
- String str = "(";
- for (int i = 0; i < count; i++) {
- double val = vec[i];
- String number_str = String::num(Math::abs(val), min_decimals);
- const bool is_finite = Math::is_finite(val);
- // Pad decimals out.
- if (is_finite) {
- number_str = number_str.pad_decimals(min_decimals);
- }
- int initial_len = number_str.length();
- // Padding. Leave room for sign later if required.
- int pad_chars_count = val < 0 ? min_chars - 1 : min_chars;
- String pad_char = (pad_with_zeros && is_finite) ? String("0") : String(" "); // Never pad NaN or inf with zeros
- if (left_justified) {
- number_str = number_str.rpad(pad_chars_count, pad_char);
- } else {
- number_str = number_str.lpad(pad_chars_count, pad_char);
- }
- // Add sign if needed.
- if (val < 0) {
- if (left_justified) {
- number_str = number_str.insert(0, "-");
- } else {
- number_str = number_str.insert(pad_with_zeros ? 0 : number_str.length() - initial_len, "-");
- }
- }
- // Add number to combined string
- str += number_str;
- if (i < count - 1) {
- str += ", ";
- }
- }
- str += ")";
- formatted += str;
- ++value_index;
- in_format = false;
- break;
- }
- case 's': { // String
- if (value_index >= values.size()) {
- return "not enough arguments for format string";
- }
- String str = values[value_index];
- // Padding.
- if (left_justified) {
- str = str.rpad(min_chars);
- } else {
- str = str.lpad(min_chars);
- }
- formatted += str;
- ++value_index;
- in_format = false;
- break;
- }
- case 'c': {
- if (value_index >= values.size()) {
- return "not enough arguments for format string";
- }
- // Convert to character.
- String str;
- if (values[value_index].is_num()) {
- int value = values[value_index];
- if (value < 0) {
- return "unsigned integer is lower than minimum";
- } else if (value >= 0xd800 && value <= 0xdfff) {
- return "unsigned integer is invalid Unicode character";
- } else if (value > 0x10ffff) {
- return "unsigned integer is greater than maximum";
- }
- str = chr(values[value_index]);
- } else if (values[value_index].get_type() == Variant::STRING) {
- str = values[value_index];
- if (str.length() != 1) {
- return "%c requires number or single-character string";
- }
- } else {
- return "%c requires number or single-character string";
- }
- // Padding.
- if (left_justified) {
- str = str.rpad(min_chars);
- } else {
- str = str.lpad(min_chars);
- }
- formatted += str;
- ++value_index;
- in_format = false;
- break;
- }
- case '-': { // Left justify
- left_justified = true;
- break;
- }
- case '+': { // Show + if positive.
- show_sign = true;
- break;
- }
- case 'u': { // Treat as unsigned (for int/hex).
- as_unsigned = true;
- break;
- }
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9': {
- int n = c - '0';
- if (in_decimals) {
- min_decimals *= 10;
- min_decimals += n;
- } else {
- if (c == '0' && min_chars == 0) {
- if (left_justified) {
- WARN_PRINT("'0' flag ignored with '-' flag in string format");
- } else {
- pad_with_zeros = true;
- }
- } else {
- min_chars *= 10;
- min_chars += n;
- }
- }
- break;
- }
- case '.': { // Float/Vector separator.
- if (in_decimals) {
- return "too many decimal points in format";
- }
- in_decimals = true;
- min_decimals = 0; // We want to add the value manually.
- break;
- }
- case '*': { // Dynamic width, based on value.
- if (value_index >= values.size()) {
- return "not enough arguments for format string";
- }
- Variant::Type value_type = values[value_index].get_type();
- if (!values[value_index].is_num() &&
- value_type != Variant::VECTOR2 && value_type != Variant::VECTOR2I &&
- value_type != Variant::VECTOR3 && value_type != Variant::VECTOR3I &&
- value_type != Variant::VECTOR4 && value_type != Variant::VECTOR4I) {
- return "* wants number or vector";
- }
- int size = values[value_index];
- if (in_decimals) {
- min_decimals = size;
- } else {
- min_chars = size;
- }
- ++value_index;
- break;
- }
- default: {
- return "unsupported format character";
- }
- }
- } else { // Not in format string.
- switch (c) {
- case '%':
- in_format = true;
- // Back to defaults:
- min_chars = 0;
- min_decimals = 6;
- pad_with_zeros = false;
- left_justified = false;
- show_sign = false;
- in_decimals = false;
- break;
- default:
- formatted += chr(c);
- }
- }
- }
- if (in_format) {
- return "incomplete format";
- }
- if (value_index != values.size()) {
- return "not all arguments converted during string formatting";
- }
- if (error) {
- *error = false;
- }
- return formatted;
- }
- String String::quote(const String "echar) const {
- return quotechar + *this + quotechar;
- }
- String String::unquote() const {
- if (!is_quoted()) {
- return *this;
- }
- return substr(1, length() - 2);
- }
- Vector<uint8_t> String::to_ascii_buffer() const {
- const String *s = this;
- if (s->is_empty()) {
- return Vector<uint8_t>();
- }
- CharString charstr = s->ascii();
- Vector<uint8_t> retval;
- size_t len = charstr.length();
- retval.resize(len);
- uint8_t *w = retval.ptrw();
- memcpy(w, charstr.ptr(), len);
- return retval;
- }
- Vector<uint8_t> String::to_utf8_buffer() const {
- const String *s = this;
- if (s->is_empty()) {
- return Vector<uint8_t>();
- }
- CharString charstr = s->utf8();
- Vector<uint8_t> retval;
- size_t len = charstr.length();
- retval.resize(len);
- uint8_t *w = retval.ptrw();
- memcpy(w, charstr.ptr(), len);
- return retval;
- }
- Vector<uint8_t> String::to_utf16_buffer() const {
- const String *s = this;
- if (s->is_empty()) {
- return Vector<uint8_t>();
- }
- Char16String charstr = s->utf16();
- Vector<uint8_t> retval;
- size_t len = charstr.length() * sizeof(char16_t);
- retval.resize(len);
- uint8_t *w = retval.ptrw();
- memcpy(w, (const void *)charstr.ptr(), len);
- return retval;
- }
- Vector<uint8_t> String::to_utf32_buffer() const {
- const String *s = this;
- if (s->is_empty()) {
- return Vector<uint8_t>();
- }
- Vector<uint8_t> retval;
- size_t len = s->length() * sizeof(char32_t);
- retval.resize(len);
- uint8_t *w = retval.ptrw();
- memcpy(w, (const void *)s->ptr(), len);
- return retval;
- }
- Vector<uint8_t> String::to_wchar_buffer() const {
- #ifdef WINDOWS_ENABLED
- return to_utf16_buffer();
- #else
- return to_utf32_buffer();
- #endif
- }
- #ifdef TOOLS_ENABLED
- /**
- * "Tools TRanslate". Performs string replacement for internationalization
- * within the editor. A translation context can optionally be specified to
- * disambiguate between identical source strings in translations. When
- * placeholders are desired, use `vformat(TTR("Example: %s"), some_string)`.
- * If a string mentions a quantity (and may therefore need a dynamic plural form),
- * use `TTRN()` instead of `TTR()`.
- *
- * NOTE: Only use `TTR()` in editor-only code (typically within the `editor/` folder).
- * For translations that can be supplied by exported projects, use `RTR()` instead.
- */
- String TTR(const String &p_text, const String &p_context) {
- if (TranslationServer::get_singleton()) {
- return TranslationServer::get_singleton()->tool_translate(p_text, p_context);
- }
- return p_text;
- }
- /**
- * "Tools TRanslate for N items". Performs string replacement for
- * internationalization within the editor. A translation context can optionally
- * be specified to disambiguate between identical source strings in
- * translations. Use `TTR()` if the string doesn't need dynamic plural form.
- * When placeholders are desired, use
- * `vformat(TTRN("%d item", "%d items", some_integer), some_integer)`.
- * The placeholder must be present in both strings to avoid run-time warnings in `vformat()`.
- *
- * NOTE: Only use `TTRN()` in editor-only code (typically within the `editor/` folder).
- * For translations that can be supplied by exported projects, use `RTRN()` instead.
- */
- String TTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
- if (TranslationServer::get_singleton()) {
- return TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
- }
- // Return message based on English plural rule if translation is not possible.
- if (p_n == 1) {
- return p_text;
- }
- return p_text_plural;
- }
- /**
- * "Docs TRanslate". Used for the editor class reference documentation,
- * handling descriptions extracted from the XML.
- * It also replaces `$DOCS_URL` with the actual URL to the documentation's branch,
- * to allow dehardcoding it in the XML and doing proper substitutions everywhere.
- */
- String DTR(const String &p_text, const String &p_context) {
- // Comes straight from the XML, so remove indentation and any trailing whitespace.
- const String text = p_text.dedent().strip_edges();
- if (TranslationServer::get_singleton()) {
- return String(TranslationServer::get_singleton()->doc_translate(text, p_context)).replace("$DOCS_URL", VERSION_DOCS_URL);
- }
- return text.replace("$DOCS_URL", VERSION_DOCS_URL);
- }
- /**
- * "Docs TRanslate for N items". Used for the editor class reference documentation
- * (with support for plurals), handling descriptions extracted from the XML.
- * It also replaces `$DOCS_URL` with the actual URL to the documentation's branch,
- * to allow dehardcoding it in the XML and doing proper substitutions everywhere.
- */
- String DTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
- const String text = p_text.dedent().strip_edges();
- const String text_plural = p_text_plural.dedent().strip_edges();
- if (TranslationServer::get_singleton()) {
- return String(TranslationServer::get_singleton()->doc_translate_plural(text, text_plural, p_n, p_context)).replace("$DOCS_URL", VERSION_DOCS_URL);
- }
- // Return message based on English plural rule if translation is not possible.
- if (p_n == 1) {
- return text.replace("$DOCS_URL", VERSION_DOCS_URL);
- }
- return text_plural.replace("$DOCS_URL", VERSION_DOCS_URL);
- }
- #endif
- /**
- * "Run-time TRanslate". Performs string replacement for internationalization
- * without the editor. A translation context can optionally be specified to
- * disambiguate between identical source strings in translations. When
- * placeholders are desired, use `vformat(RTR("Example: %s"), some_string)`.
- * If a string mentions a quantity (and may therefore need a dynamic plural form),
- * use `RTRN()` instead of `RTR()`.
- *
- * NOTE: Do not use `RTR()` in editor-only code (typically within the `editor/`
- * folder). For editor translations, use `TTR()` instead.
- */
- String RTR(const String &p_text, const String &p_context) {
- if (TranslationServer::get_singleton()) {
- String rtr = TranslationServer::get_singleton()->tool_translate(p_text, p_context);
- if (rtr.is_empty() || rtr == p_text) {
- return TranslationServer::get_singleton()->translate(p_text, p_context);
- }
- return rtr;
- }
- return p_text;
- }
- /**
- * "Run-time TRanslate for N items". Performs string replacement for
- * internationalization without the editor. A translation context can optionally
- * be specified to disambiguate between identical source strings in translations.
- * Use `RTR()` if the string doesn't need dynamic plural form. When placeholders
- * are desired, use `vformat(RTRN("%d item", "%d items", some_integer), some_integer)`.
- * The placeholder must be present in both strings to avoid run-time warnings in `vformat()`.
- *
- * NOTE: Do not use `RTRN()` in editor-only code (typically within the `editor/`
- * folder). For editor translations, use `TTRN()` instead.
- */
- String RTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
- if (TranslationServer::get_singleton()) {
- String rtr = TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
- if (rtr.is_empty() || rtr == p_text || rtr == p_text_plural) {
- return TranslationServer::get_singleton()->translate_plural(p_text, p_text_plural, p_n, p_context);
- }
- return rtr;
- }
- // Return message based on English plural rule if translation is not possible.
- if (p_n == 1) {
- return p_text;
- }
- return p_text_plural;
- }
|