String.cpp 4.1 KB

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