ArrayString.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #ifndef CORE_ARRAY_STRING_HPP
  2. #define CORE_ARRAY_STRING_HPP
  3. #include "core/utils/Error.hpp"
  4. #include "core/utils/Meta.hpp"
  5. #include "core/utils/Types.hpp"
  6. #include "core/utils/Utility.hpp"
  7. namespace Core {
  8. namespace Internal {
  9. template<typename String, typename T>
  10. CError genericAppend(String& s, const T& t) {
  11. if constexpr(requires { t.toString(s); }) {
  12. return t.toString(s);
  13. } else if constexpr(requires { static_cast<const char*>(t); }) {
  14. return s.append(static_cast<const char*>(t));
  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. char* data;
  59. public:
  60. CharString(char* buffer, i32 bufferSize);
  61. CharString(const CharString&) = delete;
  62. CharString& operator=(const CharString&) = delete;
  63. CError copyFrom(const CharString& s);
  64. bool operator==(const char* s) const;
  65. bool operator==(const CharString& other) const;
  66. bool operator!=(const char* s) const;
  67. bool operator!=(const CharString& other) const;
  68. char operator[](int index) const;
  69. int getLength() const;
  70. int getCapacity() const;
  71. CError append(char c);
  72. CError append(signed char c);
  73. CError append(unsigned char c);
  74. CError append(wchar_t c);
  75. CError append(c32 c);
  76. CError append(const char* s);
  77. CError append(const c32* s);
  78. CError append(const signed char* s);
  79. CError append(const unsigned char* s);
  80. CError append(bool b);
  81. template<typename T>
  82. CError append(const T& t) {
  83. return Internal::genericAppend(*this, t);
  84. }
  85. CError toString(CharString& s) const;
  86. CError toString(Char32String& s) const;
  87. void clear();
  88. CError print() const;
  89. CError printLine() const;
  90. template<typename... Args>
  91. CError format(CharString& s, Args&&... args) {
  92. return copyFormat(*this, s, Core::forward<Args>(args)...);
  93. }
  94. bool startsWidth(const CharString& other, int from = 0) const;
  95. int search(const CharString& other, int from = 0) const;
  96. bool contains(const CharString& other, int from = 0) const;
  97. int search(char u, int from = 0) const;
  98. bool contains(char u, int from = 0) const;
  99. CError substring(CharString& s, int from, int to) const;
  100. CError substring(CharString& s, int from = 0) const;
  101. CError replace(CharString& s, const CharString& search,
  102. const CharString& replace);
  103. void replace(char search, char replace);
  104. operator const char*() const;
  105. };
  106. class Char32String {
  107. protected:
  108. i32 length;
  109. i32 capacity;
  110. c32* data;
  111. public:
  112. Char32String(c32* buffer, i32 bufferSize);
  113. Char32String(const Char32String&) = delete;
  114. Char32String& operator=(const Char32String&) = delete;
  115. Error copyFrom(const Char32String& s);
  116. bool operator==(const c32* s) const;
  117. bool operator==(const Char32String& other) const;
  118. bool operator!=(const c32* s) const;
  119. bool operator!=(const Char32String& other) const;
  120. c32 operator[](int index) const;
  121. int getLength() const;
  122. int getCapacity() const;
  123. CError append(char c);
  124. CError append(signed char c);
  125. CError append(unsigned char c);
  126. CError append(wchar_t c);
  127. CError append(c32 c);
  128. CError append(const char* s);
  129. CError append(const c32* s);
  130. CError append(const signed char* s);
  131. CError append(const unsigned char* s);
  132. CError append(bool b);
  133. template<typename T>
  134. CError append(const T& t) {
  135. return Internal::genericAppend(*this, t);
  136. }
  137. CError toString(CharString& s) const;
  138. CError toString(Char32String& s) const;
  139. void clear();
  140. CError print() const;
  141. CError printLine() const;
  142. template<typename... Args>
  143. CError format(Char32String& s, Args&&... args) {
  144. return copyFormat(*this, s, Core::forward<Args>(args)...);
  145. }
  146. bool startsWidth(const Char32String& other, int from = 0) const;
  147. int search(const Char32String& other, int from = 0) const;
  148. bool contains(const Char32String& other, int from = 0) const;
  149. int search(c32 u, int from = 0) const;
  150. bool contains(c32 u, int from = 0) const;
  151. CError substring(Char32String& s, int from, int to) const;
  152. CError substring(Char32String& s, int from = 0) const;
  153. CError replace(Char32String& s, const Char32String& search,
  154. const Char32String& replace);
  155. void replace(c32 search, c32 replace);
  156. operator const c32*() const;
  157. };
  158. template<int N, typename C, typename B>
  159. class ArrayString final : public B {
  160. static_assert(N > 0, "size of array string must be positive");
  161. C data[static_cast<unsigned int>(N)];
  162. public:
  163. ArrayString() : B(data, N) {
  164. }
  165. ArrayString(const ArrayString& other) : B(data, N) {
  166. Core::memoryCopy(data, other.data, sizeof(data));
  167. B::length = other.length;
  168. }
  169. ArrayString& operator=(const ArrayString& other) {
  170. if(this != &other) {
  171. Core::memoryCopy(data, other.data, sizeof(data));
  172. B::length = other.length;
  173. }
  174. return *this;
  175. }
  176. template<typename... Args>
  177. CError format(Args&&... args) {
  178. ArrayString s;
  179. return B::format(s, Core::forward<Args>(args)...);
  180. }
  181. CError replace(const B& search, const B& replace) {
  182. ArrayString s;
  183. return B::replace(s, search, replace);
  184. }
  185. using B::replace;
  186. };
  187. template<typename String, typename Iterable>
  188. CError toString(String& s, const Iterable& i) {
  189. CORE_RETURN_ERROR(s.append("["));
  190. auto current = i.begin();
  191. auto end = i.end();
  192. while(current != end) {
  193. CORE_RETURN_ERROR(s.append(*current));
  194. ++current;
  195. if(current != end) {
  196. CORE_RETURN_ERROR(s.append(", "));
  197. }
  198. }
  199. return s.append("]");
  200. }
  201. template<int N>
  202. using String8 = ArrayString<N, char, CharString>;
  203. template<int N>
  204. using String32 = ArrayString<N, c32, Char32String>;
  205. }
  206. inline bool operator==(const c32* cs, const Core::Char32String& s) {
  207. return s == cs;
  208. }
  209. inline bool operator!=(const c32* cs, const Core::Char32String& s) {
  210. return s != cs;
  211. }
  212. inline bool operator==(const char* cs, const Core::CharString& s) {
  213. return s == cs;
  214. }
  215. inline bool operator!=(const char* cs, const Core::CharString& s) {
  216. return s != cs;
  217. }
  218. #endif