| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/variant/variant.h" |
| 34 |
|
| 35 |
class FuzzyTokenMatch; |
| 36 |
|
| 37 |
struct FuzzySearchToken { |
| 38 |
int idx = -1; |
| 39 |
String string; |
| 40 |
|
| 41 |
bool try_exact_match(FuzzyTokenMatch &p_match, const String &p_target, int p_offset) const; |
| 42 |
bool try_fuzzy_match(FuzzyTokenMatch &p_match, const String &p_target, int p_offset, int p_miss_budget) const; |
| 43 |
}; |
| 44 |
|
| 45 |
class FuzzyTokenMatch { |
| 46 |
friend struct FuzzySearchToken; |
| 47 |
friend class FuzzySearchResult; |
| 48 |
friend class FuzzySearch; |
| 49 |
|
| 50 |
int matched_length = 0; |
| 51 |
int token_length = 0; |
| 52 |
int token_idx = -1; |
| 53 |
Vector2i interval = Vector2i(-1, -1); |
| 54 |
|
| 55 |
void add_substring(int p_substring_start, int p_substring_length); |
| 56 |
bool intersects(const Vector2i &p_other_interval) const; |
| 57 |
bool is_case_insensitive(const String &p_original, const String &p_adjusted) const; |
| 58 |
int get_miss_count() const { return token_length - matched_length; } |
| 59 |
|
| 60 |
public: |
| 61 |
int score = 0; |
| 62 |
Vector<Vector2i> substrings; |
| 63 |
}; |
| 64 |
|
| 65 |
class FuzzySearchResult { |
| 66 |
friend class FuzzySearch; |
| 67 |
|
| 68 |
int miss_budget = 0; |
| 69 |
Vector2i match_interval = Vector2i(-1, -1); |
| 70 |
|
| 71 |
bool can_add_token_match(const FuzzyTokenMatch &p_match) const; |
| 72 |
void score_token_match(FuzzyTokenMatch &p_match, bool p_case_insensitive) const; |
| 73 |
void add_token_match(const FuzzyTokenMatch &p_match); |
| 74 |
void maybe_apply_score_bonus(); |
| 75 |
|
| 76 |
public: |
| 77 |
String target; |
| 78 |
int score = 0; |
| 79 |
int original_index = -1; |
| 80 |
int dir_index = -1; |
| 81 |
Vector<FuzzyTokenMatch> token_matches; |
| 82 |
}; |
| 83 |
|
| 84 |
class FuzzySearch { |
| 85 |
Vector<FuzzySearchToken> tokens; |
| 86 |
|
| 87 |
bool case_sensitive = false; |
| 88 |
void sort_and_filter(Vector<FuzzySearchResult> &p_results) const; |
| 89 |
|
| 90 |
public: |
| 91 |
int start_offset = 0; |
| 92 |
int max_results = 100; |
| 93 |
int max_misses = 2; |
| 94 |
bool allow_subsequences = true; |
| 95 |
|
| 96 |
void set_query(const String &p_query); |
| 97 |
void set_query(const String &p_query, bool p_case_sensitive); |
| 98 |
bool search(const String &p_target, FuzzySearchResult &p_result) const; |
| 99 |
void search_all(const PackedStringArray &p_targets, Vector<FuzzySearchResult> &p_results) const; |
| 100 |
}; |