char_string.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**************************************************************************/
  2. /* char_string.hpp */
  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. #pragma once
  31. #include <godot_cpp/templates/cowdata.hpp>
  32. #include <cstddef>
  33. #include <cstdint>
  34. namespace godot {
  35. template <typename T>
  36. class CharStringT;
  37. template <typename T>
  38. class CharProxy {
  39. template <typename TS>
  40. friend class CharStringT;
  41. const int64_t _index;
  42. CowData<T> &_cowdata;
  43. static inline const T _null = 0;
  44. _FORCE_INLINE_ CharProxy(const int64_t &p_index, CowData<T> &p_cowdata) :
  45. _index(p_index),
  46. _cowdata(p_cowdata) {}
  47. public:
  48. _FORCE_INLINE_ CharProxy(const CharProxy<T> &p_other) :
  49. _index(p_other._index),
  50. _cowdata(p_other._cowdata) {}
  51. _FORCE_INLINE_ operator T() const {
  52. if (unlikely(_index == _cowdata.size())) {
  53. return _null;
  54. }
  55. return _cowdata.get(_index);
  56. }
  57. _FORCE_INLINE_ const T *operator&() const {
  58. return _cowdata.ptr() + _index;
  59. }
  60. _FORCE_INLINE_ void operator=(const T &p_other) const {
  61. _cowdata.set(_index, p_other);
  62. }
  63. _FORCE_INLINE_ void operator=(const CharProxy<T> &p_other) const {
  64. _cowdata.set(_index, p_other.operator T());
  65. }
  66. };
  67. template <typename T>
  68. class CharStringT {
  69. friend class String;
  70. CowData<T> _cowdata;
  71. static inline const T _null = 0;
  72. public:
  73. _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
  74. _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
  75. _FORCE_INLINE_ int64_t size() const { return _cowdata.size(); }
  76. Error resize(int64_t p_size) { return _cowdata.resize(p_size); }
  77. _FORCE_INLINE_ T get(int64_t p_index) const { return _cowdata.get(p_index); }
  78. _FORCE_INLINE_ void set(int64_t p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
  79. _FORCE_INLINE_ const T &operator[](int64_t p_index) const {
  80. if (unlikely(p_index == _cowdata.size())) {
  81. return _null;
  82. }
  83. return _cowdata.get(p_index);
  84. }
  85. _FORCE_INLINE_ CharProxy<T> operator[](int64_t p_index) { return CharProxy<T>(p_index, _cowdata); }
  86. _FORCE_INLINE_ CharStringT() {}
  87. _FORCE_INLINE_ CharStringT(const CharStringT<T> &p_str) { _cowdata._ref(p_str._cowdata); }
  88. _FORCE_INLINE_ void operator=(const CharStringT<T> &p_str) { _cowdata._ref(p_str._cowdata); }
  89. _FORCE_INLINE_ CharStringT(const T *p_cstr) { copy_from(p_cstr); }
  90. void operator=(const T *p_cstr);
  91. bool operator<(const CharStringT<T> &p_right) const;
  92. CharStringT<T> &operator+=(T p_char);
  93. int64_t length() const { return size() ? size() - 1 : 0; }
  94. const T *get_data() const;
  95. operator const T *() const { return get_data(); }
  96. protected:
  97. void copy_from(const T *p_cstr);
  98. };
  99. template <>
  100. const char *CharStringT<char>::get_data() const;
  101. template <>
  102. const char16_t *CharStringT<char16_t>::get_data() const;
  103. template <>
  104. const char32_t *CharStringT<char32_t>::get_data() const;
  105. template <>
  106. const wchar_t *CharStringT<wchar_t>::get_data() const;
  107. typedef CharStringT<char> CharString;
  108. typedef CharStringT<char16_t> Char16String;
  109. typedef CharStringT<char32_t> Char32String;
  110. typedef CharStringT<wchar_t> CharWideString;
  111. } // namespace godot