String.cpp 4.1 KB

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