String.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2020-08-30
  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::marshal() {
  67. ls_std::byte_field data {};
  68. if(this->serializable != nullptr) {
  69. data = this->serializable->marshal();
  70. }
  71. return data;
  72. }
  73. void ls_std::String::parse(std::string _parseText) {
  74. this->value = std::move(_parseText);
  75. }
  76. std::string ls_std::String::toString() {
  77. return this->value;
  78. }
  79. void ls_std::String::unmarshal(const ls_std::byte_field &_data) {
  80. if(this->serializable != nullptr) {
  81. this->serializable->unmarshal(_data);
  82. }
  83. }
  84. bool ls_std::String::contains(const std::string& _text) {
  85. return this->value.find(_text) != std::string::npos;
  86. }
  87. bool ls_std::String::endsWith(const std::string &_text) {
  88. return this->value.rfind(_text) == (this->value.size() - _text.size());
  89. }
  90. bool ls_std::String::equalsIgnoreCase(String _string) {
  91. return this->toLowerCase() == _string.toLowerCase();
  92. }
  93. bool ls_std::String::equalsIgnoreCase(std::string _text) {
  94. return this->toLowerCase() == ls_std::String{std::move(_text)}.toLowerCase();
  95. }
  96. std::string ls_std::String::padLeft(size_t _width, const char _fillCharacter) {
  97. return ls_std::String::_createFillContent(this->value, _width, _fillCharacter) + this->value;
  98. }
  99. std::string ls_std::String::padRight(size_t _width, const char _fillCharacter) {
  100. return this->value + ls_std::String::_createFillContent(this->value, _width, _fillCharacter);
  101. }
  102. std::string ls_std::String::reverse() {
  103. std::string copy = this->value;
  104. std::reverse(copy.begin(), copy.end());
  105. return copy;
  106. }
  107. void ls_std::String::setSerializable(std::shared_ptr<ISerializable> _serializable) {
  108. this->serializable = std::move(_serializable);
  109. }
  110. bool ls_std::String::startsWith(const std::string &_text) {
  111. return this->value.rfind(_text, 0) == 0;
  112. }
  113. std::string ls_std::String::toLowerCase() {
  114. std::string copy = this->value;
  115. std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
  116. return copy;
  117. }
  118. std::string ls_std::String::toUpperCase() {
  119. std::string copy = this->value;
  120. std::transform(copy.begin(), copy.end(), copy.begin(), ::toupper);
  121. return copy;
  122. }
  123. std::string ls_std::String::_buildCharacterChain(size_t _amount, const char _fillCharacter) {
  124. std::string fillContent {};
  125. for(size_t iteration {} ; iteration < _amount ; iteration++) {
  126. fillContent += _fillCharacter;
  127. }
  128. return fillContent;
  129. }
  130. std::string ls_std::String::_createFillContent(const std::string& _text, size_t _width, const char _fillCharacter) {
  131. size_t fillSize = _text.size() > _width ? 0 : _width - _text.size();
  132. std::string fillContent {};
  133. if(fillSize > 0) {
  134. fillContent = ls_std::String::_buildCharacterChain(fillSize, _fillCharacter);
  135. }
  136. return fillContent;
  137. }