String.cpp 4.9 KB

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