Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/string/fuzzy_search.h
9896 views
1
/**************************************************************************/
2
/* fuzzy_search.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
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); // x and y are both inclusive indices.
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; // x is start index, y is length.
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
};
101
102