Browse Source

Address SonarLint findings in SectionPairDocument class

Patrick-Christopher Mattulat 1 year ago
parent
commit
93024b0adf

+ 9 - 8
include/ls-std/io/section-pair/model/SectionPairDocument.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-15
-* Changed:         2023-02-23
+* Changed:         2023-05-16
 *
 * */
 
@@ -16,6 +16,7 @@
 #include <ls-std/io/section-pair/type/SectionPairDocumentTypes.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
 #include <memory>
+#include <string_view>
 
 namespace ls::std::io
 {
@@ -28,14 +29,14 @@ namespace ls::std::io
 
       void add(const ls::std::io::section_pair_document_section_list_element &_section);
       void clear();
-      [[nodiscard]] ls::std::io::section_pair_document_section_list_element get(size_t _index);
-      [[nodiscard]] ls::std::io::section_pair_document_section_list_element get(const ls::std::io::section_pair_identifier &_sectionId);
-      [[nodiscard]] size_t getAmountOfSections();
-      [[nodiscard]] ::std::string getHeader();
-      [[nodiscard]] ls::std::io::section_pair_document_section_list getSectionList();
+      [[nodiscard]] ls::std::io::section_pair_document_section_list_element get(size_t _index) const;
+      [[nodiscard]] ls::std::io::section_pair_document_section_list_element get(const ls::std::io::section_pair_identifier &_sectionId) const;
+      [[nodiscard]] size_t getAmountOfSections() const;
+      [[nodiscard]] ::std::string getHeader() const;
+      [[nodiscard]] ls::std::io::section_pair_document_section_list getSectionList() const;
       [[nodiscard]] bool hasSection(const ls::std::io::section_pair_identifier &_sectionId);
       [[nodiscard]] ls::std::core::type::byte_field marshal() override;
-      void reserveNewLine(const ::std::string &_reservedNewLine);
+      void reserveNewLine(::std::string_view _reservedNewLine);
       void unmarshal(const ls::std::core::type::byte_field &_data) override;
 
     private:
@@ -47,7 +48,7 @@ namespace ls::std::io
 
       void _checkSectionExistence(const ls::std::io::section_pair_identifier &_sectionId);
       void _createSerializable();
-      [[nodiscard]] ls::std::io::section_pair_document_section_list_element _get(const ls::std::io::section_pair_identifier &_sectionId);
+      [[nodiscard]] ls::std::io::section_pair_document_section_list_element _get(const ls::std::io::section_pair_identifier &_sectionId) const;
       [[nodiscard]] bool _hasSection(const ls::std::io::section_pair_identifier &_identifier);
   };
 }

+ 11 - 10
source/ls-std/io/section-pair/model/SectionPairDocument.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-15
-* Changed:         2023-02-23
+* Changed:         2023-05-16
 *
 * */
 
@@ -33,6 +33,7 @@ using std::find_if;
 using std::make_shared;
 using std::reinterpret_pointer_cast;
 using std::string;
+using std::string_view;
 
 SectionPairDocument::SectionPairDocument() : Class("SectionPairDocument")
 {}
@@ -52,7 +53,7 @@ void SectionPairDocument::clear()
   this->sections.clear();
 }
 
-section_pair_document_section_list_element SectionPairDocument::get(size_t _index)
+section_pair_document_section_list_element SectionPairDocument::get(size_t _index) const
 {
   section_pair_document_section_list_element element{};
   IndexOutOfBoundsEvaluator{_index, this->sections.size()}.evaluate();
@@ -72,22 +73,22 @@ section_pair_document_section_list_element SectionPairDocument::get(size_t _inde
   return element;
 }
 
-section_pair_document_section_list_element SectionPairDocument::get(const section_pair_identifier &_sectionId)
+section_pair_document_section_list_element SectionPairDocument::get(const section_pair_identifier &_sectionId) const
 {
   return this->_get(_sectionId);
 }
 
-size_t SectionPairDocument::getAmountOfSections()
+size_t SectionPairDocument::getAmountOfSections() const
 {
   return this->sections.size();
 }
 
-string SectionPairDocument::getHeader()
+string SectionPairDocument::getHeader() const
 {
   return this->header;
 }
 
-section_pair_document_section_list SectionPairDocument::getSectionList()
+section_pair_document_section_list SectionPairDocument::getSectionList() const
 {
   return this->sections;
 }
@@ -103,7 +104,7 @@ byte_field SectionPairDocument::marshal()
   return this->serializable->marshal();
 }
 
-void SectionPairDocument::reserveNewLine(const string &_reservedNewLine)
+void SectionPairDocument::reserveNewLine(string_view _reservedNewLine)
 {
   this->reservedNewLine = _reservedNewLine;
 }
@@ -136,13 +137,13 @@ void SectionPairDocument::_createSerializable()
   this->serializable = make_shared<SerializableSectionPairDocument>(parameter);
 }
 
-section_pair_document_section_list_element SectionPairDocument::_get(const section_pair_identifier &_sectionId)
+section_pair_document_section_list_element SectionPairDocument::_get(const section_pair_identifier &_sectionId) const
 {
-  const auto iterator = find_if(this->sections.begin(), this->sections.end(), [_sectionId](const section_pair_document_section_list_element &_section) { return _section->getSectionId() == _sectionId; });
+  const auto iterator = find_if(this->sections.begin(), this->sections.end(), [&_sectionId](const section_pair_document_section_list_element &_section) { return _section->getSectionId() == _sectionId; });
   return iterator != this->sections.end() ? *iterator : nullptr;
 }
 
 bool SectionPairDocument::_hasSection(const section_pair_identifier &_identifier)
 {
-  return any_of(this->sections.begin(), this->sections.end(), [_identifier](const section_pair_document_section_list_element &_section) { return _section->getSectionId() == _identifier; });
+  return any_of(this->sections.begin(), this->sections.end(), [&_identifier](const section_pair_document_section_list_element &_section) { return _section->getSectionId() == _identifier; });
 }