custom_filter.h 885 B

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