ArrayString.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef CORE_ARRAY_STRING_HPP
  2. #define CORE_ARRAY_STRING_HPP
  3. #include "core/math/Math.hpp"
  4. #include "core/utils/Check.hpp"
  5. #include "core/utils/Error.hpp"
  6. #include "core/utils/Meta.hpp"
  7. #include "core/utils/Types.hpp"
  8. #include "core/utils/Utility.hpp"
  9. namespace Core {
  10. namespace Internal {
  11. template<typename String, typename T>
  12. CError genericAppend(String& s, const T& t) {
  13. if constexpr(requires { t.toString(s); }) {
  14. return t.toString(s);
  15. } else {
  16. char buffer[64];
  17. CORE_RETURN_ERROR(toString(t, buffer, CORE_SIZE(buffer)));
  18. return s.append(static_cast<const char*>(buffer));
  19. }
  20. }
  21. template<typename S, typename T, typename... Args>
  22. CError formatR(const S& f, S& s, int index, const T& t,
  23. Args&&... args) {
  24. i32 l = f.getLength();
  25. while(index < l) {
  26. auto u = f[index++];
  27. if(u == '#') {
  28. if(index >= l || f[index] != '#') {
  29. break;
  30. }
  31. index++;
  32. }
  33. CORE_RETURN_ERROR(s.append(u));
  34. }
  35. CORE_RETURN_ERROR(s.append(t));
  36. if constexpr(sizeof...(args) > 0) {
  37. return formatR(f, s, index, forward<Args>(args)...);
  38. }
  39. while(index < f.getLength()) {
  40. CORE_RETURN_ERROR(s.append(f[index++]));
  41. }
  42. return ErrorCode::NONE;
  43. }
  44. }
  45. template<typename String, typename... Args>
  46. CError copyFormat(String& result, String& s, Args&&... args) {
  47. if constexpr(sizeof...(args) > 0) {
  48. Error e = Internal::formatR(result, s, 0, forward<Args>(args)...);
  49. return e | result.copyFrom(s);
  50. }
  51. return ErrorCode::NONE;
  52. }
  53. class Char32String;
  54. class CharString {
  55. protected:
  56. i32 length;
  57. i32 capacity;
  58. u32 hash;
  59. char* data;
  60. public:
  61. CharString(char* buffer, i32 bufferSize);
  62. CharString(const CharString&) = delete;
  63. CharString& operator=(const CharString&) = delete;
  64. CError copyFrom(const CharString& s);
  65. bool operator==(const char* s) const;
  66. bool operator==(const CharString& other) const;
  67. bool operator!=(const char* s) const;
  68. bool operator!=(const CharString& other) const;
  69. char operator[](int index) const;
  70. int getLength() const;
  71. int getCapacity() const;
  72. CError append(char c);
  73. CError append(signed char c);
  74. CError append(unsigned char c);
  75. CError append(wchar_t c);
  76. CError append(c32 c);
  77. CError append(const char* s);
  78. CError append(const c32* s);
  79. CError append(const signed char* s);
  80. CError append(const unsigned char* s);
  81. CError append(bool b);
  82. template<typename T>
  83. CError append(const T& t) {
  84. return Internal::genericAppend(*this, t);
  85. }
  86. CError toString(CharString& s) const;
  87. CError toString(Char32String& s) const;
  88. void clear();
  89. u32 hashCode() const;
  90. CError print() const;
  91. CError printLine() const;
  92. template<typename... Args>
  93. CError format(CharString& s, Args&&... args) {
  94. return copyFormat(*this, s, Core::forward<Args>(args)...);
  95. }
  96. bool startsWidth(const CharString& other, int from = 0) const;
  97. int search(const CharString& other, int from = 0) const;
  98. bool contains(const CharString& other, int from = 0) const;
  99. int search(char u, int from = 0) const;
  100. bool contains(char u, int from = 0) const;
  101. CError substring(CharString& s, int from, int to) const;
  102. CError substring(CharString& s, int from = 0) const;
  103. CError replace(CharString& s, const CharString& search,
  104. const CharString& replace);
  105. void replace(char search, char replace);
  106. operator const char*() const;
  107. private:
  108. void addToHash(c32 u);
  109. };
  110. class Char32String {
  111. protected:
  112. i32 length;
  113. i32 capacity;
  114. u32 hash;
  115. c32* data;
  116. public:
  117. Char32String(c32* buffer, i32 bufferSize);
  118. Char32String(const Char32String&) = delete;
  119. Char32String& operator=(const Char32String&) = delete;
  120. Error copyFrom(const Char32String& s);
  121. bool operator==(const c32* s) const;
  122. bool operator==(const Char32String& other) const;
  123. bool operator!=(const c32* s) const;
  124. bool operator!=(const Char32String& other) const;
  125. c32 operator[](int index) const;
  126. int getLength() const;
  127. int getCapacity() const;
  128. CError append(char c);
  129. CError append(signed char c);
  130. CError append(unsigned char c);
  131. CError append(wchar_t c);
  132. CError append(c32 c);
  133. CError append(const char* s);
  134. CError append(const c32* s);
  135. CError append(const signed char* s);
  136. CError append(const unsigned char* s);
  137. CError append(bool b);
  138. template<typename T>
  139. CError append(const T& t) {
  140. return Internal::genericAppend(*this, t);
  141. }
  142. CError toString(CharString& s) const;
  143. CError toString(Char32String& s) const;
  144. void clear();
  145. u32 hashCode() const;
  146. CError print() const;
  147. CError printLine() const;
  148. template<typename... Args>
  149. CError format(Char32String& s, Args&&... args) {
  150. return copyFormat(*this, s, Core::forward<Args>(args)...);
  151. }
  152. bool startsWidth(const Char32String& other, int from = 0) const;
  153. int search(const Char32String& other, int from = 0) const;
  154. bool contains(const Char32String& other, int from = 0) const;
  155. int search(c32 u, int from = 0) const;
  156. bool contains(c32 u, int from = 0) const;
  157. CError substring(Char32String& s, int from, int to) const;
  158. CError substring(Char32String& s, int from = 0) const;
  159. CError replace(Char32String& s, const Char32String& search,
  160. const Char32String& replace);
  161. void replace(c32 search, c32 replace);
  162. operator const c32*() const;
  163. private:
  164. void addToHash(c32 u);
  165. };
  166. template<int N, typename C, typename B>
  167. class ArrayString final : public B {
  168. static_assert(N > 0, "size of array string must be positive");
  169. C data[static_cast<unsigned int>(N)];
  170. public:
  171. ArrayString() : B(data, N) {
  172. }
  173. ArrayString(const ArrayString& other) : B(data, N) {
  174. Core::memoryCopy(data, other.data, sizeof(data));
  175. B::length = other.length;
  176. B::hash = other.hash;
  177. }
  178. ArrayString& operator=(const ArrayString& other) {
  179. if(this != &other) {
  180. Core::memoryCopy(data, other.data, sizeof(data));
  181. B::length = other.length;
  182. B::hash = other.hash;
  183. }
  184. return *this;
  185. }
  186. template<typename... Args>
  187. CError format(Args&&... args) {
  188. ArrayString s;
  189. return B::format(s, Core::forward<Args>(args)...);
  190. }
  191. CError replace(const B& search, const B& replace) {
  192. ArrayString s;
  193. return B::replace(s, search, replace);
  194. }
  195. using B::replace;
  196. };
  197. template<typename String, typename Iterable>
  198. CError toString(String& s, const Iterable& i) {
  199. CORE_RETURN_ERROR(s.append("["));
  200. auto current = i.begin();
  201. auto end = i.end();
  202. while(current != end) {
  203. CORE_RETURN_ERROR(s.append(*current));
  204. ++current;
  205. if(current != end) {
  206. CORE_RETURN_ERROR(s.append(", "));
  207. }
  208. }
  209. return s.append("]");
  210. }
  211. template<int N>
  212. using String8 = ArrayString<N, char, CharString>;
  213. template<int N>
  214. using String32 = ArrayString<N, c32, Char32String>;
  215. }
  216. inline bool operator==(const c32* cs, const Core::Char32String& s) {
  217. return s == cs;
  218. }
  219. inline bool operator!=(const c32* cs, const Core::Char32String& s) {
  220. return s != cs;
  221. }
  222. inline bool operator==(const char* cs, const Core::CharString& s) {
  223. return s == cs;
  224. }
  225. inline bool operator!=(const char* cs, const Core::CharString& s) {
  226. return s != cs;
  227. }
  228. #endif