ustring.h 16 KB

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