ArrayString.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #include "core/utils/ArrayString.hpp"
  2. #include "core/utils/Error.hpp"
  3. using CharString = Core::CharString;
  4. using Char32String = Core::Char32String;
  5. using Error = Core::Error;
  6. static c32 read(const char*& s) {
  7. if(*s == '\0') {
  8. return 0;
  9. }
  10. return static_cast<c32>(*(s++));
  11. }
  12. Error Core::readUnicode(c32& u, const char*& s) {
  13. u = read(s);
  14. if((u & 0x80) == 0) {
  15. return Error::NONE;
  16. }
  17. if((u & 0xE0) == 0xC0) {
  18. c32 u2 = read(s);
  19. if(u2 == 0) {
  20. return Error::INVALID_CHAR;
  21. }
  22. u = ((u & 0x1F) << 6) | (u2 & 0x3F);
  23. return Error::NONE;
  24. } else if((u & 0xF0) == 0xE0) {
  25. c32 u2 = read(s);
  26. c32 u3 = read(s);
  27. if(u2 == 0 || u3 == 0) {
  28. return Error::INVALID_CHAR;
  29. }
  30. u = ((u & 0xF) << 12) | ((u2 & 0x3F) << 6) | (u3 & 0x3F);
  31. return Error::NONE;
  32. } else if((u & 0xF8) == 0xF0) {
  33. c32 u2 = read(s);
  34. c32 u3 = read(s);
  35. c32 u4 = read(s);
  36. if(u2 == 0 || u3 == 0 || u4 == 0) {
  37. return Error::INVALID_CHAR;
  38. }
  39. u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) | ((u3 & 0x3F) << 6) |
  40. (u4 & 0x3F);
  41. return Error::NONE;
  42. }
  43. return Error::INVALID_CHAR;
  44. }
  45. template<unsigned int L>
  46. static void unicodeToChar(c32 c, char (&buffer)[L]) {
  47. static_assert(L >= 5, "to small char buffer");
  48. buffer[0] = '\0';
  49. if(c < (1 << 7)) {
  50. buffer[0] = static_cast<char>(((c >> 0) & 0x7F) | 0x0);
  51. buffer[1] = '\0';
  52. } else if(c < (1 << 11)) {
  53. buffer[0] = static_cast<char>(((c >> 6) & 0x1F) | 0xC0);
  54. buffer[1] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
  55. buffer[2] = '\0';
  56. } else if(c < (1 << 16)) {
  57. buffer[0] = static_cast<char>(((c >> 12) & 0x0F) | 0xE0);
  58. buffer[1] = static_cast<char>(((c >> 6) & 0x3F) | 0x80);
  59. buffer[2] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
  60. buffer[3] = '\0';
  61. } else if(c < (1 << 21)) {
  62. buffer[0] = static_cast<char>(((c >> 18) & 0x07) | 0xF0);
  63. buffer[1] = static_cast<char>(((c >> 12) & 0x3F) | 0x80);
  64. buffer[2] = static_cast<char>(((c >> 6) & 0x3F) | 0x80);
  65. buffer[3] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
  66. buffer[4] = '\0';
  67. }
  68. }
  69. static Error printChar(c32 u, u32 shift, u32 a, u32 o) {
  70. return Core::putChar(static_cast<int>(((u >> shift) & a) | o));
  71. }
  72. CharString::CharString(char* buffer, i32 bufferSize)
  73. : length(0), capacity(bufferSize), hash(0), data(buffer) {
  74. data[0] = '\0';
  75. }
  76. Error CharString::copyFrom(const CharString& s) {
  77. clear();
  78. return s.toString(*this);
  79. }
  80. bool CharString::operator==(const char* s) const {
  81. const char* p = data;
  82. while(*s == *p && *s != '\0') {
  83. s++;
  84. p++;
  85. }
  86. return *s == *p;
  87. }
  88. bool CharString::operator==(const CharString& other) const {
  89. if(length != other.getLength()) {
  90. return false;
  91. }
  92. for(int i = 0; i < length; i++) {
  93. if(data[i] != other[i]) {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. bool CharString::operator!=(const char* s) const {
  100. return !((*this) == s);
  101. }
  102. bool CharString::operator!=(const CharString& other) const {
  103. return !((*this) == other);
  104. }
  105. char CharString::operator[](int index) const {
  106. return data[index];
  107. }
  108. int CharString::getLength() const {
  109. return length;
  110. }
  111. int CharString::getCapacity() const {
  112. return capacity - 1;
  113. }
  114. Error CharString::append(char c) {
  115. if(length >= capacity - 1) {
  116. return Error::CAPACITY_REACHED;
  117. }
  118. data[length++] = c;
  119. data[length] = '\0';
  120. addToHash(static_cast<c32>(c));
  121. return Error::NONE;
  122. }
  123. Error CharString::append(signed char c) {
  124. return append(static_cast<char>(c));
  125. }
  126. Error CharString::append(unsigned char c) {
  127. return append(static_cast<char>(c));
  128. }
  129. Error CharString::append(wchar_t c) {
  130. return append(static_cast<c32>(c));
  131. }
  132. Error CharString::append(c32 c) {
  133. char buffer[5];
  134. unicodeToChar(c, buffer);
  135. return append(static_cast<const char*>(buffer));
  136. }
  137. Error CharString::append(const char* s) {
  138. // stringLength as s could be some part of data
  139. for(int i = stringLength(s); i > 0; i--) {
  140. CORE_RETURN_ERROR(append(*(s++)));
  141. }
  142. return Error::NONE;
  143. }
  144. Error CharString::append(const c32* s) {
  145. // stringLength as s could be some part of data
  146. for(int i = stringLength(s); i > 0; i--) {
  147. CORE_RETURN_ERROR(append(*(s++)));
  148. }
  149. return Error::NONE;
  150. }
  151. Error CharString::append(const signed char* s) {
  152. return append(reinterpret_cast<const char*>(s));
  153. }
  154. Error CharString::append(const unsigned char* s) {
  155. return append(reinterpret_cast<const char*>(s));
  156. }
  157. Error CharString::append(bool b) {
  158. return b ? append("true") : append("false");
  159. }
  160. Error CharString::append(Error e) {
  161. return append(getErrorName(e));
  162. }
  163. Error CharString::toString(CharString& s) const {
  164. int l = length; // length changes if &s == this
  165. for(int i = 0; i < l; i++) {
  166. CORE_RETURN_ERROR(s.append(data[i]));
  167. }
  168. return Error::NONE;
  169. }
  170. Error CharString::toString(Char32String& s) const {
  171. return s.append(static_cast<const char*>(data));
  172. }
  173. void CharString::clear() {
  174. length = 0;
  175. hash = 0;
  176. data[0] = '\0';
  177. }
  178. u32 CharString::hashCode() const {
  179. return hash;
  180. }
  181. Error CharString::print() const {
  182. for(int i = 0; i < length; i++) {
  183. CORE_RETURN_ERROR(Core::putChar(data[i]));
  184. }
  185. return Error::NONE;
  186. }
  187. Error CharString::printLine() const {
  188. CORE_RETURN_ERROR(print());
  189. CORE_RETURN_ERROR(Core::putChar('\n'));
  190. return Error::NONE;
  191. }
  192. bool CharString::startsWidth(const CharString& other, int from) const {
  193. if(from > length - other.getLength()) {
  194. return false;
  195. }
  196. for(int i = 0; i < other.getLength(); i++) {
  197. if(data[from + i] != other[i]) {
  198. return false;
  199. }
  200. }
  201. return true;
  202. }
  203. int CharString::search(const CharString& other, int from) const {
  204. for(int i = from; i < length; i++) {
  205. if(startsWidth(other, i)) {
  206. return i;
  207. }
  208. }
  209. return -1;
  210. }
  211. bool CharString::contains(const CharString& other, int from) const {
  212. return search(other, from) >= 0;
  213. }
  214. int CharString::search(char u, int from) const {
  215. for(int i = from; i < length; i++) {
  216. if(data[i] == u) {
  217. return i;
  218. }
  219. }
  220. return -1;
  221. }
  222. bool CharString::contains(char u, int from) const {
  223. return search(u, from) >= 0;
  224. }
  225. Error CharString::substring(CharString& s, int from, int to) const {
  226. s.clear();
  227. from = Math::max(from, 0);
  228. to = Math::min(to, length - 1);
  229. for(int i = from; i <= to; i++) {
  230. CORE_RETURN_ERROR(s.append(data[i]));
  231. }
  232. return Error::NONE;
  233. }
  234. Error CharString::substring(CharString& s, int from) const {
  235. return substring(s, from, length - 1);
  236. }
  237. Error CharString::replace(CharString& s, const CharString& search,
  238. const CharString& replace) {
  239. int i = 0;
  240. while(i < length) {
  241. if(startsWidth(search, i)) {
  242. CORE_RETURN_ERROR(s.append(replace));
  243. i += search.getLength();
  244. } else {
  245. CORE_RETURN_ERROR(s.append(data[i]));
  246. i++;
  247. }
  248. }
  249. return copyFrom(s);
  250. }
  251. void CharString::replace(char search, char replace) {
  252. hash = 0;
  253. for(int i = 0; i < length; i++) {
  254. if(data[i] == search) {
  255. data[i] = replace;
  256. }
  257. addToHash(static_cast<c32>(data[i]));
  258. }
  259. }
  260. CharString::operator const char*() const {
  261. return data;
  262. }
  263. void CharString::addToHash(c32 u) {
  264. hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
  265. }
  266. Error CharString::formatBuffer(CharString& s, int index) {
  267. while(index < length) {
  268. CORE_RETURN_ERROR(s.append(data[index++]));
  269. }
  270. return Error::NONE;
  271. }
  272. Char32String::Char32String(c32* buffer, i32 bufferSize)
  273. : length(0), capacity(bufferSize), hash(0), data(buffer) {
  274. data[0] = '\0';
  275. }
  276. Error Char32String::copyFrom(const Char32String& s) {
  277. clear();
  278. return s.toString(*this);
  279. }
  280. bool Char32String::operator==(const c32* s) const {
  281. const c32* p = data;
  282. while(*s == *p && *s != '\0') {
  283. s++;
  284. p++;
  285. }
  286. return *s == *p;
  287. }
  288. bool Char32String::operator==(const Char32String& other) const {
  289. if(length != other.getLength()) {
  290. return false;
  291. }
  292. for(int i = 0; i < length; i++) {
  293. if(data[i] != other[i]) {
  294. return false;
  295. }
  296. }
  297. return true;
  298. }
  299. bool Char32String::operator!=(const c32* s) const {
  300. return !((*this) == s);
  301. }
  302. bool Char32String::operator!=(const Char32String& other) const {
  303. return !((*this) == other);
  304. }
  305. c32 Char32String::operator[](int index) const {
  306. return data[index];
  307. }
  308. int Char32String::getLength() const {
  309. return length;
  310. }
  311. int Char32String::getCapacity() const {
  312. return capacity - 1;
  313. }
  314. Error Char32String::append(char c) {
  315. return add(static_cast<c32>(c));
  316. }
  317. Error Char32String::append(signed char c) {
  318. return append(static_cast<char>(c));
  319. }
  320. Error Char32String::append(unsigned char c) {
  321. return append(static_cast<char>(c));
  322. }
  323. Error Char32String::append(wchar_t c) {
  324. return append(static_cast<c32>(c));
  325. }
  326. Error Char32String::append(c32 c) {
  327. if constexpr(IsSame<c32, char>) {
  328. char buffer[5];
  329. unicodeToChar(c, buffer);
  330. return append(static_cast<const char*>(buffer));
  331. } else {
  332. return add(c);
  333. }
  334. }
  335. Error Char32String::append(const char* s) {
  336. while(true) {
  337. c32 u = 0;
  338. CORE_RETURN_ERROR(readUnicode(u, s));
  339. if(u == 0) {
  340. return Error::NONE;
  341. }
  342. CORE_RETURN_ERROR(append(u));
  343. }
  344. }
  345. Error Char32String::append(const c32* s) {
  346. // stringLength as s could be some part of data
  347. for(int i = stringLength(s); i > 0; i--) {
  348. CORE_RETURN_ERROR(append(*(s++)));
  349. }
  350. return Error::NONE;
  351. }
  352. Error Char32String::append(const signed char* s) {
  353. return append(reinterpret_cast<const char*>(s));
  354. }
  355. Error Char32String::append(const unsigned char* s) {
  356. return append(reinterpret_cast<const char*>(s));
  357. }
  358. Error Char32String::append(bool b) {
  359. return b ? append("true") : append("false");
  360. }
  361. Error Char32String::append(Error e) {
  362. return append(getErrorName(e));
  363. }
  364. Error Char32String::toString(CharString& s) const {
  365. int l = length; // length changes if &s == this
  366. for(int i = 0; i < l; i++) {
  367. CORE_RETURN_ERROR(s.append(data[i]));
  368. }
  369. return Error::NONE;
  370. }
  371. Error Char32String::toString(Char32String& s) const {
  372. int l = length; // length changes if &s == this
  373. for(int i = 0; i < l; i++) {
  374. CORE_RETURN_ERROR(s.append(data[i]));
  375. }
  376. return Error::NONE;
  377. }
  378. void Char32String::clear() {
  379. length = 0;
  380. hash = 0;
  381. data[0] = '\0';
  382. }
  383. u32 Char32String::hashCode() const {
  384. return hash;
  385. }
  386. Error Char32String::print() const {
  387. for(int i = 0; i < length; i++) {
  388. c32 c = data[i];
  389. if(c < (1 << 7)) {
  390. CORE_RETURN_ERROR(printChar(c, 0, 0x7F, 0x0));
  391. } else if(c < (1 << 11)) {
  392. CORE_RETURN_ERROR(printChar(c, 6, 0x1F, 0xC0));
  393. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  394. } else if(c < (1 << 16)) {
  395. CORE_RETURN_ERROR(printChar(c, 12, 0x0F, 0xE0));
  396. CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
  397. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  398. } else if(c < (1 << 21)) {
  399. CORE_RETURN_ERROR(printChar(c, 18, 0x07, 0xF0));
  400. CORE_RETURN_ERROR(printChar(c, 12, 0x3F, 0x80));
  401. CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
  402. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  403. }
  404. }
  405. return Error::NONE;
  406. }
  407. Error Char32String::printLine() const {
  408. CORE_RETURN_ERROR(print());
  409. CORE_RETURN_ERROR(Core::putChar('\n'));
  410. return Error::NONE;
  411. }
  412. bool Char32String::startsWidth(const Char32String& other, int from) const {
  413. if(from > length - other.getLength()) {
  414. return false;
  415. }
  416. for(int i = 0; i < other.getLength(); i++) {
  417. if(data[from + i] != other[i]) {
  418. return false;
  419. }
  420. }
  421. return true;
  422. }
  423. int Char32String::search(const Char32String& other, int from) const {
  424. for(int i = from; i < length; i++) {
  425. if(startsWidth(other, i)) {
  426. return i;
  427. }
  428. }
  429. return -1;
  430. }
  431. bool Char32String::contains(const Char32String& other, int from) const {
  432. return search(other, from) >= 0;
  433. }
  434. int Char32String::search(c32 u, int from) const {
  435. for(int i = from; i < length; i++) {
  436. if(data[i] == u) {
  437. return i;
  438. }
  439. }
  440. return -1;
  441. }
  442. bool Char32String::contains(c32 u, int from) const {
  443. return search(u, from) >= 0;
  444. }
  445. Error Char32String::substring(Char32String& s, int from, int to) const {
  446. s.clear();
  447. from = Math::max(from, 0);
  448. to = Math::min(to, length - 1);
  449. for(int i = from; i <= to; i++) {
  450. CORE_RETURN_ERROR(s.append(data[i]));
  451. }
  452. return Error::NONE;
  453. }
  454. Error Char32String::substring(Char32String& s, int from) const {
  455. return substring(s, from, length - 1);
  456. }
  457. Error Char32String::replace(Char32String& s, const Char32String& search,
  458. const Char32String& replace) {
  459. int i = 0;
  460. while(i < length) {
  461. if(startsWidth(search, i)) {
  462. CORE_RETURN_ERROR(s.append(replace));
  463. i += search.getLength();
  464. } else {
  465. CORE_RETURN_ERROR(s.append(data[i]));
  466. i++;
  467. }
  468. }
  469. return copyFrom(s);
  470. }
  471. void Char32String::replace(c32 search, c32 replace) {
  472. hash = 0;
  473. for(int i = 0; i < length; i++) {
  474. if(data[i] == search) {
  475. data[i] = replace;
  476. }
  477. addToHash(static_cast<c32>(data[i]));
  478. }
  479. }
  480. Char32String::operator const c32*() const {
  481. return data;
  482. }
  483. Error Char32String::add(c32 c) {
  484. if(length >= capacity - 1) {
  485. return Error::CAPACITY_REACHED;
  486. }
  487. data[length++] = c;
  488. data[length] = '\0';
  489. addToHash(static_cast<c32>(c));
  490. return Error::NONE;
  491. }
  492. void Char32String::addToHash(c32 u) {
  493. hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
  494. }
  495. Error Char32String::formatBuffer(Char32String& s, int index) {
  496. while(index < length) {
  497. CORE_RETURN_ERROR(s.append(data[index++]));
  498. }
  499. return Error::NONE;
  500. }