String.cpp 2.2 KB

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