char_string.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /**************************************************************************/
  2. /* char_string.cpp */
  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. #include <godot_cpp/variant/char_string.hpp>
  31. #include <godot_cpp/core/memory.hpp>
  32. #include <godot_cpp/variant/node_path.hpp>
  33. #include <godot_cpp/variant/string.hpp>
  34. #include <godot_cpp/variant/string_name.hpp>
  35. #include <godot_cpp/godot.hpp>
  36. #include <cmath>
  37. namespace godot {
  38. int CharString::length() const {
  39. return _length;
  40. }
  41. const char *CharString::get_data() const {
  42. return _data;
  43. }
  44. CharString::CharString(CharString &&p_str) {
  45. SWAP(_length, p_str._length);
  46. SWAP(_data, p_str._data);
  47. }
  48. void CharString::operator=(CharString &&p_str) {
  49. SWAP(_length, p_str._length);
  50. SWAP(_data, p_str._data);
  51. }
  52. CharString::CharString(const char *str, int length) :
  53. _data(str), _length(length) {}
  54. CharString::~CharString() {
  55. if (_data != nullptr) {
  56. memdelete_arr(_data);
  57. }
  58. }
  59. int Char16String::length() const {
  60. return _length;
  61. }
  62. const char16_t *Char16String::get_data() const {
  63. return _data;
  64. }
  65. Char16String::Char16String(Char16String &&p_str) {
  66. SWAP(_length, p_str._length);
  67. SWAP(_data, p_str._data);
  68. }
  69. void Char16String::operator=(Char16String &&p_str) {
  70. SWAP(_length, p_str._length);
  71. SWAP(_data, p_str._data);
  72. }
  73. Char16String::Char16String(const char16_t *str, int length) :
  74. _data(str), _length(length) {}
  75. Char16String::~Char16String() {
  76. if (_data != nullptr) {
  77. memdelete_arr(_data);
  78. }
  79. }
  80. int Char32String::length() const {
  81. return _length;
  82. }
  83. const char32_t *Char32String::get_data() const {
  84. return _data;
  85. }
  86. Char32String::Char32String(Char32String &&p_str) {
  87. SWAP(_length, p_str._length);
  88. SWAP(_data, p_str._data);
  89. }
  90. void Char32String::operator=(Char32String &&p_str) {
  91. SWAP(_length, p_str._length);
  92. SWAP(_data, p_str._data);
  93. }
  94. Char32String::Char32String(const char32_t *str, int length) :
  95. _data(str), _length(length) {}
  96. Char32String::~Char32String() {
  97. if (_data != nullptr) {
  98. memdelete_arr(_data);
  99. }
  100. }
  101. int CharWideString::length() const {
  102. return _length;
  103. }
  104. const wchar_t *CharWideString::get_data() const {
  105. return _data;
  106. }
  107. CharWideString::CharWideString(CharWideString &&p_str) {
  108. SWAP(_length, p_str._length);
  109. SWAP(_data, p_str._data);
  110. }
  111. void CharWideString::operator=(CharWideString &&p_str) {
  112. SWAP(_length, p_str._length);
  113. SWAP(_data, p_str._data);
  114. }
  115. CharWideString::CharWideString(const wchar_t *str, int length) :
  116. _data(str), _length(length) {}
  117. CharWideString::~CharWideString() {
  118. if (_data != nullptr) {
  119. memdelete_arr(_data);
  120. }
  121. }
  122. // Custom String functions that are not part of bound API.
  123. // It's easier to have them written in C++ directly than in a Python script that generates them.
  124. String::String(const char *from) {
  125. internal::gde_interface->string_new_with_latin1_chars(_native_ptr(), from);
  126. }
  127. String::String(const wchar_t *from) {
  128. internal::gde_interface->string_new_with_wide_chars(_native_ptr(), from);
  129. }
  130. String::String(const char16_t *from) {
  131. internal::gde_interface->string_new_with_utf16_chars(_native_ptr(), from);
  132. }
  133. String::String(const char32_t *from) {
  134. internal::gde_interface->string_new_with_utf32_chars(_native_ptr(), from);
  135. }
  136. String String::utf8(const char *from, int len) {
  137. String ret;
  138. ret.parse_utf8(from, len);
  139. return ret;
  140. }
  141. void String::parse_utf8(const char *from, int len) {
  142. internal::gde_interface->string_new_with_utf8_chars_and_len(_native_ptr(), from, len);
  143. }
  144. String String::utf16(const char16_t *from, int len) {
  145. String ret;
  146. ret.parse_utf16(from, len);
  147. return ret;
  148. }
  149. void String::parse_utf16(const char16_t *from, int len) {
  150. internal::gde_interface->string_new_with_utf16_chars_and_len(_native_ptr(), from, len);
  151. }
  152. String String::num_real(double p_num, bool p_trailing) {
  153. if (p_num == (double)(int64_t)p_num) {
  154. if (p_trailing) {
  155. return num_int64((int64_t)p_num) + ".0";
  156. } else {
  157. return num_int64((int64_t)p_num);
  158. }
  159. }
  160. #ifdef REAL_T_IS_DOUBLE
  161. int decimals = 14;
  162. #else
  163. int decimals = 6;
  164. #endif
  165. // We want to align the digits to the above sane default, so we only
  166. // need to subtract log10 for numbers with a positive power of ten.
  167. if (p_num > 10) {
  168. decimals -= (int)floor(log10(p_num));
  169. }
  170. return num(p_num, decimals);
  171. }
  172. String itos(int64_t p_val) {
  173. return String::num_int64(p_val);
  174. }
  175. String uitos(uint64_t p_val) {
  176. return String::num_uint64(p_val);
  177. }
  178. String rtos(double p_val) {
  179. return String::num(p_val);
  180. }
  181. String rtoss(double p_val) {
  182. return String::num_scientific(p_val);
  183. }
  184. CharString String::utf8() const {
  185. int length = internal::gde_interface->string_to_utf8_chars(_native_ptr(), nullptr, 0);
  186. int size = length + 1;
  187. char *cstr = memnew_arr(char, size);
  188. internal::gde_interface->string_to_utf8_chars(_native_ptr(), cstr, length);
  189. cstr[length] = '\0';
  190. return CharString(cstr, length);
  191. }
  192. CharString String::ascii() const {
  193. int length = internal::gde_interface->string_to_latin1_chars(_native_ptr(), nullptr, 0);
  194. int size = length + 1;
  195. char *cstr = memnew_arr(char, size);
  196. internal::gde_interface->string_to_latin1_chars(_native_ptr(), cstr, length);
  197. cstr[length] = '\0';
  198. return CharString(cstr, length);
  199. }
  200. Char16String String::utf16() const {
  201. int length = internal::gde_interface->string_to_utf16_chars(_native_ptr(), nullptr, 0);
  202. int size = length + 1;
  203. char16_t *cstr = memnew_arr(char16_t, size);
  204. internal::gde_interface->string_to_utf16_chars(_native_ptr(), cstr, length);
  205. cstr[length] = '\0';
  206. return Char16String(cstr, length);
  207. }
  208. Char32String String::utf32() const {
  209. int length = internal::gde_interface->string_to_utf32_chars(_native_ptr(), nullptr, 0);
  210. int size = length + 1;
  211. char32_t *cstr = memnew_arr(char32_t, size);
  212. internal::gde_interface->string_to_utf32_chars(_native_ptr(), cstr, length);
  213. cstr[length] = '\0';
  214. return Char32String(cstr, length);
  215. }
  216. CharWideString String::wide_string() const {
  217. int length = internal::gde_interface->string_to_wide_chars(_native_ptr(), nullptr, 0);
  218. int size = length + 1;
  219. wchar_t *cstr = memnew_arr(wchar_t, size);
  220. internal::gde_interface->string_to_wide_chars(_native_ptr(), cstr, length);
  221. cstr[length] = '\0';
  222. return CharWideString(cstr, length);
  223. }
  224. String &String::operator=(const char *p_str) {
  225. *this = String(p_str);
  226. return *this;
  227. }
  228. String &String::operator=(const wchar_t *p_str) {
  229. *this = String(p_str);
  230. return *this;
  231. }
  232. String &String::operator=(const char16_t *p_str) {
  233. *this = String(p_str);
  234. return *this;
  235. }
  236. String &String::operator=(const char32_t *p_str) {
  237. *this = String(p_str);
  238. return *this;
  239. }
  240. bool String::operator==(const char *p_str) const {
  241. return *this == String(p_str);
  242. }
  243. bool String::operator==(const wchar_t *p_str) const {
  244. return *this == String(p_str);
  245. }
  246. bool String::operator==(const char16_t *p_str) const {
  247. return *this == String(p_str);
  248. }
  249. bool String::operator==(const char32_t *p_str) const {
  250. return *this == String(p_str);
  251. }
  252. bool String::operator!=(const char *p_str) const {
  253. return *this != String(p_str);
  254. }
  255. bool String::operator!=(const wchar_t *p_str) const {
  256. return *this != String(p_str);
  257. }
  258. bool String::operator!=(const char16_t *p_str) const {
  259. return *this != String(p_str);
  260. }
  261. bool String::operator!=(const char32_t *p_str) const {
  262. return *this != String(p_str);
  263. }
  264. String String::operator+(const char *p_str) {
  265. return *this + String(p_str);
  266. }
  267. String String::operator+(const wchar_t *p_str) {
  268. return *this + String(p_str);
  269. }
  270. String String::operator+(const char16_t *p_str) {
  271. return *this + String(p_str);
  272. }
  273. String String::operator+(const char32_t *p_str) {
  274. return *this + String(p_str);
  275. }
  276. String String::operator+(const char32_t p_char) {
  277. return *this + String::chr(p_char);
  278. }
  279. String &String::operator+=(const String &p_str) {
  280. internal::gde_interface->string_operator_plus_eq_string((GDExtensionStringPtr)this, (const GDExtensionStringPtr)&p_str);
  281. return *this;
  282. }
  283. String &String::operator+=(char32_t p_char) {
  284. internal::gde_interface->string_operator_plus_eq_char((GDExtensionStringPtr)this, p_char);
  285. return *this;
  286. }
  287. String &String::operator+=(const char *p_str) {
  288. internal::gde_interface->string_operator_plus_eq_cstr((GDExtensionStringPtr)this, p_str);
  289. return *this;
  290. }
  291. String &String::operator+=(const wchar_t *p_str) {
  292. internal::gde_interface->string_operator_plus_eq_wcstr((GDExtensionStringPtr)this, p_str);
  293. return *this;
  294. }
  295. String &String::operator+=(const char32_t *p_str) {
  296. internal::gde_interface->string_operator_plus_eq_c32str((GDExtensionStringPtr)this, p_str);
  297. return *this;
  298. }
  299. const char32_t &String::operator[](int p_index) const {
  300. return *internal::gde_interface->string_operator_index_const((GDExtensionStringPtr)this, p_index);
  301. }
  302. char32_t &String::operator[](int p_index) {
  303. return *internal::gde_interface->string_operator_index((GDExtensionStringPtr)this, p_index);
  304. }
  305. const char32_t *String::ptr() const {
  306. return internal::gde_interface->string_operator_index_const((GDExtensionStringPtr)this, 0);
  307. }
  308. char32_t *String::ptrw() {
  309. return internal::gde_interface->string_operator_index((GDExtensionStringPtr)this, 0);
  310. }
  311. bool operator==(const char *p_chr, const String &p_str) {
  312. return p_str == String(p_chr);
  313. }
  314. bool operator==(const wchar_t *p_chr, const String &p_str) {
  315. return p_str == String(p_chr);
  316. }
  317. bool operator==(const char16_t *p_chr, const String &p_str) {
  318. return p_str == String(p_chr);
  319. }
  320. bool operator==(const char32_t *p_chr, const String &p_str) {
  321. return p_str == String(p_chr);
  322. }
  323. bool operator!=(const char *p_chr, const String &p_str) {
  324. return !(p_str == p_chr);
  325. }
  326. bool operator!=(const wchar_t *p_chr, const String &p_str) {
  327. return !(p_str == p_chr);
  328. }
  329. bool operator!=(const char16_t *p_chr, const String &p_str) {
  330. return !(p_str == p_chr);
  331. }
  332. bool operator!=(const char32_t *p_chr, const String &p_str) {
  333. return !(p_str == p_chr);
  334. }
  335. String operator+(const char *p_chr, const String &p_str) {
  336. return String(p_chr) + p_str;
  337. }
  338. String operator+(const wchar_t *p_chr, const String &p_str) {
  339. return String(p_chr) + p_str;
  340. }
  341. String operator+(const char16_t *p_chr, const String &p_str) {
  342. return String(p_chr) + p_str;
  343. }
  344. String operator+(const char32_t *p_chr, const String &p_str) {
  345. return String(p_chr) + p_str;
  346. }
  347. String operator+(char32_t p_char, const String &p_str) {
  348. return String::chr(p_char) + p_str;
  349. }
  350. StringName::StringName(const char *from) :
  351. StringName(String(from)) {}
  352. StringName::StringName(const wchar_t *from) :
  353. StringName(String(from)) {}
  354. StringName::StringName(const char16_t *from) :
  355. StringName(String(from)) {}
  356. StringName::StringName(const char32_t *from) :
  357. StringName(String(from)) {}
  358. NodePath::NodePath(const char *from) :
  359. NodePath(String(from)) {}
  360. NodePath::NodePath(const wchar_t *from) :
  361. NodePath(String(from)) {}
  362. NodePath::NodePath(const char16_t *from) :
  363. NodePath(String(from)) {}
  364. NodePath::NodePath(const char32_t *from) :
  365. NodePath(String(from)) {}
  366. } // namespace godot