123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-14
- * Changed: 2024-05-31
- *
- * */
- #include <algorithm>
- #include <ls-std/boxing/String.hpp>
- using ls::std::boxing::String;
- using ls::std::core::Class;
- using ls::std::core::type::byte_type;
- using std::move;
- using std::reverse;
- using std::string;
- using std::string_view;
- using std::transform;
- using std::vector;
- String::String() : Class("String")
- {}
- String::String(string _value) : String()
- {
- this->value = ::move(_value);
- }
- String::~String() noexcept = default;
- String &String::operator=(string _value)
- {
- this->value = ::move(_value);
- return *this;
- }
- string String::operator+(String _string) const
- {
- return this->value + _string.toString();
- }
- string String::operator+(const string &_string) const
- {
- return this->value + _string;
- }
- string String::operator+(const char *_string) const
- {
- return this->value + _string;
- }
- string String::operator-(const int _number) const
- {
- const string copy = this->value;
- return copy.substr(0, copy.size() - _number);
- }
- String &String::operator+=(String _string)
- {
- this->value = this->value + _string.toString();
- return *this;
- }
- String &String::operator+=(const string &_text)
- {
- this->value = this->value + _text;
- return *this;
- }
- bool String::operator==(String _string) const
- {
- return this->value == _string.toString();
- }
- bool String::operator==(const string_view _value) const
- {
- return this->value == _value;
- }
- bool String::operator==(const char *_value) const
- {
- return this->value == _value;
- }
- bool String::operator!=(String _string) const
- {
- return this->value != _string.toString();
- }
- bool String::operator!=(const string_view _value) const
- {
- return this->value != _value;
- }
- bool String::operator!=(const char *_value) const
- {
- return this->value != _value;
- }
- void String::parse(const string &_parseText)
- {
- this->value = _parseText;
- }
- string String::toString()
- {
- return this->value;
- }
- bool String::contains(const string_view _text) const
- {
- return this->value.find(_text) != string::npos;
- }
- bool String::endsWith(const string_view _text) const
- {
- return this->value.rfind(_text) == (this->value.size() - _text.size());
- }
- bool String::equalsIgnoreCase(const String &_string) const
- {
- return this->toLowerCase() == _string.toLowerCase();
- }
- bool String::equalsIgnoreCase(string _text) const
- {
- return this->toLowerCase() == String{::move(_text)}.toLowerCase();
- }
- vector<byte_type> String::getByteData()
- {
- vector<byte_type> byteData(this->value.begin(), this->value.end());
- byteData.push_back('\0');
- return byteData;
- }
- string String::padLeft(const size_t _width, const char _fillCharacter) const
- {
- return String::_createFillContent(this->value, _width, _fillCharacter) + this->value;
- }
- string String::padRight(const size_t _width, const char _fillCharacter) const
- {
- return this->value + String::_createFillContent(this->value, _width, _fillCharacter);
- }
- string String::reverse() const
- {
- string copy = this->value;
- ::reverse(copy.begin(), copy.end());
- return copy;
- }
- bool String::startsWith(const string_view _text) const
- {
- return this->value.rfind(_text, 0) == 0;
- }
- string String::toLowerCase() const
- {
- string copy = this->value;
- transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
- return copy;
- }
- string String::toUpperCase() const
- {
- string copy = this->value;
- transform(copy.begin(), copy.end(), copy.begin(), ::toupper);
- return copy;
- }
- string String::_buildCharacterChain(const size_t _amount, const char _fillCharacter)
- {
- string fillContent{};
- for (size_t iteration{}; iteration < _amount; iteration++)
- {
- fillContent += _fillCharacter;
- }
- return fillContent;
- }
- string String::_createFillContent(const string_view _text, const size_t _width, const char _fillCharacter)
- {
- const size_t fillSize = _text.size() > _width ? 0 : _width - _text.size();
- string fillContent{};
- if (fillSize > 0)
- {
- fillContent = String::_buildCharacterChain(fillSize, _fillCharacter);
- }
- return fillContent;
- }
|