String.cpp 4.8 KB

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