String.cpp 4.4 KB

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