ustring.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*************************************************************************/
  2. /* ustring.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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<(String p_str) const;
  82. bool operator<=(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. int length() const;
  89. /* complex helpers */
  90. String substr(int p_from, int p_chars) const;
  91. int find(String p_str, int p_from = 0) const; ///< return <0 if failed
  92. int find_last(String p_str) const; ///< return <0 if failed
  93. int findn(String p_str, int p_from = 0) const; ///< return <0 if failed, case insensitive
  94. int rfind(String p_str, int p_from = -1) const; ///< return <0 if failed
  95. int rfindn(String p_str, int p_from = -1) const; ///< return <0 if failed, case insensitive
  96. int findmk(const Vector<String> &p_keys, int p_from = 0, int *r_key = NULL) const; ///< return <0 if failed
  97. bool match(const String &p_wildcard) const;
  98. bool matchn(const String &p_wildcard) const;
  99. bool begins_with(const String &p_string) const;
  100. bool begins_with(const char *p_string) const;
  101. bool ends_with(const String &p_string) const;
  102. bool is_enclosed_in(const String &p_string) const;
  103. bool is_subsequence_of(const String &p_string) const;
  104. bool is_subsequence_ofi(const String &p_string) const;
  105. bool is_quoted() const;
  106. Vector<String> bigrams() const;
  107. float similarity(const String &p_string) const;
  108. String format(const Variant &values, String placeholder = "{_}") const;
  109. String replace_first(String p_key, String p_with) const;
  110. String replace(String p_key, String p_with) const;
  111. String replacen(String p_key, String p_with) const;
  112. String insert(int p_at_pos, String p_string) const;
  113. String pad_decimals(int p_digits) const;
  114. String pad_zeros(int p_digits) const;
  115. String lpad(int min_length, const String &character = " ") const;
  116. String rpad(int min_length, const String &character = " ") const;
  117. String sprintf(const Array &values, bool *error) const;
  118. String quote(String quotechar = "\"") const;
  119. String unquote() const;
  120. static String num(double p_num, int p_decimals = -1);
  121. static String num_scientific(double p_num);
  122. static String num_real(double p_num);
  123. static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false);
  124. static String chr(CharType p_char);
  125. static String md5(const uint8_t *p_md5);
  126. static String hex_encode_buffer(const uint8_t *p_buffer, int p_len);
  127. bool is_numeric() const;
  128. double to_double() const;
  129. float to_float() const;
  130. int hex_to_int(bool p_with_prefix = true) const;
  131. int to_int() const;
  132. int64_t hex_to_int64(bool p_with_prefix = true) const;
  133. int64_t to_int64() const;
  134. static int to_int(const char *p_str, int p_len = -1);
  135. static double to_double(const char *p_str);
  136. static double to_double(const CharType *p_str, const CharType **r_end = NULL);
  137. static int64_t to_int(const CharType *p_str, int p_len = -1);
  138. String capitalize() const;
  139. String camelcase_to_underscore(bool lowercase = true) const;
  140. int get_slice_count(String p_splitter) const;
  141. String get_slice(String p_splitter, int p_slice) const;
  142. String get_slicec(CharType p_splitter, int p_slice) const;
  143. Vector<String> split(const String &p_splitter, bool p_allow_empty = true) const;
  144. Vector<String> split_spaces() const;
  145. Vector<float> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
  146. Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
  147. Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const;
  148. Vector<int> split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
  149. static CharType char_uppercase(CharType p_char);
  150. static CharType char_lowercase(CharType p_char);
  151. String to_upper() const;
  152. String to_lower() const;
  153. String left(int p_pos) const;
  154. String right(int p_pos) const;
  155. String dedent() const;
  156. String strip_edges(bool left = true, bool right = true) const;
  157. String strip_escapes() const;
  158. String get_extension() const;
  159. String get_basename() const;
  160. String plus_file(const String &p_file) const;
  161. CharType ord_at(int p_idx) const;
  162. void erase(int p_pos, int p_chars);
  163. CharString ascii(bool p_allow_extended = false) const;
  164. CharString utf8() const;
  165. bool parse_utf8(const char *p_utf8, int p_len = -1); //return true on error
  166. static String utf8(const char *p_utf8, int p_len = -1);
  167. static uint32_t hash(const CharType *p_cstr, int p_len); /* hash the string */
  168. static uint32_t hash(const CharType *p_cstr); /* hash the string */
  169. static uint32_t hash(const char *p_cstr, int p_len); /* hash the string */
  170. static uint32_t hash(const char *p_cstr); /* hash the string */
  171. uint32_t hash() const; /* hash the string */
  172. uint64_t hash64() const; /* hash the string */
  173. String md5_text() const;
  174. String sha256_text() const;
  175. Vector<uint8_t> md5_buffer() const;
  176. Vector<uint8_t> sha256_buffer() const;
  177. inline bool empty() const { return length() == 0; }
  178. // path functions
  179. bool is_abs_path() const;
  180. bool is_rel_path() const;
  181. bool is_resource_file() const;
  182. String path_to(const String &p_path) const;
  183. String path_to_file(const String &p_path) const;
  184. String get_base_dir() const;
  185. String get_file() const;
  186. static String humanize_size(size_t p_size);
  187. String simplify_path() const;
  188. String xml_escape(bool p_escape_quotes = false) const;
  189. String xml_unescape() const;
  190. String http_escape() const;
  191. String http_unescape() const;
  192. String c_escape() const;
  193. String c_escape_multiline() const;
  194. String c_unescape() const;
  195. String json_escape() const;
  196. String word_wrap(int p_chars_per_line) const;
  197. String percent_encode() const;
  198. String percent_decode() const;
  199. bool is_valid_identifier() const;
  200. bool is_valid_integer() const;
  201. bool is_valid_float() const;
  202. bool is_valid_hex_number(bool p_with_prefix) const;
  203. bool is_valid_html_color() const;
  204. bool is_valid_ip_address() const;
  205. /**
  206. * The constructors must not depend on other overloads
  207. */
  208. /* String(CharType p_char);*/
  209. inline String() {}
  210. String(const char *p_str);
  211. String(const CharType *p_str, int p_clip_to_len = -1);
  212. String(const StrRange &p_range);
  213. };
  214. bool operator==(const char *p_chr, const String &p_str);
  215. String operator+(const char *p_chr, const String &p_str);
  216. String operator+(CharType p_chr, const String &p_str);
  217. String itos(int64_t p_val);
  218. String rtos(double p_val);
  219. String rtoss(double p_val); //scientific version
  220. struct NoCaseComparator {
  221. bool operator()(const String &p_a, const String &p_b) const {
  222. return p_a.nocasecmp_to(p_b) < 0;
  223. }
  224. };
  225. struct NaturalNoCaseComparator {
  226. bool operator()(const String &p_a, const String &p_b) const {
  227. return p_a.naturalnocasecmp_to(p_b) < 0;
  228. }
  229. };
  230. /* end of namespace */
  231. //tool translate
  232. #ifdef TOOLS_ENABLED
  233. String TTR(const String &);
  234. #else
  235. #define TTR(m_val) (String())
  236. #endif
  237. //tool or regular translate
  238. String RTR(const String &);
  239. #endif