fuzzy_search.h 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #pragma once
  31. #include "core/variant/variant.h"
  32. class FuzzyTokenMatch;
  33. struct FuzzySearchToken {
  34. int idx = -1;
  35. String string;
  36. bool try_exact_match(FuzzyTokenMatch &p_match, const String &p_target, int p_offset) const;
  37. bool try_fuzzy_match(FuzzyTokenMatch &p_match, const String &p_target, int p_offset, int p_miss_budget) const;
  38. };
  39. class FuzzyTokenMatch {
  40. friend struct FuzzySearchToken;
  41. friend class FuzzySearchResult;
  42. friend class FuzzySearch;
  43. int matched_length = 0;
  44. int token_length = 0;
  45. int token_idx = -1;
  46. Vector2i interval = Vector2i(-1, -1); // x and y are both inclusive indices.
  47. void add_substring(int p_substring_start, int p_substring_length);
  48. bool intersects(const Vector2i &p_other_interval) const;
  49. bool is_case_insensitive(const String &p_original, const String &p_adjusted) const;
  50. int get_miss_count() const { return token_length - matched_length; }
  51. public:
  52. int score = 0;
  53. Vector<Vector2i> substrings; // x is start index, y is length.
  54. };
  55. class FuzzySearchResult {
  56. friend class FuzzySearch;
  57. int miss_budget = 0;
  58. Vector2i match_interval = Vector2i(-1, -1);
  59. bool can_add_token_match(const FuzzyTokenMatch &p_match) const;
  60. void score_token_match(FuzzyTokenMatch &p_match, bool p_case_insensitive) const;
  61. void add_token_match(const FuzzyTokenMatch &p_match);
  62. void maybe_apply_score_bonus();
  63. public:
  64. String target;
  65. int score = 0;
  66. int dir_index = -1;
  67. Vector<FuzzyTokenMatch> token_matches;
  68. };
  69. class FuzzySearch {
  70. Vector<FuzzySearchToken> tokens;
  71. void sort_and_filter(Vector<FuzzySearchResult> &p_results) const;
  72. public:
  73. int start_offset = 0;
  74. bool case_sensitive = false;
  75. int max_results = 100;
  76. int max_misses = 2;
  77. bool allow_subsequences = true;
  78. void set_query(const String &p_query);
  79. bool search(const String &p_target, FuzzySearchResult &p_result) const;
  80. void search_all(const PackedStringArray &p_targets, Vector<FuzzySearchResult> &p_results) const;
  81. };