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