ArrayString.cpp 11 KB

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