ArrayString.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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(signed short s) {
  359. return convertAppend(s);
  360. }
  361. Error Char32String::append(unsigned short s) {
  362. return convertAppend(s);
  363. }
  364. Error Char32String::append(signed int i) {
  365. return convertAppend(i);
  366. }
  367. Error Char32String::append(unsigned int i) {
  368. return convertAppend(i);
  369. }
  370. Error Char32String::append(signed long l) {
  371. return convertAppend(l);
  372. }
  373. Error Char32String::append(unsigned long l) {
  374. return convertAppend(l);
  375. }
  376. Error Char32String::append(signed long long ll) {
  377. return convertAppend(ll);
  378. }
  379. Error Char32String::append(unsigned long long ll) {
  380. return convertAppend(ll);
  381. }
  382. Error Char32String::append(float f) {
  383. return convertAppend(f);
  384. }
  385. Error Char32String::append(double d) {
  386. return convertAppend(d);
  387. }
  388. Error Char32String::append(long double ld) {
  389. return convertAppend(ld);
  390. }
  391. Error Char32String::append(bool b) {
  392. return b ? append("true") : append("false");
  393. }
  394. Error Char32String::append(Error e) {
  395. return append(getErrorName(e));
  396. }
  397. Error Char32String::toString(CharString& s) const {
  398. int l = length; // length changes if &s == this
  399. for(int i = 0; i < l; i++) {
  400. CORE_RETURN_ERROR(s.append(data[i]));
  401. }
  402. return Error::NONE;
  403. }
  404. Error Char32String::toString(Char32String& s) const {
  405. int l = length; // length changes if &s == this
  406. for(int i = 0; i < l; i++) {
  407. CORE_RETURN_ERROR(s.append(data[i]));
  408. }
  409. return Error::NONE;
  410. }
  411. void Char32String::clear() {
  412. length = 0;
  413. hash = 0;
  414. data[0] = '\0';
  415. }
  416. u32 Char32String::hashCode() const {
  417. return hash;
  418. }
  419. Error Char32String::print() const {
  420. for(int i = 0; i < length; i++) {
  421. c32 c = data[i];
  422. if(c < (1 << 7)) {
  423. CORE_RETURN_ERROR(printChar(c, 0, 0x7F, 0x0));
  424. } else if(c < (1 << 11)) {
  425. CORE_RETURN_ERROR(printChar(c, 6, 0x1F, 0xC0));
  426. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  427. } else if(c < (1 << 16)) {
  428. CORE_RETURN_ERROR(printChar(c, 12, 0x0F, 0xE0));
  429. CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
  430. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  431. } else if(c < (1 << 21)) {
  432. CORE_RETURN_ERROR(printChar(c, 18, 0x07, 0xF0));
  433. CORE_RETURN_ERROR(printChar(c, 12, 0x3F, 0x80));
  434. CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
  435. CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
  436. }
  437. }
  438. return Error::NONE;
  439. }
  440. Error Char32String::printLine() const {
  441. CORE_RETURN_ERROR(print());
  442. CORE_RETURN_ERROR(Core::putChar('\n'));
  443. return Error::NONE;
  444. }
  445. bool Char32String::startsWidth(const Char32String& other, int from) const {
  446. if(from > length - other.getLength()) {
  447. return false;
  448. }
  449. for(int i = 0; i < other.getLength(); i++) {
  450. if(data[from + i] != other[i]) {
  451. return false;
  452. }
  453. }
  454. return true;
  455. }
  456. int Char32String::search(const Char32String& other, int from) const {
  457. for(int i = from; i < length; i++) {
  458. if(startsWidth(other, i)) {
  459. return i;
  460. }
  461. }
  462. return -1;
  463. }
  464. bool Char32String::contains(const Char32String& other, int from) const {
  465. return search(other, from) >= 0;
  466. }
  467. int Char32String::search(c32 u, int from) const {
  468. for(int i = from; i < length; i++) {
  469. if(data[i] == u) {
  470. return i;
  471. }
  472. }
  473. return -1;
  474. }
  475. bool Char32String::contains(c32 u, int from) const {
  476. return search(u, from) >= 0;
  477. }
  478. Error Char32String::substring(Char32String& s, int from, int to) const {
  479. s.clear();
  480. from = Math::max(from, 0);
  481. to = Math::min(to, length - 1);
  482. for(int i = from; i <= to; i++) {
  483. CORE_RETURN_ERROR(s.append(data[i]));
  484. }
  485. return Error::NONE;
  486. }
  487. Error Char32String::substring(Char32String& s, int from) const {
  488. return substring(s, from, length - 1);
  489. }
  490. Error Char32String::replace(Char32String& s, const Char32String& search,
  491. const Char32String& replace) {
  492. int i = 0;
  493. while(i < length) {
  494. if(startsWidth(search, i)) {
  495. CORE_RETURN_ERROR(s.append(replace));
  496. i += search.getLength();
  497. } else {
  498. CORE_RETURN_ERROR(s.append(data[i]));
  499. i++;
  500. }
  501. }
  502. return copyFrom(s);
  503. }
  504. void Char32String::replace(c32 search, c32 replace) {
  505. hash = 0;
  506. for(int i = 0; i < length; i++) {
  507. if(data[i] == search) {
  508. data[i] = replace;
  509. }
  510. addToHash(static_cast<c32>(data[i]));
  511. }
  512. }
  513. Char32String::operator const c32*() const {
  514. return data;
  515. }
  516. Error Char32String::add(c32 c) {
  517. if(length >= capacity - 1) {
  518. return Error::CAPACITY_REACHED;
  519. }
  520. data[length++] = c;
  521. data[length] = '\0';
  522. addToHash(static_cast<c32>(c));
  523. return Error::NONE;
  524. }
  525. void Char32String::addToHash(c32 u) {
  526. hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
  527. }
  528. Error Char32String::formatBuffer(Char32String& s, int index) {
  529. while(index < length) {
  530. CORE_RETURN_ERROR(s.append(data[index++]));
  531. }
  532. return Error::NONE;
  533. }