ustring.cpp 70 KB

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