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