ustring.h 14 KB

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