ustring.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. // Note: _GODOT suffix added to header guard to avoid conflict with ICU header.
  33. #include "core/string/char_utils.h"
  34. #include "core/templates/cowdata.h"
  35. #include "core/templates/vector.h"
  36. #include "core/typedefs.h"
  37. #include "core/variant/array.h"
  38. /*************************************************************************/
  39. /* CharProxy */
  40. /*************************************************************************/
  41. template <typename T>
  42. class CharProxy {
  43. friend class Char16String;
  44. friend class CharString;
  45. friend class String;
  46. const int _index;
  47. CowData<T> &_cowdata;
  48. static const T _null = 0;
  49. _FORCE_INLINE_ CharProxy(const int &p_index, CowData<T> &p_cowdata) :
  50. _index(p_index),
  51. _cowdata(p_cowdata) {}
  52. public:
  53. _FORCE_INLINE_ CharProxy(const CharProxy<T> &p_other) :
  54. _index(p_other._index),
  55. _cowdata(p_other._cowdata) {}
  56. _FORCE_INLINE_ operator T() const {
  57. if (unlikely(_index == _cowdata.size())) {
  58. return _null;
  59. }
  60. return _cowdata.get(_index);
  61. }
  62. _FORCE_INLINE_ const T *operator&() const {
  63. return _cowdata.ptr() + _index;
  64. }
  65. _FORCE_INLINE_ void operator=(const T &p_other) const {
  66. _cowdata.set(_index, p_other);
  67. }
  68. _FORCE_INLINE_ void operator=(const CharProxy<T> &p_other) const {
  69. _cowdata.set(_index, p_other.operator T());
  70. }
  71. };
  72. /*************************************************************************/
  73. /* Char16String */
  74. /*************************************************************************/
  75. class Char16String {
  76. CowData<char16_t> _cowdata;
  77. static const char16_t _null;
  78. public:
  79. _FORCE_INLINE_ char16_t *ptrw() { return _cowdata.ptrw(); }
  80. _FORCE_INLINE_ const char16_t *ptr() const { return _cowdata.ptr(); }
  81. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  82. Error resize(int p_size) { return _cowdata.resize(p_size); }
  83. _FORCE_INLINE_ char16_t get(int p_index) const { return _cowdata.get(p_index); }
  84. _FORCE_INLINE_ void set(int p_index, const char16_t &p_elem) { _cowdata.set(p_index, p_elem); }
  85. _FORCE_INLINE_ const char16_t &operator[](int p_index) const {
  86. if (unlikely(p_index == _cowdata.size())) {
  87. return _null;
  88. }
  89. return _cowdata.get(p_index);
  90. }
  91. _FORCE_INLINE_ CharProxy<char16_t> operator[](int p_index) { return CharProxy<char16_t>(p_index, _cowdata); }
  92. _FORCE_INLINE_ Char16String() {}
  93. _FORCE_INLINE_ Char16String(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); }
  94. _FORCE_INLINE_ void operator=(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); }
  95. _FORCE_INLINE_ Char16String(const char16_t *p_cstr) { copy_from(p_cstr); }
  96. void operator=(const char16_t *p_cstr);
  97. bool operator<(const Char16String &p_right) const;
  98. Char16String &operator+=(char16_t p_char);
  99. int length() const { return size() ? size() - 1 : 0; }
  100. const char16_t *get_data() const;
  101. operator const char16_t *() const { return get_data(); }
  102. protected:
  103. void copy_from(const char16_t *p_cstr);
  104. };
  105. /*************************************************************************/
  106. /* CharString */
  107. /*************************************************************************/
  108. class CharString {
  109. CowData<char> _cowdata;
  110. static const char _null;
  111. public:
  112. _FORCE_INLINE_ char *ptrw() { return _cowdata.ptrw(); }
  113. _FORCE_INLINE_ const char *ptr() const { return _cowdata.ptr(); }
  114. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  115. Error resize(int p_size) { return _cowdata.resize(p_size); }
  116. _FORCE_INLINE_ char get(int p_index) const { return _cowdata.get(p_index); }
  117. _FORCE_INLINE_ void set(int p_index, const char &p_elem) { _cowdata.set(p_index, p_elem); }
  118. _FORCE_INLINE_ const char &operator[](int p_index) const {
  119. if (unlikely(p_index == _cowdata.size())) {
  120. return _null;
  121. }
  122. return _cowdata.get(p_index);
  123. }
  124. _FORCE_INLINE_ CharProxy<char> operator[](int p_index) { return CharProxy<char>(p_index, _cowdata); }
  125. _FORCE_INLINE_ CharString() {}
  126. _FORCE_INLINE_ CharString(const CharString &p_str) { _cowdata._ref(p_str._cowdata); }
  127. _FORCE_INLINE_ void operator=(const CharString &p_str) { _cowdata._ref(p_str._cowdata); }
  128. _FORCE_INLINE_ CharString(const char *p_cstr) { copy_from(p_cstr); }
  129. void operator=(const char *p_cstr);
  130. bool operator<(const CharString &p_right) const;
  131. bool operator==(const CharString &p_right) const;
  132. CharString &operator+=(char p_char);
  133. int length() const { return size() ? size() - 1 : 0; }
  134. const char *get_data() const;
  135. operator const char *() const { return get_data(); }
  136. protected:
  137. void copy_from(const char *p_cstr);
  138. };
  139. /*************************************************************************/
  140. /* String */
  141. /*************************************************************************/
  142. struct StrRange {
  143. const char32_t *c_str;
  144. int len;
  145. StrRange(const char32_t *p_c_str = nullptr, int p_len = 0) {
  146. c_str = p_c_str;
  147. len = p_len;
  148. }
  149. };
  150. class String {
  151. CowData<char32_t> _cowdata;
  152. static const char32_t _null;
  153. static const char32_t _replacement_char;
  154. void copy_from(const char *p_cstr);
  155. void copy_from(const char *p_cstr, const int p_clip_to);
  156. void copy_from(const wchar_t *p_cstr);
  157. void copy_from(const wchar_t *p_cstr, const int p_clip_to);
  158. void copy_from(const char32_t *p_cstr);
  159. void copy_from(const char32_t *p_cstr, const int p_clip_to);
  160. void copy_from(const char32_t &p_char);
  161. void copy_from_unchecked(const char32_t *p_char, const int p_length);
  162. bool _base_is_subsequence_of(const String &p_string, bool case_insensitive) const;
  163. int _count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const;
  164. int _count(const char *p_string, int p_from, int p_to, bool p_case_insensitive) const;
  165. String _camelcase_to_underscore() const;
  166. public:
  167. enum {
  168. npos = -1 ///<for "some" compatibility with std::string (npos is a huge value in std::string)
  169. };
  170. _FORCE_INLINE_ char32_t *ptrw() { return _cowdata.ptrw(); }
  171. _FORCE_INLINE_ const char32_t *ptr() const { return _cowdata.ptr(); }
  172. void remove_at(int p_index) { _cowdata.remove_at(p_index); }
  173. _FORCE_INLINE_ void clear() { resize(0); }
  174. _FORCE_INLINE_ char32_t get(int p_index) const { return _cowdata.get(p_index); }
  175. _FORCE_INLINE_ void set(int p_index, const char32_t &p_elem) { _cowdata.set(p_index, p_elem); }
  176. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  177. Error resize(int p_size) { return _cowdata.resize(p_size); }
  178. _FORCE_INLINE_ const char32_t &operator[](int p_index) const {
  179. if (unlikely(p_index == _cowdata.size())) {
  180. return _null;
  181. }
  182. return _cowdata.get(p_index);
  183. }
  184. _FORCE_INLINE_ CharProxy<char32_t> operator[](int p_index) { return CharProxy<char32_t>(p_index, _cowdata); }
  185. bool operator==(const String &p_str) const;
  186. bool operator!=(const String &p_str) const;
  187. String operator+(const String &p_str) const;
  188. String operator+(char32_t p_char) const;
  189. String &operator+=(const String &);
  190. String &operator+=(char32_t p_char);
  191. String &operator+=(const char *p_str);
  192. String &operator+=(const wchar_t *p_str);
  193. String &operator+=(const char32_t *p_str);
  194. /* Compatibility Operators */
  195. void operator=(const char *p_str);
  196. void operator=(const wchar_t *p_str);
  197. void operator=(const char32_t *p_str);
  198. bool operator==(const char *p_str) const;
  199. bool operator==(const wchar_t *p_str) const;
  200. bool operator==(const char32_t *p_str) const;
  201. bool operator==(const StrRange &p_str_range) const;
  202. bool operator!=(const char *p_str) const;
  203. bool operator!=(const wchar_t *p_str) const;
  204. bool operator!=(const char32_t *p_str) const;
  205. bool operator<(const char32_t *p_str) const;
  206. bool operator<(const char *p_str) const;
  207. bool operator<(const wchar_t *p_str) const;
  208. bool operator<(const String &p_str) const;
  209. bool operator<=(const String &p_str) const;
  210. bool operator>(const String &p_str) const;
  211. bool operator>=(const String &p_str) const;
  212. signed char casecmp_to(const String &p_str) const;
  213. signed char nocasecmp_to(const String &p_str) const;
  214. signed char naturalcasecmp_to(const String &p_str) const;
  215. signed char naturalnocasecmp_to(const String &p_str) const;
  216. // Special sorting for file names. Names starting with `_` are put before all others except those starting with `.`, otherwise natural comparison is used.
  217. signed char filecasecmp_to(const String &p_str) const;
  218. signed char filenocasecmp_to(const String &p_str) const;
  219. const char32_t *get_data() const;
  220. /* standard size stuff */
  221. _FORCE_INLINE_ int length() const {
  222. int s = size();
  223. return s ? (s - 1) : 0; // length does not include zero
  224. }
  225. bool is_valid_string() const;
  226. /* debug, error messages */
  227. void print_unicode_error(const String &p_message, bool p_critical = false) const;
  228. /* complex helpers */
  229. String substr(int p_from, int p_chars = -1) const;
  230. int find(const String &p_str, int p_from = 0) const; ///< return <0 if failed
  231. int find(const char *p_str, int p_from = 0) const; ///< return <0 if failed
  232. int find_char(char32_t p_char, int p_from = 0) const; ///< return <0 if failed
  233. int findn(const String &p_str, int p_from = 0) const; ///< return <0 if failed, case insensitive
  234. int findn(const char *p_str, int p_from = 0) const; ///< return <0 if failed
  235. int rfind(const String &p_str, int p_from = -1) const; ///< return <0 if failed
  236. int rfind(const char *p_str, int p_from = -1) const; ///< return <0 if failed
  237. int rfind_char(char32_t p_char, int p_from = -1) const; ///< return <0 if failed
  238. int rfindn(const String &p_str, int p_from = -1) const; ///< return <0 if failed, case insensitive
  239. int rfindn(const char *p_str, int p_from = -1) const; ///< return <0 if failed
  240. int findmk(const Vector<String> &p_keys, int p_from = 0, int *r_key = nullptr) const; ///< return <0 if failed
  241. bool match(const String &p_wildcard) const;
  242. bool matchn(const String &p_wildcard) const;
  243. bool begins_with(const String &p_string) const;
  244. bool begins_with(const char *p_string) const;
  245. bool ends_with(const String &p_string) const;
  246. bool ends_with(const char *p_string) const;
  247. bool is_enclosed_in(const String &p_string) const;
  248. bool is_subsequence_of(const String &p_string) const;
  249. bool is_subsequence_ofn(const String &p_string) const;
  250. bool is_quoted() const;
  251. bool is_lowercase() const;
  252. Vector<String> bigrams() const;
  253. float similarity(const String &p_string) const;
  254. String format(const Variant &values, const String &placeholder = "{_}") const;
  255. String replace_first(const String &p_key, const String &p_with) const;
  256. String replace_first(const char *p_key, const char *p_with) const;
  257. String replace(const String &p_key, const String &p_with) const;
  258. String replace(const char *p_key, const char *p_with) const;
  259. String replacen(const String &p_key, const String &p_with) const;
  260. String replacen(const char *p_key, const char *p_with) const;
  261. String repeat(int p_count) const;
  262. String reverse() const;
  263. String insert(int p_at_pos, const String &p_string) const;
  264. String erase(int p_pos, int p_chars = 1) const;
  265. String pad_decimals(int p_digits) const;
  266. String pad_zeros(int p_digits) const;
  267. String trim_prefix(const String &p_prefix) const;
  268. String trim_prefix(const char *p_prefix) const;
  269. String trim_suffix(const String &p_suffix) const;
  270. String trim_suffix(const char *p_suffix) const;
  271. String lpad(int min_length, const String &character = " ") const;
  272. String rpad(int min_length, const String &character = " ") const;
  273. String sprintf(const Array &values, bool *error) const;
  274. String quote(const String &quotechar = "\"") const;
  275. String unquote() const;
  276. static String num(double p_num, int p_decimals = -1);
  277. static String num_scientific(double p_num);
  278. static String num_real(double p_num, bool p_trailing = true);
  279. static String num_real(float p_num, bool p_trailing = true);
  280. static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false);
  281. static String num_uint64(uint64_t p_num, int base = 10, bool capitalize_hex = false);
  282. static String chr(char32_t p_char);
  283. static String md5(const uint8_t *p_md5);
  284. static String hex_encode_buffer(const uint8_t *p_buffer, int p_len);
  285. Vector<uint8_t> hex_decode() const;
  286. bool is_numeric() const;
  287. double to_float() const;
  288. int64_t hex_to_int() const;
  289. int64_t bin_to_int() const;
  290. int64_t to_int() const;
  291. static int64_t to_int(const char *p_str, int p_len = -1);
  292. static int64_t to_int(const wchar_t *p_str, int p_len = -1);
  293. static int64_t to_int(const char32_t *p_str, int p_len = -1, bool p_clamp = false);
  294. static double to_float(const char *p_str);
  295. static double to_float(const wchar_t *p_str, const wchar_t **r_end = nullptr);
  296. static double to_float(const char32_t *p_str, const char32_t **r_end = nullptr);
  297. static uint32_t num_characters(int64_t p_int);
  298. String capitalize() const;
  299. String to_camel_case() const;
  300. String to_pascal_case() const;
  301. String to_snake_case() const;
  302. String get_with_code_lines() const;
  303. int get_slice_count(const String &p_splitter) const;
  304. int get_slice_count(const char *p_splitter) const;
  305. String get_slice(const String &p_splitter, int p_slice) const;
  306. String get_slice(const char *p_splitter, int p_slice) const;
  307. String get_slicec(char32_t p_splitter, int p_slice) const;
  308. Vector<String> split(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
  309. Vector<String> split(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
  310. Vector<String> rsplit(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
  311. Vector<String> rsplit(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
  312. Vector<String> split_spaces() const;
  313. Vector<double> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
  314. Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
  315. Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const;
  316. Vector<int> split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
  317. String join(const Vector<String> &parts) const;
  318. static char32_t char_uppercase(char32_t p_char);
  319. static char32_t char_lowercase(char32_t p_char);
  320. String to_upper() const;
  321. String to_lower() const;
  322. int count(const String &p_string, int p_from = 0, int p_to = 0) const;
  323. int count(const char *p_string, int p_from = 0, int p_to = 0) const;
  324. int countn(const String &p_string, int p_from = 0, int p_to = 0) const;
  325. int countn(const char *p_string, int p_from = 0, int p_to = 0) const;
  326. String left(int p_len) const;
  327. String right(int p_len) const;
  328. String indent(const String &p_prefix) const;
  329. String dedent() const;
  330. String strip_edges(bool left = true, bool right = true) const;
  331. String strip_escapes() const;
  332. String lstrip(const String &p_chars) const;
  333. String rstrip(const String &p_chars) const;
  334. String get_extension() const;
  335. String get_basename() const;
  336. String path_join(const String &p_file) const;
  337. char32_t unicode_at(int p_idx) const;
  338. CharString ascii(bool p_allow_extended = false) const;
  339. CharString utf8() const;
  340. Error parse_utf8(const char *p_utf8, int p_len = -1, bool p_skip_cr = false);
  341. static String utf8(const char *p_utf8, int p_len = -1);
  342. Char16String utf16() const;
  343. Error parse_utf16(const char16_t *p_utf16, int p_len = -1, bool p_default_little_endian = true);
  344. static String utf16(const char16_t *p_utf16, int p_len = -1);
  345. static uint32_t hash(const char32_t *p_cstr, int p_len); /* hash the string */
  346. static uint32_t hash(const char32_t *p_cstr); /* hash the string */
  347. static uint32_t hash(const wchar_t *p_cstr, int p_len); /* hash the string */
  348. static uint32_t hash(const wchar_t *p_cstr); /* hash the string */
  349. static uint32_t hash(const char *p_cstr, int p_len); /* hash the string */
  350. static uint32_t hash(const char *p_cstr); /* hash the string */
  351. uint32_t hash() const; /* hash the string */
  352. uint64_t hash64() const; /* hash the string */
  353. String md5_text() const;
  354. String sha1_text() const;
  355. String sha256_text() const;
  356. Vector<uint8_t> md5_buffer() const;
  357. Vector<uint8_t> sha1_buffer() const;
  358. Vector<uint8_t> sha256_buffer() const;
  359. _FORCE_INLINE_ bool is_empty() const { return length() == 0; }
  360. _FORCE_INLINE_ bool contains(const char *p_str) const { return find(p_str) != -1; }
  361. _FORCE_INLINE_ bool contains(const String &p_str) const { return find(p_str) != -1; }
  362. _FORCE_INLINE_ bool containsn(const char *p_str) const { return findn(p_str) != -1; }
  363. _FORCE_INLINE_ bool containsn(const String &p_str) const { return findn(p_str) != -1; }
  364. // path functions
  365. bool is_absolute_path() const;
  366. bool is_relative_path() const;
  367. bool is_resource_file() const;
  368. String path_to(const String &p_path) const;
  369. String path_to_file(const String &p_path) const;
  370. String get_base_dir() const;
  371. String get_file() const;
  372. static String humanize_size(uint64_t p_size);
  373. String simplify_path() const;
  374. bool is_network_share_path() const;
  375. String xml_escape(bool p_escape_quotes = false) const;
  376. String xml_unescape() const;
  377. String uri_encode() const;
  378. String uri_decode() const;
  379. String c_escape() const;
  380. String c_escape_multiline() const;
  381. String c_unescape() const;
  382. String json_escape() const;
  383. Error parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path, String &r_fragment) const;
  384. String property_name_encode() const;
  385. // node functions
  386. static String get_invalid_node_name_characters(bool p_allow_internal = false);
  387. String validate_node_name() const;
  388. String validate_ascii_identifier() const;
  389. String validate_unicode_identifier() const;
  390. String validate_filename() const;
  391. bool is_valid_ascii_identifier() const;
  392. bool is_valid_unicode_identifier() const;
  393. bool is_valid_int() const;
  394. bool is_valid_float() const;
  395. bool is_valid_hex_number(bool p_with_prefix) const;
  396. bool is_valid_html_color() const;
  397. bool is_valid_ip_address() const;
  398. bool is_valid_filename() const;
  399. // Use `is_valid_ascii_identifier()` instead. Kept for compatibility.
  400. bool is_valid_identifier() const { return is_valid_ascii_identifier(); }
  401. /**
  402. * The constructors must not depend on other overloads
  403. */
  404. _FORCE_INLINE_ String() {}
  405. _FORCE_INLINE_ String(const String &p_str) { _cowdata._ref(p_str._cowdata); }
  406. _FORCE_INLINE_ void operator=(const String &p_str) { _cowdata._ref(p_str._cowdata); }
  407. Vector<uint8_t> to_ascii_buffer() const;
  408. Vector<uint8_t> to_utf8_buffer() const;
  409. Vector<uint8_t> to_utf16_buffer() const;
  410. Vector<uint8_t> to_utf32_buffer() const;
  411. Vector<uint8_t> to_wchar_buffer() const;
  412. String(const char *p_str);
  413. String(const wchar_t *p_str);
  414. String(const char32_t *p_str);
  415. String(const char *p_str, int p_clip_to_len);
  416. String(const wchar_t *p_str, int p_clip_to_len);
  417. String(const char32_t *p_str, int p_clip_to_len);
  418. String(const StrRange &p_range);
  419. };
  420. bool operator==(const char *p_chr, const String &p_str);
  421. bool operator==(const wchar_t *p_chr, const String &p_str);
  422. bool operator!=(const char *p_chr, const String &p_str);
  423. bool operator!=(const wchar_t *p_chr, const String &p_str);
  424. String operator+(const char *p_chr, const String &p_str);
  425. String operator+(const wchar_t *p_chr, const String &p_str);
  426. String operator+(char32_t p_chr, const String &p_str);
  427. String itos(int64_t p_val);
  428. String uitos(uint64_t p_val);
  429. String rtos(double p_val);
  430. String rtoss(double p_val); //scientific version
  431. struct NoCaseComparator {
  432. bool operator()(const String &p_a, const String &p_b) const {
  433. return p_a.nocasecmp_to(p_b) < 0;
  434. }
  435. };
  436. struct NaturalNoCaseComparator {
  437. bool operator()(const String &p_a, const String &p_b) const {
  438. return p_a.naturalnocasecmp_to(p_b) < 0;
  439. }
  440. };
  441. struct FileNoCaseComparator {
  442. bool operator()(const String &p_a, const String &p_b) const {
  443. return p_a.filenocasecmp_to(p_b) < 0;
  444. }
  445. };
  446. template <typename L, typename R>
  447. _FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) {
  448. while (true) {
  449. const char32_t l = *l_ptr;
  450. const char32_t r = *r_ptr;
  451. if (l == 0 && r == 0) {
  452. return false;
  453. } else if (l == 0) {
  454. return true;
  455. } else if (r == 0) {
  456. return false;
  457. } else if (l < r) {
  458. return true;
  459. } else if (l > r) {
  460. return false;
  461. }
  462. l_ptr++;
  463. r_ptr++;
  464. }
  465. }
  466. /* end of namespace */
  467. // Tool translate (TTR and variants) for the editor UI,
  468. // and doc translate for the class reference (DTR).
  469. #ifdef TOOLS_ENABLED
  470. // Gets parsed.
  471. String TTR(const String &p_text, const String &p_context = "");
  472. String TTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "");
  473. String DTR(const String &p_text, const String &p_context = "");
  474. String DTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "");
  475. // Use for C strings.
  476. #define TTRC(m_value) (m_value)
  477. // Use to avoid parsing (for use later with C strings).
  478. #define TTRGET(m_value) TTR(m_value)
  479. #else
  480. #define TTRC(m_value) (m_value)
  481. #define TTRGET(m_value) (m_value)
  482. #endif
  483. // Use this to mark property names for editor translation.
  484. // Often for dynamic properties defined in _get_property_list().
  485. // Property names defined directly inside EDITOR_DEF, GLOBAL_DEF, and ADD_PROPERTY macros don't need this.
  486. #define PNAME(m_value) (m_value)
  487. // Similar to PNAME, but to mark groups, i.e. properties with PROPERTY_USAGE_GROUP.
  488. // Groups defined directly inside ADD_GROUP macros don't need this.
  489. // The arguments are the same as ADD_GROUP. m_prefix is only used for extraction.
  490. #define GNAME(m_value, m_prefix) (m_value)
  491. // Runtime translate for the public node API.
  492. String RTR(const String &p_text, const String &p_context = "");
  493. String RTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "");
  494. /**
  495. * "Extractable TRanslate". Used for strings that can appear inside an exported
  496. * project (such as the ones in nodes like `FileDialog`), which are made possible
  497. * to add in the POT generator. A translation context can optionally be specified
  498. * to disambiguate between identical source strings in translations.
  499. * When placeholders are desired, use vformat(ETR("Example: %s"), some_string)`.
  500. * If a string mentions a quantity (and may therefore need a dynamic plural form),
  501. * use `ETRN()` instead of `ETR()`.
  502. *
  503. * NOTE: This function is for string extraction only, and will just return the
  504. * string it was given. The translation itself should be done internally by nodes
  505. * with `atr()` instead.
  506. */
  507. _FORCE_INLINE_ String ETR(const String &p_text, const String &p_context = "") {
  508. return p_text;
  509. }
  510. /**
  511. * "Extractable TRanslate for N items". Used for strings that can appear inside an
  512. * exported project (such as the ones in nodes like `FileDialog`), which are made
  513. * possible to add in the POT generator. A translation context can optionally be
  514. * specified to disambiguate between identical source strings in translations.
  515. * Use `ETR()` if the string doesn't need dynamic plural form. When placeholders
  516. * are desired, use `vformat(ETRN("%d item", "%d items", some_integer), some_integer)`.
  517. * The placeholder must be present in both strings to avoid run-time warnings in `vformat()`.
  518. *
  519. * NOTE: This function is for string extraction only, and will just return the
  520. * string it was given. The translation itself should be done internally by nodes
  521. * with `atr()` instead.
  522. */
  523. _FORCE_INLINE_ String ETRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "") {
  524. if (p_n == 1) {
  525. return p_text;
  526. }
  527. return p_text_plural;
  528. }
  529. bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end);
  530. _FORCE_INLINE_ void sarray_add_str(Vector<String> &arr) {
  531. }
  532. _FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str) {
  533. arr.push_back(p_str);
  534. }
  535. template <typename... P>
  536. _FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str, P... p_args) {
  537. arr.push_back(p_str);
  538. sarray_add_str(arr, p_args...);
  539. }
  540. template <typename... P>
  541. _FORCE_INLINE_ Vector<String> sarray(P... p_args) {
  542. Vector<String> arr;
  543. sarray_add_str(arr, p_args...);
  544. return arr;
  545. }
  546. #endif // USTRING_GODOT_H