ArrayString.h 15 KB

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