String.cpp 5.4 KB

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