SectionPairDocumentValidator.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-21
  6. * Changed: 2023-02-21
  7. *
  8. * */
  9. #include <ls-std/io/section-pair/validator/SectionPairDocumentValidator.hpp>
  10. #include <ls-std/io/section-pair/validator/SectionPairSectionValidator.hpp>
  11. #include <regex>
  12. ls::std::io::SectionPairDocumentValidator::SectionPairDocumentValidator(::std::string _document) : ls::std::core::Class("SectionPairDocumentValidator"), document(::std::move(_document))
  13. {}
  14. ls::std::io::SectionPairDocumentValidator::~SectionPairDocumentValidator() = default;
  15. bool ls::std::io::SectionPairDocumentValidator::isValid()
  16. {
  17. ::std::string validationRegex = ls::std::io::SectionPairDocumentValidator::_getValidationRegex();
  18. static ::std::regex documentRegex = ::std::regex{"^" + validationRegex + "$"};
  19. return ::std::regex_match(this->document, documentRegex);
  20. }
  21. ::std::string ls::std::io::SectionPairDocumentValidator::_getValidationRegex()
  22. {
  23. ::std::string newLine = R"(((\n)|(\r\n)))";
  24. ::std::string documentHeader = R"((# {1}(section\-pair document))" + newLine + R"())";
  25. ::std::string section = ls::std::io::SectionPairSectionValidator::getValidationRegex();
  26. ::std::string atLeastOneSection = R"(()" + section + R"())";
  27. ::std::string optionalSections = R"(()" + section + R"()*)";
  28. return documentHeader + atLeastOneSection + optionalSections;
  29. }