ustring.h 30 KB

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