ustring.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 USTRING_H
  31. #define USTRING_H
  32. #include "core/array.h"
  33. #include "core/cowdata.h"
  34. #include "core/typedefs.h"
  35. #include "core/vector.h"
  36. /**
  37. @author Juan Linietsky <reduzio@gmail.com>
  38. */
  39. template <class T>
  40. class CharProxy {
  41. friend class CharString;
  42. friend class String;
  43. const int _index;
  44. CowData<T> &_cowdata;
  45. _FORCE_INLINE_ CharProxy(const int &p_index, CowData<T> &cowdata) :
  46. _index(p_index),
  47. _cowdata(cowdata) {}
  48. public:
  49. _FORCE_INLINE_ operator T() const {
  50. return _cowdata.get(_index);
  51. }
  52. _FORCE_INLINE_ const T *operator&() const {
  53. return _cowdata.ptr() + _index;
  54. }
  55. _FORCE_INLINE_ void operator=(const T &other) const {
  56. _cowdata.set(_index, other);
  57. }
  58. _FORCE_INLINE_ void operator=(const CharProxy<T> &other) const {
  59. _cowdata.set(_index, other.operator T());
  60. }
  61. };
  62. class CharString {
  63. CowData<char> _cowdata;
  64. public:
  65. _FORCE_INLINE_ char *ptrw() { return _cowdata.ptrw(); }
  66. _FORCE_INLINE_ const char *ptr() const { return _cowdata.ptr(); }
  67. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  68. Error resize(int p_size) { return _cowdata.resize(p_size); }
  69. _FORCE_INLINE_ char get(int p_index) { return _cowdata.get(p_index); }
  70. _FORCE_INLINE_ const char get(int p_index) const { return _cowdata.get(p_index); }
  71. _FORCE_INLINE_ void set(int p_index, const char &p_elem) { _cowdata.set(p_index, p_elem); }
  72. _FORCE_INLINE_ const char &operator[](int p_index) const { return _cowdata.get(p_index); }
  73. _FORCE_INLINE_ CharProxy<char> operator[](int p_index) { return CharProxy<char>(p_index, _cowdata); }
  74. _FORCE_INLINE_ CharString() {}
  75. _FORCE_INLINE_ CharString(const CharString &p_str) { _cowdata._ref(p_str._cowdata); }
  76. bool operator<(const CharString &p_right) const;
  77. CharString &operator+=(char p_char);
  78. int length() const { return size() ? size() - 1 : 0; }
  79. const char *get_data() const;
  80. operator const char *() const { return get_data(); };
  81. };
  82. typedef wchar_t CharType;
  83. struct StrRange {
  84. const CharType *c_str;
  85. int len;
  86. StrRange(const CharType *p_c_str = NULL, int p_len = 0) {
  87. c_str = p_c_str;
  88. len = p_len;
  89. }
  90. };
  91. class String {
  92. CowData<CharType> _cowdata;
  93. void copy_from(const char *p_cstr);
  94. void copy_from(const CharType *p_cstr, const int p_clip_to = -1);
  95. void copy_from(const CharType &p_char);
  96. void copy_from_unchecked(const CharType *p_char, const int p_length);
  97. bool _base_is_subsequence_of(const String &p_string, bool case_insensitive) const;
  98. public:
  99. enum {
  100. npos = -1 ///<for "some" compatibility with std::string (npos is a huge value in std::string)
  101. };
  102. _FORCE_INLINE_ CharType *ptrw() { return _cowdata.ptrw(); }
  103. _FORCE_INLINE_ const CharType *ptr() const { return _cowdata.ptr(); }
  104. void remove(int p_index) { _cowdata.remove(p_index); }
  105. _FORCE_INLINE_ void clear() { resize(0); }
  106. _FORCE_INLINE_ CharType get(int p_index) { return _cowdata.get(p_index); }
  107. _FORCE_INLINE_ const CharType get(int p_index) const { return _cowdata.get(p_index); }
  108. _FORCE_INLINE_ void set(int p_index, const CharType &p_elem) { _cowdata.set(p_index, p_elem); }
  109. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  110. Error resize(int p_size) { return _cowdata.resize(p_size); }
  111. _FORCE_INLINE_ const CharType &operator[](int p_index) const { return _cowdata.get(p_index); }
  112. _FORCE_INLINE_ CharProxy<CharType> operator[](int p_index) { return CharProxy<CharType>(p_index, _cowdata); }
  113. bool operator==(const String &p_str) const;
  114. bool operator!=(const String &p_str) const;
  115. String operator+(const String &p_str) const;
  116. //String operator+(CharType p_char) const;
  117. String &operator+=(const String &);
  118. String &operator+=(CharType p_char);
  119. String &operator+=(const char *p_str);
  120. String &operator+=(const CharType *p_str);
  121. /* Compatibility Operators */
  122. void operator=(const char *p_str);
  123. void operator=(const CharType *p_str);
  124. bool operator==(const char *p_str) const;
  125. bool operator==(const CharType *p_str) const;
  126. bool operator==(const StrRange &p_str_range) const;
  127. bool operator!=(const char *p_str) const;
  128. bool operator!=(const CharType *p_str) const;
  129. bool operator<(const CharType *p_str) const;
  130. bool operator<(const char *p_str) const;
  131. bool operator<(const String &p_str) const;
  132. bool operator<=(const String &p_str) const;
  133. signed char casecmp_to(const String &p_str) const;
  134. signed char nocasecmp_to(const String &p_str) const;
  135. signed char naturalnocasecmp_to(const String &p_str) const;
  136. const CharType *c_str() const;
  137. /* standard size stuff */
  138. _FORCE_INLINE_ int length() const {
  139. int s = size();
  140. return s ? (s - 1) : 0; // length does not include zero
  141. }
  142. /* complex helpers */
  143. String substr(int p_from, int p_chars) const;
  144. int find(const String &p_str, int p_from = 0) const; ///< return <0 if failed
  145. int find(const char *p_str, int p_from) const; ///< return <0 if failed
  146. int find_last(const String &p_str) const; ///< return <0 if failed
  147. int findn(const String &p_str, int p_from = 0) const; ///< return <0 if failed, case insensitive
  148. int rfind(const String &p_str, int p_from = -1) const; ///< return <0 if failed
  149. int rfindn(const String &p_str, int p_from = -1) const; ///< return <0 if failed, case insensitive
  150. int findmk(const Vector<String> &p_keys, int p_from = 0, int *r_key = NULL) const; ///< return <0 if failed
  151. bool match(const String &p_wildcard) const;
  152. bool matchn(const String &p_wildcard) const;
  153. bool begins_with(const String &p_string) const;
  154. bool begins_with(const char *p_string) const;
  155. bool ends_with(const String &p_string) const;
  156. bool is_enclosed_in(const String &p_string) const;
  157. bool is_subsequence_of(const String &p_string) const;
  158. bool is_subsequence_ofi(const String &p_string) const;
  159. bool is_quoted() const;
  160. Vector<String> bigrams() const;
  161. float similarity(const String &p_string) const;
  162. String format(const Variant &values, String placeholder = "{_}") const;
  163. String replace_first(const String &p_key, const String &p_with) const;
  164. String replace(const String &p_key, const String &p_with) const;
  165. String replace(const char *p_key, const char *p_with) const;
  166. String replacen(const String &p_key, const String &p_with) const;
  167. String insert(int p_at_pos, const String &p_string) const;
  168. String pad_decimals(int p_digits) const;
  169. String pad_zeros(int p_digits) const;
  170. String trim_prefix(const String &p_prefix) const;
  171. String trim_suffix(const String &p_suffix) const;
  172. String lpad(int min_length, const String &character = " ") const;
  173. String rpad(int min_length, const String &character = " ") const;
  174. String sprintf(const Array &values, bool *error) const;
  175. String quote(String quotechar = "\"") const;
  176. String unquote() const;
  177. static String num(double p_num, int p_decimals = -1);
  178. static String num_scientific(double p_num);
  179. static String num_real(double p_num);
  180. static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false);
  181. static String num_uint64(uint64_t p_num, int base = 10, bool capitalize_hex = false);
  182. static String chr(CharType p_char);
  183. static String md5(const uint8_t *p_md5);
  184. static String hex_encode_buffer(const uint8_t *p_buffer, int p_len);
  185. bool is_numeric() const;
  186. double to_double() const;
  187. float to_float() const;
  188. int hex_to_int(bool p_with_prefix = true) const;
  189. int to_int() const;
  190. int64_t hex_to_int64(bool p_with_prefix = true) const;
  191. int64_t to_int64() const;
  192. static int to_int(const char *p_str, int p_len = -1);
  193. static double to_double(const char *p_str);
  194. static double to_double(const CharType *p_str, const CharType **r_end = NULL);
  195. static int64_t to_int(const CharType *p_str, int p_len = -1);
  196. String capitalize() const;
  197. String camelcase_to_underscore(bool lowercase = true) const;
  198. int get_slice_count(String p_splitter) const;
  199. String get_slice(String p_splitter, int p_slice) const;
  200. String get_slicec(CharType p_splitter, int p_slice) const;
  201. Vector<String> split(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const;
  202. Vector<String> rsplit(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const;
  203. Vector<String> split_spaces() const;
  204. Vector<float> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
  205. Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
  206. Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const;
  207. Vector<int> split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
  208. String join(Vector<String> parts);
  209. static CharType char_uppercase(CharType p_char);
  210. static CharType char_lowercase(CharType p_char);
  211. String to_upper() const;
  212. String to_lower() const;
  213. String left(int p_pos) const;
  214. String right(int p_pos) const;
  215. String dedent() const;
  216. String strip_edges(bool left = true, bool right = true) const;
  217. String strip_escapes() const;
  218. String lstrip(const String &p_chars) const;
  219. String rstrip(const String &p_chars) const;
  220. String get_extension() const;
  221. String get_basename() const;
  222. String plus_file(const String &p_file) const;
  223. CharType ord_at(int p_idx) const;
  224. void erase(int p_pos, int p_chars);
  225. CharString ascii(bool p_allow_extended = false) const;
  226. CharString utf8() const;
  227. bool parse_utf8(const char *p_utf8, int p_len = -1); //return true on error
  228. static String utf8(const char *p_utf8, int p_len = -1);
  229. static uint32_t hash(const CharType *p_cstr, int p_len); /* hash the string */
  230. static uint32_t hash(const CharType *p_cstr); /* hash the string */
  231. static uint32_t hash(const char *p_cstr, int p_len); /* hash the string */
  232. static uint32_t hash(const char *p_cstr); /* hash the string */
  233. uint32_t hash() const; /* hash the string */
  234. uint64_t hash64() const; /* hash the string */
  235. String md5_text() const;
  236. String sha256_text() const;
  237. Vector<uint8_t> md5_buffer() const;
  238. Vector<uint8_t> sha256_buffer() const;
  239. _FORCE_INLINE_ bool empty() const { return length() == 0; }
  240. // path functions
  241. bool is_abs_path() const;
  242. bool is_rel_path() const;
  243. bool is_resource_file() const;
  244. String path_to(const String &p_path) const;
  245. String path_to_file(const String &p_path) const;
  246. String get_base_dir() const;
  247. String get_file() const;
  248. static String humanize_size(size_t p_size);
  249. String simplify_path() const;
  250. String xml_escape(bool p_escape_quotes = false) const;
  251. String xml_unescape() const;
  252. String http_escape() const;
  253. String http_unescape() const;
  254. String c_escape() const;
  255. String c_escape_multiline() const;
  256. String c_unescape() const;
  257. String json_escape() const;
  258. String word_wrap(int p_chars_per_line) const;
  259. String percent_encode() const;
  260. String percent_decode() const;
  261. bool is_valid_identifier() const;
  262. bool is_valid_integer() const;
  263. bool is_valid_float() const;
  264. bool is_valid_hex_number(bool p_with_prefix) const;
  265. bool is_valid_html_color() const;
  266. bool is_valid_ip_address() const;
  267. /**
  268. * The constructors must not depend on other overloads
  269. */
  270. /* String(CharType p_char);*/
  271. _FORCE_INLINE_ String() {}
  272. _FORCE_INLINE_ String(const String &p_str) { _cowdata._ref(p_str._cowdata); }
  273. String(const char *p_str);
  274. String(const CharType *p_str, int p_clip_to_len = -1);
  275. String(const StrRange &p_range);
  276. };
  277. bool operator==(const char *p_chr, const String &p_str);
  278. String operator+(const char *p_chr, const String &p_str);
  279. String operator+(CharType p_chr, const String &p_str);
  280. String itos(int64_t p_val);
  281. String rtos(double p_val);
  282. String rtoss(double p_val); //scientific version
  283. struct NoCaseComparator {
  284. bool operator()(const String &p_a, const String &p_b) const {
  285. return p_a.nocasecmp_to(p_b) < 0;
  286. }
  287. };
  288. struct NaturalNoCaseComparator {
  289. bool operator()(const String &p_a, const String &p_b) const {
  290. return p_a.naturalnocasecmp_to(p_b) < 0;
  291. }
  292. };
  293. template <typename L, typename R>
  294. _FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) {
  295. while (true) {
  296. if (*l_ptr == 0 && *r_ptr == 0)
  297. return false;
  298. else if (*l_ptr == 0)
  299. return true;
  300. else if (*r_ptr == 0)
  301. return false;
  302. else if (*l_ptr < *r_ptr)
  303. return true;
  304. else if (*l_ptr > *r_ptr)
  305. return false;
  306. l_ptr++;
  307. r_ptr++;
  308. }
  309. CRASH_COND(true); // unreachable
  310. }
  311. /* end of namespace */
  312. //tool translate
  313. #ifdef TOOLS_ENABLED
  314. String TTR(const String &);
  315. #else
  316. #define TTR(m_val) (String())
  317. #endif
  318. //tool or regular translate
  319. String RTR(const String &);
  320. bool is_symbol(CharType c);
  321. bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end);
  322. #endif // USTRING_H