SectionPairSectionValidator.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-20
  6. * Changed: 2023-02-20
  7. *
  8. * */
  9. #include <ls-std/io/section-pair/validator/SectionPairRowValidator.hpp>
  10. #include <ls-std/io/section-pair/validator/SectionPairSectionValidator.hpp>
  11. #include <regex>
  12. #include <string>
  13. ls::std::io::SectionPairSectionValidator::SectionPairSectionValidator(::std::string _section) : ls::std::core::Class("SectionPairSectionValidator"), section(::std::move(_section))
  14. {}
  15. ls::std::io::SectionPairSectionValidator::~SectionPairSectionValidator() = default;
  16. ::std::string ls::std::io::SectionPairSectionValidator::getValidationRegex()
  17. {
  18. return ls::std::io::SectionPairSectionValidator::_getValidationRegex();
  19. }
  20. bool ls::std::io::SectionPairSectionValidator::isValid()
  21. {
  22. ::std::string validationRegex = ls::std::io::SectionPairSectionValidator::_getValidationRegex();
  23. static ::std::regex sectionRegex = ::std::regex{"^" + validationRegex};
  24. return ::std::regex_match(this->section, sectionRegex);
  25. }
  26. ::std::string ls::std::io::SectionPairSectionValidator::_getValidationRegex()
  27. {
  28. ::std::string newLine = R"(((\n)|(\r\n)))";
  29. ::std::string sectionHeader = newLine + R"(\[{1}([a-z]([a-z0-9-]){1,15})\]{1}()" + newLine + R"({2}))";
  30. ::std::string row = ls::std::io::SectionPairRowValidator::getValidationRegex();
  31. ::std::string atLeastOneRow = R"((()" + row + R"(){1}))";
  32. ::std::string optionalRows = R"((()" + row + R"()*))";
  33. return sectionHeader + atLeastOneRow + optionalRows;
  34. }