SectionPairDocumentValidator.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-21
  6. * Changed: 2023-05-16
  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. using ls::std::core::Class;
  13. using ls::std::io::SectionPairDocumentValidator;
  14. using ls::std::io::SectionPairSectionValidator;
  15. using std::move;
  16. using std::regex;
  17. using std::regex_match;
  18. using std::string;
  19. SectionPairDocumentValidator::SectionPairDocumentValidator(string _document) : Class("SectionPairDocumentValidator"), document(::move(_document))
  20. {}
  21. SectionPairDocumentValidator::~SectionPairDocumentValidator() noexcept = default;
  22. bool SectionPairDocumentValidator::isValid()
  23. {
  24. string validationRegex = SectionPairDocumentValidator::_getValidationRegex();
  25. static auto documentRegex = regex{"^" + validationRegex + "$"};
  26. return regex_match(this->document, documentRegex);
  27. }
  28. string SectionPairDocumentValidator::_getValidationRegex()
  29. {
  30. string newLine = R"(((\n)|(\r\n)))";
  31. string documentHeader = R"((# {1}(section\-pair document))" + newLine + R"())";
  32. string section = SectionPairSectionValidator::getValidationRegex();
  33. string atLeastOneSection = R"(()" + section + R"())";
  34. string optionalSections = R"(()" + section + R"()*)";
  35. return documentHeader + atLeastOneSection + optionalSections;
  36. }