SectionPairSectionIdUnmarshalValidator.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-14
  6. * Changed: 2023-02-17
  7. *
  8. * */
  9. #include <ls-std/io/NewLine.hpp>
  10. #include <ls-std/io/section-pair/validator/SectionPairIdentifierValidator.hpp>
  11. #include <ls-std/io/section-pair/validator/SectionPairSectionIdUnmarshalValidator.hpp>
  12. ls::std::io::SectionPairSectionIdUnmarshalValidator::SectionPairSectionIdUnmarshalValidator(ls::std::core::type::byte_field _data, ::std::string _newLine) : ls::std::core::Class("SectionPairSectionIdUnmarshalValidator"), data(::std::move(_data)), newLine(::std::move(_newLine))
  13. {}
  14. ls::std::io::SectionPairSectionIdUnmarshalValidator::~SectionPairSectionIdUnmarshalValidator() = default;
  15. bool ls::std::io::SectionPairSectionIdUnmarshalValidator::isValid()
  16. {
  17. bool isValidSectionId = this->data.rfind(this->newLine, 0) == 0;
  18. isValidSectionId = isValidSectionId && this->data.rfind(this->newLine + this->newLine) == (this->data.size() - 2 * this->newLine.size());
  19. if (isValidSectionId)
  20. {
  21. isValidSectionId = ls::std::io::SectionPairSectionIdUnmarshalValidator::_isValidSection(ls::std::io::SectionPairSectionIdUnmarshalValidator::_trimStartAndEnd(this->data, this->newLine, this->newLine + this->newLine));
  22. }
  23. return isValidSectionId;
  24. }
  25. bool ls::std::io::SectionPairSectionIdUnmarshalValidator::_isValidSection(const ls::std::core::type::byte_field &_bracketsNotation)
  26. {
  27. bool isValidBracketsNotation = _bracketsNotation.rfind('[', 0) == 0;
  28. isValidBracketsNotation = isValidBracketsNotation && _bracketsNotation.rfind(']') == (_bracketsNotation.size() - 1);
  29. if (isValidBracketsNotation)
  30. {
  31. ls::std::core::type::byte_field identifier = ls::std::io::SectionPairSectionIdUnmarshalValidator::_trimStartAndEnd(_bracketsNotation, "[", "]");
  32. isValidBracketsNotation = ls::std::io::SectionPairIdentifierValidator{identifier}.isValid();
  33. }
  34. return isValidBracketsNotation;
  35. }
  36. ls::std::core::type::byte_field ls::std::io::SectionPairSectionIdUnmarshalValidator::_trimStartAndEnd(const ls::std::core::type::byte_field &_trimmedWord, const ls::std::core::type::byte_field &_beginningTrimWord, const ls::std::core::type::byte_field &_endingTrimWord)
  37. {
  38. ls::std::core::type::byte_field trimmedWord = _trimmedWord.substr(_beginningTrimWord.size());
  39. trimmedWord.erase(trimmedWord.end() - (uint32_t) _endingTrimWord.size(), trimmedWord.end());
  40. return trimmedWord;
  41. }