String.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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-22
  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() noexcept = default;
  16. ls::std::boxing::String &ls::std::boxing::String::operator=(::std::string _value)
  17. {
  18. this->value = ::std::move(_value);
  19. return *this;
  20. }
  21. ::std::string ls::std::boxing::String::operator+(ls::std::boxing::String _string) const
  22. {
  23. return this->value + _string.toString();
  24. }
  25. ::std::string ls::std::boxing::String::operator+(const ::std::string &_string) const
  26. {
  27. return this->value + _string;
  28. }
  29. ::std::string ls::std::boxing::String::operator+(const char *_string) const
  30. {
  31. return this->value + _string;
  32. }
  33. ::std::string ls::std::boxing::String::operator-(int _number)
  34. {
  35. ::std::string copy = this->value;
  36. return copy.substr(0, copy.size() - _number);
  37. }
  38. ls::std::boxing::String &ls::std::boxing::String::operator+=(ls::std::boxing::String _string)
  39. {
  40. this->value = this->value + _string.toString();
  41. return *this;
  42. }
  43. ls::std::boxing::String &ls::std::boxing::String::operator+=(const ::std::string &_text)
  44. {
  45. this->value = this->value + _text;
  46. return *this;
  47. }
  48. bool ls::std::boxing::String::operator==(ls::std::boxing::String _string)
  49. {
  50. return this->value == _string.toString();
  51. }
  52. bool ls::std::boxing::String::operator==(const ::std::string &_value)
  53. {
  54. return this->value == _value;
  55. }
  56. bool ls::std::boxing::String::operator==(const char *_value)
  57. {
  58. return this->value == _value;
  59. }
  60. bool ls::std::boxing::String::operator!=(ls::std::boxing::String _string)
  61. {
  62. return this->value != _string.toString();
  63. }
  64. bool ls::std::boxing::String::operator!=(const ::std::string &_value)
  65. {
  66. return this->value != _value;
  67. }
  68. bool ls::std::boxing::String::operator!=(const char *_value)
  69. {
  70. return this->value != _value;
  71. }
  72. void ls::std::boxing::String::parse(::std::string _parseText)
  73. {
  74. this->value = ::std::move(_parseText);
  75. }
  76. ::std::string ls::std::boxing::String::toString()
  77. {
  78. return this->value;
  79. }
  80. bool ls::std::boxing::String::contains(const ::std::string &_text)
  81. {
  82. return this->value.find(_text) != ::std::string::npos;
  83. }
  84. bool ls::std::boxing::String::endsWith(const ::std::string &_text)
  85. {
  86. return this->value.rfind(_text) == (this->value.size() - _text.size());
  87. }
  88. bool ls::std::boxing::String::equalsIgnoreCase(ls::std::boxing::String _string)
  89. {
  90. return this->toLowerCase() == _string.toLowerCase();
  91. }
  92. bool ls::std::boxing::String::equalsIgnoreCase(::std::string _text)
  93. {
  94. return this->toLowerCase() == ls::std::boxing::String{::std::move(_text)}.toLowerCase();
  95. }
  96. ::std::vector<ls::std::core::type::byte> ls::std::boxing::String::getByteData()
  97. {
  98. ::std::vector<ls::std::core::type::byte> byteData(this->value.begin(), this->value.end());
  99. byteData.push_back('\0');
  100. return byteData;
  101. }
  102. ::std::string ls::std::boxing::String::padLeft(size_t _width, const char _fillCharacter)
  103. {
  104. return ls::std::boxing::String::_createFillContent(this->value, _width, _fillCharacter) + this->value;
  105. }
  106. ::std::string ls::std::boxing::String::padRight(size_t _width, const char _fillCharacter)
  107. {
  108. return this->value + ls::std::boxing::String::_createFillContent(this->value, _width, _fillCharacter);
  109. }
  110. ::std::string ls::std::boxing::String::reverse()
  111. {
  112. ::std::string copy = this->value;
  113. ::std::reverse(copy.begin(), copy.end());
  114. return copy;
  115. }
  116. bool ls::std::boxing::String::startsWith(const ::std::string &_text)
  117. {
  118. return this->value.rfind(_text, 0) == 0;
  119. }
  120. ::std::string ls::std::boxing::String::toLowerCase()
  121. {
  122. ::std::string copy = this->value;
  123. ::std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
  124. return copy;
  125. }
  126. ::std::string ls::std::boxing::String::toUpperCase()
  127. {
  128. ::std::string copy = this->value;
  129. ::std::transform(copy.begin(), copy.end(), copy.begin(), ::toupper);
  130. return copy;
  131. }
  132. ::std::string ls::std::boxing::String::_buildCharacterChain(size_t _amount, const char _fillCharacter)
  133. {
  134. ::std::string fillContent{};
  135. for (size_t iteration{}; iteration < _amount; iteration++) { fillContent += _fillCharacter; }
  136. return fillContent;
  137. }
  138. ::std::string ls::std::boxing::String::_createFillContent(const ::std::string &_text, size_t _width, const char _fillCharacter)
  139. {
  140. size_t fillSize = _text.size() > _width ? 0 : _width - _text.size();
  141. ::std::string fillContent{};
  142. if (fillSize > 0)
  143. {
  144. fillContent = ls::std::boxing::String::_buildCharacterChain(fillSize, _fillCharacter);
  145. }
  146. return fillContent;
  147. }