custom_filter.h 981 B

123456789101112131415161718192021222324252627282930313233343536
  1. #pragma once
  2. #include <algorithm>
  3. #include <cstddef>
  4. #include <regex>
  5. #include <string>
  6. #include <vector>
  7. class Glob {
  8. private:
  9. std::regex re_;
  10. public:
  11. explicit Glob(std::string glob) {
  12. for (size_t i = glob.find('.'); i != std::string::npos; i = glob.find('.', i + 2)) {
  13. glob.insert(glob.begin() + static_cast<ptrdiff_t>(i), '\\');
  14. }
  15. for (size_t i = glob.find('*'); i != std::string::npos; i = glob.find('*', i + 2)) {
  16. glob.insert(glob.begin() + static_cast<ptrdiff_t>(i), '.');
  17. }
  18. re_ = std::regex(glob);
  19. }
  20. bool operator==(std::string const & str) const { return std::regex_search(str, re_); }
  21. };
  22. struct RecursiveTestFilter {
  23. std::vector<Glob> whitelist;
  24. std::vector<Glob> blacklist;
  25. bool accepts(std::string const & str) const {
  26. auto in = [&str](auto & glob) { return glob == str; };
  27. return std::ranges::count_if(blacklist, in) == 0 and
  28. (whitelist.empty() or std::ranges::count_if(whitelist, in) > 0);
  29. }
  30. };