filter.hpp (2370B)
1 /* ReaPack: Package manager for REAPER 2 * Copyright (C) 2015-2025 Christian Fillion 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef REAPACK_FILTER_HPP 19 #define REAPACK_FILTER_HPP 20 21 #include <memory> 22 #include <string> 23 #include <string_view> 24 #include <vector> 25 26 class Filter { 27 public: 28 Filter(const std::string & = {}); 29 void set(const std::string &); 30 Filter &operator=(const std::string &f) { set(f); return *this; } 31 32 bool match(std::vector<std::string> rows) const; 33 34 private: 35 class Node { 36 public: 37 enum Flag { 38 StartAnchorFlag = 1<<0, 39 EndAnchorFlag = 1<<1, 40 LiteralFlag = 1<<2, 41 NotFlag = 1<<3, 42 FullWordFlag = 1<<4, 43 }; 44 45 Node(int flags) : m_flags(flags) {} 46 virtual ~Node() = default; 47 48 virtual bool match(const std::vector<std::string> &) const = 0; 49 bool test(Flag f) const { return (m_flags & f) != 0; } 50 51 private: 52 int m_flags; 53 }; 54 55 class Group : public Node { 56 public: 57 enum Type { 58 MatchAll, 59 MatchAny, 60 }; 61 62 Group(Type type, int flags = 0, Group *parent = nullptr); 63 void clear() { m_nodes.clear(); } 64 Group *push(const std::string_view &, int *flags); 65 66 bool match(const std::vector<std::string> &) const override; 67 68 private: 69 Group *addSubGroup(Type, int flags); 70 bool pushSynonyms(const std::string_view &, int *flags); 71 72 Group *m_parent; 73 Type m_type; 74 std::vector<std::unique_ptr<Node>> m_nodes; 75 }; 76 77 class Token : public Node { 78 public: 79 Token(const std::string_view &buf, int flags); 80 bool match(const std::vector<std::string> &) const override; 81 bool matchRow(const std::string &) const; 82 83 private: 84 std::string_view m_buf; 85 }; 86 87 std::string m_input; 88 Group m_root; 89 }; 90 91 #endif