ustring.h 31 KB

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