Bladeren bron

Add dedicated NewLineRegex class

This regex is now replacing hard-coded expressions.
Patrick-Christopher Mattulat 11 maanden geleden
bovenliggende
commit
2c9f91001a

+ 2 - 0
CMakeLists.txt

@@ -189,6 +189,7 @@ set(SOURCE_FILES_CORE
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/interface/IStorable.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/interface/IValidator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/interface/IWriter.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/regex/NewLineRegex.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/Class.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/ConditionalFunctionExecutor.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/LibraryVersion.cpp
@@ -322,6 +323,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/IndexOutOfBoundsExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/NotImplementedExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/NullPointerExceptionTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/regex/NewLineRegexTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/ClassTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/ConditionalFunctionExecutorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/LibraryVersionTest.cpp

+ 36 - 0
include/ls-std/core/regex/NewLineRegex.hpp

@@ -0,0 +1,36 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-06-12
+* Changed:         2023-06-12
+*
+* */
+
+#ifndef LS_STD_NEWLINE_REGEX_HPP
+#define LS_STD_NEWLINE_REGEX_HPP
+
+#include <regex>
+#include <string>
+
+namespace ls::std::core
+{
+  class NewLineRegex
+  {
+    public:
+
+      ~NewLineRegex();
+      [[nodiscard]] static ::std::regex get();
+
+      [[nodiscard]] static ::std::string getStringRepresentation();
+
+    private:
+
+      NewLineRegex();
+
+      [[nodiscard]] static ::std::regex _get();
+      [[nodiscard]] static ::std::string _getStringRepresentation();
+  };
+}
+
+#endif

+ 3 - 1
include/ls-std/ls-std-core.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-05-13
- * Changed:         2023-05-24
+ * Changed:         2023-06-12
  *
  * */
 
@@ -40,6 +40,8 @@
 #include <ls-std/core/interface/IValidator.hpp>
 #include <ls-std/core/interface/IWriter.hpp>
 
+#include <ls-std/core/regex/NewLineRegex.hpp>
+
 #include <ls-std/core/type/EventTypes.hpp>
 #include <ls-std/core/type/RawPointer.hpp>
 #include <ls-std/core/type/Types.hpp>

+ 40 - 0
source/ls-std/core/regex/NewLineRegex.cpp

@@ -0,0 +1,40 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-06-12
+* Changed:         2023-06-12
+*
+* */
+
+#include <ls-std/core/regex/NewLineRegex.hpp>
+
+using ls::std::core::NewLineRegex;
+using std::regex;
+using std::string;
+
+NewLineRegex::~NewLineRegex() = default;
+
+regex NewLineRegex::get()
+{
+  return NewLineRegex::_get();
+}
+
+string NewLineRegex::getStringRepresentation()
+{
+  return NewLineRegex::_getStringRepresentation();
+}
+
+NewLineRegex::NewLineRegex() = default;
+
+regex NewLineRegex::_get()
+{
+  static auto expression = regex{NewLineRegex::_getStringRepresentation()};
+  return expression;
+}
+
+string NewLineRegex::_getStringRepresentation()
+{
+  static string representation = R"lit((\r\n|\r|\n))lit";
+  return representation;
+}

+ 4 - 2
source/ls-std/io/section-pair/validator/SectionPairDocumentValidator.cpp

@@ -3,15 +3,17 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-21
-* Changed:         2023-05-16
+* Changed:         2023-06-12
 *
 * */
 
+#include <ls-std/core/regex/NewLineRegex.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairDocumentValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairSectionValidator.hpp>
 #include <regex>
 
 using ls::std::core::Class;
+using ls::std::core::NewLineRegex;
 using ls::std::io::SectionPairDocumentValidator;
 using ls::std::io::SectionPairSectionValidator;
 using std::move;
@@ -34,7 +36,7 @@ bool SectionPairDocumentValidator::isValid()
 
 string SectionPairDocumentValidator::_getValidationRegex()
 {
-  string newLine = R"(((\n)|(\r\n)))";
+  string newLine = NewLineRegex::getStringRepresentation();
   string documentHeader = R"((# {1}(section\-pair document))" + newLine + R"())";
   string section = SectionPairSectionValidator::getValidationRegex();
   string atLeastOneSection = R"(()" + section + R"())";

+ 4 - 2
source/ls-std/io/section-pair/validator/SectionPairSectionValidator.cpp

@@ -3,10 +3,11 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-20
-* Changed:         2023-05-18
+* Changed:         2023-06-12
 *
 * */
 
+#include <ls-std/core/regex/NewLineRegex.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairIdentifierValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairRowValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairSectionValidator.hpp>
@@ -14,6 +15,7 @@
 #include <string>
 
 using ls::std::core::Class;
+using ls::std::core::NewLineRegex;
 using ls::std::io::SectionPairIdentifierValidator;
 using ls::std::io::SectionPairRowValidator;
 using ls::std::io::SectionPairSectionValidator;
@@ -42,7 +44,7 @@ bool SectionPairSectionValidator::isValid()
 
 string SectionPairSectionValidator::_getValidationRegex()
 {
-  string newLine = R"(((\n)|(\r\n)))";
+  string newLine = NewLineRegex::getStringRepresentation();
   string identifier = SectionPairIdentifierValidator::getValidationRegex();
   string sectionHeader = newLine + R"(\[{1}()" + identifier + R"()\]{1}()" + newLine + R"({2}))";
   string row = SectionPairRowValidator::getValidationRegex();

+ 37 - 0
test/cases/core/regex/NewLineRegexTest.cpp

@@ -0,0 +1,37 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-06-12
+* Changed:         2023-06-12
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-core.hpp>
+
+using ls::std::core::NewLineRegex;
+using std::regex_match;
+using testing::Test;
+
+namespace
+{
+  class NewLineRegexTest : public Test
+  {
+    public:
+
+      NewLineRegexTest() = default;
+      ~NewLineRegexTest() override = default;
+  };
+
+  TEST_F(NewLineRegexTest, get)
+  {
+    ASSERT_TRUE(regex_match("\n", NewLineRegex::get()));
+    ASSERT_TRUE(regex_match("\r\n", NewLineRegex::get()));
+  }
+
+  TEST_F(NewLineRegexTest, getStringRepresentation)
+  {
+    ASSERT_STREQ(R"lit((\r\n|\r|\n))lit", NewLineRegex::getStringRepresentation().c_str());
+  }
+}

+ 2 - 3
test/cases/io/section-pair/validator/SectionPairSectionValidatorTest.cpp

@@ -3,13 +3,12 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-20
-* Changed:         2023-06-06
+* Changed:         2023-06-12
 *
 * */
 
 #include <gtest/gtest.h>
 #include <ls-std-io-test.hpp>
-#include <ls-std/ls-std-core.hpp>
 #include <ls-std/ls-std-io.hpp>
 #include <string>
 
@@ -55,7 +54,7 @@ namespace
   TEST_F(SectionPairSectionValidatorTest, getValidationRegex)
   {
     string expected =
-        R"(((\n)|(\r\n))\[{1}([a-z]([a-z0-9-]){1,63})\]{1}(((\n)|(\r\n)){2})(((([a-z]([a-z0-9-]){1,63})={1}([a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}){1}($|\n{1}|\r{1}\n{1}))|(((((([a-z]([a-z0-9-]){1,63}):{1})((\n{1})|(\r{1}\n{1})))( {2}[a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}((\n{1})|(\r{1}\n{1})))){1}(( {2}[a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}((\n{1})|(\r{1}\n{1})))*)))){1})(((([a-z]([a-z0-9-]){1,63})={1}([a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}){1}($|\n{1}|\r{1}\n{1}))|(((((([a-z]([a-z0-9-]){1,63}):{1})((\n{1})|(\r{1}\n{1})))( {2}[a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}((\n{1})|(\r{1}\n{1})))){1}(( {2}[a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}((\n{1})|(\r{1}\n{1})))*))))*))";
+        R"((\r\n|\r|\n)\[{1}([a-z]([a-z0-9-]){1,63})\]{1}((\r\n|\r|\n){2})(((([a-z]([a-z0-9-]){1,63})={1}([a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}){1}($|\n{1}|\r{1}\n{1}))|(((((([a-z]([a-z0-9-]){1,63}):{1})((\n{1})|(\r{1}\n{1})))( {2}[a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}((\n{1})|(\r{1}\n{1})))){1}(( {2}[a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}((\n{1})|(\r{1}\n{1})))*)))){1})(((([a-z]([a-z0-9-]){1,63})={1}([a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}){1}($|\n{1}|\r{1}\n{1}))|(((((([a-z]([a-z0-9-]){1,63}):{1})((\n{1})|(\r{1}\n{1})))( {2}[a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}((\n{1})|(\r{1}\n{1})))){1}(( {2}[a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$€\\§<>+:;., \*\/"]{1,512}((\n{1})|(\r{1}\n{1})))*))))*))";
     ASSERT_STREQ(expected.c_str(), SectionPairSectionValidator::getValidationRegex().c_str());
   }