yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
search.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GUI_CORE_SEARCH_H
2#define YAZE_APP_GUI_CORE_SEARCH_H
3
4#include <algorithm>
5#include <cctype>
6#include <string>
7
8namespace yaze {
9namespace gui {
10
23inline bool FuzzyMatch(const std::string& pattern, const std::string& str) {
24 if (pattern.empty()) return true;
25 if (str.empty()) return false;
26
27 size_t pattern_idx = 0;
28 for (char c : str) {
29 if (std::tolower(static_cast<unsigned char>(c)) ==
30 std::tolower(static_cast<unsigned char>(pattern[pattern_idx]))) {
31 pattern_idx++;
32 if (pattern_idx >= pattern.length()) return true;
33 }
34 }
35 return false;
36}
37
50inline int FuzzyScore(const std::string& pattern, const std::string& str) {
51 if (pattern.empty()) return 100;
52 if (!FuzzyMatch(pattern, str)) return 0;
53
54 std::string lower_pattern, lower_str;
55 lower_pattern.reserve(pattern.size());
56 lower_str.reserve(str.size());
57
58 for (char c : pattern)
59 lower_pattern += static_cast<char>(
60 std::tolower(static_cast<unsigned char>(c)));
61 for (char c : str)
62 lower_str += static_cast<char>(
63 std::tolower(static_cast<unsigned char>(c)));
64
65 if (lower_str.find(lower_pattern) == 0) return 100; // Exact prefix
66 if (lower_str.find(lower_pattern) != std::string::npos) return 80; // Contains
67 return 50; // Fuzzy only
68}
69
76inline bool PassesFilter(const std::string& filter, const std::string& str) {
77 if (filter.empty()) return true;
78 return FuzzyMatch(filter, str);
79}
80
87inline bool AnyPassesFilter(const std::string& filter,
88 const std::vector<std::string>& strings) {
89 if (filter.empty()) return true;
90 for (const auto& str : strings) {
91 if (FuzzyMatch(filter, str)) return true;
92 }
93 return false;
94}
95
96} // namespace gui
97} // namespace yaze
98
99#endif // YAZE_APP_GUI_CORE_SEARCH_H
bool PassesFilter(const std::string &filter, const std::string &str)
Check if a string matches a search filter.
Definition search.h:76
bool FuzzyMatch(const std::string &pattern, const std::string &str)
Simple fuzzy match - returns true if all chars in pattern appear in str in order.
Definition search.h:23
bool AnyPassesFilter(const std::string &filter, const std::vector< std::string > &strings)
Check if any of multiple strings match a search filter.
Definition search.h:87
int FuzzyScore(const std::string &pattern, const std::string &str)
Score a fuzzy match (higher = better, 0 = no match)
Definition search.h:50