ArrayString.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #ifndef CORE_STRING_H
  2. #define CORE_STRING_H
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include "math/Math.h"
  6. #include "utils/Check.h"
  7. #include "utils/Types.h"
  8. #include "utils/Utility.h"
  9. namespace Core {
  10. template<int N>
  11. class ArrayString final {
  12. public:
  13. int length;
  14. Hash hash;
  15. static constexpr int DATA_LENGTH =
  16. (N - Math::max(IntSize(length), IntSize(hash))) / IntSize(u32);
  17. static_assert(DATA_LENGTH > 0, "Size of array string too small");
  18. u32 data[static_cast<unsigned int>(DATA_LENGTH)];
  19. public:
  20. ArrayString() : length(0), hash(0) {
  21. data[0] = '\0';
  22. }
  23. bool operator==(const char* s) const {
  24. for(int i = 0; i < length; i++) {
  25. u32 u = 0;
  26. if(readUnicode(u, s) || data[i] != u) {
  27. return false;
  28. }
  29. }
  30. return read(s) == 0;
  31. }
  32. template<int L>
  33. bool operator==(const ArrayString<L>& other) const {
  34. if(length != other.length) {
  35. return false;
  36. }
  37. for(int i = 0; i < length; i++) {
  38. if(data[i] != other[i]) {
  39. return false;
  40. }
  41. }
  42. return true;
  43. }
  44. bool operator!=(const char* s) const {
  45. return !((*this) == s);
  46. }
  47. template<int L>
  48. bool operator!=(const ArrayString<L>& other) const {
  49. return !((*this) == other);
  50. }
  51. u32 operator[](int index) const {
  52. return data[index];
  53. }
  54. int getLength() const {
  55. return length;
  56. }
  57. constexpr int getCapacity() const {
  58. return DATA_LENGTH;
  59. }
  60. // returns true on error
  61. check_return bool append(char c) {
  62. if(c < 0) {
  63. return true;
  64. }
  65. return appendUnicode(static_cast<u32>(c));
  66. }
  67. // returns true on error
  68. check_return bool append(signed char c) {
  69. if(c < 0) {
  70. return true;
  71. }
  72. return appendUnicode(static_cast<u32>(c));
  73. }
  74. // returns true on error
  75. check_return bool append(unsigned char c) {
  76. return appendUnicode(c);
  77. }
  78. // returns true on error
  79. check_return bool append(const char* s) {
  80. while(true) {
  81. u32 u = 0;
  82. if(readUnicode(u, s)) {
  83. return true;
  84. } else if(u == 0) {
  85. return false;
  86. } else if(appendUnicode(u)) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. // returns true on error
  93. check_return bool append(const signed char* s) {
  94. return append(reinterpret_cast<const char*>(s));
  95. }
  96. // returns true on error
  97. check_return bool append(const unsigned char* s) {
  98. return append(reinterpret_cast<const char*>(s));
  99. }
  100. // returns true on error
  101. check_return bool append(signed short s) {
  102. return appendFormat("%hd", s);
  103. }
  104. // returns true on error
  105. check_return bool append(unsigned short s) {
  106. return appendFormat("%hu", s);
  107. }
  108. // returns true on error
  109. check_return bool append(signed int i) {
  110. return appendFormat("%d", i);
  111. }
  112. // returns true on error
  113. check_return bool append(unsigned int i) {
  114. return appendFormat("%u", i);
  115. }
  116. // returns true on error
  117. check_return bool append(signed long l) {
  118. return appendFormat("%ld", l);
  119. }
  120. // returns true on error
  121. check_return bool append(unsigned long l) {
  122. return appendFormat("%lu", l);
  123. }
  124. // returns true on error
  125. check_return bool append(signed long long ll) {
  126. return appendFormat("%lld", ll);
  127. }
  128. // returns true on error
  129. check_return bool append(unsigned long long ll) {
  130. return appendFormat("%llu", ll);
  131. }
  132. // returns true on error
  133. check_return bool append(float f) {
  134. return appendFormat("%.2f", static_cast<double>(f));
  135. }
  136. // returns true on error
  137. check_return bool append(double d) {
  138. return appendFormat("%.2f", d);
  139. }
  140. // returns true on error
  141. check_return bool append(long double ld) {
  142. return appendFormat("%.2Lf", ld);
  143. }
  144. // returns true on error
  145. check_return bool append(bool b) {
  146. return b ? append("true") : append("false");
  147. }
  148. // returns true on error
  149. check_return bool appendUnicode(u32 c) {
  150. if(length >= DATA_LENGTH) {
  151. return true;
  152. }
  153. data[length++] = c;
  154. addToHash(c);
  155. return false;
  156. }
  157. // returns true on error
  158. template<typename T>
  159. check_return bool append(const T& t) {
  160. return t.toString(*this);
  161. }
  162. // returns true on error
  163. template<int L>
  164. check_return bool toString(ArrayString<L>& s) const {
  165. int l = length; // length changes if &s == this
  166. for(int i = 0; i < l; i++) {
  167. if(s.appendUnicode(data[i])) {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. void clear() {
  174. length = 0;
  175. hash = 0;
  176. data[0] = '\0';
  177. }
  178. Hash hashCode() const {
  179. return hash;
  180. }
  181. // returns true on error
  182. check_return bool print() const {
  183. for(int i = 0; i < length; i++) {
  184. u32 c = data[i];
  185. if(c < (1 << 7)) {
  186. if(putchar(static_cast<int>(c & 0x7F)) == EOF) {
  187. return true;
  188. }
  189. } else if(c < (1 << 11)) {
  190. if(printChar(c, 6, 0x1F, 0xC0) ||
  191. printChar(c, 0, 0x3F, 0x80)) {
  192. return true;
  193. }
  194. } else if(c < (1 << 16)) {
  195. if(printChar(c, 12, 0x0F, 0xE0) ||
  196. printChar(c, 6, 0x3F, 0x80) ||
  197. printChar(c, 0, 0x3F, 0x80)) {
  198. return true;
  199. }
  200. } else if(c < (1 << 21)) {
  201. if(printChar(c, 18, 0x07, 0xF0) ||
  202. printChar(c, 12, 0x3F, 0x80) ||
  203. printChar(c, 6, 0x3F, 0x80) ||
  204. printChar(c, 0, 0x3F, 0x80)) {
  205. return true;
  206. }
  207. }
  208. }
  209. return false;
  210. }
  211. // returns true on error
  212. check_return bool printLine() const {
  213. return print() || putchar('\n') == EOF;
  214. }
  215. // returns true on error
  216. template<typename... Args>
  217. check_return bool format(Args&&... args) {
  218. ArrayString s;
  219. if(formatBuffer(s, 0, Core::forward<Args>(args)...)) {
  220. return true;
  221. }
  222. *this = s;
  223. return false;
  224. }
  225. private:
  226. static bool printChar(u32 u, u32 shift, u32 a, u32 o) {
  227. return putchar(static_cast<int>(((u >> shift) & a) | o)) == EOF;
  228. }
  229. static u32 read(const char*& s) {
  230. if(*s == '\0') {
  231. return 0;
  232. }
  233. return static_cast<u32>(*(s++));
  234. }
  235. // returns true on error
  236. static bool readUnicode(u32& u, const char*& s) {
  237. u = read(s);
  238. if((u & 0x80) == 0) {
  239. return false;
  240. }
  241. if((u & 0xE0) == 0xC0) {
  242. u32 u2 = read(s);
  243. if(u2 == 0) {
  244. return true;
  245. }
  246. u = ((u & 0x1F) << 6) | (u2 & 0x3F);
  247. return false;
  248. } else if((u & 0xF0) == 0xE0) {
  249. u32 u2 = read(s);
  250. u32 u3 = read(s);
  251. if(u2 == 0 || u3 == 0) {
  252. return true;
  253. }
  254. u = ((u & 0xF) << 12) | ((u2 & 0x3F) << 6) | (u3 & 0x3F);
  255. return false;
  256. } else if((u & 0xF8) == 0xF0) {
  257. u32 u2 = read(s);
  258. u32 u3 = read(s);
  259. u32 u4 = read(s);
  260. if(u2 == 0 || u3 == 0 || u4 == 0) {
  261. return true;
  262. }
  263. u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) |
  264. ((u3 & 0x3F) << 6) | (u4 & 0x3F);
  265. return false;
  266. }
  267. return true;
  268. }
  269. void addToHash(u32 u) {
  270. hash = static_cast<Hash>(2120251889) * hash + static_cast<Hash>(u);
  271. }
  272. // returns true on error
  273. template<typename T, typename... Args>
  274. check_return bool formatBuffer(ArrayString& s, int index, const T& t,
  275. Args&&... args) {
  276. while(index < length) {
  277. u32 u = data[index++];
  278. if(u == '#') {
  279. if(index < length && data[index] != '#') {
  280. break;
  281. }
  282. index++;
  283. }
  284. if(s.appendUnicode(u)) {
  285. return true;
  286. }
  287. }
  288. if(s.append(t)) {
  289. return true;
  290. }
  291. return formatBuffer(s, index, Core::forward<Args>(args)...);
  292. }
  293. // returns true on error
  294. check_return bool formatBuffer(ArrayString& s, int index) {
  295. while(index < length) {
  296. if(s.appendUnicode(data[index++])) {
  297. return true;
  298. }
  299. }
  300. return false;
  301. }
  302. // returns true on error
  303. check_format(2, 3) check_return
  304. bool appendFormat(const char* format, ...) {
  305. constexpr int BUFFER_SIZE = 64;
  306. char buffer[BUFFER_SIZE];
  307. va_list args;
  308. va_start(args, format);
  309. int written = vsnprintf(buffer, BUFFER_SIZE, format, args);
  310. va_end(args);
  311. return written >= BUFFER_SIZE ||
  312. append(static_cast<const char*>(buffer));
  313. }
  314. };
  315. }
  316. template<int N>
  317. bool operator==(const char* cs, const Core::ArrayString<N>& s) {
  318. return s == cs;
  319. }
  320. template<int N>
  321. bool operator!=(const char* cs, const Core::ArrayString<N>& s) {
  322. return s != cs;
  323. }
  324. #endif