String.cpp 5.3 KB

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