String.cpp 4.0 KB

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