From 11bd6c3d68f6fbe5ca538dc3d0b8224544bf3f9c Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 18 Sep 2022 13:44:23 -0400 Subject: [PATCH] AK: Do not require an allocated String for fuzzy matching A StringView is sufficient here. This also removes the declaration of fuzzy_match_recursive from the header, as it's only needed from within the implementation file. --- AK/FuzzyMatch.cpp | 6 +++--- AK/FuzzyMatch.h | 7 ++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/AK/FuzzyMatch.cpp b/AK/FuzzyMatch.cpp index 3420e75e8e9..8a1e313895b 100644 --- a/AK/FuzzyMatch.cpp +++ b/AK/FuzzyMatch.cpp @@ -21,7 +21,7 @@ static constexpr int const LEADING_LETTER_PENALTY = -5; // penalty applied static constexpr int const MAX_LEADING_LETTER_PENALTY = -15; // maximum penalty for leading letters static constexpr int const UNMATCHED_LETTER_PENALTY = -1; // penalty for every letter that doesn't matter -static int calculate_score(String const& string, u8* index_points, size_t index_points_size) +static int calculate_score(StringView string, u8* index_points, size_t index_points_size) { int out_score = 100; @@ -59,7 +59,7 @@ static int calculate_score(String const& string, u8* index_points, size_t index_ return out_score; } -FuzzyMatchResult fuzzy_match_recursive(String const& needle, String const& haystack, size_t needle_idx, size_t haystack_idx, +static FuzzyMatchResult fuzzy_match_recursive(StringView needle, StringView haystack, size_t needle_idx, size_t haystack_idx, u8 const* src_matches, u8* matches, int next_match, int& recursion_count) { int out_score = 0; @@ -125,7 +125,7 @@ FuzzyMatchResult fuzzy_match_recursive(String const& needle, String const& hayst // Scores are not normalized between any values and have no particular meaning. The starting value is 100 and when we // detect good indicators of a match we add to the score. When we detect bad indicators, we penalize the match and subtract // from its score. Therefore, the longer the needle/haystack the greater the range of scores could be. -FuzzyMatchResult fuzzy_match(String const& needle, String const& haystack) +FuzzyMatchResult fuzzy_match(StringView needle, StringView haystack) { int recursion_count = 0; u8 matches[MAX_MATCHES] {}; diff --git a/AK/FuzzyMatch.h b/AK/FuzzyMatch.h index cc6a61fcf3f..8480d6b6362 100644 --- a/AK/FuzzyMatch.h +++ b/AK/FuzzyMatch.h @@ -6,7 +6,7 @@ #pragma once -#include +#include namespace AK { @@ -15,10 +15,7 @@ struct FuzzyMatchResult { int score { 0 }; }; -FuzzyMatchResult fuzzy_match_recursive(String const& needle, String const& haystack, size_t needle_idx, size_t haystack_idx, - u8 const* src_matches, u8* matches, int next_match, int& recursion_count); - -FuzzyMatchResult fuzzy_match(String const& needle, String const& haystack); +FuzzyMatchResult fuzzy_match(StringView needle, StringView haystack); }