String.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-07-08
  7. *
  8. * */
  9. #include <algorithm>
  10. #include <ls_std/boxing/String.hpp>
  11. #include <ls_std/exception/IllegalArgumentException.hpp>
  12. ls_std::String::String() : ls_std::Class("String")
  13. {}
  14. ls_std::String::String(std::string _value)
  15. : ls_std::Class("String"),
  16. value(std::move(_value))
  17. {}
  18. ls_std::String::operator const char *() const
  19. {
  20. return this->value.c_str();
  21. }
  22. ls_std::String::operator std::string() const
  23. {
  24. return this->value;
  25. }
  26. ls_std::String &ls_std::String::operator=(std::string _value)
  27. {
  28. this->value = std::move(_value);
  29. return *this;
  30. }
  31. std::string ls_std::String::operator+(ls_std::String _string) const
  32. {
  33. return this->value + _string.toString();
  34. }
  35. std::string ls_std::String::operator+(const std::string &_string) const
  36. {
  37. return this->value + _string;
  38. }
  39. std::string ls_std::String::operator+(const char *_string) const
  40. {
  41. return this->value + _string;
  42. }
  43. std::string ls_std::String::operator-(int _number)
  44. {
  45. std::string copy = this->value;
  46. return copy.substr(0, copy.size() - _number);
  47. }
  48. ls_std::String &ls_std::String::operator+=(ls_std::String _string)
  49. {
  50. this->value = this->value + _string.toString();
  51. return *this;
  52. }
  53. ls_std::String &ls_std::String::operator+=(const std::string &_text)
  54. {
  55. this->value = this->value + _text;
  56. return *this;
  57. }
  58. bool ls_std::String::operator==(ls_std::String _string)
  59. {
  60. return this->value == _string.toString();
  61. }
  62. bool ls_std::String::operator==(const std::string &_value)
  63. {
  64. return this->value == _value;
  65. }
  66. bool ls_std::String::operator==(const char *_value)
  67. {
  68. return this->value == _value;
  69. }
  70. bool ls_std::String::operator!=(ls_std::String _string)
  71. {
  72. return this->value != _string.toString();
  73. }
  74. bool ls_std::String::operator!=(const std::string &_value)
  75. {
  76. return this->value != _value;
  77. }
  78. bool ls_std::String::operator!=(const char *_value)
  79. {
  80. return this->value != _value;
  81. }
  82. ls_std::byte_field ls_std::String::load()
  83. {
  84. ls_std::byte_field data{};
  85. if (this->storable != nullptr && this->serializable != nullptr)
  86. {
  87. data = this->storable->load();
  88. this->serializable->unmarshal(data);
  89. }
  90. return data;
  91. }
  92. ls_std::byte_field ls_std::String::marshal()
  93. {
  94. ls_std::byte_field data{};
  95. if (this->serializable != nullptr)
  96. {
  97. data = this->serializable->marshal();
  98. }
  99. return data;
  100. }
  101. void ls_std::String::parse(std::string _parseText)
  102. {
  103. this->value = std::move(_parseText);
  104. }
  105. void ls_std::String::save(const ls_std::byte_field &_data)
  106. {
  107. if (this->serializable != nullptr)
  108. {
  109. if (_data.empty())
  110. {
  111. this->storable->save(this->serializable->marshal());
  112. }
  113. else
  114. {
  115. this->storable->save(_data);
  116. }
  117. }
  118. }
  119. std::string ls_std::String::toString()
  120. {
  121. return this->value;
  122. }
  123. void ls_std::String::unmarshal(const ls_std::byte_field &_data)
  124. {
  125. if (this->serializable != nullptr)
  126. {
  127. this->serializable->unmarshal(_data);
  128. }
  129. }
  130. bool ls_std::String::contains(const std::string &_text)
  131. {
  132. return this->value.find(_text) != std::string::npos;
  133. }
  134. bool ls_std::String::endsWith(const std::string &_text)
  135. {
  136. return this->value.rfind(_text) == (this->value.size() - _text.size());
  137. }
  138. bool ls_std::String::equalsIgnoreCase(ls_std::String _string)
  139. {
  140. return this->toLowerCase() == _string.toLowerCase();
  141. }
  142. bool ls_std::String::equalsIgnoreCase(std::string _text)
  143. {
  144. return this->toLowerCase() == ls_std::String{std::move(_text)}.toLowerCase();
  145. }
  146. std::vector<ls_std::byte> ls_std::String::getByteData()
  147. {
  148. std::vector<ls_std::byte> byteData(this->value.begin(), this->value.end());
  149. byteData.push_back('\0');
  150. return byteData;
  151. }
  152. std::string ls_std::String::padLeft(size_t _width, const char _fillCharacter)
  153. {
  154. return ls_std::String::_createFillContent(this->value, _width, _fillCharacter) + this->value;
  155. }
  156. std::string ls_std::String::padRight(size_t _width, const char _fillCharacter)
  157. {
  158. return this->value + ls_std::String::_createFillContent(this->value, _width, _fillCharacter);
  159. }
  160. std::string ls_std::String::reverse()
  161. {
  162. std::string copy = this->value;
  163. std::reverse(copy.begin(), copy.end());
  164. return copy;
  165. }
  166. void ls_std::String::setSerializable(std::shared_ptr<ls_std::ISerializable> _serializable)
  167. {
  168. this->_assignSerializable(_serializable);
  169. }
  170. void ls_std::String::setStorable(std::shared_ptr<ls_std::IStorable> _storable)
  171. {
  172. this->_assignStorable(_storable);
  173. }
  174. bool ls_std::String::startsWith(const std::string &_text)
  175. {
  176. return this->value.rfind(_text, 0) == 0;
  177. }
  178. std::string ls_std::String::toLowerCase()
  179. {
  180. std::string copy = this->value;
  181. std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
  182. return copy;
  183. }
  184. std::string ls_std::String::toUpperCase()
  185. {
  186. std::string copy = this->value;
  187. std::transform(copy.begin(), copy.end(), copy.begin(), ::toupper);
  188. return copy;
  189. }
  190. void ls_std::String::_assignSerializable(const std::shared_ptr<ls_std::ISerializable> &_serializable)
  191. {
  192. if (_serializable == nullptr)
  193. {
  194. throw ls_std::IllegalArgumentException{};
  195. }
  196. this->serializable = _serializable;
  197. }
  198. void ls_std::String::_assignStorable(const std::shared_ptr<ls_std::IStorable> &_storable)
  199. {
  200. if (_storable == nullptr)
  201. {
  202. throw ls_std::IllegalArgumentException{};
  203. }
  204. this->storable = _storable;
  205. }
  206. std::string ls_std::String::_buildCharacterChain(size_t _amount, const char _fillCharacter)
  207. {
  208. std::string fillContent{};
  209. for (size_t iteration{}; iteration < _amount; iteration++)
  210. {
  211. fillContent += _fillCharacter;
  212. }
  213. return fillContent;
  214. }
  215. std::string ls_std::String::_createFillContent(const std::string &_text, size_t _width, const char _fillCharacter)
  216. {
  217. size_t fillSize = _text.size() > _width ? 0 : _width - _text.size();
  218. std::string fillContent{};
  219. if (fillSize > 0)
  220. {
  221. fillContent = ls_std::String::_buildCharacterChain(fillSize, _fillCharacter);
  222. }
  223. return fillContent;
  224. }