String.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* String.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 STRING_H
  31. #define STRING_H
  32. #include <gdnative/string.h>
  33. namespace godot {
  34. class NodePath;
  35. class Variant;
  36. class PoolByteArray;
  37. class PoolIntArray;
  38. class PoolRealArray;
  39. class PoolStringArray;
  40. class String;
  41. class CharString {
  42. friend class String;
  43. godot_char_string _char_string;
  44. public:
  45. ~CharString();
  46. int length() const;
  47. const char *get_data() const;
  48. };
  49. class String {
  50. godot_string _godot_string;
  51. friend class Dictionary;
  52. friend class NodePath;
  53. friend class Variant;
  54. explicit inline String(godot_string contents) :
  55. _godot_string(contents) {}
  56. public:
  57. String();
  58. String(const char *contents);
  59. String(const wchar_t *contents);
  60. String(const wchar_t c);
  61. String(const String &other);
  62. String(String &&other);
  63. ~String();
  64. static String num(double p_num, int p_decimals = -1);
  65. static String num_scientific(double p_num);
  66. static String num_real(double p_num);
  67. static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false);
  68. static String chr(godot_char_type p_char);
  69. static String md5(const uint8_t *p_md5);
  70. static String hex_encode_buffer(const uint8_t *p_buffer, int p_len);
  71. wchar_t &operator[](const int idx);
  72. wchar_t operator[](const int idx) const;
  73. void operator=(const String &s);
  74. void operator=(String &&s);
  75. bool operator==(const String &s) const;
  76. bool operator!=(const String &s) const;
  77. String operator+(const String &s) const;
  78. void operator+=(const String &s);
  79. void operator+=(const wchar_t c);
  80. bool operator<(const String &s) const;
  81. bool operator<=(const String &s) const;
  82. bool operator>(const String &s) const;
  83. bool operator>=(const String &s) const;
  84. operator NodePath() const;
  85. int length() const;
  86. const wchar_t *unicode_str() const;
  87. char *alloc_c_string() const;
  88. CharString utf8() const;
  89. CharString ascii(bool p_extended = false) const;
  90. bool begins_with(String &s) const;
  91. bool begins_with_char_array(const char *p_char_array) const;
  92. PoolStringArray bigrams() const;
  93. String c_escape() const;
  94. String c_unescape() const;
  95. String capitalize() const;
  96. bool empty() const;
  97. bool ends_with(String &text) const;
  98. void erase(int position, int chars);
  99. int find(String what, int from = 0) const;
  100. int find_last(String what) const;
  101. int findn(String what, int from = 0) const;
  102. String format(Variant values) const;
  103. String format(Variant values, String placeholder) const;
  104. String get_base_dir() const;
  105. String get_basename() const;
  106. String get_extension() const;
  107. String get_file() const;
  108. int hash() const;
  109. int hex_to_int() const;
  110. String insert(int position, String what) const;
  111. bool is_abs_path() const;
  112. bool is_rel_path() const;
  113. bool is_subsequence_of(String text) const;
  114. bool is_subsequence_ofi(String text) const;
  115. bool is_valid_float() const;
  116. bool is_valid_html_color() const;
  117. bool is_valid_identifier() const;
  118. bool is_valid_integer() const;
  119. bool is_valid_ip_address() const;
  120. String json_escape() const;
  121. String left(int position) const;
  122. bool match(String expr) const;
  123. bool matchn(String expr) const;
  124. PoolByteArray md5_buffer() const;
  125. String md5_text() const;
  126. int ord_at(int at) const;
  127. String pad_decimals(int digits) const;
  128. String pad_zeros(int digits) const;
  129. String percent_decode() const;
  130. String percent_encode() const;
  131. String plus_file(String file) const;
  132. String replace(String what, String forwhat) const;
  133. String replacen(String what, String forwhat) const;
  134. int rfind(String what, int from = -1) const;
  135. int rfindn(String what, int from = -1) const;
  136. String right(int position) const;
  137. PoolByteArray sha256_buffer() const;
  138. String sha256_text() const;
  139. float similarity(String text) const;
  140. PoolStringArray split(String divisor, bool allow_empty = true) const;
  141. PoolIntArray split_ints(String divisor, bool allow_empty = true) const;
  142. PoolRealArray split_floats(String divisor, bool allow_empty = true) const;
  143. String strip_edges(bool left = true, bool right = true) const;
  144. String substr(int from, int len) const;
  145. float to_float() const;
  146. int64_t to_int() const;
  147. String to_lower() const;
  148. String to_upper() const;
  149. String xml_escape() const;
  150. String xml_unescape() const;
  151. signed char casecmp_to(String p_str) const;
  152. signed char nocasecmp_to(String p_str) const;
  153. signed char naturalnocasecmp_to(String p_str) const;
  154. String dedent() const;
  155. PoolStringArray rsplit(const String &divisor, const bool allow_empty = true, const int maxsplit = 0) const;
  156. String rstrip(const String &chars) const;
  157. String trim_prefix(const String &prefix) const;
  158. String trim_suffix(const String &suffix) const;
  159. };
  160. String operator+(const char *a, const String &b);
  161. String operator+(const wchar_t *a, const String &b);
  162. } // namespace godot
  163. #endif // STRING_H