ustring.h 17 KB

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