/**************************************************************************/1/* fuzzy_search.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/variant/variant.h"3334class FuzzyTokenMatch;3536struct FuzzySearchToken {37int idx = -1;38String string;3940bool try_exact_match(FuzzyTokenMatch &p_match, const String &p_target, int p_offset) const;41bool try_fuzzy_match(FuzzyTokenMatch &p_match, const String &p_target, int p_offset, int p_miss_budget) const;42};4344class FuzzyTokenMatch {45friend struct FuzzySearchToken;46friend class FuzzySearchResult;47friend class FuzzySearch;4849int matched_length = 0;50int token_length = 0;51int token_idx = -1;52Vector2i interval = Vector2i(-1, -1); // x and y are both inclusive indices.5354void add_substring(int p_substring_start, int p_substring_length);55bool intersects(const Vector2i &p_other_interval) const;56bool is_case_insensitive(const String &p_original, const String &p_adjusted) const;57int get_miss_count() const { return token_length - matched_length; }5859public:60int score = 0;61Vector<Vector2i> substrings; // x is start index, y is length.62};6364class FuzzySearchResult {65friend class FuzzySearch;6667int miss_budget = 0;68Vector2i match_interval = Vector2i(-1, -1);6970bool can_add_token_match(const FuzzyTokenMatch &p_match) const;71void score_token_match(FuzzyTokenMatch &p_match, bool p_case_insensitive) const;72void add_token_match(const FuzzyTokenMatch &p_match);73void maybe_apply_score_bonus();7475public:76String target;77int score = 0;78int original_index = -1;79int dir_index = -1;80Vector<FuzzyTokenMatch> token_matches;81};8283class FuzzySearch {84Vector<FuzzySearchToken> tokens;8586bool case_sensitive = false;87void sort_and_filter(Vector<FuzzySearchResult> &p_results) const;8889public:90int start_offset = 0;91int max_results = 100;92int max_misses = 2;93bool allow_subsequences = true;9495void set_query(const String &p_query);96void set_query(const String &p_query, bool p_case_sensitive);97bool search(const String &p_target, FuzzySearchResult &p_result) const;98void search_all(const PackedStringArray &p_targets, Vector<FuzzySearchResult> &p_results) const;99};100101102