String.cpp 5.3 KB

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