ustring.cpp 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488
  1. /*************************************************************************/
  2. /* ustring.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef _MSC_VER
  31. #define _CRT_SECURE_NO_WARNINGS // to disable build-time warning which suggested to use strcpy_s instead strcpy
  32. #endif
  33. #include "ustring.h"
  34. #include "core/color.h"
  35. #include "core/crypto/crypto_core.h"
  36. #include "core/math/math_funcs.h"
  37. #include "core/os/memory.h"
  38. #include "core/print_string.h"
  39. #include "core/translation.h"
  40. #include "core/ucaps.h"
  41. #include "core/variant.h"
  42. #include <wchar.h>
  43. #include <cstdint>
  44. #ifndef NO_USE_STDLIB
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #endif
  48. #if defined(MINGW_ENABLED) || defined(_MSC_VER)
  49. #define snprintf _snprintf_s
  50. #endif
  51. #define MAX_DIGITS 6
  52. #define UPPERCASE(m_c) (((m_c) >= 'a' && (m_c) <= 'z') ? ((m_c) - ('a' - 'A')) : (m_c))
  53. #define LOWERCASE(m_c) (((m_c) >= 'A' && (m_c) <= 'Z') ? ((m_c) + ('a' - 'A')) : (m_c))
  54. #define IS_DIGIT(m_d) ((m_d) >= '0' && (m_d) <= '9')
  55. #define IS_HEX_DIGIT(m_d) (((m_d) >= '0' && (m_d) <= '9') || ((m_d) >= 'a' && (m_d) <= 'f') || ((m_d) >= 'A' && (m_d) <= 'F'))
  56. const char CharString::_null = 0;
  57. const CharType String::_null = 0;
  58. bool is_symbol(CharType c) {
  59. return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' ');
  60. }
  61. bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end) {
  62. const String &s = p_s;
  63. int beg = CLAMP(p_col, 0, s.length());
  64. int end = beg;
  65. if (s[beg] > 32 || beg == s.length()) {
  66. bool symbol = beg < s.length() && is_symbol(s[beg]);
  67. while (beg > 0 && s[beg - 1] > 32 && (symbol == is_symbol(s[beg - 1]))) {
  68. beg--;
  69. }
  70. while (end < s.length() && s[end + 1] > 32 && (symbol == is_symbol(s[end + 1]))) {
  71. end++;
  72. }
  73. if (end < s.length()) {
  74. end += 1;
  75. }
  76. r_beg = beg;
  77. r_end = end;
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. /** STRING **/
  84. bool CharString::operator<(const CharString &p_right) const {
  85. if (length() == 0) {
  86. return p_right.length() != 0;
  87. }
  88. return is_str_less(get_data(), p_right.get_data());
  89. }
  90. CharString &CharString::operator+=(char p_char) {
  91. resize(size() ? size() + 1 : 2);
  92. set(length(), 0);
  93. set(length() - 1, p_char);
  94. return *this;
  95. }
  96. const char *CharString::get_data() const {
  97. if (size()) {
  98. return &operator[](0);
  99. } else {
  100. return "";
  101. }
  102. }
  103. CharString &CharString::operator=(const char *p_cstr) {
  104. copy_from(p_cstr);
  105. return *this;
  106. }
  107. void CharString::copy_from(const char *p_cstr) {
  108. if (!p_cstr) {
  109. resize(0);
  110. return;
  111. }
  112. size_t len = strlen(p_cstr);
  113. if (len == 0) {
  114. resize(0);
  115. return;
  116. }
  117. resize(len + 1); // include terminating null char
  118. strcpy(ptrw(), p_cstr);
  119. }
  120. Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const {
  121. // Splits the URL into scheme, host, port, path. Strip credentials when present.
  122. String base = *this;
  123. r_scheme = "";
  124. r_host = "";
  125. r_port = 0;
  126. r_path = "";
  127. int pos = base.find("://");
  128. // Scheme
  129. if (pos != -1) {
  130. r_scheme = base.substr(0, pos + 3).to_lower();
  131. base = base.substr(pos + 3, base.length() - pos - 3);
  132. }
  133. pos = base.find("/");
  134. // Path
  135. if (pos != -1) {
  136. r_path = base.substr(pos, base.length() - pos);
  137. base = base.substr(0, pos);
  138. }
  139. // Host
  140. pos = base.find("@");
  141. if (pos != -1) {
  142. // Strip credentials
  143. base = base.substr(pos + 1, base.length() - pos - 1);
  144. }
  145. if (base.begins_with("[")) {
  146. // Literal IPv6
  147. pos = base.rfind("]");
  148. if (pos == -1) {
  149. return ERR_INVALID_PARAMETER;
  150. }
  151. r_host = base.substr(1, pos - 1);
  152. base = base.substr(pos + 1, base.length() - pos - 1);
  153. } else {
  154. // Anything else
  155. if (base.get_slice_count(":") > 1) {
  156. return ERR_INVALID_PARAMETER;
  157. }
  158. pos = base.rfind(":");
  159. if (pos == -1) {
  160. r_host = base;
  161. base = "";
  162. } else {
  163. r_host = base.substr(0, pos);
  164. base = base.substr(pos, base.length() - pos);
  165. }
  166. }
  167. if (r_host.empty()) {
  168. return ERR_INVALID_PARAMETER;
  169. }
  170. r_host = r_host.to_lower();
  171. // Port
  172. if (base.begins_with(":")) {
  173. base = base.substr(1, base.length() - 1);
  174. if (!base.is_valid_integer()) {
  175. return ERR_INVALID_PARAMETER;
  176. }
  177. r_port = base.to_int();
  178. if (r_port < 1 || r_port > 65535) {
  179. return ERR_INVALID_PARAMETER;
  180. }
  181. }
  182. return OK;
  183. }
  184. void String::copy_from(const char *p_cstr) {
  185. if (!p_cstr) {
  186. resize(0);
  187. return;
  188. }
  189. int len = 0;
  190. const char *ptr = p_cstr;
  191. while (*(ptr++) != 0) {
  192. len++;
  193. }
  194. if (len == 0) {
  195. resize(0);
  196. return;
  197. }
  198. resize(len + 1); // include 0
  199. CharType *dst = this->ptrw();
  200. for (int i = 0; i < len + 1; i++) {
  201. dst[i] = p_cstr[i];
  202. }
  203. }
  204. void String::copy_from(const CharType *p_cstr, const int p_clip_to) {
  205. if (!p_cstr) {
  206. resize(0);
  207. return;
  208. }
  209. int len = 0;
  210. const CharType *ptr = p_cstr;
  211. while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0) {
  212. len++;
  213. }
  214. if (len == 0) {
  215. resize(0);
  216. return;
  217. }
  218. copy_from_unchecked(p_cstr, len);
  219. }
  220. // assumes the following have already been validated:
  221. // p_char != NULL
  222. // p_length > 0
  223. // p_length <= p_char strlen
  224. void String::copy_from_unchecked(const CharType *p_char, const int p_length) {
  225. resize(p_length + 1);
  226. set(p_length, 0);
  227. CharType *dst = ptrw();
  228. for (int i = 0; i < p_length; i++) {
  229. dst[i] = p_char[i];
  230. }
  231. }
  232. void String::copy_from(const CharType &p_char) {
  233. resize(2);
  234. set(0, p_char);
  235. set(1, 0);
  236. }
  237. bool String::operator==(const String &p_str) const {
  238. if (length() != p_str.length()) {
  239. return false;
  240. }
  241. if (empty()) {
  242. return true;
  243. }
  244. int l = length();
  245. const CharType *src = c_str();
  246. const CharType *dst = p_str.c_str();
  247. /* Compare char by char */
  248. for (int i = 0; i < l; i++) {
  249. if (src[i] != dst[i]) {
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. bool String::operator!=(const String &p_str) const {
  256. return !(*this == p_str);
  257. }
  258. String String::operator+(const String &p_str) const {
  259. String res = *this;
  260. res += p_str;
  261. return res;
  262. }
  263. /*
  264. String String::operator+(CharType p_chr) const {
  265. String res=*this;
  266. res+=p_chr;
  267. return res;
  268. }
  269. */
  270. String &String::operator+=(const String &p_str) {
  271. if (empty()) {
  272. *this = p_str;
  273. return *this;
  274. }
  275. if (p_str.empty()) {
  276. return *this;
  277. }
  278. int from = length();
  279. resize(length() + p_str.size());
  280. const CharType *src = p_str.c_str();
  281. CharType *dst = ptrw();
  282. set(length(), 0);
  283. for (int i = 0; i < p_str.length(); i++) {
  284. dst[from + i] = src[i];
  285. }
  286. return *this;
  287. }
  288. String &String::operator+=(const CharType *p_str) {
  289. *this += String(p_str);
  290. return *this;
  291. }
  292. String &String::operator+=(CharType p_char) {
  293. resize(size() ? size() + 1 : 2);
  294. set(length(), 0);
  295. set(length() - 1, p_char);
  296. return *this;
  297. }
  298. String &String::operator+=(const char *p_str) {
  299. if (!p_str || p_str[0] == 0) {
  300. return *this;
  301. }
  302. int src_len = 0;
  303. const char *ptr = p_str;
  304. while (*(ptr++) != 0) {
  305. src_len++;
  306. }
  307. int from = length();
  308. resize(from + src_len + 1);
  309. CharType *dst = ptrw();
  310. set(length(), 0);
  311. for (int i = 0; i < src_len; i++) {
  312. dst[from + i] = p_str[i];
  313. }
  314. return *this;
  315. }
  316. void String::operator=(const char *p_str) {
  317. copy_from(p_str);
  318. }
  319. void String::operator=(const CharType *p_str) {
  320. copy_from(p_str);
  321. }
  322. bool String::operator==(const StrRange &p_str_range) const {
  323. int len = p_str_range.len;
  324. if (length() != len) {
  325. return false;
  326. }
  327. if (empty()) {
  328. return true;
  329. }
  330. const CharType *c_str = p_str_range.c_str;
  331. const CharType *dst = &operator[](0);
  332. /* Compare char by char */
  333. for (int i = 0; i < len; i++) {
  334. if (c_str[i] != dst[i]) {
  335. return false;
  336. }
  337. }
  338. return true;
  339. }
  340. bool String::operator==(const char *p_str) const {
  341. int len = 0;
  342. const char *aux = p_str;
  343. while (*(aux++) != 0) {
  344. len++;
  345. }
  346. if (length() != len) {
  347. return false;
  348. }
  349. if (empty()) {
  350. return true;
  351. }
  352. int l = length();
  353. const CharType *dst = c_str();
  354. /* Compare char by char */
  355. for (int i = 0; i < l; i++) {
  356. if (p_str[i] != dst[i]) {
  357. return false;
  358. }
  359. }
  360. return true;
  361. }
  362. bool String::operator==(const CharType *p_str) const {
  363. int len = 0;
  364. const CharType *aux = p_str;
  365. while (*(aux++) != 0) {
  366. len++;
  367. }
  368. if (length() != len) {
  369. return false;
  370. }
  371. if (empty()) {
  372. return true;
  373. }
  374. int l = length();
  375. const CharType *dst = c_str();
  376. /* Compare char by char */
  377. for (int i = 0; i < l; i++) {
  378. if (p_str[i] != dst[i]) {
  379. return false;
  380. }
  381. }
  382. return true;
  383. }
  384. bool String::operator!=(const char *p_str) const {
  385. return (!(*this == p_str));
  386. }
  387. bool String::operator!=(const CharType *p_str) const {
  388. return (!(*this == p_str));
  389. }
  390. bool String::operator<(const CharType *p_str) const {
  391. if (empty() && p_str[0] == 0) {
  392. return false;
  393. }
  394. if (empty()) {
  395. return true;
  396. }
  397. return is_str_less(c_str(), p_str);
  398. }
  399. bool String::operator<=(const String &p_str) const {
  400. return (*this < p_str) || (*this == p_str);
  401. }
  402. bool String::operator<(const char *p_str) const {
  403. if (empty() && p_str[0] == 0) {
  404. return false;
  405. }
  406. if (empty()) {
  407. return true;
  408. }
  409. return is_str_less(c_str(), p_str);
  410. }
  411. bool String::operator<(const String &p_str) const {
  412. return operator<(p_str.c_str());
  413. }
  414. signed char String::nocasecmp_to(const String &p_str) const {
  415. if (empty() && p_str.empty()) {
  416. return 0;
  417. }
  418. if (empty()) {
  419. return -1;
  420. }
  421. if (p_str.empty()) {
  422. return 1;
  423. }
  424. const CharType *that_str = p_str.c_str();
  425. const CharType *this_str = c_str();
  426. while (true) {
  427. if (*that_str == 0 && *this_str == 0) {
  428. return 0; //we're equal
  429. } else if (*this_str == 0) {
  430. return -1; //if this is empty, and the other one is not, then we're less.. I think?
  431. } else if (*that_str == 0) {
  432. return 1; //otherwise the other one is smaller..
  433. } else if (_find_upper(*this_str) < _find_upper(*that_str)) { //more than
  434. return -1;
  435. } else if (_find_upper(*this_str) > _find_upper(*that_str)) { //less than
  436. return 1;
  437. }
  438. this_str++;
  439. that_str++;
  440. }
  441. }
  442. signed char String::casecmp_to(const String &p_str) const {
  443. if (empty() && p_str.empty()) {
  444. return 0;
  445. }
  446. if (empty()) {
  447. return -1;
  448. }
  449. if (p_str.empty()) {
  450. return 1;
  451. }
  452. const CharType *that_str = p_str.c_str();
  453. const CharType *this_str = c_str();
  454. while (true) {
  455. if (*that_str == 0 && *this_str == 0) {
  456. return 0; //we're equal
  457. } else if (*this_str == 0) {
  458. return -1; //if this is empty, and the other one is not, then we're less.. I think?
  459. } else if (*that_str == 0) {
  460. return 1; //otherwise the other one is smaller..
  461. } else if (*this_str < *that_str) { //more than
  462. return -1;
  463. } else if (*this_str > *that_str) { //less than
  464. return 1;
  465. }
  466. this_str++;
  467. that_str++;
  468. }
  469. }
  470. signed char String::naturalnocasecmp_to(const String &p_str) const {
  471. const CharType *this_str = c_str();
  472. const CharType *that_str = p_str.c_str();
  473. if (this_str && that_str) {
  474. while (*this_str == '.' || *that_str == '.') {
  475. if (*this_str++ != '.') {
  476. return 1;
  477. }
  478. if (*that_str++ != '.') {
  479. return -1;
  480. }
  481. if (!*that_str) {
  482. return 1;
  483. }
  484. if (!*this_str) {
  485. return -1;
  486. }
  487. }
  488. while (*this_str) {
  489. if (!*that_str) {
  490. return 1;
  491. } else if (IS_DIGIT(*this_str)) {
  492. if (!IS_DIGIT(*that_str)) {
  493. return -1;
  494. }
  495. // Keep ptrs to start of numerical sequences
  496. const CharType *this_substr = this_str;
  497. const CharType *that_substr = that_str;
  498. // Compare lengths of both numerical sequences, ignoring leading zeros
  499. while (IS_DIGIT(*this_str)) {
  500. this_str++;
  501. }
  502. while (IS_DIGIT(*that_str)) {
  503. that_str++;
  504. }
  505. while (*this_substr == '0') {
  506. this_substr++;
  507. }
  508. while (*that_substr == '0') {
  509. that_substr++;
  510. }
  511. int this_len = this_str - this_substr;
  512. int that_len = that_str - that_substr;
  513. if (this_len < that_len) {
  514. return -1;
  515. } else if (this_len > that_len) {
  516. return 1;
  517. }
  518. // If lengths equal, compare lexicographically
  519. while (this_substr != this_str && that_substr != that_str) {
  520. if (*this_substr < *that_substr) {
  521. return -1;
  522. } else if (*this_substr > *that_substr) {
  523. return 1;
  524. }
  525. this_substr++;
  526. that_substr++;
  527. }
  528. } else if (IS_DIGIT(*that_str)) {
  529. return 1;
  530. } else {
  531. if (_find_upper(*this_str) < _find_upper(*that_str)) { //more than
  532. return -1;
  533. } else if (_find_upper(*this_str) > _find_upper(*that_str)) { //less than
  534. return 1;
  535. }
  536. this_str++;
  537. that_str++;
  538. }
  539. }
  540. if (*that_str) {
  541. return -1;
  542. }
  543. }
  544. return 0;
  545. }
  546. void String::erase(int p_pos, int p_chars) {
  547. *this = left(p_pos) + substr(p_pos + p_chars, length() - ((p_pos + p_chars)));
  548. }
  549. String String::capitalize() const {
  550. String aux = this->camelcase_to_underscore(true).replace("_", " ").strip_edges();
  551. String cap;
  552. for (int i = 0; i < aux.get_slice_count(" "); i++) {
  553. String slice = aux.get_slicec(' ', i);
  554. if (slice.length() > 0) {
  555. slice[0] = _find_upper(slice[0]);
  556. if (i > 0) {
  557. cap += " ";
  558. }
  559. cap += slice;
  560. }
  561. }
  562. return cap;
  563. }
  564. String String::camelcase_to_underscore(bool lowercase) const {
  565. const CharType *cstr = c_str();
  566. String new_string;
  567. const char A = 'A', Z = 'Z';
  568. const char a = 'a', z = 'z';
  569. int start_index = 0;
  570. for (int i = 1; i < this->size(); i++) {
  571. bool is_upper = cstr[i] >= A && cstr[i] <= Z;
  572. bool is_number = cstr[i] >= '0' && cstr[i] <= '9';
  573. bool are_next_2_lower = false;
  574. bool is_next_lower = false;
  575. bool is_next_number = false;
  576. bool was_precedent_upper = cstr[i - 1] >= A && cstr[i - 1] <= Z;
  577. bool was_precedent_number = cstr[i - 1] >= '0' && cstr[i - 1] <= '9';
  578. if (i + 2 < this->size()) {
  579. are_next_2_lower = cstr[i + 1] >= a && cstr[i + 1] <= z && cstr[i + 2] >= a && cstr[i + 2] <= z;
  580. }
  581. if (i + 1 < this->size()) {
  582. is_next_lower = cstr[i + 1] >= a && cstr[i + 1] <= z;
  583. is_next_number = cstr[i + 1] >= '0' && cstr[i + 1] <= '9';
  584. }
  585. const bool cond_a = is_upper && !was_precedent_upper && !was_precedent_number;
  586. const bool cond_b = was_precedent_upper && is_upper && are_next_2_lower;
  587. const bool cond_c = is_number && !was_precedent_number;
  588. const bool can_break_number_letter = is_number && !was_precedent_number && is_next_lower;
  589. const bool can_break_letter_number = !is_number && was_precedent_number && (is_next_lower || is_next_number);
  590. bool should_split = cond_a || cond_b || cond_c || can_break_number_letter || can_break_letter_number;
  591. if (should_split) {
  592. new_string += this->substr(start_index, i - start_index) + "_";
  593. start_index = i;
  594. }
  595. }
  596. new_string += this->substr(start_index, this->size() - start_index);
  597. return lowercase ? new_string.to_lower() : new_string;
  598. }
  599. int String::get_slice_count(String p_splitter) const {
  600. if (empty()) {
  601. return 0;
  602. }
  603. if (p_splitter.empty()) {
  604. return 0;
  605. }
  606. int pos = 0;
  607. int slices = 1;
  608. while ((pos = find(p_splitter, pos)) >= 0) {
  609. slices++;
  610. pos += p_splitter.length();
  611. }
  612. return slices;
  613. }
  614. String String::get_slice(String p_splitter, int p_slice) const {
  615. if (empty() || p_splitter.empty()) {
  616. return "";
  617. }
  618. int pos = 0;
  619. int prev_pos = 0;
  620. //int slices=1;
  621. if (p_slice < 0) {
  622. return "";
  623. }
  624. if (find(p_splitter) == -1) {
  625. return *this;
  626. }
  627. int i = 0;
  628. while (true) {
  629. pos = find(p_splitter, pos);
  630. if (pos == -1) {
  631. pos = length(); //reached end
  632. }
  633. int from = prev_pos;
  634. //int to=pos;
  635. if (p_slice == i) {
  636. return substr(from, pos - from);
  637. }
  638. if (pos == length()) { //reached end and no find
  639. break;
  640. }
  641. pos += p_splitter.length();
  642. prev_pos = pos;
  643. i++;
  644. }
  645. return ""; //no find!
  646. }
  647. String String::get_slicec(CharType p_splitter, int p_slice) const {
  648. if (empty()) {
  649. return String();
  650. }
  651. if (p_slice < 0) {
  652. return String();
  653. }
  654. const CharType *c = this->ptr();
  655. int i = 0;
  656. int prev = 0;
  657. int count = 0;
  658. while (true) {
  659. if (c[i] == 0 || c[i] == p_splitter) {
  660. if (p_slice == count) {
  661. return substr(prev, i - prev);
  662. } else if (c[i] == 0) {
  663. return String();
  664. } else {
  665. count++;
  666. prev = i + 1;
  667. }
  668. }
  669. i++;
  670. }
  671. }
  672. Vector<String> String::split_spaces() const {
  673. Vector<String> ret;
  674. int from = 0;
  675. int i = 0;
  676. int len = length();
  677. if (len == 0) {
  678. return ret;
  679. }
  680. bool inside = false;
  681. while (true) {
  682. bool empty = operator[](i) < 33;
  683. if (i == 0) {
  684. inside = !empty;
  685. }
  686. if (!empty && !inside) {
  687. inside = true;
  688. from = i;
  689. }
  690. if (empty && inside) {
  691. ret.push_back(substr(from, i - from));
  692. inside = false;
  693. }
  694. if (i == len) {
  695. break;
  696. }
  697. i++;
  698. }
  699. return ret;
  700. }
  701. Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
  702. Vector<String> ret;
  703. int from = 0;
  704. int len = length();
  705. while (true) {
  706. int end = find(p_splitter, from);
  707. if (end < 0) {
  708. end = len;
  709. }
  710. if (p_allow_empty || (end > from)) {
  711. if (p_maxsplit <= 0) {
  712. ret.push_back(substr(from, end - from));
  713. } else {
  714. // Put rest of the string and leave cycle.
  715. if (p_maxsplit == ret.size()) {
  716. ret.push_back(substr(from, len));
  717. break;
  718. }
  719. // Otherwise, push items until positive limit is reached.
  720. ret.push_back(substr(from, end - from));
  721. }
  722. }
  723. if (end == len) {
  724. break;
  725. }
  726. from = end + p_splitter.length();
  727. }
  728. return ret;
  729. }
  730. Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
  731. Vector<String> ret;
  732. const int len = length();
  733. int remaining_len = len;
  734. while (true) {
  735. if (remaining_len < p_splitter.length() || (p_maxsplit > 0 && p_maxsplit == ret.size())) {
  736. // no room for another splitter or hit max splits, push what's left and we're done
  737. if (p_allow_empty || remaining_len > 0) {
  738. ret.push_back(substr(0, remaining_len));
  739. }
  740. break;
  741. }
  742. int left_edge = rfind(p_splitter, remaining_len - p_splitter.length());
  743. if (left_edge < 0) {
  744. // no more splitters, we're done
  745. ret.push_back(substr(0, remaining_len));
  746. break;
  747. }
  748. int substr_start = left_edge + p_splitter.length();
  749. if (p_allow_empty || substr_start < remaining_len) {
  750. ret.push_back(substr(substr_start, remaining_len - substr_start));
  751. }
  752. remaining_len = left_edge;
  753. }
  754. ret.invert();
  755. return ret;
  756. }
  757. Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty) const {
  758. Vector<float> ret;
  759. int from = 0;
  760. int len = length();
  761. while (true) {
  762. int end = find(p_splitter, from);
  763. if (end < 0) {
  764. end = len;
  765. }
  766. if (p_allow_empty || (end > from)) {
  767. ret.push_back(String::to_double(&c_str()[from]));
  768. }
  769. if (end == len) {
  770. break;
  771. }
  772. from = end + p_splitter.length();
  773. }
  774. return ret;
  775. }
  776. Vector<float> String::split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
  777. Vector<float> ret;
  778. int from = 0;
  779. int len = length();
  780. while (true) {
  781. int idx;
  782. int end = findmk(p_splitters, from, &idx);
  783. int spl_len = 1;
  784. if (end < 0) {
  785. end = len;
  786. } else {
  787. spl_len = p_splitters[idx].length();
  788. }
  789. if (p_allow_empty || (end > from)) {
  790. ret.push_back(String::to_double(&c_str()[from]));
  791. }
  792. if (end == len) {
  793. break;
  794. }
  795. from = end + spl_len;
  796. }
  797. return ret;
  798. }
  799. Vector<int> String::split_ints(const String &p_splitter, bool p_allow_empty) const {
  800. Vector<int> ret;
  801. int from = 0;
  802. int len = length();
  803. while (true) {
  804. int end = find(p_splitter, from);
  805. if (end < 0) {
  806. end = len;
  807. }
  808. if (p_allow_empty || (end > from)) {
  809. ret.push_back(String::to_int(&c_str()[from], end - from));
  810. }
  811. if (end == len) {
  812. break;
  813. }
  814. from = end + p_splitter.length();
  815. }
  816. return ret;
  817. }
  818. Vector<int> String::split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
  819. Vector<int> ret;
  820. int from = 0;
  821. int len = length();
  822. while (true) {
  823. int idx;
  824. int end = findmk(p_splitters, from, &idx);
  825. int spl_len = 1;
  826. if (end < 0) {
  827. end = len;
  828. } else {
  829. spl_len = p_splitters[idx].length();
  830. }
  831. if (p_allow_empty || (end > from)) {
  832. ret.push_back(String::to_int(&c_str()[from], end - from));
  833. }
  834. if (end == len) {
  835. break;
  836. }
  837. from = end + spl_len;
  838. }
  839. return ret;
  840. }
  841. String String::join(Vector<String> parts) {
  842. String ret;
  843. for (int i = 0; i < parts.size(); ++i) {
  844. if (i > 0) {
  845. ret += *this;
  846. }
  847. ret += parts[i];
  848. }
  849. return ret;
  850. }
  851. CharType String::char_uppercase(CharType p_char) {
  852. return _find_upper(p_char);
  853. }
  854. CharType String::char_lowercase(CharType p_char) {
  855. return _find_lower(p_char);
  856. }
  857. String String::to_upper() const {
  858. String upper = *this;
  859. for (int i = 0; i < upper.size(); i++) {
  860. const CharType s = upper[i];
  861. const CharType t = _find_upper(s);
  862. if (s != t) { // avoid copy on write
  863. upper[i] = t;
  864. }
  865. }
  866. return upper;
  867. }
  868. String String::to_lower() const {
  869. String lower = *this;
  870. for (int i = 0; i < lower.size(); i++) {
  871. const CharType s = lower[i];
  872. const CharType t = _find_lower(s);
  873. if (s != t) { // avoid copy on write
  874. lower[i] = t;
  875. }
  876. }
  877. return lower;
  878. }
  879. const CharType *String::c_str() const {
  880. static const CharType zero = 0;
  881. return size() ? &operator[](0) : &zero;
  882. }
  883. String String::md5(const uint8_t *p_md5) {
  884. return String::hex_encode_buffer(p_md5, 16);
  885. }
  886. String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
  887. static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  888. String ret;
  889. char v[2] = { 0, 0 };
  890. for (int i = 0; i < p_len; i++) {
  891. v[0] = hex[p_buffer[i] >> 4];
  892. ret += v;
  893. v[0] = hex[p_buffer[i] & 0xF];
  894. ret += v;
  895. }
  896. return ret;
  897. }
  898. String String::chr(CharType p_char) {
  899. CharType c[2] = { p_char, 0 };
  900. return String(c);
  901. }
  902. String String::num(double p_num, int p_decimals) {
  903. #ifndef NO_USE_STDLIB
  904. if (p_decimals > 16) {
  905. p_decimals = 16;
  906. }
  907. char fmt[7];
  908. fmt[0] = '%';
  909. fmt[1] = '.';
  910. if (p_decimals < 0) {
  911. fmt[1] = 'l';
  912. fmt[2] = 'f';
  913. fmt[3] = 0;
  914. } else if (p_decimals < 10) {
  915. fmt[2] = '0' + p_decimals;
  916. fmt[3] = 'l';
  917. fmt[4] = 'f';
  918. fmt[5] = 0;
  919. } else {
  920. fmt[2] = '0' + (p_decimals / 10);
  921. fmt[3] = '0' + (p_decimals % 10);
  922. fmt[4] = 'l';
  923. fmt[5] = 'f';
  924. fmt[6] = 0;
  925. }
  926. char buf[256];
  927. #if defined(__GNUC__) || defined(_MSC_VER)
  928. snprintf(buf, 256, fmt, p_num);
  929. #else
  930. sprintf(buf, fmt, p_num);
  931. #endif
  932. buf[255] = 0;
  933. //destroy trailing zeroes
  934. {
  935. bool period = false;
  936. int z = 0;
  937. while (buf[z]) {
  938. if (buf[z] == '.') {
  939. period = true;
  940. }
  941. z++;
  942. }
  943. if (period) {
  944. z--;
  945. while (z > 0) {
  946. if (buf[z] == '0') {
  947. buf[z] = 0;
  948. } else if (buf[z] == '.') {
  949. buf[z] = 0;
  950. break;
  951. } else {
  952. break;
  953. }
  954. z--;
  955. }
  956. }
  957. }
  958. return buf;
  959. #else
  960. String s;
  961. String sd;
  962. /* integer part */
  963. bool neg = p_num < 0;
  964. p_num = ABS(p_num);
  965. int intn = (int)p_num;
  966. /* decimal part */
  967. if (p_decimals > 0 || (p_decimals == -1 && (int)p_num != p_num)) {
  968. double dec = p_num - (float)((int)p_num);
  969. int digit = 0;
  970. if (p_decimals > MAX_DIGITS)
  971. p_decimals = MAX_DIGITS;
  972. int dec_int = 0;
  973. int dec_max = 0;
  974. while (true) {
  975. dec *= 10.0;
  976. dec_int = dec_int * 10 + (int)dec % 10;
  977. dec_max = dec_max * 10 + 9;
  978. digit++;
  979. if (p_decimals == -1) {
  980. if (digit == MAX_DIGITS) //no point in going to infinite
  981. break;
  982. if ((dec - (float)((int)dec)) < 1e-6)
  983. break;
  984. }
  985. if (digit == p_decimals)
  986. break;
  987. }
  988. dec *= 10;
  989. int last = (int)dec % 10;
  990. if (last > 5) {
  991. if (dec_int == dec_max) {
  992. dec_int = 0;
  993. intn++;
  994. } else {
  995. dec_int++;
  996. }
  997. }
  998. String decimal;
  999. for (int i = 0; i < digit; i++) {
  1000. char num[2] = { 0, 0 };
  1001. num[0] = '0' + dec_int % 10;
  1002. decimal = num + decimal;
  1003. dec_int /= 10;
  1004. }
  1005. sd = '.' + decimal;
  1006. }
  1007. if (intn == 0)
  1008. s = "0";
  1009. else {
  1010. while (intn) {
  1011. CharType num = '0' + (intn % 10);
  1012. intn /= 10;
  1013. s = num + s;
  1014. }
  1015. }
  1016. s = s + sd;
  1017. if (neg)
  1018. s = "-" + s;
  1019. return s;
  1020. #endif
  1021. }
  1022. String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
  1023. bool sign = p_num < 0;
  1024. int64_t n = p_num;
  1025. int chars = 0;
  1026. do {
  1027. n /= base;
  1028. chars++;
  1029. } while (n);
  1030. if (sign) {
  1031. chars++;
  1032. }
  1033. String s;
  1034. s.resize(chars + 1);
  1035. CharType *c = s.ptrw();
  1036. c[chars] = 0;
  1037. n = p_num;
  1038. do {
  1039. int mod = ABS(n % base);
  1040. if (mod >= 10) {
  1041. char a = (capitalize_hex ? 'A' : 'a');
  1042. c[--chars] = a + (mod - 10);
  1043. } else {
  1044. c[--chars] = '0' + mod;
  1045. }
  1046. n /= base;
  1047. } while (n);
  1048. if (sign) {
  1049. c[0] = '-';
  1050. }
  1051. return s;
  1052. }
  1053. String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
  1054. uint64_t n = p_num;
  1055. int chars = 0;
  1056. do {
  1057. n /= base;
  1058. chars++;
  1059. } while (n);
  1060. String s;
  1061. s.resize(chars + 1);
  1062. CharType *c = s.ptrw();
  1063. c[chars] = 0;
  1064. n = p_num;
  1065. do {
  1066. int mod = n % base;
  1067. if (mod >= 10) {
  1068. char a = (capitalize_hex ? 'A' : 'a');
  1069. c[--chars] = a + (mod - 10);
  1070. } else {
  1071. c[--chars] = '0' + mod;
  1072. }
  1073. n /= base;
  1074. } while (n);
  1075. return s;
  1076. }
  1077. String String::num_real(double p_num) {
  1078. String s;
  1079. String sd;
  1080. /* integer part */
  1081. bool neg = p_num < 0;
  1082. p_num = ABS(p_num);
  1083. int intn = (int)p_num;
  1084. /* decimal part */
  1085. if ((int)p_num != p_num) {
  1086. double dec = p_num - (float)((int)p_num);
  1087. int digit = 0;
  1088. int decimals = MAX_DIGITS;
  1089. int dec_int = 0;
  1090. int dec_max = 0;
  1091. while (true) {
  1092. dec *= 10.0;
  1093. dec_int = dec_int * 10 + (int)dec % 10;
  1094. dec_max = dec_max * 10 + 9;
  1095. digit++;
  1096. if ((dec - (float)((int)dec)) < 1e-6) {
  1097. break;
  1098. }
  1099. if (digit == decimals) {
  1100. break;
  1101. }
  1102. }
  1103. dec *= 10;
  1104. int last = (int)dec % 10;
  1105. if (last > 5) {
  1106. if (dec_int == dec_max) {
  1107. dec_int = 0;
  1108. intn++;
  1109. } else {
  1110. dec_int++;
  1111. }
  1112. }
  1113. String decimal;
  1114. for (int i = 0; i < digit; i++) {
  1115. char num[2] = { 0, 0 };
  1116. num[0] = '0' + dec_int % 10;
  1117. decimal = num + decimal;
  1118. dec_int /= 10;
  1119. }
  1120. sd = '.' + decimal;
  1121. } else {
  1122. sd = ".0";
  1123. }
  1124. if (intn == 0) {
  1125. s = "0";
  1126. } else {
  1127. while (intn) {
  1128. CharType num = '0' + (intn % 10);
  1129. intn /= 10;
  1130. s = num + s;
  1131. }
  1132. }
  1133. s = s + sd;
  1134. if (neg) {
  1135. s = "-" + s;
  1136. }
  1137. return s;
  1138. }
  1139. String String::num_scientific(double p_num) {
  1140. #ifndef NO_USE_STDLIB
  1141. char buf[256];
  1142. #if defined(__GNUC__) || defined(_MSC_VER)
  1143. #if (defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900)) && defined(_TWO_DIGIT_EXPONENT) && !defined(_UCRT)
  1144. // MinGW and old MSC require _set_output_format() to conform to C99 output for printf
  1145. unsigned int old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
  1146. #endif
  1147. snprintf(buf, 256, "%lg", p_num);
  1148. #if (defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900)) && defined(_TWO_DIGIT_EXPONENT) && !defined(_UCRT)
  1149. _set_output_format(old_exponent_format);
  1150. #endif
  1151. #else
  1152. sprintf(buf, "%.16lg", p_num);
  1153. #endif
  1154. buf[255] = 0;
  1155. return buf;
  1156. #else
  1157. return String::num(p_num);
  1158. #endif
  1159. }
  1160. CharString String::ascii(bool p_allow_extended) const {
  1161. if (!length()) {
  1162. return CharString();
  1163. }
  1164. CharString cs;
  1165. cs.resize(size());
  1166. for (int i = 0; i < size(); i++) {
  1167. cs[i] = operator[](i);
  1168. }
  1169. return cs;
  1170. }
  1171. String String::utf8(const char *p_utf8, int p_len) {
  1172. String ret;
  1173. ret.parse_utf8(p_utf8, p_len);
  1174. return ret;
  1175. };
  1176. bool String::parse_utf8(const char *p_utf8, int p_len) {
  1177. #define _UNICERROR(m_err) print_line("Unicode error: " + String(m_err));
  1178. if (!p_utf8) {
  1179. return true;
  1180. }
  1181. String aux;
  1182. int cstr_size = 0;
  1183. int str_size = 0;
  1184. /* HANDLE BOM (Byte Order Mark) */
  1185. if (p_len < 0 || p_len >= 3) {
  1186. bool has_bom = uint8_t(p_utf8[0]) == 0xEF && uint8_t(p_utf8[1]) == 0xBB && uint8_t(p_utf8[2]) == 0xBF;
  1187. if (has_bom) {
  1188. //just skip it
  1189. if (p_len >= 0) {
  1190. p_len -= 3;
  1191. }
  1192. p_utf8 += 3;
  1193. }
  1194. }
  1195. {
  1196. const char *ptrtmp = p_utf8;
  1197. const char *ptrtmp_limit = &p_utf8[p_len];
  1198. int skip = 0;
  1199. while (ptrtmp != ptrtmp_limit && *ptrtmp) {
  1200. if (skip == 0) {
  1201. uint8_t c = *ptrtmp >= 0 ? *ptrtmp : uint8_t(256 + *ptrtmp);
  1202. /* Determine the number of characters in sequence */
  1203. if ((c & 0x80) == 0) {
  1204. skip = 0;
  1205. } else if ((c & 0xE0) == 0xC0) {
  1206. skip = 1;
  1207. } else if ((c & 0xF0) == 0xE0) {
  1208. skip = 2;
  1209. } else if ((c & 0xF8) == 0xF0) {
  1210. skip = 3;
  1211. } else if ((c & 0xFC) == 0xF8) {
  1212. skip = 4;
  1213. } else if ((c & 0xFE) == 0xFC) {
  1214. skip = 5;
  1215. } else {
  1216. _UNICERROR("invalid skip");
  1217. return true; //invalid utf8
  1218. }
  1219. if (skip == 1 && (c & 0x1E) == 0) {
  1220. //printf("overlong rejected\n");
  1221. _UNICERROR("overlong rejected");
  1222. return true; //reject overlong
  1223. }
  1224. str_size++;
  1225. } else {
  1226. --skip;
  1227. }
  1228. cstr_size++;
  1229. ptrtmp++;
  1230. }
  1231. if (skip) {
  1232. _UNICERROR("no space left");
  1233. return true; //not enough spac
  1234. }
  1235. }
  1236. if (str_size == 0) {
  1237. clear();
  1238. return false;
  1239. }
  1240. resize(str_size + 1);
  1241. CharType *dst = ptrw();
  1242. dst[str_size] = 0;
  1243. while (cstr_size) {
  1244. int len = 0;
  1245. /* Determine the number of characters in sequence */
  1246. if ((*p_utf8 & 0x80) == 0) {
  1247. len = 1;
  1248. } else if ((*p_utf8 & 0xE0) == 0xC0) {
  1249. len = 2;
  1250. } else if ((*p_utf8 & 0xF0) == 0xE0) {
  1251. len = 3;
  1252. } else if ((*p_utf8 & 0xF8) == 0xF0) {
  1253. len = 4;
  1254. } else if ((*p_utf8 & 0xFC) == 0xF8) {
  1255. len = 5;
  1256. } else if ((*p_utf8 & 0xFE) == 0xFC) {
  1257. len = 6;
  1258. } else {
  1259. _UNICERROR("invalid len");
  1260. return true; //invalid UTF8
  1261. }
  1262. if (len > cstr_size) {
  1263. _UNICERROR("no space left");
  1264. return true; //not enough space
  1265. }
  1266. if (len == 2 && (*p_utf8 & 0x1E) == 0) {
  1267. //printf("overlong rejected\n");
  1268. _UNICERROR("no space left");
  1269. return true; //reject overlong
  1270. }
  1271. /* Convert the first character */
  1272. uint32_t unichar = 0;
  1273. if (len == 1) {
  1274. unichar = *p_utf8;
  1275. } else {
  1276. unichar = (0xFF >> (len + 1)) & *p_utf8;
  1277. for (int i = 1; i < len; i++) {
  1278. if ((p_utf8[i] & 0xC0) != 0x80) {
  1279. _UNICERROR("invalid utf8");
  1280. return true; //invalid utf8
  1281. }
  1282. if (unichar == 0 && i == 2 && ((p_utf8[i] & 0x7F) >> (7 - len)) == 0) {
  1283. _UNICERROR("invalid utf8 overlong");
  1284. return true; //no overlong
  1285. }
  1286. unichar = (unichar << 6) | (p_utf8[i] & 0x3F);
  1287. }
  1288. }
  1289. //printf("char %i, len %i\n",unichar,len);
  1290. if (sizeof(wchar_t) == 2 && unichar > 0xFFFF) {
  1291. unichar = ' '; //too long for windows
  1292. }
  1293. *(dst++) = unichar;
  1294. cstr_size -= len;
  1295. p_utf8 += len;
  1296. }
  1297. return false;
  1298. }
  1299. CharString String::utf8() const {
  1300. int l = length();
  1301. if (!l) {
  1302. return CharString();
  1303. }
  1304. const CharType *d = &operator[](0);
  1305. int fl = 0;
  1306. for (int i = 0; i < l; i++) {
  1307. uint32_t c = d[i];
  1308. if (c <= 0x7f) { // 7 bits.
  1309. fl += 1;
  1310. } else if (c <= 0x7ff) { // 11 bits
  1311. fl += 2;
  1312. } else if (c <= 0xffff) { // 16 bits
  1313. fl += 3;
  1314. } else if (c <= 0x001fffff) { // 21 bits
  1315. fl += 4;
  1316. } else if (c <= 0x03ffffff) { // 26 bits
  1317. fl += 5;
  1318. } else if (c <= 0x7fffffff) { // 31 bits
  1319. fl += 6;
  1320. }
  1321. }
  1322. CharString utf8s;
  1323. if (fl == 0) {
  1324. return utf8s;
  1325. }
  1326. utf8s.resize(fl + 1);
  1327. uint8_t *cdst = (uint8_t *)utf8s.get_data();
  1328. #define APPEND_CHAR(m_c) *(cdst++) = m_c
  1329. for (int i = 0; i < l; i++) {
  1330. uint32_t c = d[i];
  1331. if (c <= 0x7f) { // 7 bits.
  1332. APPEND_CHAR(c);
  1333. } else if (c <= 0x7ff) { // 11 bits
  1334. APPEND_CHAR(uint32_t(0xc0 | ((c >> 6) & 0x1f))); // Top 5 bits.
  1335. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1336. } else if (c <= 0xffff) { // 16 bits
  1337. APPEND_CHAR(uint32_t(0xe0 | ((c >> 12) & 0x0f))); // Top 4 bits.
  1338. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Middle 6 bits.
  1339. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1340. } else if (c <= 0x001fffff) { // 21 bits
  1341. APPEND_CHAR(uint32_t(0xf0 | ((c >> 18) & 0x07))); // Top 3 bits.
  1342. APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper middle 6 bits.
  1343. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
  1344. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1345. } else if (c <= 0x03ffffff) { // 26 bits
  1346. APPEND_CHAR(uint32_t(0xf8 | ((c >> 24) & 0x03))); // Top 2 bits.
  1347. APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Upper middle 6 bits.
  1348. APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // middle 6 bits.
  1349. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
  1350. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1351. } else if (c <= 0x7fffffff) { // 31 bits
  1352. APPEND_CHAR(uint32_t(0xfc | ((c >> 30) & 0x01))); // Top 1 bit.
  1353. APPEND_CHAR(uint32_t(0x80 | ((c >> 24) & 0x3f))); // Upper upper middle 6 bits.
  1354. APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Lower upper middle 6 bits.
  1355. APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper lower middle 6 bits.
  1356. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower lower middle 6 bits.
  1357. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1358. }
  1359. }
  1360. #undef APPEND_CHAR
  1361. *cdst = 0; //trailing zero
  1362. return utf8s;
  1363. }
  1364. /*
  1365. String::String(CharType p_char) {
  1366. shared=NULL;
  1367. copy_from(p_char);
  1368. }
  1369. */
  1370. String::String(const char *p_str) {
  1371. copy_from(p_str);
  1372. }
  1373. String::String(const CharType *p_str, int p_clip_to_len) {
  1374. copy_from(p_str, p_clip_to_len);
  1375. }
  1376. String::String(const StrRange &p_range) {
  1377. if (!p_range.c_str) {
  1378. return;
  1379. }
  1380. copy_from(p_range.c_str, p_range.len);
  1381. }
  1382. int String::hex_to_int(bool p_with_prefix) const {
  1383. int len = length();
  1384. if (len == 0 || (p_with_prefix && len < 3)) {
  1385. return 0;
  1386. }
  1387. const CharType *s = ptr();
  1388. int sign = s[0] == '-' ? -1 : 1;
  1389. if (sign < 0) {
  1390. s++;
  1391. }
  1392. if (p_with_prefix) {
  1393. if (s[0] != '0' || s[1] != 'x') {
  1394. return 0;
  1395. }
  1396. s += 2;
  1397. }
  1398. int hex = 0;
  1399. while (*s) {
  1400. CharType c = LOWERCASE(*s);
  1401. int n;
  1402. if (c >= '0' && c <= '9') {
  1403. n = c - '0';
  1404. } else if (c >= 'a' && c <= 'f') {
  1405. n = (c - 'a') + 10;
  1406. } else {
  1407. return 0;
  1408. }
  1409. // Check for overflow/underflow, with special case to ensure INT32_MIN does not result in error
  1410. bool overflow = ((hex > INT32_MAX / 16) && (sign == 1 || (sign == -1 && hex != (INT32_MAX >> 4) + 1))) || (sign == -1 && hex == (INT32_MAX >> 4) + 1 && c > '0');
  1411. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + *this + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1412. hex *= 16;
  1413. hex += n;
  1414. s++;
  1415. }
  1416. return hex * sign;
  1417. }
  1418. int64_t String::hex_to_int64(bool p_with_prefix) const {
  1419. if (p_with_prefix && length() < 3) {
  1420. return 0;
  1421. }
  1422. const CharType *s = ptr();
  1423. int64_t sign = s[0] == '-' ? -1 : 1;
  1424. if (sign < 0) {
  1425. s++;
  1426. }
  1427. if (p_with_prefix) {
  1428. if (s[0] != '0' || s[1] != 'x') {
  1429. return 0;
  1430. }
  1431. s += 2;
  1432. }
  1433. int64_t hex = 0;
  1434. while (*s) {
  1435. CharType c = LOWERCASE(*s);
  1436. int64_t n;
  1437. if (c >= '0' && c <= '9') {
  1438. n = c - '0';
  1439. } else if (c >= 'a' && c <= 'f') {
  1440. n = (c - 'a') + 10;
  1441. } else {
  1442. return 0;
  1443. }
  1444. bool overflow = ((hex > INT64_MAX / 16) && (sign == 1 || (sign == -1 && hex != (INT64_MAX >> 4) + 1))) || (sign == -1 && hex == (INT64_MAX >> 4) + 1 && c > '0');
  1445. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1446. hex *= 16;
  1447. hex += n;
  1448. s++;
  1449. }
  1450. return hex * sign;
  1451. }
  1452. int64_t String::bin_to_int64(bool p_with_prefix) const {
  1453. int len = length();
  1454. if (len == 0 || (p_with_prefix && len < 3)) {
  1455. return 0;
  1456. }
  1457. const CharType *s = ptr();
  1458. int64_t sign = s[0] == '-' ? -1 : 1;
  1459. if (sign < 0) {
  1460. s++;
  1461. }
  1462. if (p_with_prefix) {
  1463. if (s[0] != '0' || s[1] != 'b') {
  1464. return 0;
  1465. }
  1466. s += 2;
  1467. }
  1468. int64_t binary = 0;
  1469. while (*s) {
  1470. CharType c = LOWERCASE(*s);
  1471. int64_t n;
  1472. if (c == '0' || c == '1') {
  1473. n = c - '0';
  1474. } else {
  1475. return 0;
  1476. }
  1477. // Check for overflow/underflow, with special case to ensure INT64_MIN does not result in error
  1478. bool overflow = ((binary > INT64_MAX / 2) && (sign == 1 || (sign == -1 && binary != (INT64_MAX >> 1) + 1))) || (sign == -1 && binary == (INT64_MAX >> 1) + 1 && c > '0');
  1479. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1480. binary *= 2;
  1481. binary += n;
  1482. s++;
  1483. }
  1484. return binary * sign;
  1485. }
  1486. int String::to_int() const {
  1487. if (length() == 0) {
  1488. return 0;
  1489. }
  1490. int to = (find(".") >= 0) ? find(".") : length();
  1491. int integer = 0;
  1492. int sign = 1;
  1493. for (int i = 0; i < to; i++) {
  1494. CharType c = operator[](i);
  1495. if (c >= '0' && c <= '9') {
  1496. bool overflow = (integer > INT32_MAX / 10) || (integer == INT32_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
  1497. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + *this + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1498. integer *= 10;
  1499. integer += c - '0';
  1500. } else if (integer == 0 && c == '-') {
  1501. sign = -sign;
  1502. }
  1503. }
  1504. return integer * sign;
  1505. }
  1506. int64_t String::to_int64() const {
  1507. if (length() == 0) {
  1508. return 0;
  1509. }
  1510. int to = (find(".") >= 0) ? find(".") : length();
  1511. int64_t integer = 0;
  1512. int64_t sign = 1;
  1513. for (int i = 0; i < to; i++) {
  1514. CharType c = operator[](i);
  1515. if (c >= '0' && c <= '9') {
  1516. bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
  1517. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1518. integer *= 10;
  1519. integer += c - '0';
  1520. } else if (integer == 0 && c == '-') {
  1521. sign = -sign;
  1522. }
  1523. }
  1524. return integer * sign;
  1525. }
  1526. int String::to_int(const char *p_str, int p_len) {
  1527. int to = 0;
  1528. if (p_len >= 0) {
  1529. to = p_len;
  1530. } else {
  1531. while (p_str[to] != 0 && p_str[to] != '.') {
  1532. to++;
  1533. }
  1534. }
  1535. int integer = 0;
  1536. int sign = 1;
  1537. for (int i = 0; i < to; i++) {
  1538. char c = p_str[i];
  1539. if (c >= '0' && c <= '9') {
  1540. bool overflow = (integer > INT32_MAX / 10) || (integer == INT32_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
  1541. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + String(p_str).substr(0, to) + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1542. integer *= 10;
  1543. integer += c - '0';
  1544. } else if (c == '-' && integer == 0) {
  1545. sign = -sign;
  1546. } else if (c != ' ') {
  1547. break;
  1548. }
  1549. }
  1550. return integer * sign;
  1551. }
  1552. bool String::is_numeric() const {
  1553. if (length() == 0) {
  1554. return false;
  1555. };
  1556. int s = 0;
  1557. if (operator[](0) == '-') {
  1558. ++s;
  1559. }
  1560. bool dot = false;
  1561. for (int i = s; i < length(); i++) {
  1562. CharType c = operator[](i);
  1563. if (c == '.') {
  1564. if (dot) {
  1565. return false;
  1566. };
  1567. dot = true;
  1568. }
  1569. if (c < '0' || c > '9') {
  1570. return false;
  1571. };
  1572. };
  1573. return true; // TODO: Use the parser below for this instead
  1574. };
  1575. template <class C>
  1576. static double built_in_strtod(const C *string, /* A decimal ASCII floating-point number,
  1577. * optionally preceded by white space. Must
  1578. * have form "-I.FE-X", where I is the integer
  1579. * part of the mantissa, F is the fractional
  1580. * part of the mantissa, and X is the
  1581. * exponent. Either of the signs may be "+",
  1582. * "-", or omitted. Either I or F may be
  1583. * omitted, or both. The decimal point isn't
  1584. * necessary unless F is present. The "E" may
  1585. * actually be an "e". E and X may both be
  1586. * omitted (but not just one). */
  1587. C **endPtr = nullptr) /* If non-NULL, store terminating Cacter's
  1588. * address here. */
  1589. {
  1590. static const int maxExponent = 511; /* Largest possible base 10 exponent. Any
  1591. * exponent larger than this will already
  1592. * produce underflow or overflow, so there's
  1593. * no need to worry about additional digits.
  1594. */
  1595. static const double powersOf10[] = { /* Table giving binary powers of 10. Entry */
  1596. 10., /* is 10^2^i. Used to convert decimal */
  1597. 100., /* exponents into floating-point numbers. */
  1598. 1.0e4,
  1599. 1.0e8,
  1600. 1.0e16,
  1601. 1.0e32,
  1602. 1.0e64,
  1603. 1.0e128,
  1604. 1.0e256
  1605. };
  1606. bool sign, expSign = false;
  1607. double fraction, dblExp;
  1608. const double *d;
  1609. const C *p;
  1610. int c;
  1611. int exp = 0; /* Exponent read from "EX" field. */
  1612. int fracExp = 0; /* Exponent that derives from the fractional
  1613. * part. Under normal circumstances, it is
  1614. * the negative of the number of digits in F.
  1615. * However, if I is very long, the last digits
  1616. * of I get dropped (otherwise a long I with a
  1617. * large negative exponent could cause an
  1618. * unnecessary overflow on I alone). In this
  1619. * case, fracExp is incremented one for each
  1620. * dropped digit. */
  1621. int mantSize; /* Number of digits in mantissa. */
  1622. int decPt; /* Number of mantissa digits BEFORE decimal
  1623. * point. */
  1624. const C *pExp; /* Temporarily holds location of exponent in
  1625. * string. */
  1626. /*
  1627. * Strip off leading blanks and check for a sign.
  1628. */
  1629. p = string;
  1630. while (*p == ' ' || *p == '\t' || *p == '\n') {
  1631. p += 1;
  1632. }
  1633. if (*p == '-') {
  1634. sign = true;
  1635. p += 1;
  1636. } else {
  1637. if (*p == '+') {
  1638. p += 1;
  1639. }
  1640. sign = false;
  1641. }
  1642. /*
  1643. * Count the number of digits in the mantissa (including the decimal
  1644. * point), and also locate the decimal point.
  1645. */
  1646. decPt = -1;
  1647. for (mantSize = 0;; mantSize += 1) {
  1648. c = *p;
  1649. if (!IS_DIGIT(c)) {
  1650. if ((c != '.') || (decPt >= 0)) {
  1651. break;
  1652. }
  1653. decPt = mantSize;
  1654. }
  1655. p += 1;
  1656. }
  1657. /*
  1658. * Now suck up the digits in the mantissa. Use two integers to collect 9
  1659. * digits each (this is faster than using floating-point). If the mantissa
  1660. * has more than 18 digits, ignore the extras, since they can't affect the
  1661. * value anyway.
  1662. */
  1663. pExp = p;
  1664. p -= mantSize;
  1665. if (decPt < 0) {
  1666. decPt = mantSize;
  1667. } else {
  1668. mantSize -= 1; /* One of the digits was the point. */
  1669. }
  1670. if (mantSize > 18) {
  1671. fracExp = decPt - 18;
  1672. mantSize = 18;
  1673. } else {
  1674. fracExp = decPt - mantSize;
  1675. }
  1676. if (mantSize == 0) {
  1677. fraction = 0.0;
  1678. p = string;
  1679. goto done;
  1680. } else {
  1681. int frac1, frac2;
  1682. frac1 = 0;
  1683. for (; mantSize > 9; mantSize -= 1) {
  1684. c = *p;
  1685. p += 1;
  1686. if (c == '.') {
  1687. c = *p;
  1688. p += 1;
  1689. }
  1690. frac1 = 10 * frac1 + (c - '0');
  1691. }
  1692. frac2 = 0;
  1693. for (; mantSize > 0; mantSize -= 1) {
  1694. c = *p;
  1695. p += 1;
  1696. if (c == '.') {
  1697. c = *p;
  1698. p += 1;
  1699. }
  1700. frac2 = 10 * frac2 + (c - '0');
  1701. }
  1702. fraction = (1.0e9 * frac1) + frac2;
  1703. }
  1704. /*
  1705. * Skim off the exponent.
  1706. */
  1707. p = pExp;
  1708. if ((*p == 'E') || (*p == 'e')) {
  1709. p += 1;
  1710. if (*p == '-') {
  1711. expSign = true;
  1712. p += 1;
  1713. } else {
  1714. if (*p == '+') {
  1715. p += 1;
  1716. }
  1717. expSign = false;
  1718. }
  1719. if (!IS_DIGIT(CharType(*p))) {
  1720. p = pExp;
  1721. goto done;
  1722. }
  1723. while (IS_DIGIT(CharType(*p))) {
  1724. exp = exp * 10 + (*p - '0');
  1725. p += 1;
  1726. }
  1727. }
  1728. if (expSign) {
  1729. exp = fracExp - exp;
  1730. } else {
  1731. exp = fracExp + exp;
  1732. }
  1733. /*
  1734. * Generate a floating-point number that represents the exponent. Do this
  1735. * by processing the exponent one bit at a time to combine many powers of
  1736. * 2 of 10. Then combine the exponent with the fraction.
  1737. */
  1738. if (exp < 0) {
  1739. expSign = true;
  1740. exp = -exp;
  1741. } else {
  1742. expSign = false;
  1743. }
  1744. if (exp > maxExponent) {
  1745. exp = maxExponent;
  1746. WARN_PRINT("Exponent too high");
  1747. }
  1748. dblExp = 1.0;
  1749. for (d = powersOf10; exp != 0; exp >>= 1, ++d) {
  1750. if (exp & 01) {
  1751. dblExp *= *d;
  1752. }
  1753. }
  1754. if (expSign) {
  1755. fraction /= dblExp;
  1756. } else {
  1757. fraction *= dblExp;
  1758. }
  1759. done:
  1760. if (endPtr != nullptr) {
  1761. *endPtr = (C *)p;
  1762. }
  1763. if (sign) {
  1764. return -fraction;
  1765. }
  1766. return fraction;
  1767. }
  1768. #define READING_SIGN 0
  1769. #define READING_INT 1
  1770. #define READING_DEC 2
  1771. #define READING_EXP 3
  1772. #define READING_DONE 4
  1773. double String::to_double(const char *p_str) {
  1774. #ifndef NO_USE_STDLIB
  1775. return built_in_strtod<char>(p_str);
  1776. //return atof(p_str); DOES NOT WORK ON ANDROID(??)
  1777. #else
  1778. return built_in_strtod<char>(p_str);
  1779. #endif
  1780. }
  1781. float String::to_float() const {
  1782. return to_double();
  1783. }
  1784. double String::to_double(const CharType *p_str, const CharType **r_end) {
  1785. return built_in_strtod<CharType>(p_str, (CharType **)r_end);
  1786. }
  1787. int64_t String::to_int(const CharType *p_str, int p_len) {
  1788. if (p_len == 0 || !p_str[0]) {
  1789. return 0;
  1790. }
  1791. ///@todo make more exact so saving and loading does not lose precision
  1792. int64_t integer = 0;
  1793. int64_t sign = 1;
  1794. int reading = READING_SIGN;
  1795. const CharType *str = p_str;
  1796. const CharType *limit = &p_str[p_len];
  1797. while (*str && reading != READING_DONE && str != limit) {
  1798. CharType c = *(str++);
  1799. switch (reading) {
  1800. case READING_SIGN: {
  1801. if (c >= '0' && c <= '9') {
  1802. reading = READING_INT;
  1803. // let it fallthrough
  1804. } else if (c == '-') {
  1805. sign = -1;
  1806. reading = READING_INT;
  1807. break;
  1808. } else if (c == '+') {
  1809. sign = 1;
  1810. reading = READING_INT;
  1811. break;
  1812. } else {
  1813. break;
  1814. }
  1815. }
  1816. case READING_INT: {
  1817. if (c >= '0' && c <= '9') {
  1818. if (integer > INT64_MAX / 10) {
  1819. String number("");
  1820. str = p_str;
  1821. while (*str && str != limit) {
  1822. number += *(str++);
  1823. }
  1824. ERR_FAIL_V_MSG(sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + number + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1825. }
  1826. integer *= 10;
  1827. integer += c - '0';
  1828. } else {
  1829. reading = READING_DONE;
  1830. }
  1831. } break;
  1832. }
  1833. }
  1834. return sign * integer;
  1835. }
  1836. double String::to_double() const {
  1837. if (empty()) {
  1838. return 0;
  1839. }
  1840. #ifndef NO_USE_STDLIB
  1841. return built_in_strtod<CharType>(c_str());
  1842. //return wcstod(c_str(),NULL); DOES NOT WORK ON ANDROID :(
  1843. #else
  1844. return built_in_strtod<CharType>(c_str());
  1845. #endif
  1846. }
  1847. bool operator==(const char *p_chr, const String &p_str) {
  1848. return p_str == p_chr;
  1849. }
  1850. String operator+(const char *p_chr, const String &p_str) {
  1851. String tmp = p_chr;
  1852. tmp += p_str;
  1853. return tmp;
  1854. }
  1855. String operator+(CharType p_chr, const String &p_str) {
  1856. return (String::chr(p_chr) + p_str);
  1857. }
  1858. uint32_t String::hash(const char *p_cstr) {
  1859. uint32_t hashv = 5381;
  1860. uint32_t c;
  1861. while ((c = *p_cstr++)) {
  1862. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1863. }
  1864. return hashv;
  1865. }
  1866. uint32_t String::hash(const char *p_cstr, int p_len) {
  1867. uint32_t hashv = 5381;
  1868. for (int i = 0; i < p_len; i++) {
  1869. hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
  1870. }
  1871. return hashv;
  1872. }
  1873. uint32_t String::hash(const CharType *p_cstr, int p_len) {
  1874. uint32_t hashv = 5381;
  1875. for (int i = 0; i < p_len; i++) {
  1876. hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
  1877. }
  1878. return hashv;
  1879. }
  1880. uint32_t String::hash(const CharType *p_cstr) {
  1881. uint32_t hashv = 5381;
  1882. uint32_t c;
  1883. while ((c = *p_cstr++)) {
  1884. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1885. }
  1886. return hashv;
  1887. }
  1888. uint32_t String::hash() const {
  1889. /* simple djb2 hashing */
  1890. const CharType *chr = c_str();
  1891. uint32_t hashv = 5381;
  1892. uint32_t c;
  1893. while ((c = *chr++)) {
  1894. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1895. }
  1896. return hashv;
  1897. }
  1898. uint64_t String::hash64() const {
  1899. /* simple djb2 hashing */
  1900. const CharType *chr = c_str();
  1901. uint64_t hashv = 5381;
  1902. uint64_t c;
  1903. while ((c = *chr++)) {
  1904. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1905. }
  1906. return hashv;
  1907. }
  1908. String String::md5_text() const {
  1909. CharString cs = utf8();
  1910. unsigned char hash[16];
  1911. CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
  1912. return String::hex_encode_buffer(hash, 16);
  1913. }
  1914. String String::sha1_text() const {
  1915. CharString cs = utf8();
  1916. unsigned char hash[20];
  1917. CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
  1918. return String::hex_encode_buffer(hash, 20);
  1919. }
  1920. String String::sha256_text() const {
  1921. CharString cs = utf8();
  1922. unsigned char hash[32];
  1923. CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
  1924. return String::hex_encode_buffer(hash, 32);
  1925. }
  1926. Vector<uint8_t> String::md5_buffer() const {
  1927. CharString cs = utf8();
  1928. unsigned char hash[16];
  1929. CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
  1930. Vector<uint8_t> ret;
  1931. ret.resize(16);
  1932. for (int i = 0; i < 16; i++) {
  1933. ret.write[i] = hash[i];
  1934. }
  1935. return ret;
  1936. };
  1937. Vector<uint8_t> String::sha1_buffer() const {
  1938. CharString cs = utf8();
  1939. unsigned char hash[20];
  1940. CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
  1941. Vector<uint8_t> ret;
  1942. ret.resize(20);
  1943. for (int i = 0; i < 20; i++) {
  1944. ret.write[i] = hash[i];
  1945. }
  1946. return ret;
  1947. }
  1948. Vector<uint8_t> String::sha256_buffer() const {
  1949. CharString cs = utf8();
  1950. unsigned char hash[32];
  1951. CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
  1952. Vector<uint8_t> ret;
  1953. ret.resize(32);
  1954. for (int i = 0; i < 32; i++) {
  1955. ret.write[i] = hash[i];
  1956. }
  1957. return ret;
  1958. }
  1959. String String::insert(int p_at_pos, const String &p_string) const {
  1960. if (p_at_pos < 0) {
  1961. return *this;
  1962. }
  1963. if (p_at_pos > length()) {
  1964. p_at_pos = length();
  1965. }
  1966. String pre;
  1967. if (p_at_pos > 0) {
  1968. pre = substr(0, p_at_pos);
  1969. }
  1970. String post;
  1971. if (p_at_pos < length()) {
  1972. post = substr(p_at_pos, length() - p_at_pos);
  1973. }
  1974. return pre + p_string + post;
  1975. }
  1976. String String::substr(int p_from, int p_chars) const {
  1977. if (p_chars == -1) {
  1978. p_chars = length() - p_from;
  1979. }
  1980. if (empty() || p_from < 0 || p_from >= length() || p_chars <= 0) {
  1981. return "";
  1982. }
  1983. if ((p_from + p_chars) > length()) {
  1984. p_chars = length() - p_from;
  1985. }
  1986. if (p_from == 0 && p_chars >= length()) {
  1987. return String(*this);
  1988. }
  1989. String s = String();
  1990. s.copy_from_unchecked(&c_str()[p_from], p_chars);
  1991. return s;
  1992. }
  1993. int String::find_last(const String &p_str) const {
  1994. int pos = -1;
  1995. int findfrom = 0;
  1996. int findres = -1;
  1997. while ((findres = find(p_str, findfrom)) != -1) {
  1998. pos = findres;
  1999. findfrom = pos + 1;
  2000. }
  2001. return pos;
  2002. }
  2003. int String::find(const String &p_str, int p_from) const {
  2004. if (p_from < 0) {
  2005. return -1;
  2006. }
  2007. const int src_len = p_str.length();
  2008. const int len = length();
  2009. if (src_len == 0 || len == 0) {
  2010. return -1; // won't find anything!
  2011. }
  2012. const CharType *src = c_str();
  2013. const CharType *str = p_str.c_str();
  2014. for (int i = p_from; i <= (len - src_len); i++) {
  2015. bool found = true;
  2016. for (int j = 0; j < src_len; j++) {
  2017. int read_pos = i + j;
  2018. if (read_pos >= len) {
  2019. ERR_PRINT("read_pos>=len");
  2020. return -1;
  2021. };
  2022. if (src[read_pos] != str[j]) {
  2023. found = false;
  2024. break;
  2025. }
  2026. }
  2027. if (found) {
  2028. return i;
  2029. }
  2030. }
  2031. return -1;
  2032. }
  2033. int String::find(const char *p_str, int p_from) const {
  2034. if (p_from < 0) {
  2035. return -1;
  2036. }
  2037. const int len = length();
  2038. if (len == 0) {
  2039. return -1; // won't find anything!
  2040. }
  2041. const CharType *src = c_str();
  2042. int src_len = 0;
  2043. while (p_str[src_len] != '\0') {
  2044. src_len++;
  2045. }
  2046. if (src_len == 1) {
  2047. const char needle = p_str[0];
  2048. for (int i = p_from; i < len; i++) {
  2049. if (src[i] == needle) {
  2050. return i;
  2051. }
  2052. }
  2053. } else {
  2054. for (int i = p_from; i <= (len - src_len); i++) {
  2055. bool found = true;
  2056. for (int j = 0; j < src_len; j++) {
  2057. int read_pos = i + j;
  2058. if (read_pos >= len) {
  2059. ERR_PRINT("read_pos>=len");
  2060. return -1;
  2061. };
  2062. if (src[read_pos] != p_str[j]) {
  2063. found = false;
  2064. break;
  2065. }
  2066. }
  2067. if (found) {
  2068. return i;
  2069. }
  2070. }
  2071. }
  2072. return -1;
  2073. }
  2074. int String::find_char(const CharType &p_char, int p_from) const {
  2075. return _cowdata.find(p_char, p_from);
  2076. }
  2077. int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
  2078. if (p_from < 0) {
  2079. return -1;
  2080. }
  2081. if (p_keys.size() == 0) {
  2082. return -1;
  2083. }
  2084. //int src_len=p_str.length();
  2085. const String *keys = &p_keys[0];
  2086. int key_count = p_keys.size();
  2087. int len = length();
  2088. if (len == 0) {
  2089. return -1; // won't find anything!
  2090. }
  2091. const CharType *src = c_str();
  2092. for (int i = p_from; i < len; i++) {
  2093. bool found = true;
  2094. for (int k = 0; k < key_count; k++) {
  2095. found = true;
  2096. if (r_key) {
  2097. *r_key = k;
  2098. }
  2099. const CharType *cmp = keys[k].c_str();
  2100. int l = keys[k].length();
  2101. for (int j = 0; j < l; j++) {
  2102. int read_pos = i + j;
  2103. if (read_pos >= len) {
  2104. found = false;
  2105. break;
  2106. };
  2107. if (src[read_pos] != cmp[j]) {
  2108. found = false;
  2109. break;
  2110. }
  2111. }
  2112. if (found) {
  2113. break;
  2114. }
  2115. }
  2116. if (found) {
  2117. return i;
  2118. }
  2119. }
  2120. return -1;
  2121. }
  2122. int String::findn(const String &p_str, int p_from) const {
  2123. if (p_from < 0) {
  2124. return -1;
  2125. }
  2126. int src_len = p_str.length();
  2127. if (src_len == 0 || length() == 0) {
  2128. return -1; // won't find anything!
  2129. }
  2130. const CharType *srcd = c_str();
  2131. for (int i = p_from; i <= (length() - src_len); i++) {
  2132. bool found = true;
  2133. for (int j = 0; j < src_len; j++) {
  2134. int read_pos = i + j;
  2135. if (read_pos >= length()) {
  2136. ERR_PRINT("read_pos>=length()");
  2137. return -1;
  2138. };
  2139. CharType src = _find_lower(srcd[read_pos]);
  2140. CharType dst = _find_lower(p_str[j]);
  2141. if (src != dst) {
  2142. found = false;
  2143. break;
  2144. }
  2145. }
  2146. if (found) {
  2147. return i;
  2148. }
  2149. }
  2150. return -1;
  2151. }
  2152. int String::rfind(const String &p_str, int p_from) const {
  2153. // establish a limit
  2154. int limit = length() - p_str.length();
  2155. if (limit < 0) {
  2156. return -1;
  2157. }
  2158. // establish a starting point
  2159. if (p_from < 0) {
  2160. p_from = limit;
  2161. } else if (p_from > limit) {
  2162. p_from = limit;
  2163. }
  2164. int src_len = p_str.length();
  2165. int len = length();
  2166. if (src_len == 0 || len == 0) {
  2167. return -1; // won't find anything!
  2168. }
  2169. const CharType *src = c_str();
  2170. for (int i = p_from; i >= 0; i--) {
  2171. bool found = true;
  2172. for (int j = 0; j < src_len; j++) {
  2173. int read_pos = i + j;
  2174. if (read_pos >= len) {
  2175. ERR_PRINT("read_pos>=len");
  2176. return -1;
  2177. };
  2178. if (src[read_pos] != p_str[j]) {
  2179. found = false;
  2180. break;
  2181. }
  2182. }
  2183. if (found) {
  2184. return i;
  2185. }
  2186. }
  2187. return -1;
  2188. }
  2189. int String::rfindn(const String &p_str, int p_from) const {
  2190. // establish a limit
  2191. int limit = length() - p_str.length();
  2192. if (limit < 0) {
  2193. return -1;
  2194. }
  2195. // establish a starting point
  2196. if (p_from < 0) {
  2197. p_from = limit;
  2198. } else if (p_from > limit) {
  2199. p_from = limit;
  2200. }
  2201. int src_len = p_str.length();
  2202. int len = length();
  2203. if (src_len == 0 || len == 0) {
  2204. return -1; // won't find anything!
  2205. }
  2206. const CharType *src = c_str();
  2207. for (int i = p_from; i >= 0; i--) {
  2208. bool found = true;
  2209. for (int j = 0; j < src_len; j++) {
  2210. int read_pos = i + j;
  2211. if (read_pos >= len) {
  2212. ERR_PRINT("read_pos>=len");
  2213. return -1;
  2214. };
  2215. CharType srcc = _find_lower(src[read_pos]);
  2216. CharType dstc = _find_lower(p_str[j]);
  2217. if (srcc != dstc) {
  2218. found = false;
  2219. break;
  2220. }
  2221. }
  2222. if (found) {
  2223. return i;
  2224. }
  2225. }
  2226. return -1;
  2227. }
  2228. bool String::ends_with(const String &p_string) const {
  2229. int l = p_string.length();
  2230. if (l > length()) {
  2231. return false;
  2232. }
  2233. if (l == 0) {
  2234. return true;
  2235. }
  2236. const CharType *p = &p_string[0];
  2237. const CharType *s = &operator[](length() - l);
  2238. for (int i = 0; i < l; i++) {
  2239. if (p[i] != s[i]) {
  2240. return false;
  2241. }
  2242. }
  2243. return true;
  2244. }
  2245. bool String::begins_with(const String &p_string) const {
  2246. int l = p_string.length();
  2247. if (l > length()) {
  2248. return false;
  2249. }
  2250. if (l == 0) {
  2251. return true;
  2252. }
  2253. const CharType *p = &p_string[0];
  2254. const CharType *s = &operator[](0);
  2255. for (int i = 0; i < l; i++) {
  2256. if (p[i] != s[i]) {
  2257. return false;
  2258. }
  2259. }
  2260. return true;
  2261. }
  2262. bool String::begins_with(const char *p_string) const {
  2263. int l = length();
  2264. if (l == 0 || !p_string) {
  2265. return false;
  2266. }
  2267. const CharType *str = &operator[](0);
  2268. int i = 0;
  2269. while (*p_string && i < l) {
  2270. if (*p_string != str[i]) {
  2271. return false;
  2272. }
  2273. i++;
  2274. p_string++;
  2275. }
  2276. return *p_string == 0;
  2277. }
  2278. bool String::is_enclosed_in(const String &p_string) const {
  2279. return begins_with(p_string) && ends_with(p_string);
  2280. }
  2281. bool String::is_subsequence_of(const String &p_string) const {
  2282. return _base_is_subsequence_of(p_string, false);
  2283. }
  2284. bool String::is_subsequence_ofi(const String &p_string) const {
  2285. return _base_is_subsequence_of(p_string, true);
  2286. }
  2287. bool String::is_quoted() const {
  2288. return is_enclosed_in("\"") || is_enclosed_in("'");
  2289. }
  2290. int String::_count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const {
  2291. if (p_string.empty()) {
  2292. return 0;
  2293. }
  2294. int len = length();
  2295. int slen = p_string.length();
  2296. if (len < slen) {
  2297. return 0;
  2298. }
  2299. String str;
  2300. if (p_from >= 0 && p_to >= 0) {
  2301. if (p_to == 0) {
  2302. p_to = len;
  2303. } else if (p_from >= p_to) {
  2304. return 0;
  2305. }
  2306. if (p_from == 0 && p_to == len) {
  2307. str = String();
  2308. str.copy_from_unchecked(&c_str()[0], len);
  2309. } else {
  2310. str = substr(p_from, p_to - p_from);
  2311. }
  2312. } else {
  2313. return 0;
  2314. }
  2315. int c = 0;
  2316. int idx = -1;
  2317. do {
  2318. idx = p_case_insensitive ? str.findn(p_string) : str.find(p_string);
  2319. if (idx != -1) {
  2320. str = str.substr(idx + slen, str.length() - slen);
  2321. ++c;
  2322. }
  2323. } while (idx != -1);
  2324. return c;
  2325. }
  2326. int String::count(const String &p_string, int p_from, int p_to) const {
  2327. return _count(p_string, p_from, p_to, false);
  2328. }
  2329. int String::countn(const String &p_string, int p_from, int p_to) const {
  2330. return _count(p_string, p_from, p_to, true);
  2331. }
  2332. bool String::_base_is_subsequence_of(const String &p_string, bool case_insensitive) const {
  2333. int len = length();
  2334. if (len == 0) {
  2335. // Technically an empty string is subsequence of any string
  2336. return true;
  2337. }
  2338. if (len > p_string.length()) {
  2339. return false;
  2340. }
  2341. const CharType *src = &operator[](0);
  2342. const CharType *tgt = &p_string[0];
  2343. for (; *src && *tgt; tgt++) {
  2344. bool match = false;
  2345. if (case_insensitive) {
  2346. CharType srcc = _find_lower(*src);
  2347. CharType tgtc = _find_lower(*tgt);
  2348. match = srcc == tgtc;
  2349. } else {
  2350. match = *src == *tgt;
  2351. }
  2352. if (match) {
  2353. src++;
  2354. if (!*src) {
  2355. return true;
  2356. }
  2357. }
  2358. }
  2359. return false;
  2360. }
  2361. Vector<String> String::bigrams() const {
  2362. int n_pairs = length() - 1;
  2363. Vector<String> b;
  2364. if (n_pairs <= 0) {
  2365. return b;
  2366. }
  2367. b.resize(n_pairs);
  2368. for (int i = 0; i < n_pairs; i++) {
  2369. b.write[i] = substr(i, 2);
  2370. }
  2371. return b;
  2372. }
  2373. // Similarity according to Sorensen-Dice coefficient
  2374. float String::similarity(const String &p_string) const {
  2375. if (operator==(p_string)) {
  2376. // Equal strings are totally similar
  2377. return 1.0f;
  2378. }
  2379. if (length() < 2 || p_string.length() < 2) {
  2380. // No way to calculate similarity without a single bigram
  2381. return 0.0f;
  2382. }
  2383. Vector<String> src_bigrams = bigrams();
  2384. Vector<String> tgt_bigrams = p_string.bigrams();
  2385. int src_size = src_bigrams.size();
  2386. int tgt_size = tgt_bigrams.size();
  2387. float sum = src_size + tgt_size;
  2388. float inter = 0;
  2389. for (int i = 0; i < src_size; i++) {
  2390. for (int j = 0; j < tgt_size; j++) {
  2391. if (src_bigrams[i] == tgt_bigrams[j]) {
  2392. inter++;
  2393. break;
  2394. }
  2395. }
  2396. }
  2397. return (2.0f * inter) / sum;
  2398. }
  2399. static bool _wildcard_match(const CharType *p_pattern, const CharType *p_string, bool p_case_sensitive) {
  2400. switch (*p_pattern) {
  2401. case '\0':
  2402. return !*p_string;
  2403. case '*':
  2404. return _wildcard_match(p_pattern + 1, p_string, p_case_sensitive) || (*p_string && _wildcard_match(p_pattern, p_string + 1, p_case_sensitive));
  2405. case '?':
  2406. return *p_string && (*p_string != '.') && _wildcard_match(p_pattern + 1, p_string + 1, p_case_sensitive);
  2407. default:
  2408. 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);
  2409. }
  2410. }
  2411. bool String::match(const String &p_wildcard) const {
  2412. if (!p_wildcard.length() || !length()) {
  2413. return false;
  2414. }
  2415. return _wildcard_match(p_wildcard.c_str(), c_str(), true);
  2416. }
  2417. bool String::matchn(const String &p_wildcard) const {
  2418. if (!p_wildcard.length() || !length()) {
  2419. return false;
  2420. }
  2421. return _wildcard_match(p_wildcard.c_str(), c_str(), false);
  2422. }
  2423. String String::format(const Variant &values, String placeholder) const {
  2424. String new_string = String(this->ptr());
  2425. if (values.get_type() == Variant::ARRAY) {
  2426. Array values_arr = values;
  2427. for (int i = 0; i < values_arr.size(); i++) {
  2428. String i_as_str = String::num_int64(i);
  2429. if (values_arr[i].get_type() == Variant::ARRAY) { //Array in Array structure [["name","RobotGuy"],[0,"godot"],["strength",9000.91]]
  2430. Array value_arr = values_arr[i];
  2431. if (value_arr.size() == 2) {
  2432. Variant v_key = value_arr[0];
  2433. String key = v_key;
  2434. if (key.left(1) == "\"" && key.right(key.length() - 1) == "\"") {
  2435. key = key.substr(1, key.length() - 2);
  2436. }
  2437. Variant v_val = value_arr[1];
  2438. String val = v_val;
  2439. if (val.left(1) == "\"" && val.right(val.length() - 1) == "\"") {
  2440. val = val.substr(1, val.length() - 2);
  2441. }
  2442. new_string = new_string.replace(placeholder.replace("_", key), val);
  2443. } else {
  2444. ERR_PRINT(String("STRING.format Inner Array size != 2 ").ascii().get_data());
  2445. }
  2446. } else { //Array structure ["RobotGuy","Logis","rookie"]
  2447. Variant v_val = values_arr[i];
  2448. String val = v_val;
  2449. if (val.left(1) == "\"" && val.right(val.length() - 1) == "\"") {
  2450. val = val.substr(1, val.length() - 2);
  2451. }
  2452. if (placeholder.find("_") > -1) {
  2453. new_string = new_string.replace(placeholder.replace("_", i_as_str), val);
  2454. } else {
  2455. new_string = new_string.replace_first(placeholder, val);
  2456. }
  2457. }
  2458. }
  2459. } else if (values.get_type() == Variant::DICTIONARY) {
  2460. Dictionary d = values;
  2461. List<Variant> keys;
  2462. d.get_key_list(&keys);
  2463. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  2464. String key = E->get();
  2465. String val = d[E->get()];
  2466. if (key.left(1) == "\"" && key.right(key.length() - 1) == "\"") {
  2467. key = key.substr(1, key.length() - 2);
  2468. }
  2469. if (val.left(1) == "\"" && val.right(val.length() - 1) == "\"") {
  2470. val = val.substr(1, val.length() - 2);
  2471. }
  2472. new_string = new_string.replace(placeholder.replace("_", key), val);
  2473. }
  2474. } else {
  2475. ERR_PRINT(String("Invalid type: use Array or Dictionary.").ascii().get_data());
  2476. }
  2477. return new_string;
  2478. }
  2479. String String::replace(const String &p_key, const String &p_with) const {
  2480. String new_string;
  2481. int search_from = 0;
  2482. int result = 0;
  2483. while ((result = find(p_key, search_from)) >= 0) {
  2484. new_string += substr(search_from, result - search_from);
  2485. new_string += p_with;
  2486. search_from = result + p_key.length();
  2487. }
  2488. if (search_from == 0) {
  2489. return *this;
  2490. }
  2491. new_string += substr(search_from, length() - search_from);
  2492. return new_string;
  2493. }
  2494. String String::replace(const char *p_key, const char *p_with) const {
  2495. String new_string;
  2496. int search_from = 0;
  2497. int result = 0;
  2498. while ((result = find(p_key, search_from)) >= 0) {
  2499. new_string += substr(search_from, result - search_from);
  2500. new_string += p_with;
  2501. int k = 0;
  2502. while (p_key[k] != '\0') {
  2503. k++;
  2504. }
  2505. search_from = result + k;
  2506. }
  2507. if (search_from == 0) {
  2508. return *this;
  2509. }
  2510. new_string += substr(search_from, length() - search_from);
  2511. return new_string;
  2512. }
  2513. String String::replace_first(const String &p_key, const String &p_with) const {
  2514. int pos = find(p_key);
  2515. if (pos >= 0) {
  2516. return substr(0, pos) + p_with + substr(pos + p_key.length(), length());
  2517. }
  2518. return *this;
  2519. }
  2520. String String::replacen(const String &p_key, const String &p_with) const {
  2521. String new_string;
  2522. int search_from = 0;
  2523. int result = 0;
  2524. while ((result = findn(p_key, search_from)) >= 0) {
  2525. new_string += substr(search_from, result - search_from);
  2526. new_string += p_with;
  2527. search_from = result + p_key.length();
  2528. }
  2529. if (search_from == 0) {
  2530. return *this;
  2531. }
  2532. new_string += substr(search_from, length() - search_from);
  2533. return new_string;
  2534. }
  2535. String String::repeat(int p_count) const {
  2536. ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number.");
  2537. String new_string;
  2538. const CharType *src = this->c_str();
  2539. new_string.resize(length() * p_count + 1);
  2540. new_string[length() * p_count] = 0;
  2541. for (int i = 0; i < p_count; i++) {
  2542. for (int j = 0; j < length(); j++) {
  2543. new_string[i * length() + j] = src[j];
  2544. }
  2545. }
  2546. return new_string;
  2547. }
  2548. String String::left(int p_pos) const {
  2549. if (p_pos <= 0) {
  2550. return "";
  2551. }
  2552. if (p_pos >= length()) {
  2553. return *this;
  2554. }
  2555. return substr(0, p_pos);
  2556. }
  2557. String String::right(int p_pos) const {
  2558. if (p_pos >= length()) {
  2559. return "";
  2560. }
  2561. if (p_pos <= 0) {
  2562. return *this;
  2563. }
  2564. return substr(p_pos, (length() - p_pos));
  2565. }
  2566. CharType String::ord_at(int p_idx) const {
  2567. ERR_FAIL_INDEX_V(p_idx, length(), 0);
  2568. return operator[](p_idx);
  2569. }
  2570. String String::dedent() const {
  2571. String new_string;
  2572. String indent;
  2573. bool has_indent = false;
  2574. bool has_text = false;
  2575. int line_start = 0;
  2576. int indent_stop = -1;
  2577. for (int i = 0; i < length(); i++) {
  2578. CharType c = operator[](i);
  2579. if (c == '\n') {
  2580. if (has_text) {
  2581. new_string += substr(indent_stop, i - indent_stop);
  2582. }
  2583. new_string += "\n";
  2584. has_text = false;
  2585. line_start = i + 1;
  2586. indent_stop = -1;
  2587. } else if (!has_text) {
  2588. if (c > 32) {
  2589. has_text = true;
  2590. if (!has_indent) {
  2591. has_indent = true;
  2592. indent = substr(line_start, i - line_start);
  2593. indent_stop = i;
  2594. }
  2595. }
  2596. if (has_indent && indent_stop < 0) {
  2597. int j = i - line_start;
  2598. if (j >= indent.length() || c != indent[j]) {
  2599. indent_stop = i;
  2600. }
  2601. }
  2602. }
  2603. }
  2604. if (has_text) {
  2605. new_string += substr(indent_stop, length() - indent_stop);
  2606. }
  2607. return new_string;
  2608. }
  2609. String String::strip_edges(bool left, bool right) const {
  2610. int len = length();
  2611. int beg = 0, end = len;
  2612. if (left) {
  2613. for (int i = 0; i < len; i++) {
  2614. if (operator[](i) <= 32) {
  2615. beg++;
  2616. } else {
  2617. break;
  2618. }
  2619. }
  2620. }
  2621. if (right) {
  2622. for (int i = (int)(len - 1); i >= 0; i--) {
  2623. if (operator[](i) <= 32) {
  2624. end--;
  2625. } else {
  2626. break;
  2627. }
  2628. }
  2629. }
  2630. if (beg == 0 && end == len) {
  2631. return *this;
  2632. }
  2633. return substr(beg, end - beg);
  2634. }
  2635. String String::strip_escapes() const {
  2636. String new_string;
  2637. for (int i = 0; i < length(); i++) {
  2638. // Escape characters on first page of the ASCII table, before 32 (Space).
  2639. if (operator[](i) < 32) {
  2640. continue;
  2641. }
  2642. new_string += operator[](i);
  2643. }
  2644. return new_string;
  2645. }
  2646. String String::lstrip(const String &p_chars) const {
  2647. int len = length();
  2648. int beg;
  2649. for (beg = 0; beg < len; beg++) {
  2650. if (p_chars.find_char(get(beg)) == -1) {
  2651. break;
  2652. }
  2653. }
  2654. if (beg == 0) {
  2655. return *this;
  2656. }
  2657. return substr(beg, len - beg);
  2658. }
  2659. String String::rstrip(const String &p_chars) const {
  2660. int len = length();
  2661. int end;
  2662. for (end = len - 1; end >= 0; end--) {
  2663. if (p_chars.find_char(get(end)) == -1) {
  2664. break;
  2665. }
  2666. }
  2667. if (end == len - 1) {
  2668. return *this;
  2669. }
  2670. return substr(0, end + 1);
  2671. }
  2672. String String::simplify_path() const {
  2673. String s = *this;
  2674. String drive;
  2675. if (s.begins_with("local://")) {
  2676. drive = "local://";
  2677. s = s.substr(8, s.length());
  2678. } else if (s.begins_with("res://")) {
  2679. drive = "res://";
  2680. s = s.substr(6, s.length());
  2681. } else if (s.begins_with("user://")) {
  2682. drive = "user://";
  2683. s = s.substr(7, s.length());
  2684. } else if (s.begins_with("/") || s.begins_with("\\")) {
  2685. drive = s.substr(0, 1);
  2686. s = s.substr(1, s.length() - 1);
  2687. } else {
  2688. int p = s.find(":/");
  2689. if (p == -1) {
  2690. p = s.find(":\\");
  2691. }
  2692. if (p != -1 && p < s.find("/")) {
  2693. drive = s.substr(0, p + 2);
  2694. s = s.substr(p + 2, s.length());
  2695. }
  2696. }
  2697. s = s.replace("\\", "/");
  2698. while (true) { // in case of using 2 or more slash
  2699. String compare = s.replace("//", "/");
  2700. if (s == compare) {
  2701. break;
  2702. } else {
  2703. s = compare;
  2704. }
  2705. }
  2706. Vector<String> dirs = s.split("/", false);
  2707. for (int i = 0; i < dirs.size(); i++) {
  2708. String d = dirs[i];
  2709. if (d == ".") {
  2710. dirs.remove(i);
  2711. i--;
  2712. } else if (d == "..") {
  2713. if (i == 0) {
  2714. dirs.remove(i);
  2715. i--;
  2716. } else {
  2717. dirs.remove(i);
  2718. dirs.remove(i - 1);
  2719. i -= 2;
  2720. }
  2721. }
  2722. }
  2723. s = "";
  2724. for (int i = 0; i < dirs.size(); i++) {
  2725. if (i > 0) {
  2726. s += "/";
  2727. }
  2728. s += dirs[i];
  2729. }
  2730. return drive + s;
  2731. }
  2732. static int _humanize_digits(int p_num) {
  2733. if (p_num < 100) {
  2734. return 2;
  2735. } else if (p_num < 1024) {
  2736. return 1;
  2737. } else {
  2738. return 0;
  2739. }
  2740. }
  2741. String String::humanize_size(uint64_t p_size) {
  2742. uint64_t _div = 1;
  2743. Vector<String> prefixes;
  2744. prefixes.push_back(RTR("B"));
  2745. prefixes.push_back(RTR("KiB"));
  2746. prefixes.push_back(RTR("MiB"));
  2747. prefixes.push_back(RTR("GiB"));
  2748. prefixes.push_back(RTR("TiB"));
  2749. prefixes.push_back(RTR("PiB"));
  2750. prefixes.push_back(RTR("EiB"));
  2751. int prefix_idx = 0;
  2752. while (prefix_idx < prefixes.size() - 1 && p_size > (_div * 1024)) {
  2753. _div *= 1024;
  2754. prefix_idx++;
  2755. }
  2756. const int digits = prefix_idx > 0 ? _humanize_digits(p_size / _div) : 0;
  2757. const double divisor = prefix_idx > 0 ? _div : 1;
  2758. return String::num(p_size / divisor).pad_decimals(digits) + " " + prefixes[prefix_idx];
  2759. }
  2760. bool String::is_abs_path() const {
  2761. if (length() > 1) {
  2762. return (operator[](0) == '/' || operator[](0) == '\\' || find(":/") != -1 || find(":\\") != -1);
  2763. } else if ((length()) == 1) {
  2764. return (operator[](0) == '/' || operator[](0) == '\\');
  2765. } else {
  2766. return false;
  2767. }
  2768. }
  2769. bool String::is_valid_identifier() const {
  2770. int len = length();
  2771. if (len == 0) {
  2772. return false;
  2773. }
  2774. const wchar_t *str = &operator[](0);
  2775. for (int i = 0; i < len; i++) {
  2776. if (i == 0) {
  2777. if (str[0] >= '0' && str[0] <= '9') {
  2778. return false; // no start with number plz
  2779. }
  2780. }
  2781. bool valid_char = (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '_';
  2782. if (!valid_char) {
  2783. return false;
  2784. }
  2785. }
  2786. return true;
  2787. }
  2788. //kind of poor should be rewritten properly
  2789. String String::word_wrap(int p_chars_per_line) const {
  2790. int from = 0;
  2791. int last_space = 0;
  2792. String ret;
  2793. for (int i = 0; i < length(); i++) {
  2794. if (i - from >= p_chars_per_line) {
  2795. if (last_space == -1) {
  2796. ret += substr(from, i - from + 1) + "\n";
  2797. } else {
  2798. ret += substr(from, last_space - from) + "\n";
  2799. i = last_space; //rewind
  2800. }
  2801. from = i + 1;
  2802. last_space = -1;
  2803. } else if (operator[](i) == ' ' || operator[](i) == '\t') {
  2804. last_space = i;
  2805. } else if (operator[](i) == '\n') {
  2806. ret += substr(from, i - from) + "\n";
  2807. from = i + 1;
  2808. last_space = -1;
  2809. }
  2810. }
  2811. if (from < length()) {
  2812. ret += substr(from, length());
  2813. }
  2814. return ret;
  2815. }
  2816. String String::http_escape() const {
  2817. const CharString temp = utf8();
  2818. String res;
  2819. for (int i = 0; i < temp.length(); ++i) {
  2820. char ord = temp[i];
  2821. if (ord == '.' || ord == '-' || ord == '_' || ord == '~' ||
  2822. (ord >= 'a' && ord <= 'z') ||
  2823. (ord >= 'A' && ord <= 'Z') ||
  2824. (ord >= '0' && ord <= '9')) {
  2825. res += ord;
  2826. } else {
  2827. char h_Val[3];
  2828. #if defined(__GNUC__) || defined(_MSC_VER)
  2829. snprintf(h_Val, 3, "%02hhX", ord);
  2830. #else
  2831. sprintf(h_Val, "%02hhX", ord);
  2832. #endif
  2833. res += "%";
  2834. res += h_Val;
  2835. }
  2836. }
  2837. return res;
  2838. }
  2839. String String::http_unescape() const {
  2840. String res;
  2841. for (int i = 0; i < length(); ++i) {
  2842. if (ord_at(i) == '%' && i + 2 < length()) {
  2843. CharType ord1 = ord_at(i + 1);
  2844. if ((ord1 >= '0' && ord1 <= '9') || (ord1 >= 'A' && ord1 <= 'Z')) {
  2845. CharType ord2 = ord_at(i + 2);
  2846. if ((ord2 >= '0' && ord2 <= '9') || (ord2 >= 'A' && ord2 <= 'Z')) {
  2847. char bytes[3] = { (char)ord1, (char)ord2, 0 };
  2848. res += (char)strtol(bytes, nullptr, 16);
  2849. i += 2;
  2850. }
  2851. } else {
  2852. res += ord_at(i);
  2853. }
  2854. } else {
  2855. res += ord_at(i);
  2856. }
  2857. }
  2858. return String::utf8(res.ascii());
  2859. }
  2860. String String::c_unescape() const {
  2861. String escaped = *this;
  2862. escaped = escaped.replace("\\a", "\a");
  2863. escaped = escaped.replace("\\b", "\b");
  2864. escaped = escaped.replace("\\f", "\f");
  2865. escaped = escaped.replace("\\n", "\n");
  2866. escaped = escaped.replace("\\r", "\r");
  2867. escaped = escaped.replace("\\t", "\t");
  2868. escaped = escaped.replace("\\v", "\v");
  2869. escaped = escaped.replace("\\'", "\'");
  2870. escaped = escaped.replace("\\\"", "\"");
  2871. escaped = escaped.replace("\\?", "\?");
  2872. escaped = escaped.replace("\\\\", "\\");
  2873. return escaped;
  2874. }
  2875. String String::c_escape() const {
  2876. String escaped = *this;
  2877. escaped = escaped.replace("\\", "\\\\");
  2878. escaped = escaped.replace("\a", "\\a");
  2879. escaped = escaped.replace("\b", "\\b");
  2880. escaped = escaped.replace("\f", "\\f");
  2881. escaped = escaped.replace("\n", "\\n");
  2882. escaped = escaped.replace("\r", "\\r");
  2883. escaped = escaped.replace("\t", "\\t");
  2884. escaped = escaped.replace("\v", "\\v");
  2885. escaped = escaped.replace("\'", "\\'");
  2886. escaped = escaped.replace("\?", "\\?");
  2887. escaped = escaped.replace("\"", "\\\"");
  2888. return escaped;
  2889. }
  2890. String String::c_escape_multiline() const {
  2891. String escaped = *this;
  2892. escaped = escaped.replace("\\", "\\\\");
  2893. escaped = escaped.replace("\"", "\\\"");
  2894. return escaped;
  2895. }
  2896. String String::json_escape() const {
  2897. String escaped = *this;
  2898. escaped = escaped.replace("\\", "\\\\");
  2899. escaped = escaped.replace("\b", "\\b");
  2900. escaped = escaped.replace("\f", "\\f");
  2901. escaped = escaped.replace("\n", "\\n");
  2902. escaped = escaped.replace("\r", "\\r");
  2903. escaped = escaped.replace("\t", "\\t");
  2904. escaped = escaped.replace("\v", "\\v");
  2905. escaped = escaped.replace("\"", "\\\"");
  2906. return escaped;
  2907. }
  2908. String String::xml_escape(bool p_escape_quotes) const {
  2909. String str = *this;
  2910. str = str.replace("&", "&amp;");
  2911. str = str.replace("<", "&lt;");
  2912. str = str.replace(">", "&gt;");
  2913. if (p_escape_quotes) {
  2914. str = str.replace("'", "&apos;");
  2915. str = str.replace("\"", "&quot;");
  2916. }
  2917. /*
  2918. for (int i=1;i<32;i++) {
  2919. char chr[2]={i,0};
  2920. str=str.replace(chr,"&#"+String::num(i)+";");
  2921. }*/
  2922. return str;
  2923. }
  2924. static _FORCE_INLINE_ int _xml_unescape(const CharType *p_src, int p_src_len, CharType *p_dst) {
  2925. int len = 0;
  2926. while (p_src_len) {
  2927. if (*p_src == '&') {
  2928. int eat = 0;
  2929. if (p_src_len >= 4 && p_src[1] == '#') {
  2930. CharType c = 0;
  2931. bool overflow = false;
  2932. if (p_src[2] == 'x') {
  2933. // Hex entity &#x<num>;
  2934. for (int i = 3; i < p_src_len; i++) {
  2935. eat = i + 1;
  2936. CharType ct = p_src[i];
  2937. if (ct == ';') {
  2938. break;
  2939. } else if (ct >= '0' && ct <= '9') {
  2940. ct = ct - '0';
  2941. } else if (ct >= 'a' && ct <= 'f') {
  2942. ct = (ct - 'a') + 10;
  2943. } else if (ct >= 'A' && ct <= 'F') {
  2944. ct = (ct - 'A') + 10;
  2945. } else {
  2946. break;
  2947. }
  2948. if (c > (WCHAR_MAX >> 4)) {
  2949. overflow = true;
  2950. break;
  2951. }
  2952. c <<= 4;
  2953. c |= ct;
  2954. }
  2955. } else {
  2956. // Decimal entity &#<num>;
  2957. for (int i = 2; i < p_src_len; i++) {
  2958. eat = i + 1;
  2959. CharType ct = p_src[i];
  2960. if (ct == ';' || ct < '0' || ct > '9') {
  2961. break;
  2962. }
  2963. }
  2964. if (p_src[eat - 1] == ';') {
  2965. int64_t val = String::to_int(p_src + 2, eat - 3);
  2966. if (val > 0 && val <= WCHAR_MAX) {
  2967. c = (CharType)val;
  2968. } else {
  2969. overflow = true;
  2970. }
  2971. }
  2972. }
  2973. // Value must be non-zero, in the range of char32_t,
  2974. // actually end with ';'. If invalid, leave the entity as-is
  2975. if (c == '\0' || overflow || p_src[eat - 1] != ';') {
  2976. eat = 1;
  2977. c = *p_src;
  2978. }
  2979. if (p_dst) {
  2980. *p_dst = c;
  2981. }
  2982. } else if (p_src_len >= 4 && p_src[1] == 'g' && p_src[2] == 't' && p_src[3] == ';') {
  2983. if (p_dst) {
  2984. *p_dst = '>';
  2985. }
  2986. eat = 4;
  2987. } else if (p_src_len >= 4 && p_src[1] == 'l' && p_src[2] == 't' && p_src[3] == ';') {
  2988. if (p_dst) {
  2989. *p_dst = '<';
  2990. }
  2991. eat = 4;
  2992. } else if (p_src_len >= 5 && p_src[1] == 'a' && p_src[2] == 'm' && p_src[3] == 'p' && p_src[4] == ';') {
  2993. if (p_dst) {
  2994. *p_dst = '&';
  2995. }
  2996. eat = 5;
  2997. } 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] == ';') {
  2998. if (p_dst) {
  2999. *p_dst = '"';
  3000. }
  3001. eat = 6;
  3002. } 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] == ';') {
  3003. if (p_dst) {
  3004. *p_dst = '\'';
  3005. }
  3006. eat = 6;
  3007. } else {
  3008. if (p_dst) {
  3009. *p_dst = *p_src;
  3010. }
  3011. eat = 1;
  3012. }
  3013. if (p_dst) {
  3014. p_dst++;
  3015. }
  3016. len++;
  3017. p_src += eat;
  3018. p_src_len -= eat;
  3019. } else {
  3020. if (p_dst) {
  3021. *p_dst = *p_src;
  3022. p_dst++;
  3023. }
  3024. len++;
  3025. p_src++;
  3026. p_src_len--;
  3027. }
  3028. }
  3029. return len;
  3030. }
  3031. String String::xml_unescape() const {
  3032. String str;
  3033. int l = length();
  3034. int len = _xml_unescape(c_str(), l, nullptr);
  3035. if (len == 0) {
  3036. return String();
  3037. }
  3038. str.resize(len + 1);
  3039. _xml_unescape(c_str(), l, str.ptrw());
  3040. str[len] = 0;
  3041. return str;
  3042. }
  3043. String String::pad_decimals(int p_digits) const {
  3044. String s = *this;
  3045. int c = s.find(".");
  3046. if (c == -1) {
  3047. if (p_digits <= 0) {
  3048. return s;
  3049. }
  3050. s += ".";
  3051. c = s.length() - 1;
  3052. } else {
  3053. if (p_digits <= 0) {
  3054. return s.substr(0, c);
  3055. }
  3056. }
  3057. if (s.length() - (c + 1) > p_digits) {
  3058. s = s.substr(0, c + p_digits + 1);
  3059. } else {
  3060. while (s.length() - (c + 1) < p_digits) {
  3061. s += "0";
  3062. }
  3063. }
  3064. return s;
  3065. }
  3066. String String::pad_zeros(int p_digits) const {
  3067. String s = *this;
  3068. int end = s.find(".");
  3069. if (end == -1) {
  3070. end = s.length();
  3071. }
  3072. if (end == 0) {
  3073. return s;
  3074. }
  3075. int begin = 0;
  3076. while (begin < end && (s[begin] < '0' || s[begin] > '9')) {
  3077. begin++;
  3078. }
  3079. if (begin >= end) {
  3080. return s;
  3081. }
  3082. while (end - begin < p_digits) {
  3083. s = s.insert(begin, "0");
  3084. end++;
  3085. }
  3086. return s;
  3087. }
  3088. String String::trim_prefix(const String &p_prefix) const {
  3089. String s = *this;
  3090. if (s.begins_with(p_prefix)) {
  3091. return s.substr(p_prefix.length(), s.length() - p_prefix.length());
  3092. }
  3093. return s;
  3094. }
  3095. String String::trim_suffix(const String &p_suffix) const {
  3096. String s = *this;
  3097. if (s.ends_with(p_suffix)) {
  3098. return s.substr(0, s.length() - p_suffix.length());
  3099. }
  3100. return s;
  3101. }
  3102. bool String::is_valid_integer() const {
  3103. int len = length();
  3104. if (len == 0) {
  3105. return false;
  3106. }
  3107. int from = 0;
  3108. if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
  3109. from++;
  3110. }
  3111. for (int i = from; i < len; i++) {
  3112. if (operator[](i) < '0' || operator[](i) > '9') {
  3113. return false; // no start with number plz
  3114. }
  3115. }
  3116. return true;
  3117. }
  3118. bool String::is_valid_hex_number(bool p_with_prefix) const {
  3119. int len = length();
  3120. if (len == 0) {
  3121. return false;
  3122. }
  3123. int from = 0;
  3124. if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
  3125. from++;
  3126. }
  3127. if (p_with_prefix) {
  3128. if (len < 3) {
  3129. return false;
  3130. }
  3131. if (operator[](from) != '0' || operator[](from + 1) != 'x') {
  3132. return false;
  3133. }
  3134. from += 2;
  3135. }
  3136. for (int i = from; i < len; i++) {
  3137. CharType c = operator[](i);
  3138. if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
  3139. continue;
  3140. }
  3141. return false;
  3142. }
  3143. return true;
  3144. };
  3145. bool String::is_valid_float() const {
  3146. int len = length();
  3147. if (len == 0) {
  3148. return false;
  3149. }
  3150. int from = 0;
  3151. if (operator[](0) == '+' || operator[](0) == '-') {
  3152. from++;
  3153. }
  3154. bool exponent_found = false;
  3155. bool period_found = false;
  3156. bool sign_found = false;
  3157. bool exponent_values_found = false;
  3158. bool numbers_found = false;
  3159. for (int i = from; i < len; i++) {
  3160. if (operator[](i) >= '0' && operator[](i) <= '9') {
  3161. if (exponent_found) {
  3162. exponent_values_found = true;
  3163. } else {
  3164. numbers_found = true;
  3165. }
  3166. } else if (numbers_found && !exponent_found && operator[](i) == 'e') {
  3167. exponent_found = true;
  3168. } else if (!period_found && !exponent_found && operator[](i) == '.') {
  3169. period_found = true;
  3170. } else if ((operator[](i) == '-' || operator[](i) == '+') && exponent_found && !exponent_values_found && !sign_found) {
  3171. sign_found = true;
  3172. } else {
  3173. return false; // no start with number plz
  3174. }
  3175. }
  3176. return numbers_found;
  3177. }
  3178. String String::path_to_file(const String &p_path) const {
  3179. // Don't get base dir for src, this is expected to be a dir already.
  3180. String src = this->replace("\\", "/");
  3181. String dst = p_path.replace("\\", "/").get_base_dir();
  3182. String rel = src.path_to(dst);
  3183. if (rel == dst) { // failed
  3184. return p_path;
  3185. } else {
  3186. return rel + p_path.get_file();
  3187. }
  3188. }
  3189. String String::path_to(const String &p_path) const {
  3190. String src = this->replace("\\", "/");
  3191. String dst = p_path.replace("\\", "/");
  3192. if (!src.ends_with("/")) {
  3193. src += "/";
  3194. }
  3195. if (!dst.ends_with("/")) {
  3196. dst += "/";
  3197. }
  3198. String base;
  3199. if (src.begins_with("res://") && dst.begins_with("res://")) {
  3200. base = "res:/";
  3201. src = src.replace("res://", "/");
  3202. dst = dst.replace("res://", "/");
  3203. } else if (src.begins_with("user://") && dst.begins_with("user://")) {
  3204. base = "user:/";
  3205. src = src.replace("user://", "/");
  3206. dst = dst.replace("user://", "/");
  3207. } else if (src.begins_with("/") && dst.begins_with("/")) {
  3208. //nothing
  3209. } else {
  3210. //dos style
  3211. String src_begin = src.get_slicec('/', 0);
  3212. String dst_begin = dst.get_slicec('/', 0);
  3213. if (src_begin != dst_begin) {
  3214. return p_path; //impossible to do this
  3215. }
  3216. base = src_begin;
  3217. src = src.substr(src_begin.length(), src.length());
  3218. dst = dst.substr(dst_begin.length(), dst.length());
  3219. }
  3220. //remove leading and trailing slash and split
  3221. Vector<String> src_dirs = src.substr(1, src.length() - 2).split("/");
  3222. Vector<String> dst_dirs = dst.substr(1, dst.length() - 2).split("/");
  3223. //find common parent
  3224. int common_parent = 0;
  3225. while (true) {
  3226. if (src_dirs.size() == common_parent) {
  3227. break;
  3228. }
  3229. if (dst_dirs.size() == common_parent) {
  3230. break;
  3231. }
  3232. if (src_dirs[common_parent] != dst_dirs[common_parent]) {
  3233. break;
  3234. }
  3235. common_parent++;
  3236. }
  3237. common_parent--;
  3238. String dir;
  3239. for (int i = src_dirs.size() - 1; i > common_parent; i--) {
  3240. dir += "../";
  3241. }
  3242. for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
  3243. dir += dst_dirs[i] + "/";
  3244. }
  3245. if (dir.length() == 0) {
  3246. dir = "./";
  3247. }
  3248. return dir;
  3249. }
  3250. bool String::is_valid_html_color() const {
  3251. return Color::html_is_valid(*this);
  3252. }
  3253. bool String::is_valid_filename() const {
  3254. String stripped = strip_edges();
  3255. if (*this != stripped) {
  3256. return false;
  3257. }
  3258. if (stripped == String()) {
  3259. return false;
  3260. }
  3261. return !(find(":") != -1 || find("/") != -1 || find("\\") != -1 || find("?") != -1 || find("*") != -1 || find("\"") != -1 || find("|") != -1 || find("%") != -1 || find("<") != -1 || find(">") != -1);
  3262. }
  3263. bool String::is_valid_ip_address() const {
  3264. if (find(":") >= 0) {
  3265. Vector<String> ip = split(":");
  3266. for (int i = 0; i < ip.size(); i++) {
  3267. String n = ip[i];
  3268. if (n.empty()) {
  3269. continue;
  3270. }
  3271. if (n.is_valid_hex_number(false)) {
  3272. int nint = n.hex_to_int(false);
  3273. if (nint < 0 || nint > 0xffff) {
  3274. return false;
  3275. }
  3276. continue;
  3277. };
  3278. if (!n.is_valid_ip_address()) {
  3279. return false;
  3280. }
  3281. };
  3282. } else {
  3283. Vector<String> ip = split(".");
  3284. if (ip.size() != 4) {
  3285. return false;
  3286. }
  3287. for (int i = 0; i < ip.size(); i++) {
  3288. String n = ip[i];
  3289. if (!n.is_valid_integer()) {
  3290. return false;
  3291. }
  3292. int val = n.to_int();
  3293. if (val < 0 || val > 255) {
  3294. return false;
  3295. }
  3296. }
  3297. };
  3298. return true;
  3299. }
  3300. bool String::is_resource_file() const {
  3301. return begins_with("res://") && find("::") == -1;
  3302. }
  3303. bool String::is_rel_path() const {
  3304. return !is_abs_path();
  3305. }
  3306. String String::get_base_dir() const {
  3307. int basepos = find(":/");
  3308. if (basepos == -1) {
  3309. basepos = find(":\\");
  3310. }
  3311. String rs;
  3312. String base;
  3313. if (basepos != -1) {
  3314. int end = basepos + 3;
  3315. rs = substr(end, length());
  3316. base = substr(0, end);
  3317. } else {
  3318. if (begins_with("/")) {
  3319. rs = substr(1, length());
  3320. base = "/";
  3321. } else {
  3322. rs = *this;
  3323. }
  3324. }
  3325. int sep = MAX(rs.find_last("/"), rs.find_last("\\"));
  3326. if (sep == -1) {
  3327. return base;
  3328. }
  3329. return base + rs.substr(0, sep);
  3330. }
  3331. String String::get_file() const {
  3332. int sep = MAX(find_last("/"), find_last("\\"));
  3333. if (sep == -1) {
  3334. return *this;
  3335. }
  3336. return substr(sep + 1, length());
  3337. }
  3338. String String::get_extension() const {
  3339. int pos = find_last(".");
  3340. if (pos < 0 || pos < MAX(find_last("/"), find_last("\\"))) {
  3341. return "";
  3342. }
  3343. return substr(pos + 1, length());
  3344. }
  3345. String String::plus_file(const String &p_file) const {
  3346. if (empty()) {
  3347. return p_file;
  3348. }
  3349. if (operator[](length() - 1) == '/' || (p_file.size() > 0 && p_file.operator[](0) == '/')) {
  3350. return *this + p_file;
  3351. }
  3352. return *this + "/" + p_file;
  3353. }
  3354. String String::percent_encode() const {
  3355. CharString cs = utf8();
  3356. String encoded;
  3357. for (int i = 0; i < cs.length(); i++) {
  3358. uint8_t c = cs[i];
  3359. if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '~' || c == '.') {
  3360. char p[2] = { (char)c, 0 };
  3361. encoded += p;
  3362. } else {
  3363. char p[4] = { '%', 0, 0, 0 };
  3364. static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  3365. p[1] = hex[c >> 4];
  3366. p[2] = hex[c & 0xF];
  3367. encoded += p;
  3368. }
  3369. }
  3370. return encoded;
  3371. }
  3372. String String::percent_decode() const {
  3373. CharString pe;
  3374. CharString cs = utf8();
  3375. for (int i = 0; i < cs.length(); i++) {
  3376. uint8_t c = cs[i];
  3377. if (c == '%' && i < length() - 2) {
  3378. uint8_t a = LOWERCASE(cs[i + 1]);
  3379. uint8_t b = LOWERCASE(cs[i + 2]);
  3380. if (a >= '0' && a <= '9') {
  3381. c = (a - '0') << 4;
  3382. } else if (a >= 'a' && a <= 'f') {
  3383. c = (a - 'a' + 10) << 4;
  3384. } else {
  3385. continue;
  3386. }
  3387. uint8_t d = 0;
  3388. if (b >= '0' && b <= '9') {
  3389. d = (b - '0');
  3390. } else if (b >= 'a' && b <= 'f') {
  3391. d = (b - 'a' + 10);
  3392. } else {
  3393. continue;
  3394. }
  3395. c += d;
  3396. i += 2;
  3397. }
  3398. pe += c;
  3399. }
  3400. return String::utf8(pe.ptr());
  3401. }
  3402. String String::property_name_encode() const {
  3403. // Escape and quote strings with extended ASCII or further Unicode characters
  3404. // as well as '"', '=' or ' ' (32)
  3405. const CharType *cstr = c_str();
  3406. for (int i = 0; cstr[i]; i++) {
  3407. if (cstr[i] == '=' || cstr[i] == '"' || cstr[i] < 33 || cstr[i] > 126) {
  3408. return "\"" + c_escape_multiline() + "\"";
  3409. }
  3410. }
  3411. // Keep as is
  3412. return *this;
  3413. }
  3414. // Changes made to the set of invalid characters must also be reflected in the String documentation.
  3415. const String String::invalid_node_name_characters = ". : @ / \"";
  3416. String String::validate_node_name() const {
  3417. Vector<String> chars = String::invalid_node_name_characters.split(" ");
  3418. String name = this->replace(chars[0], "");
  3419. for (int i = 1; i < chars.size(); i++) {
  3420. name = name.replace(chars[i], "");
  3421. }
  3422. return name;
  3423. }
  3424. String String::get_basename() const {
  3425. int pos = find_last(".");
  3426. if (pos < 0 || pos < MAX(find_last("/"), find_last("\\"))) {
  3427. return *this;
  3428. }
  3429. return substr(0, pos);
  3430. }
  3431. String itos(int64_t p_val) {
  3432. return String::num_int64(p_val);
  3433. }
  3434. String uitos(uint64_t p_val) {
  3435. return String::num_uint64(p_val);
  3436. }
  3437. String rtos(double p_val) {
  3438. return String::num(p_val);
  3439. }
  3440. String rtoss(double p_val) {
  3441. return String::num_scientific(p_val);
  3442. }
  3443. // Right-pad with a character.
  3444. String String::rpad(int min_length, const String &character) const {
  3445. String s = *this;
  3446. int padding = min_length - s.length();
  3447. if (padding > 0) {
  3448. for (int i = 0; i < padding; i++) {
  3449. s = s + character;
  3450. }
  3451. }
  3452. return s;
  3453. }
  3454. // Left-pad with a character.
  3455. String String::lpad(int min_length, const String &character) const {
  3456. String s = *this;
  3457. int padding = min_length - s.length();
  3458. if (padding > 0) {
  3459. for (int i = 0; i < padding; i++) {
  3460. s = character + s;
  3461. }
  3462. }
  3463. return s;
  3464. }
  3465. // sprintf is implemented in GDScript via:
  3466. // "fish %s pie" % "frog"
  3467. // "fish %s %d pie" % ["frog", 12]
  3468. // In case of an error, the string returned is the error description and "error" is true.
  3469. String String::sprintf(const Array &values, bool *error) const {
  3470. String formatted;
  3471. CharType *self = (CharType *)c_str();
  3472. bool in_format = false;
  3473. int value_index = 0;
  3474. int min_chars = 0;
  3475. int min_decimals = 0;
  3476. bool in_decimals = false;
  3477. bool pad_with_zeroes = false;
  3478. bool left_justified = false;
  3479. bool show_sign = false;
  3480. *error = true;
  3481. for (; *self; self++) {
  3482. const CharType c = *self;
  3483. if (in_format) { // We have % - lets see what else we get.
  3484. switch (c) {
  3485. case '%': { // Replace %% with %
  3486. formatted += chr(c);
  3487. in_format = false;
  3488. break;
  3489. }
  3490. case 'd': // Integer (signed)
  3491. case 'o': // Octal
  3492. case 'x': // Hexadecimal (lowercase)
  3493. case 'X': { // Hexadecimal (uppercase)
  3494. if (value_index >= values.size()) {
  3495. return "not enough arguments for format string";
  3496. }
  3497. if (!values[value_index].is_num()) {
  3498. return "a number is required";
  3499. }
  3500. int64_t value = values[value_index];
  3501. int base = 16;
  3502. bool capitalize = false;
  3503. switch (c) {
  3504. case 'd':
  3505. base = 10;
  3506. break;
  3507. case 'o':
  3508. base = 8;
  3509. break;
  3510. case 'x':
  3511. break;
  3512. case 'X':
  3513. base = 16;
  3514. capitalize = true;
  3515. break;
  3516. }
  3517. // Get basic number.
  3518. String str = String::num_int64(ABS(value), base, capitalize);
  3519. int number_len = str.length();
  3520. // Padding.
  3521. int pad_chars_count = (value < 0 || show_sign) ? min_chars - 1 : min_chars;
  3522. String pad_char = pad_with_zeroes ? String("0") : String(" ");
  3523. if (left_justified) {
  3524. str = str.rpad(pad_chars_count, pad_char);
  3525. } else {
  3526. str = str.lpad(pad_chars_count, pad_char);
  3527. }
  3528. // Sign.
  3529. if (show_sign && value >= 0) {
  3530. str = str.insert(pad_with_zeroes ? 0 : str.length() - number_len, "+");
  3531. } else if (value < 0) {
  3532. str = str.insert(pad_with_zeroes ? 0 : str.length() - number_len, "-");
  3533. }
  3534. formatted += str;
  3535. ++value_index;
  3536. in_format = false;
  3537. break;
  3538. }
  3539. case 'f': { // Float
  3540. if (value_index >= values.size()) {
  3541. return "not enough arguments for format string";
  3542. }
  3543. if (!values[value_index].is_num()) {
  3544. return "a number is required";
  3545. }
  3546. double value = values[value_index];
  3547. bool is_negative = (value < 0);
  3548. String str = String::num(ABS(value), min_decimals);
  3549. // Pad decimals out.
  3550. str = str.pad_decimals(min_decimals);
  3551. int initial_len = str.length();
  3552. // Padding. Leave room for sign later if required.
  3553. int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars;
  3554. String pad_char = pad_with_zeroes ? String("0") : String(" ");
  3555. if (left_justified) {
  3556. if (pad_with_zeroes) {
  3557. return "left justification cannot be used with zeros as the padding";
  3558. } else {
  3559. str = str.rpad(pad_chars_count, pad_char);
  3560. }
  3561. } else {
  3562. str = str.lpad(pad_chars_count, pad_char);
  3563. }
  3564. // Add sign if needed.
  3565. if (show_sign || is_negative) {
  3566. String sign_char = is_negative ? "-" : "+";
  3567. if (left_justified) {
  3568. str = str.insert(0, sign_char);
  3569. } else {
  3570. str = str.insert(pad_with_zeroes ? 0 : str.length() - initial_len, sign_char);
  3571. }
  3572. }
  3573. formatted += str;
  3574. ++value_index;
  3575. in_format = false;
  3576. break;
  3577. }
  3578. case 's': { // String
  3579. if (value_index >= values.size()) {
  3580. return "not enough arguments for format string";
  3581. }
  3582. String str = values[value_index];
  3583. // Padding.
  3584. if (left_justified) {
  3585. str = str.rpad(min_chars);
  3586. } else {
  3587. str = str.lpad(min_chars);
  3588. }
  3589. formatted += str;
  3590. ++value_index;
  3591. in_format = false;
  3592. break;
  3593. }
  3594. case 'c': {
  3595. if (value_index >= values.size()) {
  3596. return "not enough arguments for format string";
  3597. }
  3598. // Convert to character.
  3599. String str;
  3600. if (values[value_index].is_num()) {
  3601. int value = values[value_index];
  3602. if (value < 0) {
  3603. return "unsigned byte integer is lower than maximum";
  3604. } else if (value > 255) {
  3605. return "unsigned byte integer is greater than maximum";
  3606. }
  3607. str = chr(values[value_index]);
  3608. } else if (values[value_index].get_type() == Variant::STRING) {
  3609. str = values[value_index];
  3610. if (str.length() != 1) {
  3611. return "%c requires number or single-character string";
  3612. }
  3613. } else {
  3614. return "%c requires number or single-character string";
  3615. }
  3616. // Padding.
  3617. if (left_justified) {
  3618. str = str.rpad(min_chars);
  3619. } else {
  3620. str = str.lpad(min_chars);
  3621. }
  3622. formatted += str;
  3623. ++value_index;
  3624. in_format = false;
  3625. break;
  3626. }
  3627. case '-': { // Left justify
  3628. left_justified = true;
  3629. break;
  3630. }
  3631. case '+': { // Show + if positive.
  3632. show_sign = true;
  3633. break;
  3634. }
  3635. case '0':
  3636. case '1':
  3637. case '2':
  3638. case '3':
  3639. case '4':
  3640. case '5':
  3641. case '6':
  3642. case '7':
  3643. case '8':
  3644. case '9': {
  3645. int n = c - '0';
  3646. if (in_decimals) {
  3647. min_decimals *= 10;
  3648. min_decimals += n;
  3649. } else {
  3650. if (c == '0' && min_chars == 0) {
  3651. pad_with_zeroes = true;
  3652. } else {
  3653. min_chars *= 10;
  3654. min_chars += n;
  3655. }
  3656. }
  3657. break;
  3658. }
  3659. case '.': { // Float separator.
  3660. if (in_decimals) {
  3661. return "too many decimal points in format";
  3662. }
  3663. in_decimals = true;
  3664. min_decimals = 0; // We want to add the value manually.
  3665. break;
  3666. }
  3667. case '*': { // Dynamic width, based on value.
  3668. if (value_index >= values.size()) {
  3669. return "not enough arguments for format string";
  3670. }
  3671. if (!values[value_index].is_num()) {
  3672. return "* wants number";
  3673. }
  3674. int size = values[value_index];
  3675. if (in_decimals) {
  3676. min_decimals = size;
  3677. } else {
  3678. min_chars = size;
  3679. }
  3680. ++value_index;
  3681. break;
  3682. }
  3683. default: {
  3684. return "unsupported format character";
  3685. }
  3686. }
  3687. } else { // Not in format string.
  3688. switch (c) {
  3689. case '%':
  3690. in_format = true;
  3691. // Back to defaults:
  3692. min_chars = 0;
  3693. min_decimals = 6;
  3694. pad_with_zeroes = false;
  3695. left_justified = false;
  3696. show_sign = false;
  3697. in_decimals = false;
  3698. break;
  3699. default:
  3700. formatted += chr(c);
  3701. }
  3702. }
  3703. }
  3704. if (in_format) {
  3705. return "incomplete format";
  3706. }
  3707. if (value_index != values.size()) {
  3708. return "not all arguments converted during string formatting";
  3709. }
  3710. *error = false;
  3711. return formatted;
  3712. }
  3713. String String::quote(String quotechar) const {
  3714. return quotechar + *this + quotechar;
  3715. }
  3716. String String::unquote() const {
  3717. if (!is_quoted()) {
  3718. return *this;
  3719. }
  3720. return substr(1, length() - 2);
  3721. }
  3722. #ifdef TOOLS_ENABLED
  3723. String TTR(const String &p_text) {
  3724. if (TranslationServer::get_singleton()) {
  3725. return TranslationServer::get_singleton()->tool_translate(p_text);
  3726. }
  3727. return p_text;
  3728. }
  3729. #endif
  3730. String RTR(const String &p_text) {
  3731. if (TranslationServer::get_singleton()) {
  3732. String rtr = TranslationServer::get_singleton()->tool_translate(p_text);
  3733. if (rtr == String() || rtr == p_text) {
  3734. return TranslationServer::get_singleton()->translate(p_text);
  3735. } else {
  3736. return rtr;
  3737. }
  3738. }
  3739. return p_text;
  3740. }