ustring.h 30 KB

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