ustring.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*************************************************************************/
  2. /* ustring.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef RSTRING_H
  31. #define RSTRING_H
  32. #include "array.h"
  33. #include "typedefs.h"
  34. #include "vector.h"
  35. /**
  36. @author red <red@killy>
  37. */
  38. class CharString : public Vector<char> {
  39. public:
  40. bool operator<(const CharString &p_right) const;
  41. int length() const { return size() ? size() - 1 : 0; }
  42. const char *get_data() const;
  43. operator const char *() { return get_data(); };
  44. };
  45. typedef wchar_t CharType;
  46. struct StrRange {
  47. const CharType *c_str;
  48. int len;
  49. StrRange(const CharType *p_c_str = NULL, int p_len = 0) {
  50. c_str = p_c_str;
  51. len = p_len;
  52. }
  53. };
  54. class String : public Vector<CharType> {
  55. void copy_from(const char *p_cstr);
  56. void copy_from(const CharType *p_cstr, int p_clip_to = -1);
  57. void copy_from(const CharType &p_char);
  58. bool _base_is_subsequence_of(const String &p_string, bool case_insensitive) const;
  59. public:
  60. enum {
  61. npos = -1 ///<for "some" compatibility with std::string (npos is a huge value in std::string)
  62. };
  63. bool operator==(const String &p_str) const;
  64. bool operator!=(const String &p_str) const;
  65. String operator+(const String &p_str) const;
  66. //String operator+(CharType p_char) const;
  67. String &operator+=(const String &);
  68. String &operator+=(CharType p_char);
  69. String &operator+=(const char *p_str);
  70. String &operator+=(const CharType *p_str);
  71. /* Compatibility Operators */
  72. void operator=(const char *p_str);
  73. void operator=(const CharType *p_str);
  74. bool operator==(const char *p_str) const;
  75. bool operator==(const CharType *p_str) const;
  76. bool operator==(const StrRange &p_str_range) const;
  77. bool operator!=(const char *p_str) const;
  78. bool operator!=(const CharType *p_str) const;
  79. bool operator<(const CharType *p_str) const;
  80. bool operator<(const char *p_str) const;
  81. bool operator<(const String &p_str) const;
  82. bool operator<=(const String &p_str) const;
  83. signed char casecmp_to(const String &p_str) const;
  84. signed char nocasecmp_to(const String &p_str) const;
  85. signed char naturalnocasecmp_to(const String &p_str) const;
  86. const CharType *c_str() const;
  87. /* standard size stuff */
  88. _FORCE_INLINE_ int length() const {
  89. int s = size();
  90. return s ? (s - 1) : 0; // length does not include zero
  91. }
  92. /* complex helpers */
  93. String substr(int p_from, int p_chars) const;
  94. int find(const String &p_str, int p_from = 0) const; ///< return <0 if failed
  95. int find(const char *p_str, int p_from) const; ///< return <0 if failed
  96. int find_last(const String &p_str) const; ///< return <0 if failed
  97. int findn(const String &p_str, int p_from = 0) const; ///< return <0 if failed, case insensitive
  98. int rfind(const String &p_str, int p_from = -1) const; ///< return <0 if failed
  99. int rfindn(const String &p_str, int p_from = -1) const; ///< return <0 if failed, case insensitive
  100. int findmk(const Vector<String> &p_keys, int p_from = 0, int *r_key = NULL) const; ///< return <0 if failed
  101. bool match(const String &p_wildcard) const;
  102. bool matchn(const String &p_wildcard) const;
  103. bool begins_with(const String &p_string) const;
  104. bool begins_with(const char *p_string) const;
  105. bool ends_with(const String &p_string) const;
  106. bool is_enclosed_in(const String &p_string) const;
  107. bool is_subsequence_of(const String &p_string) const;
  108. bool is_subsequence_ofi(const String &p_string) const;
  109. bool is_quoted() const;
  110. Vector<String> bigrams() const;
  111. float similarity(const String &p_string) const;
  112. String format(const Variant &values, String placeholder = "{_}") const;
  113. String replace_first(const String &p_key, const String &p_with) const;
  114. String replace(const String &p_key, const String &p_with) const;
  115. String replace(const char *p_key, const char *p_with) const;
  116. String replacen(const String &p_key, const String &p_with) const;
  117. String insert(int p_at_pos, const String &p_string) const;
  118. String pad_decimals(int p_digits) const;
  119. String pad_zeros(int p_digits) const;
  120. String lpad(int min_length, const String &character = " ") const;
  121. String rpad(int min_length, const String &character = " ") const;
  122. String sprintf(const Array &values, bool *error) const;
  123. String quote(String quotechar = "\"") const;
  124. String unquote() const;
  125. static String num(double p_num, int p_decimals = -1);
  126. static String num_scientific(double p_num);
  127. static String num_real(double p_num);
  128. static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false);
  129. static String num_uint64(uint64_t p_num, int base = 10, bool capitalize_hex = false);
  130. static String chr(CharType p_char);
  131. static String md5(const uint8_t *p_md5);
  132. static String hex_encode_buffer(const uint8_t *p_buffer, int p_len);
  133. bool is_numeric() const;
  134. double to_double() const;
  135. float to_float() const;
  136. int hex_to_int(bool p_with_prefix = true) const;
  137. int to_int() const;
  138. int64_t hex_to_int64(bool p_with_prefix = true) const;
  139. int64_t to_int64() const;
  140. static int to_int(const char *p_str, int p_len = -1);
  141. static double to_double(const char *p_str);
  142. static double to_double(const CharType *p_str, const CharType **r_end = NULL);
  143. static int64_t to_int(const CharType *p_str, int p_len = -1);
  144. String capitalize() const;
  145. String camelcase_to_underscore(bool lowercase = true) const;
  146. int get_slice_count(String p_splitter) const;
  147. String get_slice(String p_splitter, int p_slice) const;
  148. String get_slicec(CharType p_splitter, int p_slice) const;
  149. Vector<String> split(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const;
  150. Vector<String> split_spaces() const;
  151. Vector<float> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
  152. Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
  153. Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const;
  154. Vector<int> split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
  155. String join(Vector<String> parts);
  156. static CharType char_uppercase(CharType p_char);
  157. static CharType char_lowercase(CharType p_char);
  158. String to_upper() const;
  159. String to_lower() const;
  160. String left(int p_pos) const;
  161. String right(int p_pos) const;
  162. String dedent() const;
  163. String strip_edges(bool left = true, bool right = true) const;
  164. String strip_escapes() const;
  165. String get_extension() const;
  166. String get_basename() const;
  167. String plus_file(const String &p_file) const;
  168. CharType ord_at(int p_idx) const;
  169. void erase(int p_pos, int p_chars);
  170. CharString ascii(bool p_allow_extended = false) const;
  171. CharString utf8() const;
  172. bool parse_utf8(const char *p_utf8, int p_len = -1); //return true on error
  173. static String utf8(const char *p_utf8, int p_len = -1);
  174. static uint32_t hash(const CharType *p_cstr, int p_len); /* hash the string */
  175. static uint32_t hash(const CharType *p_cstr); /* hash the string */
  176. static uint32_t hash(const char *p_cstr, int p_len); /* hash the string */
  177. static uint32_t hash(const char *p_cstr); /* hash the string */
  178. uint32_t hash() const; /* hash the string */
  179. uint64_t hash64() const; /* hash the string */
  180. String md5_text() const;
  181. String sha256_text() const;
  182. Vector<uint8_t> md5_buffer() const;
  183. Vector<uint8_t> sha256_buffer() const;
  184. _FORCE_INLINE_ bool empty() const { return length() == 0; }
  185. // path functions
  186. bool is_abs_path() const;
  187. bool is_rel_path() const;
  188. bool is_resource_file() const;
  189. String path_to(const String &p_path) const;
  190. String path_to_file(const String &p_path) const;
  191. String get_base_dir() const;
  192. String get_file() const;
  193. static String humanize_size(size_t p_size);
  194. String simplify_path() const;
  195. String xml_escape(bool p_escape_quotes = false) const;
  196. String xml_unescape() const;
  197. String http_escape() const;
  198. String http_unescape() const;
  199. String c_escape() const;
  200. String c_escape_multiline() const;
  201. String c_unescape() const;
  202. String json_escape() const;
  203. String word_wrap(int p_chars_per_line) const;
  204. String percent_encode() const;
  205. String percent_decode() const;
  206. bool is_valid_identifier() const;
  207. bool is_valid_integer() const;
  208. bool is_valid_float() const;
  209. bool is_valid_hex_number(bool p_with_prefix) const;
  210. bool is_valid_html_color() const;
  211. bool is_valid_ip_address() const;
  212. /**
  213. * The constructors must not depend on other overloads
  214. */
  215. /* String(CharType p_char);*/
  216. inline String() {}
  217. inline String(const String &p_str) :
  218. Vector(p_str) {}
  219. String(const char *p_str);
  220. String(const CharType *p_str, int p_clip_to_len = -1);
  221. String(const StrRange &p_range);
  222. };
  223. bool operator==(const char *p_chr, const String &p_str);
  224. String operator+(const char *p_chr, const String &p_str);
  225. String operator+(CharType p_chr, const String &p_str);
  226. String itos(int64_t p_val);
  227. String rtos(double p_val);
  228. String rtoss(double p_val); //scientific version
  229. struct NoCaseComparator {
  230. bool operator()(const String &p_a, const String &p_b) const {
  231. return p_a.nocasecmp_to(p_b) < 0;
  232. }
  233. };
  234. struct NaturalNoCaseComparator {
  235. bool operator()(const String &p_a, const String &p_b) const {
  236. return p_a.naturalnocasecmp_to(p_b) < 0;
  237. }
  238. };
  239. template <typename L, typename R>
  240. _FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) {
  241. while (true) {
  242. if (*l_ptr == 0 && *r_ptr == 0)
  243. return false;
  244. else if (*l_ptr == 0)
  245. return true;
  246. else if (*r_ptr == 0)
  247. return false;
  248. else if (*l_ptr < *r_ptr)
  249. return true;
  250. else if (*l_ptr > *r_ptr)
  251. return false;
  252. l_ptr++;
  253. r_ptr++;
  254. }
  255. CRASH_COND(true); // unreachable
  256. }
  257. /* end of namespace */
  258. //tool translate
  259. #ifdef TOOLS_ENABLED
  260. String TTR(const String &);
  261. #else
  262. #define TTR(m_val) (String())
  263. #endif
  264. //tool or regular translate
  265. String RTR(const String &);
  266. bool is_symbol(CharType c);
  267. bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end);
  268. #endif