ArrayString.hpp 15 KB

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