ustring.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280
  1. /*************************************************************************/
  2. /* ustring.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 "ucaps.h"
  35. #include "color.h"
  36. #define MAX_DIGITS 6
  37. #define UPPERCASE(m_c) (((m_c)>='a' && (m_c)<='z')?((m_c)-('a'-'A')):(m_c))
  38. #define LOWERCASE(m_c) (((m_c)>='A' && (m_c)<='Z')?((m_c)+('a'-'A')):(m_c))
  39. #ifndef NO_USE_STDLIB
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #endif
  43. /** STRING **/
  44. const char *CharString::get_data() const {
  45. if (size())
  46. return &operator[](0);
  47. else
  48. return "";
  49. }
  50. void String::copy_from(const char *p_cstr) {
  51. int len=0;
  52. const char *ptr=p_cstr;
  53. while (*(ptr++)!=0)
  54. len++;
  55. if (len==0) {
  56. resize(0);
  57. return;
  58. }
  59. resize(len+1); // include 0
  60. for(int i=0;i<len+1;i++) {
  61. set(i,p_cstr[i]);
  62. }
  63. }
  64. void String::copy_from(const CharType* p_cstr, int p_clip_to) {
  65. int len=0;
  66. const CharType *ptr=p_cstr;
  67. while (*(ptr++)!=0)
  68. len++;
  69. if (p_clip_to>=0 && len>p_clip_to)
  70. len=p_clip_to;
  71. if (len==0) {
  72. resize(0);
  73. return;
  74. }
  75. resize(len+1);
  76. set(len,0);
  77. CharType *dst = &operator[](0);
  78. for (int i=0;i<len;i++) {
  79. dst[i]=p_cstr[i];
  80. }
  81. }
  82. void String::copy_from(const CharType& p_char) {
  83. resize(2);
  84. set(0,p_char);
  85. set(1,0);
  86. }
  87. bool String::operator==(const String& p_str) const {
  88. if (length()!=p_str.length())
  89. return false;
  90. if (empty())
  91. return true;
  92. int l=length();
  93. const CharType *src = c_str();
  94. const CharType *dst = p_str.c_str();
  95. /* Compare char by char */
  96. for (int i=0;i<l;i++) {
  97. if (src[i]!=dst[i])
  98. return false;
  99. }
  100. return true;
  101. }
  102. bool String::operator!=(const String& p_str) const {
  103. return !(*this==p_str);
  104. }
  105. String String::operator+(const String& p_str) const {
  106. String res=*this;
  107. res+=p_str;
  108. return res;
  109. }
  110. /*
  111. String String::operator+(CharType p_chr) const {
  112. String res=*this;
  113. res+=p_chr;
  114. return res;
  115. }
  116. */
  117. String& String::operator+=(const String &p_str) {
  118. if (empty()) {
  119. *this=p_str;
  120. return *this;
  121. }
  122. if (p_str.empty())
  123. return *this;
  124. int from=length();
  125. resize( length() + p_str.size() );
  126. const CharType *src = p_str.c_str();
  127. CharType *dst = &operator[](0);
  128. set( length(), 0 );
  129. for (int i=0;i<p_str.length();i++)
  130. dst[from+i]=src[i];
  131. return *this;
  132. }
  133. String& String::operator+=(const CharType *p_str) {
  134. *this+=String(p_str);
  135. return *this;
  136. }
  137. String& String::operator+=(CharType p_char) {
  138. resize( size() ? size() + 1 : 2);
  139. set( length(), 0 );
  140. set( length() -1, p_char );
  141. return *this;
  142. }
  143. String& String::operator+=(const char * p_str) {
  144. if (!p_str || p_str[0]==0)
  145. return *this;
  146. int src_len=0;
  147. const char *ptr=p_str;
  148. while (*(ptr++)!=0)
  149. src_len++;
  150. int from=length();
  151. resize( from + src_len + 1 );
  152. CharType *dst = &operator[](0);
  153. set( length(), 0 );
  154. for (int i=0;i<src_len;i++)
  155. dst[from+i]=p_str[i];
  156. return *this;
  157. }
  158. void String::operator=(const char *p_str) {
  159. copy_from(p_str);
  160. }
  161. void String::operator=(const CharType *p_str) {
  162. copy_from(p_str);
  163. }
  164. bool String::operator==(const StrRange &p_range) const {
  165. int len=p_range.len;
  166. if (length()!=len)
  167. return false;
  168. if (empty())
  169. return true;
  170. const CharType *c_str=p_range.c_str;
  171. int l=length();
  172. const CharType *dst = p_range.c_str;
  173. /* Compare char by char */
  174. for (int i=0;i<l;i++) {
  175. if (c_str[i]!=dst[i])
  176. return false;
  177. }
  178. return true;
  179. }
  180. bool String::operator==(const char *p_str) const {
  181. int len=0;
  182. const char *aux=p_str;
  183. while (*(aux++)!=0)
  184. len++;
  185. if (length()!=len)
  186. return false;
  187. if (empty())
  188. return true;
  189. int l=length();
  190. const CharType *dst = c_str();
  191. /* Compare char by char */
  192. for (int i=0;i<l;i++) {
  193. if (p_str[i]!=dst[i])
  194. return false;
  195. }
  196. return true;
  197. }
  198. bool String::operator==(const CharType *p_str) const {
  199. int len=0;
  200. const CharType *aux=p_str;
  201. while (*(aux++)!=0)
  202. len++;
  203. if (length()!=len)
  204. return false;
  205. if (empty())
  206. return true;
  207. int l=length();
  208. const CharType *dst = c_str();
  209. /* Compare char by char */
  210. for (int i=0;i<l;i++) {
  211. if (p_str[i]!=dst[i])
  212. return false;
  213. }
  214. return true;
  215. }
  216. bool String::operator!=(const char *p_str) const {
  217. return (! ( *this==p_str ) );
  218. }
  219. bool String::operator!=(const CharType *p_str) const {
  220. return (! ( *this==p_str ) );
  221. }
  222. bool String::operator<(const CharType *p_str) const {
  223. if (empty() && p_str[0]==0)
  224. return false;
  225. if (empty())
  226. return true;
  227. const CharType *this_str=c_str();
  228. while (true) {
  229. if (*p_str==0 && *this_str==0)
  230. return false; //this can't be equal, sadly
  231. else if (*this_str==0)
  232. return true; //if this is empty, and the other one is not, then we're less.. I think?
  233. else if (*p_str==0)
  234. return false; //otherwise the other one is smaller..
  235. else if (*this_str < *p_str ) //more than
  236. return true;
  237. else if (*this_str > *p_str ) //less than
  238. return false;
  239. this_str++;
  240. p_str++;
  241. }
  242. return false; //should never reach here anyway
  243. }
  244. bool String::operator<=(String p_str) const {
  245. return (*this<p_str) || (*this==p_str);
  246. }
  247. bool String::operator<(const char *p_str) const {
  248. if (empty() && p_str[0]==0)
  249. return false;
  250. if (empty())
  251. return true;
  252. const CharType *this_str=c_str();
  253. while (true) {
  254. if (*p_str==0 && *this_str==0)
  255. return false; //this can't be equal, sadly
  256. else if (*this_str==0)
  257. return true; //if this is empty, and the other one is not, then we're less.. I think?
  258. else if (*p_str==0)
  259. return false; //otherwise the other one is smaller..
  260. else if (*this_str < *p_str ) //more than
  261. return true;
  262. else if (*this_str > *p_str ) //less than
  263. return false;
  264. this_str++;
  265. p_str++;
  266. }
  267. return false; //should never reach here anyway
  268. }
  269. bool String::operator<(String p_str) const {
  270. return operator<(p_str.c_str());
  271. }
  272. signed char String::nocasecmp_to(const String &p_str) const {
  273. if (empty() && p_str.empty())
  274. return 0;
  275. if (empty())
  276. return -1;
  277. if (p_str.empty())
  278. return 1;
  279. const CharType *that_str=p_str.c_str();
  280. const CharType *this_str=c_str();
  281. while (true) {
  282. if (*that_str==0 && *this_str==0)
  283. return 0; //we're equal
  284. else if (*this_str==0)
  285. return -1; //if this is empty, and the other one is not, then we're less.. I think?
  286. else if (*that_str==0)
  287. return 1; //otherwise the other one is smaller..
  288. else if (_find_upper(*this_str) < _find_upper(*that_str) ) //more than
  289. return -1;
  290. else if (_find_upper(*this_str) > _find_upper(*that_str) ) //less than
  291. return 1;
  292. this_str++;
  293. that_str++;
  294. }
  295. return 0; //should never reach anyway
  296. }
  297. signed char String::casecmp_to(const String &p_str) const {
  298. if (empty() && p_str.empty())
  299. return 0;
  300. if (empty())
  301. return -1;
  302. if (p_str.empty())
  303. return 1;
  304. const CharType *that_str=p_str.c_str();
  305. const CharType *this_str=c_str();
  306. while (true) {
  307. if (*that_str==0 && *this_str==0)
  308. return 0; //we're equal
  309. else if (*this_str==0)
  310. return -1; //if this is empty, and the other one is not, then we're less.. I think?
  311. else if (*that_str==0)
  312. return 1; //otherwise the other one is smaller..
  313. else if (*this_str < *that_str ) //more than
  314. return -1;
  315. else if (*this_str > *that_str ) //less than
  316. return 1;
  317. this_str++;
  318. that_str++;
  319. }
  320. return 0; //should never reach anyway
  321. }
  322. void String::erase(int p_pos, int p_chars) {
  323. *this=left(p_pos)+substr( p_pos + p_chars , length() - ( (p_pos+p_chars) ) );
  324. }
  325. String String::capitalize() const {
  326. String aux=this->replace("_"," ").to_lower();
  327. String cap;
  328. for (int i=0;i<aux.get_slice_count(" ");i++) {
  329. String slice=aux.get_slice(" ",i);
  330. if (slice.length()>0) {
  331. slice[0]=_find_upper(slice[0]);
  332. if (i>0)
  333. cap+=" ";
  334. cap+=slice;
  335. }
  336. }
  337. return cap;
  338. }
  339. int String::get_slice_count(String p_splitter) const{
  340. if (empty())
  341. return 0;
  342. if (p_splitter.empty())
  343. return 0;
  344. int pos=0;
  345. int slices=1;
  346. while ( (pos=find(p_splitter,pos))>=0) {
  347. slices++;
  348. pos+=p_splitter.length();
  349. }
  350. return slices;
  351. }
  352. String String::get_slice(String p_splitter, int p_slice) const {
  353. if (empty() || p_splitter.empty())
  354. return "";
  355. int pos=0;
  356. int prev_pos=0;
  357. // int slices=1;
  358. if (p_slice<0)
  359. return "";
  360. if (find(p_splitter)==-1)
  361. return *this;
  362. int i=0;
  363. while(true) {
  364. pos=find(p_splitter,pos);
  365. if (pos==-1)
  366. pos=length(); //reached end
  367. int from=prev_pos;
  368. // int to=pos;
  369. if (p_slice==i) {
  370. return substr( from, pos-from );
  371. }
  372. if (pos==length()) //reached end and no find
  373. break;
  374. pos+=p_splitter.length();
  375. prev_pos=pos;
  376. i++;
  377. }
  378. return ""; //no find!
  379. }
  380. Vector<String> String::split(const String &p_splitter,bool p_allow_empty) const {
  381. Vector<String> ret;
  382. int from=0;
  383. int len = length();
  384. while(true) {
  385. int end=find(p_splitter,from);
  386. if (end<0)
  387. end=len;
  388. if (p_allow_empty || (end>from))
  389. ret.push_back(substr(from,end-from));
  390. if (end==len)
  391. break;
  392. from = end+p_splitter.length();
  393. }
  394. return ret;
  395. }
  396. Vector<float> String::split_floats(const String &p_splitter,bool p_allow_empty) const {
  397. Vector<float> ret;
  398. int from=0;
  399. int len = length();
  400. while(true) {
  401. int end=find(p_splitter,from);
  402. if (end<0)
  403. end=len;
  404. if (p_allow_empty || (end>from))
  405. ret.push_back(String::to_double(&c_str()[from],end-from));
  406. if (end==len)
  407. break;
  408. from = end+p_splitter.length();
  409. }
  410. return ret;
  411. }
  412. Vector<float> String::split_floats_mk(const Vector<String> &p_splitters,bool p_allow_empty) const {
  413. Vector<float> ret;
  414. int from=0;
  415. int len = length();
  416. while(true) {
  417. int idx;
  418. int end=findmk(p_splitters,from,&idx);
  419. int spl_len=1;
  420. if (end<0) {
  421. end=len;
  422. } else {
  423. spl_len=p_splitters[idx].length();
  424. }
  425. if (p_allow_empty || (end>from))
  426. ret.push_back(String::to_double(&c_str()[from],end-from));
  427. if (end==len)
  428. break;
  429. from = end+spl_len;
  430. }
  431. return ret;
  432. }
  433. Vector<int> String::split_ints(const String &p_splitter,bool p_allow_empty) const {
  434. Vector<int> ret;
  435. int from=0;
  436. int len = length();
  437. while(true) {
  438. int end=find(p_splitter,from);
  439. if (end<0)
  440. end=len;
  441. if (p_allow_empty || (end>from))
  442. ret.push_back(String::to_int(&c_str()[from],end-from));
  443. if (end==len)
  444. break;
  445. from = end+p_splitter.length();
  446. }
  447. return ret;
  448. }
  449. Vector<int> String::split_ints_mk(const Vector<String> &p_splitters,bool p_allow_empty) const {
  450. Vector<int> ret;
  451. int from=0;
  452. int len = length();
  453. while(true) {
  454. int idx;
  455. int end=findmk(p_splitters,from,&idx);
  456. int spl_len=1;
  457. if (end<0) {
  458. end=len;
  459. } else {
  460. spl_len=p_splitters[idx].length();
  461. }
  462. if (p_allow_empty || (end>from))
  463. ret.push_back(String::to_int(&c_str()[from],end-from));
  464. if (end==len)
  465. break;
  466. from = end+spl_len;
  467. }
  468. return ret;
  469. }
  470. CharType String::char_uppercase(CharType p_char) {
  471. return _find_upper(p_char);
  472. }
  473. CharType String::char_lowercase(CharType p_char){
  474. return _find_lower(p_char);
  475. }
  476. String String::to_upper() const {
  477. String upper=*this;
  478. for(int i=0;i<upper.size();i++) {
  479. upper[i]=_find_upper(upper[i]);
  480. }
  481. return upper;
  482. }
  483. String String::to_lower() const {
  484. String upper=*this;
  485. for(int i=0;i<upper.size();i++) {
  486. upper[i]=_find_lower(upper[i]);
  487. }
  488. return upper;
  489. }
  490. int String::length() const {
  491. int s=size();
  492. return s?(s-1):0; // length does not include zero
  493. }
  494. const CharType * String::c_str() const {
  495. static const CharType zero=0;
  496. return size()?&operator[](0):&zero;
  497. }
  498. String String::md5(const uint8_t *p_md5) {
  499. String ret;
  500. for(int i=0;i<16;i++) {
  501. static const char hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  502. char v[2]={0,0};
  503. v[0]=hex[p_md5[i]>>4];
  504. ret+=v;
  505. v[0]=hex[p_md5[i]&0xF];
  506. ret+=v;
  507. }
  508. return ret;
  509. }
  510. String String::chr(CharType p_char) {
  511. CharType c[2]={p_char,0};
  512. return String(c);
  513. }
  514. String String::num(double p_num,int p_decimals) {
  515. #ifndef NO_USE_STDLIB
  516. if (p_decimals>12)
  517. p_decimals=12;
  518. char fmt[7];
  519. fmt[0]='%';
  520. fmt[1]='.';
  521. if (p_decimals<0) {
  522. fmt[1]='l';
  523. fmt[2]='f';
  524. fmt[3]=0;
  525. } else if(p_decimals<10) {
  526. fmt[2]='0'+p_decimals;
  527. fmt[3]='l';
  528. fmt[4]='f';
  529. fmt[5]=0;
  530. } else {
  531. fmt[2]='0'+(p_decimals/10);
  532. fmt[3]='0'+(p_decimals%10);
  533. fmt[4]='l';
  534. fmt[5]='f';
  535. fmt[6]=0;
  536. }
  537. char buf[256];
  538. #if defined(__GNUC__)
  539. #ifdef MINGW_ENABLED
  540. //snprintf is inexplicably broken in mingw
  541. //sprintf(buf,fmt,p_num);
  542. _snprintf(buf,256,fmt,p_num);
  543. #else
  544. snprintf(buf,256,fmt,p_num);
  545. #endif
  546. #elif defined(_MSC_VER)
  547. _snprintf(buf,256,fmt,p_num);
  548. #else
  549. sprintf(buf,fmt,p_num);
  550. #endif
  551. buf[255]=0;
  552. //destroy trailing zeroes
  553. {
  554. bool period=false;
  555. int z=0;
  556. while(buf[z]) {
  557. if (buf[z]=='.')
  558. period=true;
  559. z++;
  560. }
  561. if (period) {
  562. z--;
  563. while(z>0) {
  564. if (buf[z]=='0') {
  565. buf[z]=0;
  566. } else if (buf[z]=='.') {
  567. buf[z]=0;
  568. break;
  569. } else {
  570. break;
  571. }
  572. z--;
  573. }
  574. }
  575. }
  576. return buf;
  577. #else
  578. String s;
  579. String sd;
  580. /* integer part */
  581. bool neg=p_num<0;
  582. p_num=ABS(p_num);
  583. int intn=(int)p_num;
  584. /* decimal part */
  585. if (p_decimals>0 || (p_decimals==-1 && (int)p_num!=p_num)) {
  586. double dec=p_num-(float)((int)p_num);
  587. int digit=0;
  588. if (p_decimals>MAX_DIGITS)
  589. p_decimals=MAX_DIGITS;
  590. int dec_int=0;
  591. int dec_max=0;
  592. while (true) {
  593. dec*=10.0;
  594. dec_int=dec_int*10+(int)dec%10;
  595. dec_max=dec_max*10+9;
  596. digit++;
  597. if (p_decimals==-1) {
  598. if (digit==MAX_DIGITS) //no point in going to infinite
  599. break;
  600. if ((dec-(float)((int)dec))<1e-6)
  601. break;
  602. }
  603. if (digit==p_decimals)
  604. break;
  605. }
  606. dec*=10;
  607. int last=(int)dec%10;
  608. if (last>5) {
  609. if (dec_int==dec_max) {
  610. dec_int=0;
  611. intn++;
  612. } else {
  613. dec_int++;
  614. }
  615. }
  616. String decimal;
  617. for (int i=0;i<digit;i++) {
  618. char num[2]={0,0};
  619. num[0]='0'+dec_int%10;
  620. decimal=num+decimal;
  621. dec_int/=10;
  622. }
  623. sd='.'+decimal;
  624. }
  625. if (intn==0)
  626. s="0";
  627. else {
  628. while(intn) {
  629. CharType num='0'+(intn%10);
  630. intn/=10;
  631. s=num+s;
  632. }
  633. }
  634. s=s+sd;
  635. if (neg)
  636. s="-"+s;
  637. return s;
  638. #endif
  639. }
  640. String String::num_int64(int64_t p_num) {
  641. bool sign=p_num<0;
  642. int64_t num=ABS(p_num);
  643. int64_t n=num;
  644. int chars=0;
  645. do {
  646. n/=10;
  647. chars++;
  648. } while(n);
  649. if (sign)
  650. chars++;
  651. String s;
  652. s.resize(chars+1);
  653. CharType *c=s.ptr();
  654. c[chars]=0;
  655. n=num;
  656. do {
  657. c[--chars]='0'+(n%10);
  658. n/=10;
  659. } while(n);
  660. if (sign)
  661. c[0]='-';
  662. return s;
  663. }
  664. String String::num_real(double p_num) {
  665. String s;
  666. String sd;
  667. /* integer part */
  668. bool neg=p_num<0;
  669. p_num=ABS(p_num);
  670. int intn=(int)p_num;
  671. /* decimal part */
  672. if ((int)p_num!=p_num) {
  673. double dec=p_num-(float)((int)p_num);
  674. int digit=0;
  675. int decimals=MAX_DIGITS;
  676. int dec_int=0;
  677. int dec_max=0;
  678. while (true) {
  679. dec*=10.0;
  680. dec_int=dec_int*10+(int)dec%10;
  681. dec_max=dec_max*10+9;
  682. digit++;
  683. if ((dec-(float)((int)dec))<1e-6)
  684. break;
  685. if (digit==decimals)
  686. break;
  687. }
  688. dec*=10;
  689. int last=(int)dec%10;
  690. if (last>5) {
  691. if (dec_int==dec_max) {
  692. dec_int=0;
  693. intn++;
  694. } else {
  695. dec_int++;
  696. }
  697. }
  698. String decimal;
  699. for (int i=0;i<digit;i++) {
  700. char num[2]={0,0};
  701. num[0]='0'+dec_int%10;
  702. decimal=num+decimal;
  703. dec_int/=10;
  704. }
  705. sd='.'+decimal;
  706. } else {
  707. sd=".0";
  708. }
  709. if (intn==0)
  710. s="0";
  711. else {
  712. while(intn) {
  713. CharType num='0'+(intn%10);
  714. intn/=10;
  715. s=num+s;
  716. }
  717. }
  718. s=s+sd;
  719. if (neg)
  720. s="-"+s;
  721. return s;
  722. }
  723. String String::num_scientific(double p_num) {
  724. #ifndef NO_USE_STDLIB
  725. char buf[256];
  726. #if defined(_MSC_VER) || defined(MINGW_ENABLED)
  727. _snprintf(buf,256,"%lg",p_num);
  728. #elif defined(__GNUC__)
  729. snprintf(buf,256,"%lg",p_num);
  730. #else
  731. sprintf(buf,"%.16lg",p_num);
  732. #endif
  733. buf[255]=0;
  734. return buf;
  735. #else
  736. return String::num(p_num);
  737. #endif
  738. }
  739. CharString String::ascii(bool p_allow_extended) const {
  740. if (!length())
  741. return CharString();
  742. CharString cs;
  743. cs.resize(size());
  744. for (int i=0;i<size();i++)
  745. cs[i]=operator[](i);
  746. return cs;
  747. }
  748. String String::utf8(const char* p_utf8,int p_len) {
  749. String ret;
  750. ret.parse_utf8(p_utf8,p_len);
  751. return ret;
  752. };
  753. #if 0
  754. _FORCE_INLINE static int parse_utf8_char(const char *p_utf8,unsigned int *p_ucs4,int p_left) { //return len
  755. int len=0;
  756. /* Determine the number of characters in sequence */
  757. if ((*p_utf8 & 0x80)==0)
  758. len=1;
  759. else if ((*p_utf8 & 0xE0)==0xC0)
  760. len=2;
  761. else if ((*p_utf8 & 0xF0)==0xE0)
  762. len=3;
  763. else if ((*p_utf8 & 0xF8)==0xF0)
  764. len=4;
  765. else if ((*p_utf8 & 0xFC)==0xF8)
  766. len=5;
  767. else if ((*p_utf8 & 0xFE)==0xFC)
  768. len=6;
  769. else
  770. return -1; //invalid UTF8
  771. if (len>p_left)
  772. return -1; //not enough space
  773. if (len==2 && (*p_utf8&0x1E)==0) {
  774. //printf("overlong rejected\n");
  775. return -1; //reject overlong
  776. }
  777. /* Convert the first character */
  778. unsigned int unichar=0;
  779. if (len == 1)
  780. unichar=*p_utf8;
  781. else {
  782. unichar=(0xFF >> (len +1)) & *p_utf8;;
  783. for (int i=1;i<len;i++) {
  784. if ((p_utf8[i] & 0xC0) != 0x80) {
  785. //printf("invalid utf8\n");
  786. return -1; //invalid utf8
  787. }
  788. if (unichar==0 && i==2 && ((p_utf8[i] & 0x7F) >> (7 - len)) == 0) {
  789. //printf("no overlong\n");
  790. return -1; //no overlong
  791. }
  792. unichar = (unichar << 6) | (p_utf8[i] & 0x3F);
  793. }
  794. }
  795. *p_ucs4=unichar;
  796. return len;
  797. }
  798. #endif
  799. bool String::parse_utf8(const char* p_utf8,int p_len) {
  800. #define _UNICERROR(m_err) print_line("unicode error: "+String(m_err));
  801. String aux;
  802. int cstr_size=0;
  803. int str_size=0;
  804. /* HANDLE BOM (Byte Order Mark) */
  805. if (p_len<0 || p_len>=3) {
  806. bool has_bom = uint8_t(p_utf8[0])==0xEF && uint8_t(p_utf8[1])==0xBB && uint8_t(p_utf8[2])==0xBF;
  807. if (has_bom) {
  808. //just skip it
  809. if (p_len>=0)
  810. p_len-=3;
  811. p_utf8+=3;
  812. }
  813. }
  814. {
  815. const char *ptrtmp=p_utf8;
  816. const char *ptrtmp_limit=&p_utf8[p_len];
  817. int skip=0;
  818. while (ptrtmp!=ptrtmp_limit && *ptrtmp) {
  819. if (skip==0) {
  820. uint8_t c = *ptrtmp;
  821. /* Determine the number of characters in sequence */
  822. if ((c & 0x80)==0)
  823. skip=0;
  824. else if ((c & 0xE0)==0xC0)
  825. skip=1;
  826. else if ((c & 0xF0)==0xE0)
  827. skip=2;
  828. else if ((c & 0xF8)==0xF0)
  829. skip=3;
  830. else if ((c & 0xFC)==0xF8)
  831. skip=4;
  832. else if ((c & 0xFE)==0xFC)
  833. skip=5;
  834. else {
  835. _UNICERROR("invalid skip");
  836. return true; //invalid utf8
  837. }
  838. if (skip==1 && (c&0x1E)==0) {
  839. //printf("overlong rejected\n");
  840. _UNICERROR("overlong rejected");
  841. return true; //reject overlong
  842. }
  843. str_size++;
  844. } else {
  845. --skip;
  846. }
  847. cstr_size++;
  848. ptrtmp++;
  849. }
  850. if (skip) {
  851. _UNICERROR("no space left");
  852. return true; //not enough spac
  853. }
  854. }
  855. if (str_size==0) {
  856. clear();
  857. return false;
  858. }
  859. resize(str_size+1);
  860. CharType *dst = &operator[](0);
  861. dst[str_size]=0;
  862. while (cstr_size) {
  863. int len=0;
  864. /* Determine the number of characters in sequence */
  865. if ((*p_utf8 & 0x80)==0)
  866. len=1;
  867. else if ((*p_utf8 & 0xE0)==0xC0)
  868. len=2;
  869. else if ((*p_utf8 & 0xF0)==0xE0)
  870. len=3;
  871. else if ((*p_utf8 & 0xF8)==0xF0)
  872. len=4;
  873. else if ((*p_utf8 & 0xFC)==0xF8)
  874. len=5;
  875. else if ((*p_utf8 & 0xFE)==0xFC)
  876. len=6;
  877. else {
  878. _UNICERROR("invalid len");
  879. return true; //invalid UTF8
  880. }
  881. if (len>cstr_size) {
  882. _UNICERROR("no space left");
  883. return true; //not enough space
  884. }
  885. if (len==2 && (*p_utf8&0x1E)==0) {
  886. //printf("overlong rejected\n");
  887. _UNICERROR("no space left");
  888. return true; //reject overlong
  889. }
  890. /* Convert the first character */
  891. uint32_t unichar=0;
  892. if (len == 1)
  893. unichar=*p_utf8;
  894. else {
  895. unichar=(0xFF >> (len +1)) & *p_utf8;;
  896. for (int i=1;i<len;i++) {
  897. if ((p_utf8[i] & 0xC0) != 0x80) {
  898. _UNICERROR("invalid utf8");
  899. return true; //invalid utf8
  900. }
  901. if (unichar==0 && i==2 && ((p_utf8[i] & 0x7F) >> (7 - len)) == 0) {
  902. _UNICERROR("invalid utf8 overlong");
  903. return true; //no overlong
  904. }
  905. unichar = (unichar << 6) | (p_utf8[i] & 0x3F);
  906. }
  907. }
  908. // printf("char %i, len %i\n",unichar,len);
  909. if (sizeof(wchar_t)==2 && unichar>0xFFFF) {
  910. unichar=' '; //too long for windows
  911. }
  912. *(dst++) = unichar;
  913. cstr_size-=len;
  914. p_utf8 += len;
  915. }
  916. return false;
  917. }
  918. CharString String::utf8() const {
  919. int l = length();
  920. if (!l)
  921. return CharString();
  922. const CharType *d=&operator[](0);
  923. int fl=0;
  924. for (int i=0;i<l;i++) {
  925. uint32_t c=d[i];
  926. if (c <= 0x7f) // 7 bits.
  927. fl+=1;
  928. else if (c <= 0x7ff) { // 11 bits
  929. fl+=2;
  930. } else if (c <= 0xffff) { // 16 bits
  931. fl+=3;
  932. } else if (c <= 0x001fffff) { // 21 bits
  933. fl+=4;
  934. } else if (c <= 0x03ffffff) { // 26 bits
  935. fl+=5;
  936. } else if (c <= 0x7fffffff) { // 31 bits
  937. fl+=6;
  938. }
  939. }
  940. CharString utf8s;
  941. if (fl==0) {
  942. return utf8s;
  943. }
  944. utf8s.resize(fl+1);
  945. uint8_t *cdst=(uint8_t*)utf8s.get_data();
  946. #define APPEND_CHAR( m_c ) *(cdst++) = m_c
  947. for (int i=0;i<l;i++) {
  948. uint32_t c=d[i];
  949. if (c <= 0x7f) // 7 bits.
  950. APPEND_CHAR( c );
  951. else if (c <= 0x7ff) { // 11 bits
  952. APPEND_CHAR( uint32_t(0xc0 | ((c >> 6) & 0x1f)) ); // Top 5 bits.
  953. APPEND_CHAR( uint32_t(0x80 | (c & 0x3f)) ); // Bottom 6 bits.
  954. } else if (c <= 0xffff) { // 16 bits
  955. APPEND_CHAR( uint32_t(0xe0 | ((c >> 12) & 0x0f)) ); // Top 4 bits.
  956. APPEND_CHAR( uint32_t(0x80 | ((c >> 6) & 0x3f)) ); // Middle 6 bits.
  957. APPEND_CHAR( uint32_t(0x80 | (c & 0x3f)) ); // Bottom 6 bits.
  958. } else if (c <= 0x001fffff) { // 21 bits
  959. APPEND_CHAR( uint32_t(0xf0 | ((c >> 18) & 0x07)) ); // Top 3 bits.
  960. APPEND_CHAR( uint32_t(0x80 | ((c >> 12) & 0x3f)) ); // Upper middle 6 bits.
  961. APPEND_CHAR( uint32_t(0x80 | ((c >> 6) & 0x3f)) ); // Lower middle 6 bits.
  962. APPEND_CHAR( uint32_t(0x80 | (c & 0x3f)) ); // Bottom 6 bits.
  963. } else if (c <= 0x03ffffff) { // 26 bits
  964. APPEND_CHAR( uint32_t(0xf8 | ((c >> 24) & 0x03)) ); // Top 2 bits.
  965. APPEND_CHAR( uint32_t(0x80 | ((c >> 18) & 0x3f)) ); // Upper middle 6 bits.
  966. APPEND_CHAR( uint32_t(0x80 | ((c >> 12) & 0x3f)) ); // middle 6 bits.
  967. APPEND_CHAR( uint32_t(0x80 | ((c >> 6) & 0x3f)) ); // Lower middle 6 bits.
  968. APPEND_CHAR( uint32_t(0x80 | (c & 0x3f)) ); // Bottom 6 bits.
  969. } else if (c <= 0x7fffffff) { // 31 bits
  970. APPEND_CHAR( uint32_t(0xfc | ((c >> 30) & 0x01)) ); // Top 1 bit.
  971. APPEND_CHAR( uint32_t(0x80 | ((c >> 24) & 0x3f)) ); // Upper upper middle 6 bits.
  972. APPEND_CHAR( uint32_t(0x80 | ((c >> 18) & 0x3f)) ); // Lower upper middle 6 bits.
  973. APPEND_CHAR( uint32_t(0x80 | ((c >> 12) & 0x3f)) ); // Upper lower middle 6 bits.
  974. APPEND_CHAR( uint32_t(0x80 | ((c >> 6) & 0x3f)) ); // Lower lower middle 6 bits.
  975. APPEND_CHAR( uint32_t(0x80 | (c & 0x3f)) ); // Bottom 6 bits.
  976. }
  977. }
  978. #undef APPEND_CHAR
  979. *cdst=0; //trailing zero
  980. return utf8s;
  981. }
  982. /*
  983. String::String(CharType p_char) {
  984. shared=NULL;
  985. copy_from(p_char);
  986. }
  987. */
  988. String::String(const char *p_str) {
  989. copy_from(p_str);
  990. }
  991. String::String(const CharType *p_str,int p_clip_to_len) {
  992. copy_from(p_str,p_clip_to_len);
  993. }
  994. String::String(const StrRange& p_range) {
  995. if (!p_range.c_str)
  996. return;
  997. copy_from(p_range.c_str,p_range.len);
  998. }
  999. int String::hex_to_int() const {
  1000. int l = length();
  1001. if (l<3)
  1002. return 0;
  1003. const CharType *s=ptr();
  1004. int sign = s[0]=='-' ? -1 : 1;
  1005. if (sign<0) {
  1006. s++;
  1007. l--;
  1008. if (l<2)
  1009. return 0;
  1010. }
  1011. if (s[0]!='0' || s[1]!='x')
  1012. return 0;
  1013. s+=2;
  1014. l-=2;
  1015. int hex=0;
  1016. while(*s) {
  1017. CharType c = LOWERCASE(*s);
  1018. int n;
  1019. if (c>='0' && c<='9') {
  1020. n=c-'0';
  1021. } else if (c>='a' && c<='f') {
  1022. n=(c-'a')+10;
  1023. } else {
  1024. return 0;
  1025. }
  1026. hex*=16;
  1027. hex+=n;
  1028. s++;
  1029. }
  1030. return hex*sign;
  1031. }
  1032. int String::to_int() const {
  1033. if (length()==0)
  1034. return 0;
  1035. int to=(find(".")>=0) ? find(".") : length() ;
  1036. int integer=0;
  1037. int sign=1;
  1038. for (int i=0;i<to;i++) {
  1039. CharType c=operator[](i);
  1040. if (c>='0' && c<='9') {
  1041. integer*=10;
  1042. integer+=c-'0';
  1043. } else if (integer==0 && c=='-') {
  1044. sign=-sign;
  1045. }
  1046. }
  1047. return integer*sign;
  1048. }
  1049. int64_t String::to_int64() const {
  1050. if (length()==0)
  1051. return 0;
  1052. int to=(find(".")>=0) ? find(".") : length() ;
  1053. int64_t integer=0;
  1054. int64_t sign=1;
  1055. for (int i=0;i<to;i++) {
  1056. CharType c=operator[](i);
  1057. if (c>='0' && c<='9') {
  1058. integer*=10;
  1059. integer+=c-'0';
  1060. } else if (integer==0 && c=='-') {
  1061. sign=-sign;
  1062. }
  1063. }
  1064. return integer*sign;
  1065. }
  1066. int String::to_int(const char* p_str) {
  1067. int to=0;
  1068. while(p_str[to]!=0 && p_str[to]!='.')
  1069. to++;
  1070. int integer=0;
  1071. int sign=1;
  1072. for (int i=0;i<to;i++) {
  1073. char c=p_str[i];
  1074. if (c>='0' && c<='9') {
  1075. integer*=10;
  1076. integer+=c-'0';
  1077. } else if (c=='-' && integer==0) {
  1078. sign=-sign;
  1079. } else if (c!=' ')
  1080. break;
  1081. }
  1082. return integer*sign;
  1083. }
  1084. bool String::is_numeric() const {
  1085. if (length() == 0) {
  1086. return false;
  1087. };
  1088. int s = 0;
  1089. if (operator[](0) == '-') ++s;
  1090. bool dot = false;
  1091. for (int i=s; i<length(); i++) {
  1092. CharType c = operator[](i);
  1093. if (c == '.') {
  1094. if (dot) {
  1095. return false;
  1096. };
  1097. dot = true;
  1098. }
  1099. if (c < '0' || c > '9') {
  1100. return false;
  1101. };
  1102. };
  1103. return true; // TODO: Use the parser below for this instead
  1104. };
  1105. #define IS_DIGIT(m_d) ( (m_d)>='0' && (m_d)<='9' )
  1106. template<class C>
  1107. static double built_in_strtod(const C *string, /* A decimal ASCII floating-point number,
  1108. * optionally preceded by white space. Must
  1109. * have form "-I.FE-X", where I is the integer
  1110. * part of the mantissa, F is the fractional
  1111. * part of the mantissa, and X is the
  1112. * exponent. Either of the signs may be "+",
  1113. * "-", or omitted. Either I or F may be
  1114. * omitted, or both. The decimal point isn't
  1115. * necessary unless F is present. The "E" may
  1116. * actually be an "e". E and X may both be
  1117. * omitted (but not just one). */
  1118. C **endPtr=NULL) /* If non-NULL, store terminating Cacter's
  1119. * address here. */
  1120. {
  1121. static const int maxExponent = 511; /* Largest possible base 10 exponent. Any
  1122. * exponent larger than this will already
  1123. * produce underflow or overflow, so there's
  1124. * no need to worry about additional digits.
  1125. */
  1126. static const double powersOf10[] = { /* Table giving binary powers of 10. Entry */
  1127. 10., /* is 10^2^i. Used to convert decimal */
  1128. 100., /* exponents into floating-point numbers. */
  1129. 1.0e4,
  1130. 1.0e8,
  1131. 1.0e16,
  1132. 1.0e32,
  1133. 1.0e64,
  1134. 1.0e128,
  1135. 1.0e256
  1136. };
  1137. int sign, expSign = false;
  1138. double fraction, dblExp;
  1139. const double *d;
  1140. register const C *p;
  1141. register int c;
  1142. int exp = 0; /* Exponent read from "EX" field. */
  1143. int fracExp = 0; /* Exponent that derives from the fractional
  1144. * part. Under normal circumstatnces, it is
  1145. * the negative of the number of digits in F.
  1146. * However, if I is very long, the last digits
  1147. * of I get dropped (otherwise a long I with a
  1148. * large negative exponent could cause an
  1149. * unnecessary overflow on I alone). In this
  1150. * case, fracExp is incremented one for each
  1151. * dropped digit. */
  1152. int mantSize; /* Number of digits in mantissa. */
  1153. int decPt; /* Number of mantissa digits BEFORE decimal
  1154. * point. */
  1155. const C *pExp; /* Temporarily holds location of exponent in
  1156. * string. */
  1157. /*
  1158. * Strip off leading blanks and check for a sign.
  1159. */
  1160. p = string;
  1161. while (*p == ' ' || *p=='\t' || *p=='\n') {
  1162. p += 1;
  1163. }
  1164. if (*p == '-') {
  1165. sign = true;
  1166. p += 1;
  1167. } else {
  1168. if (*p == '+') {
  1169. p += 1;
  1170. }
  1171. sign = false;
  1172. }
  1173. /*
  1174. * Count the number of digits in the mantissa (including the decimal
  1175. * point), and also locate the decimal point.
  1176. */
  1177. decPt = -1;
  1178. for (mantSize = 0; ; mantSize += 1)
  1179. {
  1180. c = *p;
  1181. if (!IS_DIGIT(c)) {
  1182. if ((c != '.') || (decPt >= 0)) {
  1183. break;
  1184. }
  1185. decPt = mantSize;
  1186. }
  1187. p += 1;
  1188. }
  1189. /*
  1190. * Now suck up the digits in the mantissa. Use two integers to collect 9
  1191. * digits each (this is faster than using floating-point). If the mantissa
  1192. * has more than 18 digits, ignore the extras, since they can't affect the
  1193. * value anyway.
  1194. */
  1195. pExp = p;
  1196. p -= mantSize;
  1197. if (decPt < 0) {
  1198. decPt = mantSize;
  1199. } else {
  1200. mantSize -= 1; /* One of the digits was the point. */
  1201. }
  1202. if (mantSize > 18) {
  1203. fracExp = decPt - 18;
  1204. mantSize = 18;
  1205. } else {
  1206. fracExp = decPt - mantSize;
  1207. }
  1208. if (mantSize == 0) {
  1209. fraction = 0.0;
  1210. p = string;
  1211. goto done;
  1212. } else {
  1213. int frac1, frac2;
  1214. frac1 = 0;
  1215. for ( ; mantSize > 9; mantSize -= 1) {
  1216. c = *p;
  1217. p += 1;
  1218. if (c == '.') {
  1219. c = *p;
  1220. p += 1;
  1221. }
  1222. frac1 = 10*frac1 + (c - '0');
  1223. }
  1224. frac2 = 0;
  1225. for (; mantSize > 0; mantSize -= 1) {
  1226. c = *p;
  1227. p += 1;
  1228. if (c == '.') {
  1229. c = *p;
  1230. p += 1;
  1231. }
  1232. frac2 = 10*frac2 + (c - '0');
  1233. }
  1234. fraction = (1.0e9 * frac1) + frac2;
  1235. }
  1236. /*
  1237. * Skim off the exponent.
  1238. */
  1239. p = pExp;
  1240. if ((*p == 'E') || (*p == 'e')) {
  1241. p += 1;
  1242. if (*p == '-') {
  1243. expSign = true;
  1244. p += 1;
  1245. } else {
  1246. if (*p == '+') {
  1247. p += 1;
  1248. }
  1249. expSign = false;
  1250. }
  1251. if (!IS_DIGIT(CharType(*p))) {
  1252. p = pExp;
  1253. goto done;
  1254. }
  1255. while (IS_DIGIT(CharType(*p))) {
  1256. exp = exp * 10 + (*p - '0');
  1257. p += 1;
  1258. }
  1259. }
  1260. if (expSign) {
  1261. exp = fracExp - exp;
  1262. } else {
  1263. exp = fracExp + exp;
  1264. }
  1265. /*
  1266. * Generate a floating-point number that represents the exponent. Do this
  1267. * by processing the exponent one bit at a time to combine many powers of
  1268. * 2 of 10. Then combine the exponent with the fraction.
  1269. */
  1270. if (exp < 0) {
  1271. expSign = true;
  1272. exp = -exp;
  1273. } else {
  1274. expSign = false;
  1275. }
  1276. if (exp > maxExponent) {
  1277. exp = maxExponent;
  1278. WARN_PRINT("Exponent too high");
  1279. }
  1280. dblExp = 1.0;
  1281. for (d = powersOf10; exp != 0; exp >>= 1, ++d) {
  1282. if (exp & 01) {
  1283. dblExp *= *d;
  1284. }
  1285. }
  1286. if (expSign) {
  1287. fraction /= dblExp;
  1288. } else {
  1289. fraction *= dblExp;
  1290. }
  1291. done:
  1292. if (endPtr != NULL) {
  1293. *endPtr = (C *) p;
  1294. }
  1295. if (sign) {
  1296. return -fraction;
  1297. }
  1298. return fraction;
  1299. }
  1300. #define READING_SIGN 0
  1301. #define READING_INT 1
  1302. #define READING_DEC 2
  1303. #define READING_EXP 3
  1304. #define READING_DONE 4
  1305. double String::to_double(const char* p_str) {
  1306. #ifndef NO_USE_STDLIB
  1307. return built_in_strtod<char>(p_str);
  1308. //return atof(p_str); DOES NOT WORK ON ANDROID(??)
  1309. #else
  1310. return built_in_strtod<char>(p_str);
  1311. #endif
  1312. #if 0
  1313. #if 0
  1314. return atof(p_str);
  1315. #else
  1316. if (!p_str[0])
  1317. return 0;
  1318. ///@todo make more exact so saving and loading does not lose precision
  1319. double integer=0;
  1320. double decimal=0;
  1321. double decimal_mult=0.1;
  1322. double sign=1.0;
  1323. double exp=0;
  1324. double exp_sign=1.0;
  1325. int reading=READING_SIGN;
  1326. const char *str=p_str;
  1327. while(*str && reading!=READING_DONE) {
  1328. CharType c=*(str++);
  1329. switch(reading) {
  1330. case READING_SIGN: {
  1331. if (c>='0' && c<='9')
  1332. reading=READING_INT;
  1333. // let it fallthrough
  1334. else if (c=='-') {
  1335. sign=-1.0;
  1336. reading=READING_INT;
  1337. break;
  1338. } else if (c=='.') {
  1339. reading=READING_DEC;
  1340. break;
  1341. } else {
  1342. break;
  1343. }
  1344. }
  1345. case READING_INT: {
  1346. if (c>='0' && c<='9') {
  1347. integer*=10;
  1348. integer+=c-'0';
  1349. } else if (c=='.') {
  1350. reading=READING_DEC;
  1351. } else if (c=='e') {
  1352. reading=READING_EXP;
  1353. } else {
  1354. reading=READING_DONE;
  1355. }
  1356. } break;
  1357. case READING_DEC: {
  1358. if (c>='0' && c<='9') {
  1359. decimal+=(c-'0')*decimal_mult;
  1360. decimal_mult*=0.1;
  1361. } else if (c=='e') {
  1362. reading=READING_EXP;
  1363. } else {
  1364. reading=READING_DONE;
  1365. }
  1366. } break;
  1367. case READING_EXP: {
  1368. if (c>='0' && c<='9') {
  1369. exp*=10.0;
  1370. exp+=(c-'0');
  1371. } else if (c=='-' && exp==0) {
  1372. exp_sign=-1.0;
  1373. } else if (exp_sign>=0 && c=='+') {
  1374. //redundant...
  1375. exp_sign=1.0;
  1376. } else {
  1377. reading=READING_DONE;
  1378. }
  1379. } break;
  1380. }
  1381. }
  1382. return sign*(integer+decimal)*Math::pow(10,exp_sign*exp);
  1383. #endif
  1384. #endif
  1385. }
  1386. float String::to_float() const {
  1387. return to_double();
  1388. }
  1389. double String::to_double(const CharType* p_str, int p_len, const CharType **r_end) {
  1390. #if 0
  1391. //ndef NO_USE_STDLIB
  1392. return wcstod(p_str,p_len<0?NULL:p_str+p_len);
  1393. #else
  1394. if (p_len==0 || !p_str[0])
  1395. return 0;
  1396. ///@todo make more exact so saving and loading does not lose precision
  1397. double integer=0;
  1398. double decimal=0;
  1399. double decimal_mult=0.1;
  1400. double sign=1.0;
  1401. double exp=0;
  1402. double exp_sign=1.0;
  1403. int reading=READING_SIGN;
  1404. const CharType *str=p_str;
  1405. const CharType *limit=&p_str[p_len];
  1406. while(reading!=READING_DONE && str!=limit) {
  1407. CharType c=*(str++);
  1408. switch(reading) {
  1409. case READING_SIGN: {
  1410. if (c>='0' && c<='9')
  1411. reading=READING_INT;
  1412. // let it fallthrough
  1413. else if (c=='-') {
  1414. sign=-1.0;
  1415. reading=READING_INT;
  1416. break;
  1417. } else if (c=='.') {
  1418. reading=READING_DEC;
  1419. break;
  1420. } else if (c==0) {
  1421. reading=READING_DONE;
  1422. break;
  1423. } else {
  1424. break;
  1425. }
  1426. }
  1427. case READING_INT: {
  1428. if (c>='0' && c<='9') {
  1429. integer*=10;
  1430. integer+=c-'0';
  1431. } else if (c=='.') {
  1432. reading=READING_DEC;
  1433. } else if (c=='e' || c=='E') {
  1434. reading=READING_EXP;
  1435. } else {
  1436. reading=READING_DONE;
  1437. }
  1438. } break;
  1439. case READING_DEC: {
  1440. if (c>='0' && c<='9') {
  1441. decimal+=(c-'0')*decimal_mult;
  1442. decimal_mult*=0.1;
  1443. } else if (c=='e' || c=='E') {
  1444. reading=READING_EXP;
  1445. } else {
  1446. reading=READING_DONE;
  1447. }
  1448. } break;
  1449. case READING_EXP: {
  1450. if (c>='0' && c<='9') {
  1451. exp*=10.0;
  1452. exp+=(c-'0');
  1453. } else if (c=='-' && exp==0) {
  1454. exp_sign=-1.0;
  1455. } else if (exp_sign>=0 && c=='+') {
  1456. //redundant...
  1457. exp_sign=1.0;
  1458. } else {
  1459. reading=READING_DONE;
  1460. }
  1461. } break;
  1462. }
  1463. }
  1464. if (r_end)
  1465. *r_end=str-1;
  1466. return sign*(integer+decimal)*Math::pow(10,exp_sign*exp);
  1467. #endif
  1468. }
  1469. int64_t String::to_int(const CharType* p_str,int p_len) {
  1470. if (p_len==0 || !p_str[0])
  1471. return 0;
  1472. ///@todo make more exact so saving and loading does not lose precision
  1473. int64_t integer=0;
  1474. int64_t sign=1;
  1475. int reading=READING_SIGN;
  1476. const CharType *str=p_str;
  1477. const CharType *limit=&p_str[p_len];
  1478. while(*str && reading!=READING_DONE && str!=limit) {
  1479. CharType c=*(str++);
  1480. switch(reading) {
  1481. case READING_SIGN: {
  1482. if (c>='0' && c<='9') {
  1483. reading=READING_INT;
  1484. // let it fallthrough
  1485. } else if (c=='-') {
  1486. sign=-1;
  1487. reading=READING_INT;
  1488. break;
  1489. } else if (c=='+') {
  1490. sign=1;
  1491. reading=READING_INT;
  1492. break;
  1493. } else {
  1494. break;
  1495. }
  1496. }
  1497. case READING_INT: {
  1498. if (c>='0' && c<='9') {
  1499. integer*=10;
  1500. integer+=c-'0';
  1501. } else {
  1502. reading=READING_DONE;
  1503. }
  1504. } break;
  1505. }
  1506. }
  1507. return sign*integer;
  1508. }
  1509. double String::to_double() const {
  1510. if (empty())
  1511. return 0;
  1512. #ifndef NO_USE_STDLIB
  1513. return built_in_strtod<CharType>(c_str());
  1514. //return wcstod(c_str(),NULL); DOES NOT WORK ON ANDROID :(
  1515. #else
  1516. return built_in_strtod<CharType>(c_str());
  1517. #endif
  1518. #if 0
  1519. #ifndef NO_USE_STDLIB
  1520. return atof(utf8().get_data());
  1521. #else
  1522. double integer=0;
  1523. double decimal=0;
  1524. double decimal_mult=0.1;
  1525. double sign=1.0;
  1526. double exp=0;
  1527. double exp_sign=1.0;
  1528. int reading=READING_SIGN;
  1529. const CharType *str=&operator[](0);
  1530. while(*str && reading!=READING_DONE) {
  1531. CharType c=*(str++);
  1532. switch(reading) {
  1533. case READING_SIGN: {
  1534. if (c>='0' && c<='9')
  1535. reading=READING_INT;
  1536. // let it fallthrough
  1537. else if (c=='-') {
  1538. sign=-1.0;
  1539. reading=READING_INT;
  1540. break;
  1541. } else if (c=='.') {
  1542. reading=READING_DEC;
  1543. break;
  1544. } else {
  1545. break;
  1546. }
  1547. }
  1548. case READING_INT: {
  1549. if (c>='0' && c<='9') {
  1550. integer*=10;
  1551. integer+=c-'0';
  1552. } else if (c=='.') {
  1553. reading=READING_DEC;
  1554. } else if (c=='e') {
  1555. reading=READING_EXP;
  1556. } else {
  1557. reading=READING_DONE;
  1558. }
  1559. } break;
  1560. case READING_DEC: {
  1561. if (c>='0' && c<='9') {
  1562. decimal+=(c-'0')*decimal_mult;
  1563. decimal_mult*=0.1;
  1564. } else if (c=='e') {
  1565. reading=READING_EXP;
  1566. } else {
  1567. reading=READING_DONE;
  1568. }
  1569. } break;
  1570. case READING_EXP: {
  1571. if (c>='0' && c<='9') {
  1572. exp*=10.0;
  1573. exp+=(c-'0');
  1574. } else if (c=='-' && exp==0) {
  1575. exp_sign=-1.0;
  1576. } else if (exp_sign>=0 && c=='+') {
  1577. //redundant...
  1578. exp_sign=1.0;
  1579. } else {
  1580. reading=READING_DONE;
  1581. }
  1582. } break;
  1583. }
  1584. }
  1585. return sign*(integer+decimal)*Math::pow(10,exp_sign*exp);
  1586. #endif
  1587. #if 0
  1588. double ret=sign*(integer+decimal)*Math::pow(10,exp_sign*exp);
  1589. print_line(*this +" == "+rtos(ret));
  1590. return ret;
  1591. #endif
  1592. #endif
  1593. }
  1594. bool operator==(const char*p_chr, const String& p_str) {
  1595. return p_str==p_chr;
  1596. }
  1597. String operator+(const char*p_chr, const String& p_str) {
  1598. String tmp=p_chr;
  1599. tmp+=p_str;
  1600. return tmp;
  1601. }
  1602. String operator+(CharType p_chr, const String& p_str) {
  1603. return (String::chr(p_chr)+p_str);
  1604. }
  1605. uint32_t String::hash(const char* p_cstr) {
  1606. uint32_t hashv = 5381;
  1607. uint32_t c;
  1608. while ((c = *p_cstr++))
  1609. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1610. return hashv;
  1611. }
  1612. uint32_t String::hash(const char* p_cstr,int p_len) {
  1613. uint32_t hashv = 5381;
  1614. for(int i=0;i<p_len;i++)
  1615. hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
  1616. return hashv;
  1617. }
  1618. uint32_t String::hash(const CharType* p_cstr,int p_len) {
  1619. uint32_t hashv = 5381;
  1620. for(int i=0;i<p_len;i++)
  1621. hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
  1622. return hashv;
  1623. }
  1624. uint32_t String::hash(const CharType* p_cstr) {
  1625. uint32_t hashv = 5381;
  1626. uint32_t c;
  1627. while ((c = *p_cstr++))
  1628. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1629. return hashv;
  1630. }
  1631. uint32_t String::hash() const {
  1632. /* simple djb2 hashing */
  1633. const CharType * chr = c_str();
  1634. uint32_t hashv = 5381;
  1635. uint32_t c;
  1636. while ((c = *chr++))
  1637. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1638. return hashv;
  1639. }
  1640. uint64_t String::hash64() const {
  1641. /* simple djb2 hashing */
  1642. const CharType * chr = c_str();
  1643. uint64_t hashv = 5381;
  1644. uint64_t c;
  1645. while ((c = *chr++))
  1646. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1647. return hashv;
  1648. }
  1649. String String::insert(int p_at_pos,String p_string) const {
  1650. if (p_at_pos<0)
  1651. return *this;
  1652. if (p_at_pos>length())
  1653. p_at_pos=length();
  1654. String pre;
  1655. if (p_at_pos>0)
  1656. pre=substr( 0, p_at_pos );
  1657. String post;
  1658. if (p_at_pos<length())
  1659. post=substr( p_at_pos, length()-p_at_pos);
  1660. return pre+p_string+post;
  1661. }
  1662. String String::substr(int p_from,int p_chars) const{
  1663. if (empty() || p_from<0 || p_from>=length() || p_chars<=0)
  1664. return "";
  1665. if ( (p_from+p_chars)>length()) {
  1666. p_chars=length()-p_from;
  1667. }
  1668. return String(&c_str()[p_from],p_chars);
  1669. }
  1670. int String::find_last(String p_str) const {
  1671. int pos=-1;
  1672. int findfrom=0;
  1673. int findres=-1;
  1674. while((findres=find(p_str,findfrom))!=-1) {
  1675. pos=findres;
  1676. findfrom=pos+1;
  1677. }
  1678. return pos;
  1679. }
  1680. int String::find(String p_str,int p_from) const {
  1681. if (p_from<0)
  1682. return -1;
  1683. int src_len=p_str.length();
  1684. int len=length();
  1685. if(src_len==0 || len==0)
  1686. return -1; //wont find anything!
  1687. const CharType *src = c_str();
  1688. for (int i=p_from;i<=(len-src_len);i++) {
  1689. bool found=true;
  1690. for (int j=0;j<src_len;j++) {
  1691. int read_pos=i+j;
  1692. if (read_pos>=len) {
  1693. ERR_PRINT("read_pos>=len");
  1694. return -1;
  1695. };
  1696. if (src[read_pos]!=p_str[j]) {
  1697. found=false;
  1698. break;
  1699. }
  1700. }
  1701. if (found)
  1702. return i;
  1703. }
  1704. return -1;
  1705. }
  1706. int String::findmk(const Vector<String>& p_keys,int p_from,int *r_key) const {
  1707. if (p_from<0)
  1708. return -1;
  1709. if (p_keys.size()==0)
  1710. return -1;
  1711. //int src_len=p_str.length();
  1712. const String *keys=&p_keys[0];
  1713. int key_count=p_keys.size();
  1714. int len = length();
  1715. if(len==0)
  1716. return -1; //wont find anything!
  1717. const CharType *src = c_str();
  1718. for (int i=p_from;i<len;i++) {
  1719. bool found=true;
  1720. for(int k=0;k<key_count;k++) {
  1721. found=true;
  1722. if (r_key)
  1723. *r_key=k;
  1724. const CharType *cmp=keys[k].c_str();
  1725. int l=keys[k].length();
  1726. for (int j=0;j<l;j++) {
  1727. int read_pos=i+j;
  1728. if (read_pos>=len) {
  1729. found=false;
  1730. break;
  1731. };
  1732. if (src[read_pos]!=cmp[j]) {
  1733. found=false;
  1734. break;
  1735. }
  1736. }
  1737. if (found)
  1738. break;
  1739. }
  1740. if (found)
  1741. return i;
  1742. }
  1743. return -1;
  1744. }
  1745. int String::findn(String p_str,int p_from) const {
  1746. if (p_from<0)
  1747. return -1;
  1748. int src_len=p_str.length();
  1749. if(src_len==0 || length()==0)
  1750. return -1; //wont find anything!
  1751. const CharType *srcd = c_str();
  1752. for (int i=p_from;i<=(length()-src_len);i++) {
  1753. bool found=true;
  1754. for (int j=0;j<src_len;j++) {
  1755. int read_pos=i+j;
  1756. if (read_pos>=length()) {
  1757. ERR_PRINT("read_pos>=length()");
  1758. return -1;
  1759. };
  1760. CharType src=srcd[read_pos];
  1761. CharType dst=p_str[j];
  1762. if (src>='a' && src<='z')
  1763. src-='a'-'A';
  1764. if (dst>='a' && dst<='z')
  1765. dst-='a'-'A';
  1766. if (src!=dst) {
  1767. found=false;
  1768. break;
  1769. }
  1770. }
  1771. if (found)
  1772. return i;
  1773. }
  1774. return -1;
  1775. }
  1776. int String::rfind(String p_str,int p_from) const {
  1777. return -1;
  1778. }
  1779. int String::rfindn(String p_str,int p_from) const {
  1780. return -1;
  1781. }
  1782. bool String::ends_with(const String& p_string) const {
  1783. int pos = find_last(p_string);
  1784. if (pos==-1)
  1785. return false;
  1786. return pos+p_string.length()==length();
  1787. }
  1788. bool String::begins_with(const String& p_string) const {
  1789. if (p_string.length() > length())
  1790. return false;
  1791. int l=p_string.length();
  1792. if (l==0)
  1793. return true;
  1794. const CharType *src=&p_string[0];
  1795. const CharType *str=&operator[](0);
  1796. for (int i=0;i<l;i++) {
  1797. if (src[i]!=str[i])
  1798. return false;
  1799. }
  1800. return true;
  1801. }
  1802. bool String::begins_with(const char* p_string) const {
  1803. int l=length();
  1804. if (l==0)
  1805. return false;
  1806. const CharType *str=&operator[](0);
  1807. int i=0;
  1808. while (*p_string && i<l) {
  1809. if (*p_string != str[i])
  1810. return false;
  1811. i++;
  1812. p_string++;
  1813. }
  1814. return true;
  1815. }
  1816. static bool _wildcard_match(const CharType* p_pattern, const CharType* p_string,bool p_case_sensitive) {
  1817. switch (*p_pattern) {
  1818. case '\0':
  1819. return !*p_string;
  1820. case '*' :
  1821. return _wildcard_match(p_pattern+1, p_string,p_case_sensitive) || (*p_string && _wildcard_match(p_pattern, p_string+1,p_case_sensitive));
  1822. case '?' :
  1823. return *p_string && (*p_string != '.') && _wildcard_match(p_pattern+1, p_string+1,p_case_sensitive);
  1824. default :
  1825. 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);
  1826. }
  1827. }
  1828. bool String::match(const String& p_wildcard) const {
  1829. if (!p_wildcard.length() || !length())
  1830. return false;
  1831. return _wildcard_match(p_wildcard.c_str(),c_str(),true);
  1832. }
  1833. bool String::matchn(const String& p_wildcard) const {
  1834. if (!p_wildcard.length() || !length())
  1835. return false;
  1836. return _wildcard_match(p_wildcard.c_str(),c_str(),false);
  1837. }
  1838. String String::replace(String p_key,String p_with) const {
  1839. String new_string;
  1840. int search_from=0;
  1841. int result=0;
  1842. while( (result=find(p_key,search_from))>=0 ) {
  1843. new_string+=substr(search_from,result-search_from);
  1844. new_string+=p_with;
  1845. search_from=result+p_key.length();
  1846. }
  1847. new_string+=substr(search_from,length()-search_from);
  1848. return new_string;
  1849. }
  1850. String String::replace_first(String p_key,String p_with) const {
  1851. String new_string;
  1852. int search_from=0;
  1853. int result=0;
  1854. while( (result=find(p_key,search_from))>=0 ) {
  1855. new_string+=substr(search_from,result-search_from);
  1856. new_string+=p_with;
  1857. search_from=result+p_key.length();
  1858. break;
  1859. }
  1860. new_string+=substr(search_from,length()-search_from);
  1861. return new_string;
  1862. }
  1863. String String::replacen(String p_key,String p_with) const {
  1864. String new_string;
  1865. int search_from=0;
  1866. int result=0;
  1867. while( (result=findn(p_key,search_from))>=0 ) {
  1868. new_string+=substr(search_from,result-search_from);
  1869. new_string+=p_with;
  1870. search_from=result+p_key.length();
  1871. }
  1872. new_string+=substr(search_from,length()-search_from);
  1873. return new_string;
  1874. }
  1875. String String::left(int p_pos) const {
  1876. if (p_pos<=0)
  1877. return "";
  1878. if (p_pos>=length())
  1879. return *this;
  1880. return substr(0,p_pos);
  1881. }
  1882. String String::right(int p_pos) const {
  1883. if (p_pos>=size())
  1884. return *this;
  1885. if (p_pos<0)
  1886. return "";
  1887. return substr(p_pos+1,(length()-p_pos)-1);
  1888. }
  1889. CharType String::ord_at(int p_idx) const {
  1890. ERR_FAIL_INDEX_V(p_idx, length(), 0);
  1891. return operator[](p_idx);
  1892. }
  1893. String String::strip_edges() const {
  1894. int len=length();
  1895. int beg=0,end=len;
  1896. for (int i=0;i<length();i++) {
  1897. if (operator[](i)<=32)
  1898. beg++;
  1899. else
  1900. break;
  1901. }
  1902. for (int i=(int)(length()-1);i>=0;i--) {
  1903. if (operator[](i)<=32)
  1904. end--;
  1905. else
  1906. break;
  1907. }
  1908. if (beg==0 && end==len)
  1909. return *this;
  1910. return substr(beg,end-beg);
  1911. }
  1912. String String::strip_escapes() const {
  1913. int len=length();
  1914. int beg=0,end=len;
  1915. for (int i=0;i<length();i++) {
  1916. if (operator[](i)<=31)
  1917. beg++;
  1918. else
  1919. break;
  1920. }
  1921. for (int i=(int)(length()-1);i>=0;i--) {
  1922. if (operator[](i)<=31)
  1923. end--;
  1924. else
  1925. break;
  1926. }
  1927. if (beg==0 && end==len)
  1928. return *this;
  1929. return substr(beg,end-beg);
  1930. }
  1931. String String::simplify_path() const {
  1932. String s = *this;
  1933. String drive;
  1934. if (s.begins_with("local://")) {
  1935. drive="local://";
  1936. s=s.substr(8,s.length());
  1937. } else if (s.begins_with("res://")) {
  1938. drive="res://";
  1939. s=s.substr(6,s.length());
  1940. } else if (s.begins_with("user://")) {
  1941. drive="user://";
  1942. s=s.substr(6,s.length());
  1943. } else if (s.begins_with("/") || s.begins_with("\\")) {
  1944. drive=s.substr(0,1);
  1945. s=s.substr(1,s.length()-1);
  1946. } else {
  1947. int p = s.find(":/");
  1948. if (p==-1)
  1949. p=s.find(":\\");
  1950. if (p!=-1 && p < s.find("/")) {
  1951. drive=s.substr(0,p+2);
  1952. s=s.substr(p+2,s.length());
  1953. }
  1954. }
  1955. s =s.replace("\\","/");
  1956. Vector<String> dirs = s.split("/",false);
  1957. for(int i=0;i<dirs.size();i++) {
  1958. String d = dirs[i];
  1959. if (d==".") {
  1960. dirs.remove(i);
  1961. i--;
  1962. } else if (d=="..") {
  1963. if (i==0) {
  1964. dirs.remove(i);
  1965. i--;
  1966. } else {
  1967. dirs.remove(i);
  1968. dirs.remove(i-1);
  1969. i-=2;
  1970. }
  1971. }
  1972. }
  1973. s="";
  1974. for(int i=0;i<dirs.size();i++) {
  1975. if (i>0)
  1976. s+="/";
  1977. s+=dirs[i];
  1978. }
  1979. return drive+s;
  1980. }
  1981. static int _humanize_digits(int p_num) {
  1982. if (p_num<10)
  1983. return 2;
  1984. else if (p_num<100)
  1985. return 2;
  1986. else if (p_num<1024)
  1987. return 1;
  1988. else
  1989. return 0;
  1990. }
  1991. String String::humanize_size(size_t p_size) {
  1992. uint64_t _div=1;
  1993. static const char* prefix[]={" Bytes"," KB"," MB"," GB","TB"," PB","HB",""};
  1994. int prefix_idx=0;
  1995. while(p_size>(_div*1024) && prefix[prefix_idx][0]) {
  1996. _div*=1024;
  1997. prefix_idx++;
  1998. }
  1999. int digits=prefix_idx>0?_humanize_digits(p_size/_div):0;
  2000. double divisor = prefix_idx>0?_div:1;
  2001. return String::num(p_size/divisor,digits)+prefix[prefix_idx];
  2002. }
  2003. bool String::is_abs_path() const {
  2004. if (length()>1)
  2005. return (operator[](0)=='/' || operator[](0)=='\\' || find(":/")!=-1 || find(":\\")!=-1);
  2006. else if ((length())==1)
  2007. return (operator[](0)=='/' || operator[](0)=='\\');
  2008. else
  2009. return false;
  2010. }
  2011. bool String::is_valid_identifier() const {
  2012. int len = length();
  2013. if (len==0)
  2014. return false;
  2015. const wchar_t * str = &operator[](0);
  2016. for(int i=0;i<len;i++) {
  2017. if (i==0) {
  2018. if (str[0]>='0' && str[0]<='9')
  2019. return false; // no start with number plz
  2020. }
  2021. bool valid_char = (str[i]>='0' && str[i]<='9') || (str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z') || str[i]=='_';
  2022. if (!valid_char)
  2023. return false;
  2024. }
  2025. return true;
  2026. }
  2027. //kind of poor should be rewritten properly
  2028. String String::c_unescape() const {
  2029. String escaped=*this;
  2030. escaped=escaped.replace("\\a","\a");
  2031. escaped=escaped.replace("\\b","\b");
  2032. escaped=escaped.replace("\\f","\f");
  2033. escaped=escaped.replace("\\n","\n");
  2034. escaped=escaped.replace("\\r","\r");
  2035. escaped=escaped.replace("\\t","\t");
  2036. escaped=escaped.replace("\\v","\v");
  2037. escaped=escaped.replace("\\'","\'");
  2038. escaped=escaped.replace("\\\"","\"");
  2039. escaped=escaped.replace("\\?","\?");
  2040. escaped=escaped.replace("\\\\","\\");
  2041. return escaped;
  2042. }
  2043. String String::c_escape() const {
  2044. String escaped=*this;
  2045. escaped=escaped.replace("\\","\\\\");
  2046. escaped=escaped.replace("\a","\\a");
  2047. escaped=escaped.replace("\b","\\b");
  2048. escaped=escaped.replace("\f","\\f");
  2049. escaped=escaped.replace("\n","\\n");
  2050. escaped=escaped.replace("\r","\\r");
  2051. escaped=escaped.replace("\t","\\t");
  2052. escaped=escaped.replace("\v","\\v");
  2053. escaped=escaped.replace("\'","\\'");
  2054. escaped=escaped.replace("\"","\\\"");
  2055. escaped=escaped.replace("\?","\\?");
  2056. return escaped;
  2057. }
  2058. String String::xml_escape(bool p_escape_quotes) const {
  2059. String str=*this;
  2060. str=str.replace("&","&amp;");
  2061. str=str.replace("<","&gt;");
  2062. str=str.replace(">","&lt;");
  2063. if (p_escape_quotes) {
  2064. str=str.replace("'","&apos;");
  2065. str=str.replace("\"","&quot;");
  2066. }
  2067. /*
  2068. for (int i=1;i<32;i++) {
  2069. char chr[2]={i,0};
  2070. str=str.replace(chr,"&#"+String::num(i)+";");
  2071. }*/
  2072. return str;
  2073. }
  2074. String String::xml_unescape() const {
  2075. String str=*this;
  2076. str=str.strip_edges();
  2077. //str=str.replace("\"","");
  2078. str=str.replace("&gt;","<");
  2079. str=str.replace("&lt;",">");
  2080. str=str.replace("&apos;","'");
  2081. str=str.replace("&quot;","\"");
  2082. /*
  2083. for (int i=1;i<32;i++) {
  2084. char chr[2]={i,0};
  2085. str=str.replace("&#"+String::num(i)+";",chr);
  2086. }*/
  2087. str=str.replace("&amp;","&");
  2088. return str;
  2089. }
  2090. String String::pad_decimals(int p_digits) const {
  2091. String s=*this;
  2092. int c = s.find(".");
  2093. if (c==-1) {
  2094. if (p_digits<=0) {
  2095. return s;
  2096. }
  2097. s+=".";
  2098. c=s.length()-1;
  2099. } else {
  2100. if (p_digits<=0) {
  2101. return s.substr(0,c);
  2102. }
  2103. }
  2104. if (s.length()-(c+1) > p_digits) {
  2105. s=s.substr(0,c+p_digits+1);
  2106. } else {
  2107. while(s.length()-(c+1) < p_digits) {
  2108. s+="0";
  2109. }
  2110. }
  2111. return s;
  2112. }
  2113. String String::pad_zeros(int p_digits) const {
  2114. String s=*this;
  2115. int end = s.find(".");
  2116. if (end==-1) {
  2117. end=s.length();
  2118. }
  2119. if (end==0)
  2120. return s;
  2121. int begin=0;
  2122. while (begin<end && (s[begin]<'0' || s[begin]>'9')) {
  2123. begin++;
  2124. }
  2125. if (begin>=end)
  2126. return s;
  2127. while(end-begin < p_digits) {
  2128. s=s.insert(begin,"0");
  2129. end++;
  2130. }
  2131. return s;
  2132. }
  2133. bool String::is_valid_integer() const {
  2134. int len = length();
  2135. if (len==0)
  2136. return false;
  2137. int from=0;
  2138. if (operator[](0)=='+' || operator[](0)=='-')
  2139. from++;
  2140. for(int i=from;i<len;i++) {
  2141. if (operator[](i)<'0' || operator[](i)>'9')
  2142. return false; // no start with number plz
  2143. }
  2144. return true;
  2145. }
  2146. bool String::is_valid_float() const {
  2147. int len = length();
  2148. if (len==0)
  2149. return false;
  2150. int from=0;
  2151. if (operator[](0)=='+' || operator[](0)=='-') {
  2152. from++;
  2153. }
  2154. //this was pulled out of my ass, i wonder if it's correct...
  2155. bool exponent_found=false;
  2156. bool period_found=false;
  2157. bool sign_found=false;
  2158. bool exponent_values_found=false;
  2159. bool numbers_found=false;
  2160. for(int i=from;i<len;i++) {
  2161. if (operator[](i)>='0' && operator[](i)<='9') {
  2162. if (exponent_found)
  2163. exponent_values_found=true;
  2164. else
  2165. numbers_found=true;
  2166. } else if (numbers_found && !exponent_found && operator[](i)=='e') {
  2167. exponent_found=true;
  2168. } else if (!period_found && !exponent_found && operator[](i)=='.') {
  2169. period_found=true;
  2170. } else if ((operator[](i)=='-' || operator[](i)=='+') && exponent_found && !exponent_values_found && !sign_found) {
  2171. sign_found=true;
  2172. } else
  2173. return false; // no start with number plz
  2174. }
  2175. return numbers_found;
  2176. }
  2177. String String::path_to_file(const String& p_path) const {
  2178. String src=this->replace("\\","/").get_base_dir();
  2179. String dst=p_path.replace("\\","/").get_base_dir();
  2180. return src.path_to(dst)+p_path.get_file();
  2181. }
  2182. String String::path_to(const String& p_path) const {
  2183. String src=this->replace("\\","/");
  2184. String dst=p_path.replace("\\","/");
  2185. if (!src.ends_with("/"))
  2186. src+="/";
  2187. if (!dst.ends_with("/"))
  2188. dst+="/";
  2189. String base;
  2190. if (src.begins_with("res://") && dst.begins_with("res://")) {
  2191. base="res:/";
  2192. src=src.replace("res://","/");
  2193. dst=dst.replace("res://","/");
  2194. } else if (src.begins_with("user://") && dst.begins_with("user://")) {
  2195. base="user:/";
  2196. src=src.replace("user://","/");
  2197. dst=dst.replace("user://","/");
  2198. } else if (src.begins_with("/") && dst.begins_with("/")) {
  2199. //nothing
  2200. } else {
  2201. //dos style
  2202. String src_begin=src.get_slice("/",0);
  2203. String dst_begin=dst.get_slice("/",0);
  2204. ERR_FAIL_COND_V(src_begin!=dst_begin,p_path); //return dst absolute path
  2205. base=src_begin;
  2206. src=src.substr(src_begin.length(),src.length());
  2207. dst=dst.substr(dst_begin.length(),dst.length());
  2208. }
  2209. //remove leading and trailing slash and split
  2210. Vector<String> src_dirs=src.substr(1,src.length()-2).split("/");
  2211. Vector<String> dst_dirs=dst.substr(1,dst.length()-2).split("/");
  2212. //find common parent
  2213. int common_parent=0;
  2214. while(true) {
  2215. if (src_dirs.size()==common_parent)
  2216. break;
  2217. if (dst_dirs.size()==common_parent)
  2218. break;
  2219. if (src_dirs[common_parent]!=dst_dirs[common_parent])
  2220. break;
  2221. common_parent++;
  2222. }
  2223. common_parent--;
  2224. String dir;
  2225. for(int i=src_dirs.size()-1;i>common_parent;i--) {
  2226. dir+="../";
  2227. }
  2228. for(int i=common_parent+1;i<dst_dirs.size();i++) {
  2229. dir+=dst_dirs[i]+"/";
  2230. }
  2231. if (dir.length()==0)
  2232. dir="./";
  2233. return dir;
  2234. }
  2235. bool String::is_valid_html_color() const {
  2236. return Color::html_is_valid(*this);
  2237. }
  2238. bool String::is_valid_ip_address() const {
  2239. Vector<String> ip = split(".");
  2240. if (ip.size()!=4)
  2241. return false;
  2242. for(int i=0;i<ip.size();i++) {
  2243. String n = ip[i];
  2244. if (!n.is_valid_integer())
  2245. return false;
  2246. int val = n.to_int();
  2247. if (val<0 || val>255)
  2248. return false;
  2249. }
  2250. return true;
  2251. }
  2252. bool String::is_resource_file() const {
  2253. return begins_with("res://") && find("::")==-1;
  2254. }
  2255. bool String::is_rel_path() const {
  2256. return !is_abs_path();
  2257. }
  2258. String String::get_base_dir() const {
  2259. int basepos = find("://");
  2260. String rs;
  2261. String base;
  2262. if (basepos!=-1) {
  2263. int end = basepos+3;
  2264. rs = substr(end,length());
  2265. base = substr(0,end);
  2266. } else {
  2267. if (begins_with("/")) {
  2268. rs=substr(1,length());
  2269. base="/";
  2270. } else {
  2271. rs=*this;
  2272. }
  2273. }
  2274. int sep = MAX( rs.find_last("/"), rs.find_last("\\") );
  2275. if (sep==-1)
  2276. return base;
  2277. return base+rs.substr(0,sep);
  2278. }
  2279. String String::get_file() const {
  2280. int sep = MAX( find_last("/"), find_last("\\") );
  2281. if (sep==-1)
  2282. return *this;
  2283. return substr(sep+1,length());
  2284. }
  2285. String String::extension() const {
  2286. int pos = find_last(".");
  2287. if (pos<0)
  2288. return *this;
  2289. return substr( pos+1, length() );
  2290. }
  2291. String String::plus_file(const String& p_file) const {
  2292. if (length()>0 && operator [](length()-1)=='/')
  2293. return *this+p_file;
  2294. else
  2295. return *this+"/"+p_file;
  2296. }
  2297. String String::percent_encode() const {
  2298. CharString cs = utf8();
  2299. String encoded;
  2300. for(int i=0;i<cs.length();i++) {
  2301. uint8_t c = cs[i];
  2302. if ( (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || c=='-' || c=='_' || c=='~' || c=='.') {
  2303. char p[2]={c,0};
  2304. encoded+=p;
  2305. } else {
  2306. char p[4]={'%',0,0,0};
  2307. static const char hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  2308. p[1]=hex[c>>4];
  2309. p[2]=hex[c&0xF];
  2310. encoded+=p;
  2311. }
  2312. }
  2313. return encoded;
  2314. }
  2315. String String::percent_decode() const {
  2316. CharString pe;
  2317. for(int i=0;i<length();i++) {
  2318. uint8_t c=operator[](i);
  2319. if (c=='%' && i<length()-2) {
  2320. uint8_t a = LOWERCASE(operator[](i+1));
  2321. uint8_t b = LOWERCASE(operator[](i+2));
  2322. c=0;
  2323. if (a>='0' && a<='9')
  2324. c=(a-'0')<<4;
  2325. else if (a>='a' && a<='f')
  2326. c=(a-'a'+10)<<4;
  2327. else
  2328. continue;
  2329. uint8_t d=0;
  2330. if (b>='0' && b<='9')
  2331. d=(b-'0');
  2332. else if (b>='a' && b<='f')
  2333. d=(b-'a'+10);
  2334. else
  2335. continue;
  2336. c+=d;
  2337. i+=2;
  2338. }
  2339. pe.push_back(c);
  2340. }
  2341. pe.push_back(0);
  2342. return String::utf8(pe.ptr());
  2343. }
  2344. String String::basename() const {
  2345. int pos = find_last(".");
  2346. if (pos<0)
  2347. return *this;
  2348. return substr( 0, pos );
  2349. }
  2350. String itos(int64_t p_val) {
  2351. return String::num_int64(p_val);
  2352. }
  2353. String rtos(double p_val) {
  2354. return String::num(p_val);
  2355. }
  2356. String rtoss(double p_val) {
  2357. return String::num_scientific(p_val);
  2358. }