ArrayString.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. if(length >= capacity - 1) {
  112. return Error::CAPACITY_REACHED;
  113. }
  114. data[length++] = c;
  115. data[length] = '\0';
  116. addToHash(static_cast<c32>(c));
  117. return Error::NONE;
  118. }
  119. Error CharString::append(signed char c) {
  120. return append(static_cast<char>(c));
  121. }
  122. Error CharString::append(unsigned char c) {
  123. return append(static_cast<char>(c));
  124. }
  125. Error CharString::append(wchar_t c) {
  126. return append(static_cast<c32>(c));
  127. }
  128. Error CharString::append(c32 c) {
  129. char buffer[5];
  130. unicodeToChar(c, buffer);
  131. return append(static_cast<const char*>(buffer));
  132. }
  133. Error CharString::append(const char* s) {
  134. // stringLength as s could be some part of data
  135. for(int i = stringLength(s); i > 0; i--) {
  136. CORE_RETURN_ERROR(append(*(s++)));
  137. }
  138. return Error::NONE;
  139. }
  140. Error CharString::append(const c32* s) {
  141. // stringLength as s could be some part of data
  142. for(int i = stringLength(s); i > 0; i--) {
  143. CORE_RETURN_ERROR(append(*(s++)));
  144. }
  145. return Error::NONE;
  146. }
  147. Error CharString::append(const signed char* s) {
  148. return append(reinterpret_cast<const char*>(s));
  149. }
  150. Error CharString::append(const unsigned char* s) {
  151. return append(reinterpret_cast<const char*>(s));
  152. }
  153. Error CharString::append(bool b) {
  154. return b ? append("true") : append("false");
  155. }
  156. Error CharString::append(Error e) {
  157. return append(getErrorName(e));
  158. }
  159. Error CharString::toString(CharString& s) const {
  160. int l = length; // length changes if &s == this
  161. for(int i = 0; i < l; i++) {
  162. CORE_RETURN_ERROR(s.append(data[i]));
  163. }
  164. return Error::NONE;
  165. }
  166. void CharString::clear() {
  167. length = 0;
  168. hash = 0;
  169. data[0] = '\0';
  170. }
  171. u32 CharString::hashCode() const {
  172. return hash;
  173. }
  174. Error CharString::print() const {
  175. for(int i = 0; i < length; i++) {
  176. CORE_RETURN_ERROR(Core::putChar(data[i]));
  177. }
  178. return Error::NONE;
  179. }
  180. Error CharString::printLine() const {
  181. CORE_RETURN_ERROR(print());
  182. CORE_RETURN_ERROR(Core::putChar('\n'));
  183. return Error::NONE;
  184. }
  185. bool CharString::startsWidth(const CharString& other, int from) const {
  186. if(from > length - other.getLength()) {
  187. return false;
  188. }
  189. for(int i = 0; i < other.getLength(); i++) {
  190. if(data[from + i] != other[i]) {
  191. return false;
  192. }
  193. }
  194. return true;
  195. }
  196. int CharString::search(const CharString& other, int from) const {
  197. for(int i = from; i < length; i++) {
  198. if(startsWidth(other, i)) {
  199. return i;
  200. }
  201. }
  202. return -1;
  203. }
  204. bool CharString::contains(const CharString& other, int from) const {
  205. return search(other, from) >= 0;
  206. }
  207. int CharString::search(char u, int from) const {
  208. for(int i = from; i < length; i++) {
  209. if(data[i] == u) {
  210. return i;
  211. }
  212. }
  213. return -1;
  214. }
  215. bool CharString::contains(char u, int from) const {
  216. return search(u, from) >= 0;
  217. }
  218. Error CharString::substring(CharString& s, int from, int to) const {
  219. s.clear();
  220. from = Math::max(from, 0);
  221. to = Math::min(to, length - 1);
  222. for(int i = from; i <= to; i++) {
  223. CORE_RETURN_ERROR(s.append(data[i]));
  224. }
  225. return Error::NONE;
  226. }
  227. Error CharString::substring(CharString& s, int from) const {
  228. return substring(s, from, length - 1);
  229. }
  230. Error CharString::replace(CharString& s, const CharString& search,
  231. const CharString& replace) {
  232. int i = 0;
  233. while(i < length) {
  234. if(startsWidth(search, i)) {
  235. CORE_RETURN_ERROR(s.append(replace));
  236. i += search.getLength();
  237. } else {
  238. CORE_RETURN_ERROR(s.append(data[i]));
  239. i++;
  240. }
  241. }
  242. return copyFrom(s);
  243. }
  244. void CharString::replace(char search, char replace) {
  245. hash = 0;
  246. for(int i = 0; i < length; i++) {
  247. if(data[i] == search) {
  248. data[i] = replace;
  249. }
  250. addToHash(static_cast<c32>(data[i]));
  251. }
  252. }
  253. CharString::operator const char*() const {
  254. return data;
  255. }
  256. void CharString::addToHash(c32 u) {
  257. hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
  258. }
  259. Error CharString::formatBuffer(CharString& s, int index) {
  260. while(index < length) {
  261. CORE_RETURN_ERROR(s.append(data[index++]));
  262. }
  263. return Error::NONE;
  264. }