String.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include <cassert>
  24. #include <cstdio>
  25. #include <cstring>
  26. #include "List.h"
  27. #include "Types.h"
  28. namespace crown
  29. {
  30. namespace string
  31. {
  32. static const char* EMPTY = "";
  33. static bool is_alpha(char c);
  34. static bool is_digit(char c);
  35. static bool is_upper(char c);
  36. static bool is_lower(char c);
  37. static bool is_whitespace(char c);
  38. static size_t strlen(const char* str);
  39. static const char* strstr(const char* str1, const char* str2);
  40. static int32_t strcmp(const char* str1, const char* str2);
  41. static char* strcpy(char* dest, const char* src);
  42. static char* strncpy(char* dest, const char* src, size_t len);
  43. static int32_t find_first(const char* str, char c);
  44. static int32_t find_last(const char* str, char c);
  45. //bool parse_int(int32_t* value) const;
  46. //bool parse_uint(uint32_t* value) const;
  47. //bool parse_float(float* value) const;
  48. //inline void MakeLower()
  49. //{
  50. // for (uint32_t i = 0; i < mLength; i++)
  51. // {
  52. // if (is_upper(mText[i]))
  53. // {
  54. // mText[i] += 32;
  55. // }
  56. // }
  57. //}
  58. //inline void MakeUpper()
  59. //{
  60. // for (uint32_t i = 0; i < mLength; i++)
  61. // {
  62. // if (is_lower(mText[i]))
  63. // {
  64. // mText[i] -= 32;
  65. // }
  66. // }
  67. //}
  68. //inline bool StartsWith(const Str& begin) const
  69. //{
  70. // if (mLength < begin.mLength)
  71. // return false;
  72. // for (uint32_t i = 0; i < begin.mLength; i++)
  73. // {
  74. // if (mText[i] != begin.mText[i])
  75. // return false;
  76. // }
  77. // return true;
  78. //}
  79. //inline bool EndsWith(const Str& end) const
  80. //{
  81. // if (mLength < end.mLength)
  82. // return false;
  83. // uint32_t beginIndex = mLength - end.mLength;
  84. // for (uint32_t i = beginIndex; i < mLength; i++)
  85. // {
  86. // if (mText[i] != end.mText[i - beginIndex])
  87. // return false;
  88. // }
  89. // return true;
  90. //}
  91. //inline int32_t Find(const Str& Str) const
  92. //{
  93. // bool found = true;
  94. // for (uint32_t i = 0; i < mLength; i++)
  95. // {
  96. // if (mLength - i < Str.mLength)
  97. // {
  98. // return -1;
  99. // }
  100. // for (uint32_t j = 0; j < Str.mLength; j++)
  101. // {
  102. // if (mText[i + j] != Str.mText[j])
  103. // {
  104. // found = false;
  105. // }
  106. // }
  107. // if (found)
  108. // {
  109. // return i;
  110. // }
  111. // found = true;
  112. // }
  113. // return -1;
  114. //}
  115. //inline Str GetSubstring(uint32_t start, uint32_t end) const
  116. //{
  117. // assert(start <= mLength);
  118. // assert(end <= mLength);
  119. // assert(start <= end);
  120. // uint32_t len = end - start;
  121. // char* tmp = new char[len + 1];
  122. // for (uint32_t i = 0; i < len; i++)
  123. // {
  124. // tmp[i] = mText[i + start];
  125. // }
  126. // tmp[len] = '\0';
  127. // Str ret(tmp);
  128. // delete[] tmp;
  129. // return ret;
  130. //}
  131. //inline void Remove(uint32_t start, uint32_t end)
  132. //{
  133. // assert(start <= mLength);
  134. // assert(end <= mLength);
  135. // assert(start <= end);
  136. // uint32_t len = end - start;
  137. // char* tmp = new char[mLength - len + 1];
  138. // uint32_t i;
  139. // for (i = 0; i < start; i++)
  140. // {
  141. // tmp[i] = mText[i];
  142. // }
  143. // i += len;
  144. // for (; i < mLength; i++)
  145. // {
  146. // tmp[i - len] = mText[i];
  147. // }
  148. // mLength = mLength - len;
  149. // tmp[mLength] = '\0';
  150. // delete[] mText;
  151. // mText = tmp;
  152. //}
  153. ////! Replaces all the occurencies of the given character with the new one
  154. //inline void Replace(char toFind, char toReplace)
  155. //{
  156. // for (uint32_t i = 0; i < mLength; i++)
  157. // {
  158. // if (mText[i] == toFind)
  159. // mText[i] = toReplace;
  160. // }
  161. //}
  162. ////! Replaces all the occurencies of the given Str with the new one
  163. //inline void Replace(const Str& toFind, const Str& toReplace)
  164. //{
  165. // assert(toReplace.mLength > 0);
  166. // if (mLength < toReplace.mLength)
  167. // return;
  168. // List<char> tmp(get_default_allocator());
  169. // uint32_t i;
  170. // for (i = 0; i < mLength - (toFind.mLength - 1); i++)
  171. // {
  172. // bool found = true;
  173. // for(uint32_t j = 0; j < toFind.mLength; j++)
  174. // if (mText[i + j] != toFind.mText[j])
  175. // {
  176. // found = false;
  177. // break;
  178. // }
  179. // if (found)
  180. // {
  181. // for(uint32_t j = 0; j < toReplace.mLength; j++)
  182. // tmp.push_back(toReplace.mText[j]);
  183. // i += toFind.mLength-1;
  184. // }
  185. // else
  186. // tmp.push_back(mText[i]);
  187. // }
  188. // while(i <= mLength)
  189. // {
  190. // tmp.push_back(mText[i]);
  191. // i++;
  192. // }
  193. // *this = tmp.begin();
  194. //}
  195. //inline void Split(char ch, List<Str>& split) const
  196. //{
  197. // uint32_t lastChar = 0;
  198. // uint32_t strPtr = 0;
  199. // uint32_t charCount = 0;
  200. // while (strPtr <= mLength)
  201. // {
  202. // lastChar = strPtr;
  203. // while (mText[strPtr] != ch && mText[strPtr] != '\0')
  204. // {
  205. // strPtr++;
  206. // charCount++;
  207. // }
  208. // if (charCount > 0)
  209. // {
  210. // split.push_back(this->GetSubstring(lastChar, lastChar + charCount));
  211. // }
  212. // charCount = 0;
  213. // strPtr++;
  214. // }
  215. //}
  216. //inline Str Trim()
  217. //{
  218. // int32_t beginIndex = 0;
  219. // int32_t endIndex = mLength - 1;
  220. // while (is_whitespace(mText[beginIndex]))
  221. // {
  222. // beginIndex++;
  223. // }
  224. // while (is_whitespace(mText[endIndex]))
  225. // {
  226. // endIndex--;
  227. // }
  228. // return GetSubstring(beginIndex, endIndex + 1);
  229. //}
  230. //inline int32_t GetOccurrenceCount(char ch) const
  231. //{
  232. // int32_t count = 0;
  233. // for (uint32_t i = 0; i < mLength; i++)
  234. // {
  235. // if (mText[i] == ch)
  236. // {
  237. // count++;
  238. // }
  239. // }
  240. // return count;
  241. //}
  242. ////-----------------------------------------------------------------------------
  243. //inline bool parse_int(int32_t* value) const
  244. //{
  245. // if (sscanf(mText, "%d", value) != 1)
  246. // {
  247. // return false;
  248. // }
  249. // return true;
  250. //}
  251. ////-----------------------------------------------------------------------------
  252. //inline bool parse_uint(uint32_t* value) const
  253. //{
  254. // if (sscanf(mText, "%u", value) != 1)
  255. // {
  256. // return false;
  257. // }
  258. // return true;
  259. //}
  260. ////-----------------------------------------------------------------------------
  261. //inline bool parse_float(float* value) const
  262. //{
  263. // if (sscanf(mText, "%f", value) != 1)
  264. // {
  265. // return false;
  266. // }
  267. // return true;
  268. //}
  269. //-----------------------------------------------------------------------------
  270. inline bool is_alpha(char c)
  271. {
  272. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  273. }
  274. //-----------------------------------------------------------------------------
  275. inline bool is_digit(char c)
  276. {
  277. return !(c < '0' || c > '9');
  278. }
  279. //-----------------------------------------------------------------------------
  280. inline bool is_upper(char c)
  281. {
  282. return (c >= 'A' && c <= 'Z');
  283. }
  284. //-----------------------------------------------------------------------------
  285. inline bool is_lower(char c)
  286. {
  287. return (c >= 'a' && c <= 'z');
  288. }
  289. //-----------------------------------------------------------------------------
  290. inline bool is_whitespace(char c)
  291. {
  292. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  293. }
  294. //-----------------------------------------------------------------------------
  295. inline size_t strlen(const char* str)
  296. {
  297. size_t chars = 0;
  298. while(*str)
  299. {
  300. if ((*str & 0xC0) != 0x80)
  301. {
  302. chars++;
  303. }
  304. str++;
  305. }
  306. return chars;
  307. }
  308. //-----------------------------------------------------------------------------
  309. inline const char* strstr(const char* str1, const char* str2)
  310. {
  311. return ::strstr(str1, str2);
  312. }
  313. //-----------------------------------------------------------------------------
  314. inline int32_t strcmp(const char* str1, const char* str2)
  315. {
  316. return ::strcmp(str1, str2);
  317. }
  318. //-----------------------------------------------------------------------------
  319. inline char* strcpy(char* dest, const char* src)
  320. {
  321. return ::strcpy(dest, src);
  322. }
  323. //-----------------------------------------------------------------------------
  324. inline char* strncpy(char* dest, const char* src, size_t len)
  325. {
  326. return ::strncpy(dest, src, len);
  327. }
  328. //-----------------------------------------------------------------------------
  329. inline int32_t find_first(const char* str, char c)
  330. {
  331. assert(str != NULL);
  332. size_t strLen = string::strlen(str);
  333. for (size_t i = 0; i < strLen; i++)
  334. {
  335. if (str[i] == c)
  336. {
  337. return i;
  338. }
  339. }
  340. return -1;
  341. }
  342. //-----------------------------------------------------------------------------
  343. inline int32_t find_last(const char* str, char c)
  344. {
  345. assert(str != NULL);
  346. size_t strLen = string::strlen(str);
  347. for (size_t i = strLen; i > 0; i--)
  348. {
  349. if (str[i - 1] == c)
  350. {
  351. return i - 1;
  352. }
  353. }
  354. return -1;
  355. }
  356. } // namespace string
  357. } // namespace crown