ArrayString.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include "core/utils/ArrayString.hpp"
  2. #include "core/utils/Error.hpp"
  3. using CharString = Core::CharString;
  4. using Error = Core::Error;
  5. static c32 read(const char*& s) {
  6. if(*s == '\0') {
  7. return 0;
  8. }
  9. return static_cast<c32>(*(s++));
  10. }
  11. Error Core::readUnicode(c32& u, const char*& s) {
  12. u = read(s);
  13. if((u & 0x80) == 0) {
  14. return Error::NONE;
  15. }
  16. if((u & 0xE0) == 0xC0) {
  17. c32 u2 = read(s);
  18. if(u2 == 0) {
  19. return Error::INVALID_CHAR;
  20. }
  21. u = ((u & 0x1F) << 6) | (u2 & 0x3F);
  22. return Error::NONE;
  23. } else if((u & 0xF0) == 0xE0) {
  24. c32 u2 = read(s);
  25. c32 u3 = read(s);
  26. if(u2 == 0 || u3 == 0) {
  27. return Error::INVALID_CHAR;
  28. }
  29. u = ((u & 0xF) << 12) | ((u2 & 0x3F) << 6) | (u3 & 0x3F);
  30. return Error::NONE;
  31. } else if((u & 0xF8) == 0xF0) {
  32. c32 u2 = read(s);
  33. c32 u3 = read(s);
  34. c32 u4 = read(s);
  35. if(u2 == 0 || u3 == 0 || u4 == 0) {
  36. return Error::INVALID_CHAR;
  37. }
  38. u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) | ((u3 & 0x3F) << 6) |
  39. (u4 & 0x3F);
  40. return Error::NONE;
  41. }
  42. return Error::INVALID_CHAR;
  43. }
  44. template<unsigned int L>
  45. void unicodeToChar(c32 c, char (&buffer)[L]) {
  46. static_assert(L >= 5, "to small char buffer");
  47. buffer[0] = '\0';
  48. if(c < (1 << 7)) {
  49. buffer[0] = static_cast<char>(((c >> 0) & 0x7F) | 0x0);
  50. buffer[1] = '\0';
  51. } else if(c < (1 << 11)) {
  52. buffer[0] = static_cast<char>(((c >> 6) & 0x1F) | 0xC0);
  53. buffer[1] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
  54. buffer[2] = '\0';
  55. } else if(c < (1 << 16)) {
  56. buffer[0] = static_cast<char>(((c >> 12) & 0x0F) | 0xE0);
  57. buffer[1] = static_cast<char>(((c >> 6) & 0x3F) | 0x80);
  58. buffer[2] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
  59. buffer[3] = '\0';
  60. } else if(c < (1 << 21)) {
  61. buffer[0] = static_cast<char>(((c >> 18) & 0x07) | 0xF0);
  62. buffer[1] = static_cast<char>(((c >> 12) & 0x3F) | 0x80);
  63. buffer[2] = static_cast<char>(((c >> 6) & 0x3F) | 0x80);
  64. buffer[3] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
  65. buffer[4] = '\0';
  66. }
  67. }
  68. CharString::CharString(char* buffer, i32 bufferSize)
  69. : length(0), capacity(bufferSize), hash(0), data(buffer) {
  70. data[0] = '\0';
  71. }
  72. Error CharString::copyFrom(const CharString& s) {
  73. clear();
  74. return s.toString(*this);
  75. }
  76. bool CharString::operator==(const char* s) const {
  77. const char* p = data;
  78. while(*s == *p && *s != '\0') {
  79. s++;
  80. p++;
  81. }
  82. return *s == *p;
  83. }
  84. bool CharString::operator==(const CharString& other) const {
  85. if(length != other.getLength()) {
  86. return false;
  87. }
  88. for(int i = 0; i < length; i++) {
  89. if(data[i] != other[i]) {
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. bool CharString::operator!=(const char* s) const {
  96. return !((*this) == s);
  97. }
  98. bool CharString::operator!=(const CharString& other) const {
  99. return !((*this) == other);
  100. }
  101. char CharString::operator[](int index) const {
  102. return data[index];
  103. }
  104. int CharString::getLength() const {
  105. return length;
  106. }
  107. int CharString::getCapacity() const {
  108. return capacity - 1;
  109. }
  110. Error CharString::append(char c) {
  111. return add(c);
  112. }
  113. Error CharString::append(signed char c) {
  114. return append(static_cast<char>(c));
  115. }
  116. Error CharString::append(unsigned char c) {
  117. return append(static_cast<char>(c));
  118. }
  119. Error CharString::append(wchar_t c) {
  120. return append(static_cast<c32>(c));
  121. }
  122. Error CharString::append(c32 c) {
  123. char buffer[5];
  124. unicodeToChar(c, buffer);
  125. return append(static_cast<const char*>(buffer));
  126. }
  127. Error CharString::append(const char* s) {
  128. // stringLength as s could be some part of data
  129. for(int i = stringLength(s); i > 0; i--) {
  130. CORE_RETURN_ERROR(append(*(s++)));
  131. }
  132. return Error::NONE;
  133. }
  134. Error CharString::append(const c32* s) {
  135. // stringLength as s could be some part of data
  136. for(int i = stringLength(s); i > 0; i--) {
  137. CORE_RETURN_ERROR(append(*(s++)));
  138. }
  139. return Error::NONE;
  140. }
  141. Error CharString::append(const signed char* s) {
  142. return append(reinterpret_cast<const char*>(s));
  143. }
  144. Error CharString::append(const unsigned char* s) {
  145. return append(reinterpret_cast<const char*>(s));
  146. }
  147. Error CharString::append(signed short s) {
  148. return convertAppend(s);
  149. }
  150. Error CharString::append(unsigned short s) {
  151. return convertAppend(s);
  152. }
  153. Error CharString::append(signed int i) {
  154. return convertAppend(i);
  155. }
  156. Error CharString::append(unsigned int i) {
  157. return convertAppend(i);
  158. }
  159. Error CharString::append(signed long l) {
  160. return convertAppend(l);
  161. }
  162. Error CharString::append(unsigned long l) {
  163. return convertAppend(l);
  164. }
  165. Error CharString::append(signed long long ll) {
  166. return convertAppend(ll);
  167. }
  168. Error CharString::append(unsigned long long ll) {
  169. return convertAppend(ll);
  170. }
  171. Error CharString::append(float f) {
  172. return convertAppend(f);
  173. }
  174. Error CharString::append(double d) {
  175. return convertAppend(d);
  176. }
  177. Error CharString::append(long double ld) {
  178. return convertAppend(ld);
  179. }
  180. Error CharString::append(bool b) {
  181. return b ? append("true") : append("false");
  182. }
  183. Error CharString::append(Error e) {
  184. return append(getErrorName(e));
  185. }
  186. Error CharString::toString(CharString& s) const {
  187. int l = length; // length changes if &s == this
  188. for(int i = 0; i < l; i++) {
  189. CORE_RETURN_ERROR(s.append(data[i]));
  190. }
  191. return Error::NONE;
  192. }
  193. void CharString::clear() {
  194. length = 0;
  195. hash = 0;
  196. data[0] = '\0';
  197. }
  198. u32 CharString::hashCode() const {
  199. return hash;
  200. }
  201. Error CharString::print() const {
  202. for(int i = 0; i < length; i++) {
  203. CORE_RETURN_ERROR(Core::putChar(data[i]));
  204. }
  205. return Error::NONE;
  206. }
  207. Error CharString::printLine() const {
  208. CORE_RETURN_ERROR(print());
  209. CORE_RETURN_ERROR(Core::putChar('\n'));
  210. return Error::NONE;
  211. }
  212. bool CharString::startsWidth(const CharString& other, int from) const {
  213. if(from > length - other.getLength()) {
  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. int CharString::search(const CharString& other, int from) const {
  224. for(int i = from; i < length; i++) {
  225. if(startsWidth(other, i)) {
  226. return i;
  227. }
  228. }
  229. return -1;
  230. }
  231. bool CharString::contains(const CharString& other, int from) const {
  232. return search(other, from) >= 0;
  233. }
  234. int CharString::search(char u, int from) const {
  235. for(int i = from; i < length; i++) {
  236. if(data[i] == u) {
  237. return i;
  238. }
  239. }
  240. return -1;
  241. }
  242. bool CharString::contains(char u, int from) const {
  243. return search(u, from) >= 0;
  244. }
  245. Error CharString::substring(CharString& s, int from, int to) const {
  246. s.clear();
  247. from = Math::max(from, 0);
  248. to = Math::min(to, length - 1);
  249. for(int i = from; i <= to; i++) {
  250. CORE_RETURN_ERROR(s.append(data[i]));
  251. }
  252. return Error::NONE;
  253. }
  254. Error CharString::substring(CharString& s, int from) const {
  255. return substring(s, from, length - 1);
  256. }
  257. Error CharString::replace(CharString& s, const CharString& search,
  258. const CharString& replace) {
  259. int i = 0;
  260. while(i < length) {
  261. if(startsWidth(search, i)) {
  262. CORE_RETURN_ERROR(s.append(replace));
  263. i += search.getLength();
  264. } else {
  265. CORE_RETURN_ERROR(s.append(data[i]));
  266. i++;
  267. }
  268. }
  269. return copyFrom(s);
  270. }
  271. void CharString::replace(char search, char replace) {
  272. hash = 0;
  273. for(int i = 0; i < length; i++) {
  274. if(data[i] == search) {
  275. data[i] = replace;
  276. }
  277. addToHash(static_cast<c32>(data[i]));
  278. }
  279. }
  280. CharString::operator const char*() const {
  281. return data;
  282. }
  283. Error CharString::add(char c) {
  284. if(length >= capacity - 1) {
  285. return Error::CAPACITY_REACHED;
  286. }
  287. data[length++] = c;
  288. data[length] = '\0';
  289. addToHash(static_cast<c32>(c));
  290. return Error::NONE;
  291. }
  292. void CharString::addToHash(c32 u) {
  293. hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
  294. }
  295. Error CharString::formatBuffer(CharString& s, int index) {
  296. while(index < length) {
  297. CORE_RETURN_ERROR(s.append(data[index++]));
  298. }
  299. return Error::NONE;
  300. }