ustring.cpp 78 KB

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