ustring.cpp 78 KB

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