Browse Source

Address SonarLint findings in boxing module classes

Patrick-Christopher Mattulat 1 year ago
parent
commit
f2dab7597f

+ 3 - 2
include/ls-std/boxing/Boolean.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2023-02-22
+ * Changed:         2023-05-17
  *
  * */
 
@@ -56,7 +56,7 @@ namespace ls::std::boxing
 
       // implementation
 
-      void parse(::std::string _parseText) override;
+      void parse(const ::std::string &_parseText) override;
       [[nodiscard]] ::std::string toString() override;
 
       // additional functionality
@@ -74,6 +74,7 @@ namespace ls::std::boxing
       const ::std::string FALSE_STRING = "false";
       const ::std::string TRUE_STRING = "true";
 
+      [[nodiscard]] static ::std::string _fetchClassName();
       [[nodiscard]] ::std::string _toString() const;
   };
 }

+ 3 - 2
include/ls-std/boxing/Double.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2023-02-22
+ * Changed:         2023-05-17
  *
  * */
 
@@ -74,7 +74,7 @@ namespace ls::std::boxing
 
       // implementation
 
-      void parse(::std::string _parseText) override;
+      void parse(const ::std::string &_parseText) override;
       [[nodiscard]] ::std::string toString() override;
 
       // additional functionality
@@ -89,6 +89,7 @@ namespace ls::std::boxing
       double value{};
 
       void _assignEpsilon(double _epsilon);
+      [[nodiscard]] static ::std::string _fetchClassName();
   };
 }
 

+ 4 - 2
include/ls-std/boxing/Float.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2023-02-22
+ * Changed:         2023-05-17
  *
  * */
 
@@ -14,6 +14,7 @@
 #include <ls-std/core/interface/IBoxing.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
 #include <memory>
+#include <string>
 
 namespace ls::std::boxing
 {
@@ -74,7 +75,7 @@ namespace ls::std::boxing
 
       // implementation
 
-      void parse(::std::string _parseText) override;
+      void parse(const ::std::string &_parseText) override;
       [[nodiscard]] ::std::string toString() override;
 
       // additional functionality
@@ -89,6 +90,7 @@ namespace ls::std::boxing
       float value{};
 
       void _assignEpsilon(float _epsilon);
+      [[nodiscard]] static ::std::string _fetchClassName();
   };
 }
 

+ 3 - 1
include/ls-std/boxing/Integer.hpp

@@ -83,7 +83,7 @@ namespace ls::std::boxing
 
       // implementation
 
-      void parse(::std::string _parseText) override;
+      void parse(const ::std::string &_parseText) override;
       [[nodiscard]] ::std::string toString() override;
 
       // additional functionality
@@ -93,6 +93,8 @@ namespace ls::std::boxing
     private:
 
       int value{};
+
+      [[nodiscard]] static ::std::string _fetchClassName();
   };
 }
 

+ 4 - 9
include/ls-std/boxing/Long.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2023-02-22
+ * Changed:         2023-05-17
  *
  * */
 
@@ -77,13 +77,6 @@ namespace ls::std::boxing
         return !_long.value;
       }
 
-      bool operator&&(const ls::std::boxing::Long &_long) const;
-      bool operator&&(ls::std::core::type::long_type _value) const;
-      bool operator&&(bool _expression) const;
-      bool operator||(const ls::std::boxing::Long &_long) const;
-      bool operator||(ls::std::core::type::long_type _value) const;
-      bool operator||(bool _expression) const;
-
       // increment / decrement operator
 
       void operator++();
@@ -91,7 +84,7 @@ namespace ls::std::boxing
 
       // implementation
 
-      void parse(::std::string _parseText) override;
+      void parse(const ::std::string &_parseText) override;
       [[nodiscard]] ::std::string toString() override;
 
       // additional functionality
@@ -101,6 +94,8 @@ namespace ls::std::boxing
     private:
 
       ls::std::core::type::long_type value{};
+
+      [[nodiscard]] static ::std::string _fetchClassName();
   };
 }
 

+ 21 - 20
include/ls-std/boxing/String.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2023-02-23
+ * Changed:         2023-05-17
  *
  * */
 
@@ -16,6 +16,7 @@
 #include <ls-std/os/dynamic-goal.hpp>
 #include <memory>
 #include <string>
+#include <string_view>
 #include <vector>
 
 namespace ls::std::boxing
@@ -37,7 +38,7 @@ namespace ls::std::boxing
       ::std::string operator+(ls::std::boxing::String _string) const;
       ::std::string operator+(const ::std::string &_string) const;
       ::std::string operator+(const char *_string) const;
-      ::std::string operator-(int _number);
+      ::std::string operator-(int _number) const;
 
       // compound operators
 
@@ -46,38 +47,38 @@ namespace ls::std::boxing
 
       // comparison operators
 
-      bool operator==(ls::std::boxing::String _string);
-      bool operator==(const ::std::string &_value);
-      bool operator==(const char *_value);
-      bool operator!=(ls::std::boxing::String _string);
-      bool operator!=(const ::std::string &_value);
-      bool operator!=(const char *_value);
+      bool operator==(ls::std::boxing::String _string) const;
+      bool operator==(::std::string_view _value) const;
+      bool operator==(const char *_value) const;
+      bool operator!=(ls::std::boxing::String _string) const;
+      bool operator!=(::std::string_view _value) const;
+      bool operator!=(const char *_value) const;
 
       // implementation
 
-      void parse(::std::string _parseText) override;
+      void parse(const ::std::string &_parseText) override;
       [[nodiscard]] ::std::string toString() override;
 
       // additional functionality
 
-      [[nodiscard]] bool contains(const ::std::string &_text);
-      [[nodiscard]] bool endsWith(const ::std::string &_text);
-      [[nodiscard]] bool equalsIgnoreCase(ls::std::boxing::String _string);
-      [[nodiscard]] bool equalsIgnoreCase(::std::string _text);
+      [[nodiscard]] bool contains(::std::string_view _text) const;
+      [[nodiscard]] bool endsWith(::std::string_view _text) const;
+      [[nodiscard]] bool equalsIgnoreCase(const ls::std::boxing::String &_string) const;
+      [[nodiscard]] bool equalsIgnoreCase(::std::string _text) const;
       [[nodiscard]] ::std::vector<ls::std::core::type::byte_type> getByteData();
-      [[nodiscard]] ::std::string padLeft(size_t _width, char _fillCharacter);
-      [[nodiscard]] ::std::string padRight(size_t _width, char _fillCharacter);
-      [[nodiscard]] ::std::string reverse();
-      [[nodiscard]] bool startsWith(const ::std::string &_text);
-      [[nodiscard]] ::std::string toLowerCase();
-      [[nodiscard]] ::std::string toUpperCase();
+      [[nodiscard]] ::std::string padLeft(size_t _width, char _fillCharacter) const;
+      [[nodiscard]] ::std::string padRight(size_t _width, char _fillCharacter) const;
+      [[nodiscard]] ::std::string reverse() const;
+      [[nodiscard]] bool startsWith(::std::string_view _text) const;
+      [[nodiscard]] ::std::string toLowerCase() const;
+      [[nodiscard]] ::std::string toUpperCase() const;
 
     private:
 
       ::std::string value{};
 
       [[nodiscard]] static ::std::string _buildCharacterChain(size_t _amount, char _fillCharacter);
-      [[nodiscard]] static ::std::string _createFillContent(const ::std::string &_text, size_t _width, char _fillCharacter);
+      [[nodiscard]] static ::std::string _createFillContent(::std::string_view _text, size_t _width, char _fillCharacter);
   };
 }
 

+ 2 - 2
include/ls-std/core/interface/IBoxing.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-07
- * Changed:         2023-02-05
+ * Changed:         2023-05-17
  *
  * */
 
@@ -22,7 +22,7 @@ namespace ls::std::core::interface_type
       IBoxing();
       virtual ~IBoxing();
 
-      virtual void parse(::std::string _parseText) = 0;
+      virtual void parse(const ::std::string &_parseText) = 0;
       virtual ::std::string toString() = 0;
   };
 }

+ 17 - 12
source/ls-std/boxing/Boolean.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2023-02-23
+ * Changed:         2023-05-17
  *
  * */
 
@@ -17,12 +17,10 @@ using ls::std::core::IllegalArgumentException;
 using std::string;
 using std::transform;
 
-Boolean::Boolean(bool _value) : Boolean()
-{
-  this->value = _value;
-}
+Boolean::Boolean(bool _value) : Class(Boolean::_fetchClassName()), value(_value)
+{}
 
-Boolean::Boolean() : Class("Boolean")
+Boolean::Boolean() : Class(Boolean::_fetchClassName())
 {}
 
 Boolean::~Boolean() noexcept = default;
@@ -69,22 +67,23 @@ bool Boolean::operator||(int _value) const
   return this->value || _value;
 }
 
-void Boolean::parse(string _parseText)
+void Boolean::parse(const string &_parseText)
 {
-  transform(_parseText.begin(), _parseText.end(), _parseText.begin(), ::tolower);
+  string parseText = _parseText;
+  transform(parseText.begin(), parseText.end(), parseText.begin(), ::tolower);
 
-  if (_parseText != this->TRUE_STRING && _parseText != this->FALSE_STRING)
+  if (parseText != this->TRUE_STRING && parseText != this->FALSE_STRING)
   {
-    throw IllegalArgumentException{_parseText + " is not a valid string representation"};
+    throw IllegalArgumentException{parseText + " is not a valid string representation"};
   }
   else
   {
-    if (_parseText == this->TRUE_STRING)
+    if (parseText == this->TRUE_STRING)
     {
       this->value = true;
     }
 
-    if (_parseText == this->FALSE_STRING)
+    if (parseText == this->FALSE_STRING)
     {
       this->value = false;
     }
@@ -121,6 +120,12 @@ bool Boolean::XOR(bool _leftExpression, bool _rightExpression)
   return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
 }
 
+string Boolean::_fetchClassName()
+{
+  static const string className = "Boolean";
+  return className;
+}
+
 string Boolean::_toString() const
 {
   string booleanString{};

+ 11 - 5
source/ls-std/boxing/Double.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2023-02-23
+ * Changed:         2023-05-17
  *
  * */
 
@@ -19,14 +19,14 @@ using std::stod;
 using std::string;
 using std::to_string;
 
-Double::Double() : Class("Double")
+Double::Double() : Class(Double::_fetchClassName())
 {
   this->_assignEpsilon(0.00000001);
 }
 
-Double::Double(double _value) : Double()
+Double::Double(double _value) : Class(Double::_fetchClassName()), value(_value)
 {
-  this->value = _value;
+  this->_assignEpsilon(0.00000001);
 }
 
 Double::~Double() noexcept = default;
@@ -200,7 +200,7 @@ void Double::operator--()
   this->value -= 1.0f;
 }
 
-void Double::parse(string _parseText)
+void Double::parse(const string &_parseText)
 {
   this->value = stod(_parseText);
 }
@@ -234,3 +234,9 @@ void Double::_assignEpsilon(double _epsilon)
 
   this->epsilon = _epsilon;
 }
+
+string Double::_fetchClassName()
+{
+  static const string className = "Double";
+  return className;
+}

+ 11 - 7
source/ls-std/boxing/Float.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2023-02-23
+ * Changed:         2023-05-17
  *
  * */
 
@@ -19,13 +19,11 @@ using std::stof;
 using std::string;
 using std::to_string;
 
-Float::Float() : Class("Float"), epsilon(0.00001f)
+Float::Float() : Class(Float::_fetchClassName()), epsilon(0.00001F)
 {}
 
-Float::Float(float _value) : Float()
-{
-  this->value = _value;
-}
+Float::Float(float _value) : Class(Float::_fetchClassName()), epsilon(0.00001f), value(_value)
+{}
 
 Float::~Float() noexcept = default;
 
@@ -198,7 +196,7 @@ void Float::operator--()
   this->value -= 1.0f;
 }
 
-void Float::parse(string _parseText)
+void Float::parse(const string &_parseText)
 {
   this->value = stof(_parseText);
 }
@@ -232,3 +230,9 @@ void Float::_assignEpsilon(float _epsilon)
 
   this->epsilon = _epsilon;
 }
+
+string Float::_fetchClassName()
+{
+  static const string className = "Float";
+  return className;
+}

+ 10 - 6
source/ls-std/boxing/Integer.cpp

@@ -17,12 +17,10 @@ using std::stoi;
 using std::string;
 using std::to_string;
 
-Integer::Integer(int _value) : Integer()
-{
-  this->value = _value;
-}
+Integer::Integer(int _value) : Class(Integer::_fetchClassName()), value(_value)
+{}
 
-Integer::Integer() : Class("Integer")
+Integer::Integer() : Class(Integer::_fetchClassName())
 {}
 
 Integer::~Integer() noexcept = default;
@@ -226,7 +224,7 @@ void Integer::operator--()
   this->value -= 1;
 }
 
-void Integer::parse(string _parseText)
+void Integer::parse(const string &_parseText)
 {
   this->value = stoi(_parseText);
 }
@@ -240,3 +238,9 @@ int Integer::getValue() const
 {
   return this->value;
 }
+
+string Integer::_fetchClassName()
+{
+  static const string className = "Integer";
+  return className;
+}

+ 11 - 37
source/ls-std/boxing/Long.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2023-02-23
+ * Changed:         2023-05-17
  *
  * */
 
@@ -18,12 +18,10 @@ using std::stoll;
 using std::string;
 using std::to_string;
 
-Long::Long(long_type _value) : Long()
-{
-  this->value = _value;
-}
+Long::Long(long_type _value) : Class(Long::_fetchClassName()), value(_value)
+{}
 
-Long::Long() : Class("Long")
+Long::Long() : Class(Long::_fetchClassName())
 {}
 
 Long::~Long() noexcept = default;
@@ -217,36 +215,6 @@ bool Long::operator<=(long_type _value) const
   return this->value <= _value;
 }
 
-bool Long::operator&&(const Long &_long) const
-{
-  return this->value && _long.getValue();
-}
-
-bool Long::operator&&(long_type _value) const
-{
-  return this->value && _value;
-}
-
-bool Long::operator&&(bool _expression) const
-{
-  return this->value && _expression;
-}
-
-bool Long::operator||(const Long &_long) const
-{
-  return this->value || _long.getValue();
-}
-
-bool Long::operator||(long_type _value) const
-{
-  return this->value || _value;
-}
-
-bool Long::operator||(bool _expression) const
-{
-  return this->value || _expression;
-}
-
 void Long::operator++()
 {
   this->value += 1;
@@ -257,7 +225,7 @@ void Long::operator--()
   this->value -= 1;
 }
 
-void Long::parse(string _parseText)
+void Long::parse(const string &_parseText)
 {
   this->value = stoll(_parseText);
 }
@@ -271,3 +239,9 @@ long_type Long::getValue() const
 {
   return this->value;
 }
+
+string Long::_fetchClassName()
+{
+  static const string className = "Long";
+  return className;
+}

+ 22 - 21
source/ls-std/boxing/String.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2023-02-23
+ * Changed:         2023-05-17
  *
  * */
 
@@ -16,6 +16,7 @@ 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;
 
@@ -50,7 +51,7 @@ string String::operator+(const char *_string) const
   return this->value + _string;
 }
 
-string String::operator-(int _number)
+string String::operator-(int _number) const
 {
   string copy = this->value;
   return copy.substr(0, copy.size() - _number);
@@ -68,39 +69,39 @@ String &String::operator+=(const string &_text)
   return *this;
 }
 
-bool String::operator==(String _string)
+bool String::operator==(String _string) const
 {
   return this->value == _string.toString();
 }
 
-bool String::operator==(const string &_value)
+bool String::operator==(string_view _value) const
 {
   return this->value == _value;
 }
 
-bool String::operator==(const char *_value)
+bool String::operator==(const char *_value) const
 {
   return this->value == _value;
 }
 
-bool String::operator!=(String _string)
+bool String::operator!=(String _string) const
 {
   return this->value != _string.toString();
 }
 
-bool String::operator!=(const string &_value)
+bool String::operator!=(string_view _value) const
 {
   return this->value != _value;
 }
 
-bool String::operator!=(const char *_value)
+bool String::operator!=(const char *_value) const
 {
   return this->value != _value;
 }
 
-void String::parse(string _parseText)
+void String::parse(const string &_parseText)
 {
-  this->value = ::move(_parseText);
+  this->value = _parseText;
 }
 
 string String::toString()
@@ -108,22 +109,22 @@ string String::toString()
   return this->value;
 }
 
-bool String::contains(const string &_text)
+bool String::contains(string_view _text) const
 {
   return this->value.find(_text) != string::npos;
 }
 
-bool String::endsWith(const string &_text)
+bool String::endsWith(string_view _text) const
 {
   return this->value.rfind(_text) == (this->value.size() - _text.size());
 }
 
-bool String::equalsIgnoreCase(String _string)
+bool String::equalsIgnoreCase(const String &_string) const
 {
   return this->toLowerCase() == _string.toLowerCase();
 }
 
-bool String::equalsIgnoreCase(string _text)
+bool String::equalsIgnoreCase(string _text) const
 {
   return this->toLowerCase() == String{::move(_text)}.toLowerCase();
 }
@@ -136,17 +137,17 @@ vector<byte_type> String::getByteData()
   return byteData;
 }
 
-string String::padLeft(size_t _width, const char _fillCharacter)
+string String::padLeft(size_t _width, const char _fillCharacter) const
 {
   return String::_createFillContent(this->value, _width, _fillCharacter) + this->value;
 }
 
-string String::padRight(size_t _width, const char _fillCharacter)
+string String::padRight(size_t _width, const char _fillCharacter) const
 {
   return this->value + String::_createFillContent(this->value, _width, _fillCharacter);
 }
 
-string String::reverse()
+string String::reverse() const
 {
   string copy = this->value;
   ::reverse(copy.begin(), copy.end());
@@ -154,12 +155,12 @@ string String::reverse()
   return copy;
 }
 
-bool String::startsWith(const string &_text)
+bool String::startsWith(string_view _text) const
 {
   return this->value.rfind(_text, 0) == 0;
 }
 
-string String::toLowerCase()
+string String::toLowerCase() const
 {
   string copy = this->value;
   transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
@@ -167,7 +168,7 @@ string String::toLowerCase()
   return copy;
 }
 
-string String::toUpperCase()
+string String::toUpperCase() const
 {
   string copy = this->value;
   transform(copy.begin(), copy.end(), copy.begin(), ::toupper);
@@ -187,7 +188,7 @@ string String::_buildCharacterChain(size_t _amount, const char _fillCharacter)
   return fillContent;
 }
 
-string String::_createFillContent(const string &_text, size_t _width, const char _fillCharacter)
+string String::_createFillContent(string_view _text, size_t _width, const char _fillCharacter)
 {
   size_t fillSize = _text.size() > _width ? 0 : _width - _text.size();
   string fillContent{};

+ 1 - 45
test/cases/boxing/LongTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2023-03-25
+ * Changed:         2023-05-17
  *
  * */
 
@@ -364,50 +364,6 @@ namespace
     ASSERT_TRUE(!x);
   }
 
-  TEST_F(LongTest, operator_and_with_reference)
-  {
-    Long x{1};
-    Long y{1};
-
-    ASSERT_TRUE(x && y);
-  }
-
-  TEST_F(LongTest, operator_and_with_value)
-  {
-    Long x{1};
-    ASSERT_TRUE(x && (long_type) 1);
-  }
-
-  TEST_F(LongTest, operator_and_with_boolean)
-  {
-    Long x{1};
-    ASSERT_TRUE(x && true);
-  }
-
-  TEST_F(LongTest, operator_or_with_reference)
-  {
-    Long x{};
-    Long y{1};
-
-    ASSERT_TRUE(x || y);
-  }
-
-  TEST_F(LongTest, operator_or_with_value)
-  {
-    Long x{};
-    bool orWithValue = x || (long_type) 1;
-
-    ASSERT_TRUE(orWithValue);
-  }
-
-  TEST_F(LongTest, operator_or_with_boolean)
-  {
-    Long x{};
-    bool orWithBoolean = x || true;
-
-    ASSERT_TRUE(orWithBoolean);
-  }
-
   // increment / decrement operator
 
   TEST_F(LongTest, operator_increment)