ArrayString.hpp 14 KB

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