String.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 "Assert.h"
  24. #include <cstdio>
  25. #include <cstring>
  26. #include "List.h"
  27. #include "Types.h"
  28. namespace crown
  29. {
  30. namespace string
  31. {
  32. const char* const EMPTY = "";
  33. bool is_alpha(char c);
  34. bool is_digit(char c);
  35. bool is_upper(char c);
  36. bool is_lower(char c);
  37. bool is_whitespace(char c);
  38. size_t strlen(const char* str);
  39. const char* strstr(const char* str1, const char* str2);
  40. int32_t strcmp(const char* str1, const char* str2);
  41. char* strcpy(char* dest, const char* src);
  42. char* strncpy(char* dest, const char* src, size_t len);
  43. char* strcat(char* dest, const char* src);
  44. char* strncat(char* dest, const char* src, size_t len);
  45. const char* begin(const char* str);
  46. const char* end(const char* str);
  47. const char* find_first(const char* str, char c);
  48. const char* find_last(const char* str, char c);
  49. void substring(const char* begin, const char* end, char* out, size_t len);
  50. //inline void MakeLower()
  51. //{
  52. // for (uint32_t i = 0; i < mLength; i++)
  53. // {
  54. // if (is_upper(mText[i]))
  55. // {
  56. // mText[i] += 32;
  57. // }
  58. // }
  59. //}
  60. //inline void MakeUpper()
  61. //{
  62. // for (uint32_t i = 0; i < mLength; i++)
  63. // {
  64. // if (is_lower(mText[i]))
  65. // {
  66. // mText[i] -= 32;
  67. // }
  68. // }
  69. //}
  70. //inline bool StartsWith(const Str& begin) const
  71. //{
  72. // if (mLength < begin.mLength)
  73. // return false;
  74. // for (uint32_t i = 0; i < begin.mLength; i++)
  75. // {
  76. // if (mText[i] != begin.mText[i])
  77. // return false;
  78. // }
  79. // return true;
  80. //}
  81. //inline bool EndsWith(const Str& end) const
  82. //{
  83. // if (mLength < end.mLength)
  84. // return false;
  85. // uint32_t beginIndex = mLength - end.mLength;
  86. // for (uint32_t i = beginIndex; i < mLength; i++)
  87. // {
  88. // if (mText[i] != end.mText[i - beginIndex])
  89. // return false;
  90. // }
  91. // return true;
  92. //}
  93. //inline int32_t Find(const Str& Str) const
  94. //{
  95. // bool found = true;
  96. // for (uint32_t i = 0; i < mLength; i++)
  97. // {
  98. // if (mLength - i < Str.mLength)
  99. // {
  100. // return -1;
  101. // }
  102. // for (uint32_t j = 0; j < Str.mLength; j++)
  103. // {
  104. // if (mText[i + j] != Str.mText[j])
  105. // {
  106. // found = false;
  107. // }
  108. // }
  109. // if (found)
  110. // {
  111. // return i;
  112. // }
  113. // found = true;
  114. // }
  115. // return -1;
  116. //}
  117. //inline void Remove(uint32_t start, uint32_t end)
  118. //{
  119. // CE_ASSERT(start <= mLength);
  120. // CE_ASSERT(end <= mLength);
  121. // CE_ASSERT(start <= end);
  122. // uint32_t len = end - start;
  123. // char* tmp = new char[mLength - len + 1];
  124. // uint32_t i;
  125. // for (i = 0; i < start; i++)
  126. // {
  127. // tmp[i] = mText[i];
  128. // }
  129. // i += len;
  130. // for (; i < mLength; i++)
  131. // {
  132. // tmp[i - len] = mText[i];
  133. // }
  134. // mLength = mLength - len;
  135. // tmp[mLength] = '\0';
  136. // delete[] mText;
  137. // mText = tmp;
  138. //}
  139. ////! Replaces all the occurencies of the given character with the new one
  140. //inline void Replace(char toFind, char toReplace)
  141. //{
  142. // for (uint32_t i = 0; i < mLength; i++)
  143. // {
  144. // if (mText[i] == toFind)
  145. // mText[i] = toReplace;
  146. // }
  147. //}
  148. ////! Replaces all the occurencies of the given Str with the new one
  149. //inline void Replace(const Str& toFind, const Str& toReplace)
  150. //{
  151. // CE_ASSERT(toReplace.mLength > 0);
  152. // if (mLength < toReplace.mLength)
  153. // return;
  154. // List<char> tmp(get_default_allocator());
  155. // uint32_t i;
  156. // for (i = 0; i < mLength - (toFind.mLength - 1); i++)
  157. // {
  158. // bool found = true;
  159. // for(uint32_t j = 0; j < toFind.mLength; j++)
  160. // if (mText[i + j] != toFind.mText[j])
  161. // {
  162. // found = false;
  163. // break;
  164. // }
  165. // if (found)
  166. // {
  167. // for(uint32_t j = 0; j < toReplace.mLength; j++)
  168. // tmp.push_back(toReplace.mText[j]);
  169. // i += toFind.mLength-1;
  170. // }
  171. // else
  172. // tmp.push_back(mText[i]);
  173. // }
  174. // while(i <= mLength)
  175. // {
  176. // tmp.push_back(mText[i]);
  177. // i++;
  178. // }
  179. // *this = tmp.begin();
  180. //}
  181. //inline void Split(char ch, List<Str>& split) const
  182. //{
  183. // uint32_t lastChar = 0;
  184. // uint32_t strPtr = 0;
  185. // uint32_t charCount = 0;
  186. // while (strPtr <= mLength)
  187. // {
  188. // lastChar = strPtr;
  189. // while (mText[strPtr] != ch && mText[strPtr] != '\0')
  190. // {
  191. // strPtr++;
  192. // charCount++;
  193. // }
  194. // if (charCount > 0)
  195. // {
  196. // split.push_back(this->GetSubstring(lastChar, lastChar + charCount));
  197. // }
  198. // charCount = 0;
  199. // strPtr++;
  200. // }
  201. //}
  202. //inline Str Trim()
  203. //{
  204. // int32_t beginIndex = 0;
  205. // int32_t endIndex = mLength - 1;
  206. // while (is_whitespace(mText[beginIndex]))
  207. // {
  208. // beginIndex++;
  209. // }
  210. // while (is_whitespace(mText[endIndex]))
  211. // {
  212. // endIndex--;
  213. // }
  214. // return GetSubstring(beginIndex, endIndex + 1);
  215. //}
  216. //inline int32_t GetOccurrenceCount(char ch) const
  217. //{
  218. // int32_t count = 0;
  219. // for (uint32_t i = 0; i < mLength; i++)
  220. // {
  221. // if (mText[i] == ch)
  222. // {
  223. // count++;
  224. // }
  225. // }
  226. // return count;
  227. //}
  228. //-----------------------------------------------------------------------------
  229. inline bool is_alpha(char c)
  230. {
  231. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  232. }
  233. //-----------------------------------------------------------------------------
  234. inline bool is_digit(char c)
  235. {
  236. return !(c < '0' || c > '9');
  237. }
  238. //-----------------------------------------------------------------------------
  239. inline bool is_upper(char c)
  240. {
  241. return (c >= 'A' && c <= 'Z');
  242. }
  243. //-----------------------------------------------------------------------------
  244. inline bool is_lower(char c)
  245. {
  246. return (c >= 'a' && c <= 'z');
  247. }
  248. //-----------------------------------------------------------------------------
  249. inline bool is_whitespace(char c)
  250. {
  251. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  252. }
  253. //-----------------------------------------------------------------------------
  254. inline size_t strlen(const char* str)
  255. {
  256. size_t chars = 0;
  257. while(*str)
  258. {
  259. if ((*str & 0xC0) != 0x80)
  260. {
  261. chars++;
  262. }
  263. str++;
  264. }
  265. return chars;
  266. }
  267. //-----------------------------------------------------------------------------
  268. inline const char* strstr(const char* str1, const char* str2)
  269. {
  270. return ::strstr(str1, str2);
  271. }
  272. //-----------------------------------------------------------------------------
  273. inline int32_t strcmp(const char* str1, const char* str2)
  274. {
  275. return ::strcmp(str1, str2);
  276. }
  277. //-----------------------------------------------------------------------------
  278. inline char* strcpy(char* dest, const char* src)
  279. {
  280. return ::strcpy(dest, src);
  281. }
  282. //-----------------------------------------------------------------------------
  283. inline char* strncpy(char* dest, const char* src, size_t len)
  284. {
  285. return ::strncpy(dest, src, len);
  286. }
  287. //-----------------------------------------------------------------------------
  288. inline char* strcat(char* dest, const char* src)
  289. {
  290. return ::strcat(dest, src);
  291. }
  292. //-----------------------------------------------------------------------------
  293. inline char* strncat(char* dest, const char* src, size_t len)
  294. {
  295. return ::strncat(dest, src, len);
  296. }
  297. //-----------------------------------------------------------------------------
  298. inline const char* begin(const char* str)
  299. {
  300. CE_ASSERT(str != NULL, "Str must be != NULL");
  301. return str;
  302. }
  303. //-----------------------------------------------------------------------------
  304. inline const char* end(const char* str)
  305. {
  306. CE_ASSERT(str != NULL, "Str must be != NULL");
  307. return str + string::strlen(str) + 1;
  308. }
  309. //-----------------------------------------------------------------------------
  310. inline const char* find_first(const char* str, char c)
  311. {
  312. CE_ASSERT(str != NULL, "Str must be != NULL");
  313. const char* str_begin = string::begin(str);
  314. while (str_begin != string::end(str))
  315. {
  316. if ((*str_begin) == c)
  317. {
  318. return str_begin;
  319. }
  320. str_begin++;
  321. }
  322. return string::end(str);
  323. }
  324. //-----------------------------------------------------------------------------
  325. inline const char* find_last(const char* str, char c)
  326. {
  327. CE_ASSERT(str != NULL, "Str must be != NULL");
  328. const char* str_end = string::end(str) - 1;
  329. while (str_end != string::begin(str) - 1)
  330. {
  331. if ((*str_end) == c)
  332. {
  333. return str_end;
  334. }
  335. str_end--;
  336. }
  337. return string::end(str);
  338. }
  339. //-----------------------------------------------------------------------------
  340. inline void substring(const char* begin, const char* end, char* out, size_t len)
  341. {
  342. CE_ASSERT(begin != NULL, "Begin must be != NULL");
  343. CE_ASSERT(end != NULL, "End must be != NULL");
  344. CE_ASSERT(out != NULL, "Out must be != NULL");
  345. size_t i = 0;
  346. char* out_iterator = out;
  347. while (begin != end && i < len)
  348. {
  349. (*out_iterator) = (*begin);
  350. begin++;
  351. out_iterator++;
  352. i++;
  353. }
  354. out[i] = '\0';
  355. }
  356. } // namespace string
  357. } // namespace crown