String.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef STRING_H
  2. #define STRING_H
  3. namespace godot {
  4. #include <godot/godot_string.h>
  5. #include <string.h>
  6. class String
  7. {
  8. godot_string _godot_string;
  9. public:
  10. String()
  11. {
  12. godot_string_new(&_godot_string);
  13. }
  14. String(const char *contents)
  15. {
  16. godot_string_new_data(&_godot_string, contents, strlen(contents));
  17. }
  18. String(const wchar_t *contents)
  19. {
  20. // @Todo
  21. // godot_string_new_data(&_godot_string, contents, strlen(contents));
  22. godot_string_new(&_godot_string);
  23. }
  24. String(const wchar_t c)
  25. {
  26. // @Todo
  27. godot_string_new(&_godot_string);
  28. }
  29. String(const String& other)
  30. {
  31. godot_string_new(&_godot_string);
  32. godot_string_copy_string(&_godot_string, &other._godot_string);
  33. }
  34. ~String()
  35. {
  36. godot_string_destroy(&_godot_string);
  37. }
  38. String substr(int p_from,int p_chars) const
  39. {
  40. return String(); // @Todo
  41. }
  42. wchar_t &operator [](const int idx)
  43. {
  44. return *godot_string_operator_index(&_godot_string, idx);
  45. }
  46. wchar_t operator [](const int idx) const
  47. {
  48. return *godot_string_operator_index((godot_string *) &_godot_string, idx);
  49. }
  50. int length() const
  51. {
  52. int len = 0;
  53. godot_string_get_data(&_godot_string, nullptr, &len);
  54. return len;
  55. }
  56. bool operator ==(const String &s)
  57. {
  58. return godot_string_operator_equal(&_godot_string, &s._godot_string);
  59. }
  60. bool operator !=(const String &s)
  61. {
  62. return !(*this == s);
  63. }
  64. String operator +(const String &s)
  65. {
  66. String new_string;
  67. godot_string_operator_plus(&new_string._godot_string, &_godot_string, &s._godot_string);
  68. return new_string;
  69. }
  70. void operator +=(const String &s)
  71. {
  72. godot_string_operator_plus(&_godot_string, &_godot_string, &s._godot_string);
  73. }
  74. void operator +=(const wchar_t c)
  75. {
  76. // @Todo
  77. }
  78. bool operator <(const String &s)
  79. {
  80. return godot_string_operator_less(&_godot_string, &s._godot_string);
  81. }
  82. bool operator <=(const String &s)
  83. {
  84. return godot_string_operator_less(&_godot_string, &s._godot_string) || (*this == s);
  85. }
  86. bool operator >(const String &s)
  87. {
  88. return !(*this <= s);
  89. }
  90. bool operator >=(const String &s)
  91. {
  92. return !(*this < s);
  93. }
  94. const wchar_t *c_string()
  95. {
  96. return godot_string_c_str(&_godot_string);
  97. }
  98. };
  99. }
  100. #endif // STRING_H