ArrayString.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 CharType* s) const {
  18. for(int i = 0; i < length; i++, s++) {
  19. if(*s == '\0' && *s != data[i]) {
  20. return false;
  21. }
  22. }
  23. return *s == '\0';
  24. }
  25. template<int L>
  26. bool operator==(const ArrayString<L, CharType>& other) const {
  27. if(length != other.getLength()) {
  28. return false;
  29. }
  30. for(int i = 0; i < length; i++) {
  31. if(data[i] != other[i]) {
  32. return false;
  33. }
  34. }
  35. return true;
  36. }
  37. bool operator!=(const CharType* s) const {
  38. return !((*this) == s);
  39. }
  40. template<int L>
  41. bool operator!=(const ArrayString<L, CharType>& other) const {
  42. return !((*this) == other);
  43. }
  44. CharType operator[](int index) const {
  45. return data[index];
  46. }
  47. int getLength() const {
  48. return length;
  49. }
  50. constexpr int getCapacity() const {
  51. return N - 1;
  52. }
  53. check_return Error append(char c) {
  54. return add(static_cast<CharType>(c));
  55. }
  56. check_return Error append(signed char c) {
  57. return append(static_cast<char>(c));
  58. }
  59. check_return Error append(unsigned char c) {
  60. return append(static_cast<char>(c));
  61. }
  62. check_return Error append(wchar_t c) {
  63. return append(static_cast<c32>(c));
  64. }
  65. check_return Error append(c32 c) {
  66. if constexpr(IsSame<CharType, char>) {
  67. char buffer[5];
  68. unicodeToChar(c, buffer);
  69. return append(static_cast<const char*>(buffer));
  70. } else {
  71. return add(c);
  72. }
  73. }
  74. check_return Error append(const char* s) {
  75. if constexpr(IsSame<CharType, char>) {
  76. // stringLength as s could be some part of data
  77. for(int i = stringLength(s); i > 0; i--) {
  78. CORE_RETURN_ERROR(append(*(s++)));
  79. }
  80. return Error::NONE;
  81. } else {
  82. while(true) {
  83. c32 u = 0;
  84. CORE_RETURN_ERROR(readUnicode(u, s));
  85. if(u == 0) {
  86. return Error::NONE;
  87. }
  88. CORE_RETURN_ERROR(append(u));
  89. }
  90. }
  91. }
  92. check_return Error append(const c32* s) {
  93. // stringLength as s could be some part of data
  94. for(int i = stringLength(s); i > 0; i--) {
  95. CORE_RETURN_ERROR(append(*(s++)));
  96. }
  97. return Error::NONE;
  98. }
  99. check_return Error append(const signed char* s) {
  100. return append(reinterpret_cast<const char*>(s));
  101. }
  102. check_return Error append(const unsigned char* s) {
  103. return append(reinterpret_cast<const char*>(s));
  104. }
  105. check_return Error append(signed short s) {
  106. return convertAppend(s);
  107. }
  108. check_return Error append(unsigned short s) {
  109. return convertAppend(s);
  110. }
  111. check_return Error append(signed int i) {
  112. return convertAppend(i);
  113. }
  114. check_return Error append(unsigned int i) {
  115. return convertAppend(i);
  116. }
  117. check_return Error append(signed long l) {
  118. return convertAppend(l);
  119. }
  120. check_return Error append(unsigned long l) {
  121. return convertAppend(l);
  122. }
  123. check_return Error append(signed long long ll) {
  124. return convertAppend(ll);
  125. }
  126. check_return Error append(unsigned long long ll) {
  127. return convertAppend(ll);
  128. }
  129. check_return Error append(float f) {
  130. return convertAppend(static_cast<double>(f));
  131. }
  132. check_return Error append(double d) {
  133. return convertAppend(d);
  134. }
  135. check_return Error append(long double ld) {
  136. return convertAppend(ld);
  137. }
  138. check_return Error append(bool b) {
  139. return b ? append("true") : append("false");
  140. }
  141. check_return Error append(Error e) {
  142. return append(getErrorName(e));
  143. }
  144. template<typename T>
  145. check_return Error append(const T& t) {
  146. return t.toString(*this);
  147. }
  148. template<int L, typename C>
  149. check_return Error toString(ArrayString<L, C>& s) const {
  150. if constexpr(IsSame<CharType, char> && !IsSame<C, char>) {
  151. // utf32 to utf8
  152. return s.append(data);
  153. }
  154. int l = length; // length changes if &s == this
  155. for(int i = 0; i < l; i++) {
  156. CORE_RETURN_ERROR(s.append(data[i]));
  157. }
  158. return Error::NONE;
  159. }
  160. void clear() {
  161. length = 0;
  162. hash = 0;
  163. data[0] = '\0';
  164. }
  165. u32 hashCode() const {
  166. return hash;
  167. }
  168. check_return Error print() const {
  169. if constexpr(IsSame<CharType, char>) {
  170. for(int i = 0; i < length; i++) {
  171. CORE_RETURN_ERROR(Core::putChar(data[i]));
  172. }
  173. return Error::NONE;
  174. } else {
  175. for(int i = 0; i < length; i++) {
  176. c32 c = data[i];
  177. if(c < (1 << 7)) {
  178. CORE_RETURN_ERROR(printChar(c, 0, 0x7F, 0x0));
  179. } else if(c < (1 << 11)) {
  180. CORE_RETURN_ERROR(printChar(c, 6, 0x1F, 0xC0));
  181. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  182. } else if(c < (1 << 16)) {
  183. CORE_RETURN_ERROR(printChar(c, 12, 0x0F, 0xE0));
  184. CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
  185. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  186. } else if(c < (1 << 21)) {
  187. CORE_RETURN_ERROR(printChar(c, 18, 0x07, 0xF0));
  188. CORE_RETURN_ERROR(printChar(c, 12, 0x3F, 0x80));
  189. CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
  190. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  191. }
  192. }
  193. return Error::NONE;
  194. }
  195. }
  196. check_return Error printLine() const {
  197. CORE_RETURN_ERROR(print());
  198. CORE_RETURN_ERROR(Core::putChar('\n'));
  199. return Error::NONE;
  200. }
  201. template<typename... Args>
  202. check_return Error format(Args&&... args) {
  203. ArrayString s;
  204. Error e = formatBuffer(s, 0, Core::forward<Args>(args)...);
  205. if(e == Error::NONE || e == Error::CAPACITY_REACHED) {
  206. *this = s;
  207. }
  208. return e;
  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 CharType*() const {
  287. return data;
  288. }
  289. private:
  290. Error add(CharType c) {
  291. if(length >= N - 1) {
  292. return Error::CAPACITY_REACHED;
  293. }
  294. data[length++] = c;
  295. data[length] = '\0';
  296. addToHash(static_cast<c32>(c));
  297. return Error::NONE;
  298. }
  299. template<unsigned int L>
  300. void unicodeToChar(c32 c, char (&buffer)[L]) {
  301. static_assert(L >= 5, "to small char buffer");
  302. if(c < (1 << 7)) {
  303. buffer[0] = static_cast<char>(((c >> 0) & 0x7F) | 0x0);
  304. buffer[1] = '\0';
  305. } else if(c < (1 << 11)) {
  306. buffer[0] = static_cast<char>(((c >> 6) & 0x1F) | 0xC0);
  307. buffer[1] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
  308. buffer[2] = '\0';
  309. } else if(c < (1 << 16)) {
  310. buffer[0] = static_cast<char>(((c >> 12) & 0x0F) | 0xE0);
  311. buffer[1] = static_cast<char>(((c >> 6) & 0x3F) | 0x80);
  312. buffer[2] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
  313. buffer[3] = '\0';
  314. } else if(c < (1 << 21)) {
  315. buffer[0] = static_cast<char>(((c >> 18) & 0x07) | 0xF0);
  316. buffer[1] = static_cast<char>(((c >> 12) & 0x3F) | 0x80);
  317. buffer[2] = static_cast<char>(((c >> 6) & 0x3F) | 0x80);
  318. buffer[3] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
  319. buffer[4] = '\0';
  320. } else {
  321. buffer[0] = '\0';
  322. }
  323. }
  324. check_return static Error printChar(c32 u, u32 shift, u32 a, u32 o) {
  325. return Core::putChar(static_cast<int>(((u >> shift) & a) | o));
  326. }
  327. static c32 read(const char*& s) {
  328. if(*s == '\0') {
  329. return 0;
  330. }
  331. return static_cast<c32>(*(s++));
  332. }
  333. static Error readUnicode(c32& u, const char*& s) {
  334. u = read(s);
  335. if((u & 0x80) == 0) {
  336. return Error::NONE;
  337. }
  338. if((u & 0xE0) == 0xC0) {
  339. c32 u2 = read(s);
  340. if(u2 == 0) {
  341. return Error::INVALID_CHAR;
  342. }
  343. u = ((u & 0x1F) << 6) | (u2 & 0x3F);
  344. return Error::NONE;
  345. } else if((u & 0xF0) == 0xE0) {
  346. c32 u2 = read(s);
  347. c32 u3 = read(s);
  348. if(u2 == 0 || u3 == 0) {
  349. return Error::INVALID_CHAR;
  350. }
  351. u = ((u & 0xF) << 12) | ((u2 & 0x3F) << 6) | (u3 & 0x3F);
  352. return Error::NONE;
  353. } else if((u & 0xF8) == 0xF0) {
  354. c32 u2 = read(s);
  355. c32 u3 = read(s);
  356. c32 u4 = read(s);
  357. if(u2 == 0 || u3 == 0 || u4 == 0) {
  358. return Error::INVALID_CHAR;
  359. }
  360. u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) |
  361. ((u3 & 0x3F) << 6) | (u4 & 0x3F);
  362. return Error::NONE;
  363. }
  364. return Error::INVALID_CHAR;
  365. }
  366. void addToHash(c32 u) {
  367. hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
  368. }
  369. template<typename T, typename... Args>
  370. check_return Error formatBuffer(ArrayString& s, int index, const T& t,
  371. Args&&... args) {
  372. while(index < length) {
  373. CharType u = data[index++];
  374. if(u == '#') {
  375. if(index >= length ||
  376. (index < length && data[index] != '#')) {
  377. break;
  378. }
  379. index++;
  380. }
  381. CORE_RETURN_ERROR(s.append(u));
  382. }
  383. CORE_RETURN_ERROR(s.append(t));
  384. return formatBuffer(s, index, Core::forward<Args>(args)...);
  385. }
  386. check_return Error formatBuffer(ArrayString& s, int index) {
  387. while(index < length) {
  388. CORE_RETURN_ERROR(s.append(data[index++]));
  389. }
  390. return Error::NONE;
  391. }
  392. template<typename T>
  393. check_return Error convertAppend(T t) {
  394. char buffer[64];
  395. CORE_RETURN_ERROR(Core::toString(t, buffer, CORE_SIZE(buffer)));
  396. return append(static_cast<const char*>(buffer));
  397. }
  398. };
  399. template<typename String, typename Iterable>
  400. check_return Error toString(String& s, const Iterable& i) {
  401. CORE_RETURN_ERROR(s.append("["));
  402. auto current = i.begin();
  403. auto end = i.end();
  404. while(current != end) {
  405. CORE_RETURN_ERROR(s.append(*current));
  406. ++current;
  407. if(current != end) {
  408. CORE_RETURN_ERROR(s.append(", "));
  409. }
  410. }
  411. return s.append("]");
  412. }
  413. template<int N>
  414. using String8 = ArrayString<N, char>;
  415. template<int N>
  416. using String32 = ArrayString<N, char32_t>;
  417. }
  418. template<int N, typename CharType>
  419. bool operator==(const CharType* cs, const Core::ArrayString<N, CharType>& s) {
  420. return s == cs;
  421. }
  422. template<int N, typename CharType>
  423. bool operator!=(const CharType* cs, const Core::ArrayString<N, CharType>& s) {
  424. return s != cs;
  425. }
  426. #endif