ArrayString.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #include "core/utils/ArrayString.hpp"
  2. #include <limits.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <uchar.h>
  6. #include "core/data/Array.hpp"
  7. #include "core/math/Math.hpp"
  8. #include "core/utils/Error.hpp"
  9. #include "core/utils/Utility.hpp"
  10. using CharString = Core::CharString;
  11. using Char32String = Core::Char32String;
  12. using Error = Core::Error;
  13. namespace ErrorCode = Core::ErrorCode;
  14. constexpr int stringLength(const c32* c) {
  15. const c32* i = c + 1;
  16. while(*(c++) != '\0') {}
  17. return static_cast<int>(c - i);
  18. }
  19. static Error readUnicode(c32& u, const char*& s) {
  20. size_t limit = MB_CUR_MAX;
  21. size_t n = mbrtoc32(&u, s, limit, nullptr);
  22. if(n > limit) {
  23. return ErrorCode::INVALID_CHAR;
  24. }
  25. s += n;
  26. return ErrorCode::NONE;
  27. }
  28. using C32Buffer = Core::Array<char, MB_LEN_MAX + 1>;
  29. static Error convertC32(C32Buffer& buffer, c32 c) {
  30. size_t n = c32rtomb(buffer.begin(), c, nullptr);
  31. if(n >= static_cast<size_t>(buffer.getLength())) {
  32. return ErrorCode::INVALID_CHAR;
  33. }
  34. buffer[static_cast<i64>(n)] = '\0';
  35. return ErrorCode::NONE;
  36. }
  37. CharString::CharString(char* buffer, i32 bufferSize)
  38. : length(0), capacity(bufferSize), data(buffer) {
  39. data[0] = '\0';
  40. }
  41. Error CharString::copyFrom(const CharString& s) {
  42. clear();
  43. return s.toString(*this);
  44. }
  45. bool CharString::operator==(const char* s) const {
  46. return strcmp(data, s) == 0;
  47. }
  48. bool CharString::operator==(const CharString& other) const {
  49. return length == other.length && strcmp(data, other.data) == 0;
  50. }
  51. bool CharString::operator!=(const char* s) const {
  52. return !((*this) == s);
  53. }
  54. bool CharString::operator!=(const CharString& other) const {
  55. return !((*this) == other);
  56. }
  57. char CharString::operator[](int index) const {
  58. return data[index];
  59. }
  60. int CharString::getLength() const {
  61. return length;
  62. }
  63. int CharString::getCapacity() const {
  64. return capacity - 1;
  65. }
  66. Error CharString::append(char c) {
  67. if(length >= capacity - 1) {
  68. return ErrorCode::CAPACITY_REACHED;
  69. }
  70. data[length++] = c;
  71. data[length] = '\0';
  72. return ErrorCode::NONE;
  73. }
  74. Error CharString::append(signed char c) {
  75. return append(static_cast<char>(c));
  76. }
  77. Error CharString::append(unsigned char c) {
  78. return append(static_cast<char>(c));
  79. }
  80. Error CharString::append(wchar_t c) {
  81. return append(static_cast<c32>(c));
  82. }
  83. Error CharString::append(c32 c) {
  84. C32Buffer buffer;
  85. CORE_RETURN_ERROR(convertC32(buffer, c));
  86. return append(static_cast<const char*>(buffer.begin()));
  87. }
  88. Error CharString::append(const char* s) {
  89. // stringLength as s could be some part of data
  90. for(size_t i = strlen(s); i > 0; i--) {
  91. CORE_RETURN_ERROR(append(*(s++)));
  92. }
  93. return ErrorCode::NONE;
  94. }
  95. Error CharString::append(const c32* s) {
  96. // stringLength as s could be some part of data
  97. for(int i = stringLength(s); i > 0; i--) {
  98. CORE_RETURN_ERROR(append(*(s++)));
  99. }
  100. return ErrorCode::NONE;
  101. }
  102. Error CharString::append(const signed char* s) {
  103. return append(reinterpret_cast<const char*>(s));
  104. }
  105. Error CharString::append(const unsigned char* s) {
  106. return append(reinterpret_cast<const char*>(s));
  107. }
  108. Error CharString::append(bool b) {
  109. return b ? append("true") : append("false");
  110. }
  111. Error CharString::toString(CharString& s) const {
  112. int l = length; // length changes if &s == this
  113. for(int i = 0; i < l; i++) {
  114. CORE_RETURN_ERROR(s.append(data[i]));
  115. }
  116. return ErrorCode::NONE;
  117. }
  118. Error CharString::toString(Char32String& s) const {
  119. return s.append(static_cast<const char*>(data));
  120. }
  121. void CharString::clear() {
  122. length = 0;
  123. data[0] = '\0';
  124. }
  125. Error CharString::print() const {
  126. for(int i = 0; i < length; i++) {
  127. CORE_RETURN_ERROR(Core::putChar(data[i]));
  128. }
  129. return ErrorCode::NONE;
  130. }
  131. Error CharString::printLine() const {
  132. CORE_RETURN_ERROR(print());
  133. return Core::putChar('\n');
  134. }
  135. bool CharString::startsWidth(const CharString& other, int from) const {
  136. if(from > length - other.getLength()) {
  137. return false;
  138. }
  139. for(int i = 0; i < other.getLength(); i++) {
  140. if(data[from + i] != other[i]) {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. int CharString::search(const CharString& other, int from) const {
  147. for(int i = from; i < length; i++) {
  148. if(startsWidth(other, i)) {
  149. return i;
  150. }
  151. }
  152. return -1;
  153. }
  154. bool CharString::contains(const CharString& other, int from) const {
  155. return search(other, from) >= 0;
  156. }
  157. int CharString::search(char u, int from) const {
  158. for(int i = from; i < length; i++) {
  159. if(data[i] == u) {
  160. return i;
  161. }
  162. }
  163. return -1;
  164. }
  165. bool CharString::contains(char u, int from) const {
  166. return search(u, from) >= 0;
  167. }
  168. Error CharString::substring(CharString& s, int from, int to) const {
  169. s.clear();
  170. from = Math::max(from, 0);
  171. to = Math::min(to, length - 1);
  172. for(int i = from; i <= to; i++) {
  173. CORE_RETURN_ERROR(s.append(data[i]));
  174. }
  175. return ErrorCode::NONE;
  176. }
  177. Error CharString::substring(CharString& s, int from) const {
  178. return substring(s, from, length - 1);
  179. }
  180. Error CharString::replace(CharString& s, const CharString& search,
  181. const CharString& replace) {
  182. int i = 0;
  183. while(i < length) {
  184. if(startsWidth(search, i)) {
  185. CORE_RETURN_ERROR(s.append(replace));
  186. i += search.getLength();
  187. } else {
  188. CORE_RETURN_ERROR(s.append(data[i]));
  189. i++;
  190. }
  191. }
  192. return copyFrom(s);
  193. }
  194. void CharString::replace(char search, char replace) {
  195. for(int i = 0; i < length; i++) {
  196. if(data[i] == search) {
  197. data[i] = replace;
  198. }
  199. }
  200. }
  201. CharString::operator const char*() const {
  202. return data;
  203. }
  204. Char32String::Char32String(c32* buffer, i32 bufferSize)
  205. : length(0), capacity(bufferSize), data(buffer) {
  206. data[0] = '\0';
  207. }
  208. Error Char32String::copyFrom(const Char32String& s) {
  209. clear();
  210. return s.toString(*this);
  211. }
  212. bool Char32String::operator==(const c32* s) const {
  213. const c32* p = data;
  214. while(*s == *p && *s != '\0') {
  215. s++;
  216. p++;
  217. }
  218. return *s == *p;
  219. }
  220. bool Char32String::operator==(const Char32String& other) const {
  221. if(length != other.getLength()) {
  222. return false;
  223. }
  224. for(int i = 0; i < length; i++) {
  225. if(data[i] != other[i]) {
  226. return false;
  227. }
  228. }
  229. return true;
  230. }
  231. bool Char32String::operator!=(const c32* s) const {
  232. return !((*this) == s);
  233. }
  234. bool Char32String::operator!=(const Char32String& other) const {
  235. return !((*this) == other);
  236. }
  237. c32 Char32String::operator[](int index) const {
  238. return data[index];
  239. }
  240. int Char32String::getLength() const {
  241. return length;
  242. }
  243. int Char32String::getCapacity() const {
  244. return capacity - 1;
  245. }
  246. Error Char32String::append(char c) {
  247. return append(static_cast<c32>(c));
  248. }
  249. Error Char32String::append(signed char c) {
  250. return append(static_cast<char>(c));
  251. }
  252. Error Char32String::append(unsigned char c) {
  253. return append(static_cast<char>(c));
  254. }
  255. Error Char32String::append(wchar_t c) {
  256. return append(static_cast<c32>(c));
  257. }
  258. Error Char32String::append(c32 c) {
  259. if(length >= capacity - 1) {
  260. return ErrorCode::CAPACITY_REACHED;
  261. }
  262. data[length++] = c;
  263. data[length] = '\0';
  264. return ErrorCode::NONE;
  265. }
  266. Error Char32String::append(const char* s) {
  267. while(true) {
  268. c32 u = 0;
  269. CORE_RETURN_ERROR(readUnicode(u, s));
  270. if(u == 0) {
  271. return ErrorCode::NONE;
  272. }
  273. CORE_RETURN_ERROR(append(u));
  274. }
  275. }
  276. Error Char32String::append(const c32* s) {
  277. // stringLength as s could be some part of data
  278. for(int i = stringLength(s); i > 0; i--) {
  279. CORE_RETURN_ERROR(append(*(s++)));
  280. }
  281. return ErrorCode::NONE;
  282. }
  283. Error Char32String::append(const signed char* s) {
  284. return append(reinterpret_cast<const char*>(s));
  285. }
  286. Error Char32String::append(const unsigned char* s) {
  287. return append(reinterpret_cast<const char*>(s));
  288. }
  289. Error Char32String::append(bool b) {
  290. return b ? append("true") : append("false");
  291. }
  292. Error Char32String::toString(CharString& s) const {
  293. int l = length; // length changes if &s == this
  294. for(int i = 0; i < l; i++) {
  295. CORE_RETURN_ERROR(s.append(data[i]));
  296. }
  297. return ErrorCode::NONE;
  298. }
  299. Error Char32String::toString(Char32String& s) const {
  300. int l = length; // length changes if &s == this
  301. for(int i = 0; i < l; i++) {
  302. CORE_RETURN_ERROR(s.append(data[i]));
  303. }
  304. return ErrorCode::NONE;
  305. }
  306. void Char32String::clear() {
  307. length = 0;
  308. data[0] = '\0';
  309. }
  310. Error Char32String::print() const {
  311. for(int i = 0; i < length; i++) {
  312. C32Buffer buffer;
  313. CORE_RETURN_ERROR(convertC32(buffer, data[i]));
  314. CORE_RETURN_ERROR(putChars(buffer.begin()));
  315. }
  316. return ErrorCode::NONE;
  317. }
  318. Error Char32String::printLine() const {
  319. CORE_RETURN_ERROR(print());
  320. CORE_RETURN_ERROR(Core::putChar('\n'));
  321. return ErrorCode::NONE;
  322. }
  323. bool Char32String::startsWidth(const Char32String& other, int from) const {
  324. if(from > length - other.getLength()) {
  325. return false;
  326. }
  327. for(int i = 0; i < other.getLength(); i++) {
  328. if(data[from + i] != other[i]) {
  329. return false;
  330. }
  331. }
  332. return true;
  333. }
  334. int Char32String::search(const Char32String& other, int from) const {
  335. for(int i = from; i < length; i++) {
  336. if(startsWidth(other, i)) {
  337. return i;
  338. }
  339. }
  340. return -1;
  341. }
  342. bool Char32String::contains(const Char32String& other, int from) const {
  343. return search(other, from) >= 0;
  344. }
  345. int Char32String::search(c32 u, int from) const {
  346. for(int i = from; i < length; i++) {
  347. if(data[i] == u) {
  348. return i;
  349. }
  350. }
  351. return -1;
  352. }
  353. bool Char32String::contains(c32 u, int from) const {
  354. return search(u, from) >= 0;
  355. }
  356. Error Char32String::substring(Char32String& s, int from, int to) const {
  357. s.clear();
  358. from = Math::max(from, 0);
  359. to = Math::min(to, length - 1);
  360. for(int i = from; i <= to; i++) {
  361. CORE_RETURN_ERROR(s.append(data[i]));
  362. }
  363. return ErrorCode::NONE;
  364. }
  365. Error Char32String::substring(Char32String& s, int from) const {
  366. return substring(s, from, length - 1);
  367. }
  368. Error Char32String::replace(Char32String& s, const Char32String& search,
  369. const Char32String& replace) {
  370. int i = 0;
  371. while(i < length) {
  372. if(startsWidth(search, i)) {
  373. CORE_RETURN_ERROR(s.append(replace));
  374. i += search.getLength();
  375. } else {
  376. CORE_RETURN_ERROR(s.append(data[i]));
  377. i++;
  378. }
  379. }
  380. return copyFrom(s);
  381. }
  382. void Char32String::replace(c32 search, c32 replace) {
  383. for(int i = 0; i < length; i++) {
  384. if(data[i] == search) {
  385. data[i] = replace;
  386. }
  387. }
  388. }
  389. Char32String::operator const c32*() const {
  390. return data;
  391. }