ArrayString.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #ifndef CORE_STRING_H
  2. #define CORE_STRING_H
  3. #include "math/Math.h"
  4. #include "utils/Check.h"
  5. #include "utils/Utility.h"
  6. namespace Core {
  7. template<int N>
  8. class ArrayString final {
  9. int length;
  10. u32 hash;
  11. static_assert(N > 0, "size of array string must be positive");
  12. u32 data[static_cast<unsigned int>(N)];
  13. public:
  14. ArrayString() : length(0), hash(0) {
  15. data[0] = '\0';
  16. }
  17. bool operator==(const char* s) const {
  18. for(int i = 0; i < length; i++) {
  19. u32 u = 0;
  20. if(readUnicode(u, s) != Error::NONE || data[i] != u) {
  21. return false;
  22. }
  23. }
  24. return read(s) == 0;
  25. }
  26. template<int L>
  27. bool operator==(const ArrayString<L>& other) const {
  28. if(length != other.getLength()) {
  29. return false;
  30. }
  31. for(int i = 0; i < length; i++) {
  32. if(data[i] != other[i]) {
  33. return false;
  34. }
  35. }
  36. return true;
  37. }
  38. bool operator!=(const char* s) const {
  39. return !((*this) == s);
  40. }
  41. template<int L>
  42. bool operator!=(const ArrayString<L>& other) const {
  43. return !((*this) == other);
  44. }
  45. u32 operator[](int index) const {
  46. return data[index];
  47. }
  48. int getLength() const {
  49. return length;
  50. }
  51. constexpr int getCapacity() const {
  52. return N;
  53. }
  54. check_return Error append(char c) {
  55. if(c < 0) {
  56. return Error::NEGATIVE_ARGUMENT;
  57. }
  58. return appendUnicode(static_cast<u32>(c));
  59. }
  60. check_return Error append(signed char c) {
  61. if(c < 0) {
  62. return Error::NEGATIVE_ARGUMENT;
  63. }
  64. return appendUnicode(static_cast<u32>(c));
  65. }
  66. check_return Error append(unsigned char c) {
  67. return appendUnicode(c);
  68. }
  69. check_return Error append(const char* s) {
  70. while(true) {
  71. u32 u = 0;
  72. CORE_RETURN_ERROR(readUnicode(u, s));
  73. if(u == 0) {
  74. return Error::NONE;
  75. }
  76. CORE_RETURN_ERROR(appendUnicode(u));
  77. }
  78. }
  79. check_return Error append(const signed char* s) {
  80. return append(reinterpret_cast<const char*>(s));
  81. }
  82. check_return Error append(const unsigned char* s) {
  83. return append(reinterpret_cast<const char*>(s));
  84. }
  85. check_return Error append(signed short s) {
  86. return convertAppend(s);
  87. }
  88. check_return Error append(unsigned short s) {
  89. return convertAppend(s);
  90. }
  91. check_return Error append(signed int i) {
  92. return convertAppend(i);
  93. }
  94. check_return Error append(unsigned int i) {
  95. return convertAppend(i);
  96. }
  97. check_return Error append(signed long l) {
  98. return convertAppend(l);
  99. }
  100. check_return Error append(unsigned long l) {
  101. return convertAppend(l);
  102. }
  103. check_return Error append(signed long long ll) {
  104. return convertAppend(ll);
  105. }
  106. check_return Error append(unsigned long long ll) {
  107. return convertAppend(ll);
  108. }
  109. check_return Error append(float f) {
  110. return convertAppend(static_cast<double>(f));
  111. }
  112. check_return Error append(double d) {
  113. return convertAppend(d);
  114. }
  115. check_return Error append(long double ld) {
  116. return convertAppend(ld);
  117. }
  118. check_return Error append(bool b) {
  119. return b ? append("true") : append("false");
  120. }
  121. check_return Error appendUnicode(u32 c) {
  122. if(length >= N) {
  123. return Error::CAPACITY_REACHED;
  124. }
  125. data[length++] = c;
  126. addToHash(c);
  127. return Error::NONE;
  128. }
  129. check_return Error append(Error e) {
  130. return append(getErrorName(e));
  131. }
  132. template<typename T>
  133. check_return Error append(const T& t) {
  134. return t.toString(*this);
  135. }
  136. template<int L>
  137. check_return Error toString(ArrayString<L>& s) const {
  138. int l = length; // length changes if &s == this
  139. for(int i = 0; i < l; i++) {
  140. CORE_RETURN_ERROR(s.appendUnicode(data[i]));
  141. }
  142. return Error::NONE;
  143. }
  144. void clear() {
  145. length = 0;
  146. hash = 0;
  147. data[0] = '\0';
  148. }
  149. u32 hashCode() const {
  150. return hash;
  151. }
  152. check_return Error print() const {
  153. for(int i = 0; i < length; i++) {
  154. u32 c = data[i];
  155. if(c < (1 << 7)) {
  156. CORE_RETURN_ERROR(printChar(c, 0, 0x7F, 0x0));
  157. } else if(c < (1 << 11)) {
  158. CORE_RETURN_ERROR(printChar(c, 6, 0x1F, 0xC0));
  159. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  160. } else if(c < (1 << 16)) {
  161. CORE_RETURN_ERROR(printChar(c, 12, 0x0F, 0xE0));
  162. CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
  163. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  164. } else if(c < (1 << 21)) {
  165. CORE_RETURN_ERROR(printChar(c, 18, 0x07, 0xF0));
  166. CORE_RETURN_ERROR(printChar(c, 12, 0x3F, 0x80));
  167. CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
  168. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  169. }
  170. }
  171. return Error::NONE;
  172. }
  173. check_return Error printLine() const {
  174. CORE_RETURN_ERROR(print());
  175. CORE_RETURN_ERROR(Core::putChar('\n'));
  176. return Error::NONE;
  177. }
  178. template<typename... Args>
  179. check_return Error format(Args&&... args) {
  180. ArrayString s;
  181. CORE_RETURN_ERROR(formatBuffer(s, 0, Core::forward<Args>(args)...));
  182. *this = s;
  183. return Error::NONE;
  184. }
  185. template<int L>
  186. bool startsWidth(const ArrayString<L>& other, int from = 0) const {
  187. if(from + other.getLength() > length) {
  188. return false;
  189. }
  190. for(int i = 0; i < other.getLength(); i++) {
  191. if(data[from + i] != other[i]) {
  192. return false;
  193. }
  194. }
  195. return true;
  196. }
  197. template<int L>
  198. int search(const ArrayString<L>& other, int from = 0) const {
  199. for(int i = from; i < length; i++) {
  200. if(startsWidth(other, i)) {
  201. return i;
  202. }
  203. }
  204. return -1;
  205. }
  206. template<int L>
  207. bool contains(const ArrayString<L>& other, int from = 0) const {
  208. return search(other, from) >= 0;
  209. }
  210. int search(u32 u, int from = 0) const {
  211. for(int i = from; i < length; i++) {
  212. if(data[i] == u) {
  213. return i;
  214. }
  215. }
  216. return -1;
  217. }
  218. bool contains(u32 u, int from = 0) const {
  219. return search(u, from) >= 0;
  220. }
  221. ArrayString substring(int from, int to) const {
  222. from = Math::max(from, 0);
  223. to = Math::min(to, length - 1);
  224. ArrayString<N> s;
  225. for(int i = from; i <= to; i++) {
  226. (void)s.appendUnicode(data[i]);
  227. }
  228. return s;
  229. }
  230. ArrayString substring(int from = 0) const {
  231. return substring(from, length - 1);
  232. }
  233. template<int L1, int L2>
  234. check_return Error replace(const ArrayString<L1>& search,
  235. const ArrayString<L2>& replace) {
  236. ArrayString<N> s;
  237. int i = 0;
  238. while(i < length) {
  239. if(startsWidth(search, i)) {
  240. CORE_RETURN_ERROR(s.append(replace));
  241. i += search.getLength();
  242. } else {
  243. CORE_RETURN_ERROR(s.appendUnicode(data[i]));
  244. i++;
  245. }
  246. }
  247. *this = s;
  248. return Error::NONE;
  249. }
  250. void replace(u32 search, u32 replace) {
  251. hash = 0;
  252. for(int i = 0; i < length; i++) {
  253. if(data[i] == search) {
  254. data[i] = replace;
  255. }
  256. addToHash(data[i]);
  257. }
  258. }
  259. private:
  260. check_return static Error printChar(u32 u, u32 shift, u32 a, u32 o) {
  261. return Core::putChar(static_cast<int>(((u >> shift) & a) | o));
  262. }
  263. static u32 read(const char*& s) {
  264. if(*s == '\0') {
  265. return 0;
  266. }
  267. return static_cast<u32>(*(s++));
  268. }
  269. static Error readUnicode(u32& u, const char*& s) {
  270. u = read(s);
  271. if((u & 0x80) == 0) {
  272. return Error::NONE;
  273. }
  274. if((u & 0xE0) == 0xC0) {
  275. u32 u2 = read(s);
  276. if(u2 == 0) {
  277. return Error::INVALID_CHAR;
  278. }
  279. u = ((u & 0x1F) << 6) | (u2 & 0x3F);
  280. return Error::NONE;
  281. } else if((u & 0xF0) == 0xE0) {
  282. u32 u2 = read(s);
  283. u32 u3 = read(s);
  284. if(u2 == 0 || u3 == 0) {
  285. return Error::INVALID_CHAR;
  286. }
  287. u = ((u & 0xF) << 12) | ((u2 & 0x3F) << 6) | (u3 & 0x3F);
  288. return Error::NONE;
  289. } else if((u & 0xF8) == 0xF0) {
  290. u32 u2 = read(s);
  291. u32 u3 = read(s);
  292. u32 u4 = read(s);
  293. if(u2 == 0 || u3 == 0 || u4 == 0) {
  294. return Error::INVALID_CHAR;
  295. }
  296. u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) |
  297. ((u3 & 0x3F) << 6) | (u4 & 0x3F);
  298. return Error::NONE;
  299. }
  300. return Error::INVALID_CHAR;
  301. }
  302. void addToHash(u32 u) {
  303. hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
  304. }
  305. template<typename T, typename... Args>
  306. check_return Error formatBuffer(ArrayString& s, int index, const T& t,
  307. Args&&... args) {
  308. while(index < length) {
  309. u32 u = data[index++];
  310. if(u == '#') {
  311. if(index >= length ||
  312. (index < length && data[index] != '#')) {
  313. break;
  314. }
  315. index++;
  316. }
  317. CORE_RETURN_ERROR(s.appendUnicode(u));
  318. }
  319. CORE_RETURN_ERROR(s.append(t));
  320. return formatBuffer(s, index, Core::forward<Args>(args)...);
  321. }
  322. check_return Error formatBuffer(ArrayString& s, int index) {
  323. while(index < length) {
  324. CORE_RETURN_ERROR(s.appendUnicode(data[index++]));
  325. }
  326. return Error::NONE;
  327. }
  328. template<typename T>
  329. check_return Error convertAppend(T t) {
  330. char buffer[64];
  331. CORE_RETURN_ERROR(Core::toString(t, buffer, CORE_SIZE(buffer)));
  332. return append(static_cast<const char*>(buffer));
  333. }
  334. };
  335. template<int L, typename Iterable>
  336. check_return Error toString(ArrayString<L>& s, const Iterable& i) {
  337. CORE_RETURN_ERROR(s.append("["));
  338. auto current = i.begin();
  339. auto end = i.end();
  340. while(current != end) {
  341. CORE_RETURN_ERROR(s.append(*current));
  342. ++current;
  343. if(current != end) {
  344. CORE_RETURN_ERROR(s.append(", "));
  345. }
  346. }
  347. return s.append("]");
  348. }
  349. }
  350. template<int N>
  351. bool operator==(const char* cs, const Core::ArrayString<N>& s) {
  352. return s == cs;
  353. }
  354. template<int N>
  355. bool operator!=(const char* cs, const Core::ArrayString<N>& s) {
  356. return s != cs;
  357. }
  358. #endif