ArrayString.hpp 14 KB

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