Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion FuzzySearch.Test/FuzzyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ public void Contains_WithScore_CamelCaseMatch_GetsBonus()
Assert.IsGreaterThanOrEqualTo(Fuzzy.camelCaseMatchBonus, score, "Score should include camelCase bonus");
}

[TestMethod]
public void Contains_WithScore_LatePrefixMatch_ScoresLowerThanStartMatch()
{
// Arrange
string pattern = "foo";

// Act
bool earlyResult = Fuzzy.Contains("foo", pattern, out int earlyScore);
bool lateResult = Fuzzy.Contains("xxxxxxxxxxfoo", pattern, out int lateScore);

// Assert
Assert.IsTrue(earlyResult, "Start-position match should return true.");
Assert.IsTrue(lateResult, "Late-position match should return true.");
Assert.IsGreaterThan(lateScore, earlyScore, "Match at the start should score higher than a late prefix match.");
}

#endregion

#region Apply Bonuses Tests
Expand Down Expand Up @@ -329,7 +345,23 @@ public void PenalizeNonPatternCharacters_FirstPatternChar_AppliesPrefixPenalty()
int initialScore = 10;
int patternIdx = 0;
int strIdx = 3; // 3 chars before first match
int expectedPenalty = Math.Max(strIdx * Fuzzy.unmatchedPrefixLetterPenalty, Fuzzy.maxPrefixPenalty);
int expectedPenalty = -3;

// Act
int result = Fuzzy.PenalizeNonPatternCharacters(initialScore, patternIdx, strIdx);

// Assert
Assert.AreEqual(initialScore + expectedPenalty, result);
}

[TestMethod]
public void PenalizeNonPatternCharacters_FirstPatternChar_CapsPrefixPenalty()
{
// Arrange
int initialScore = 10;
int patternIdx = 0;
int strIdx = 10;
int expectedPenalty = -5;

// Act
int result = Fuzzy.PenalizeNonPatternCharacters(initialScore, patternIdx, strIdx);
Expand Down
8 changes: 2 additions & 6 deletions FuzzySearch/Fuzzy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
internal const int camelCaseMatchBonus = 10;

/// <summary>The penalty for each unmatched character at the beginning of the string.</summary>
internal const int unmatchedPrefixLetterPenalty = 0;
internal const int unmatchedPrefixLetterPenalty = -1;

/// <summary>The maximum prefix penalty that can be applied.</summary>
internal const int maxPrefixPenalty = 0;
internal const int maxPrefixPenalty = -5;

/// <summary>The penalty for each unmatched character in the string.</summary>
internal const int unmatchedLetterPenalty = -1;
Expand Down Expand Up @@ -96,7 +96,7 @@
/// or the pattern is empty and the subject is not; otherwise, <c>false</c>.
/// </param>
/// <returns>A score representing the quality of the match. Higher scores indicate better matches.</returns>
internal static int CalculateScore(ReadOnlySpan<char> subject, ReadOnlySpan<char> pattern, out bool wholePatternIsPresent)

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

Check warning on line 99 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.
{
if (pattern.IsEmpty)
{
Expand All @@ -119,8 +119,6 @@
int? bestLetterIdx = null;
int bestLetterScore = 0;

List<int> matchedIndices = [];

// Loop over characters in subject
while (strIdx != strLength)
{
Expand All @@ -139,7 +137,6 @@
if (bestLetterIdx is not null && (advanced || patternRepeat))
{
score += bestLetterScore;
matchedIndices.Add((int)bestLetterIdx);
bestLetter = null;
bestLower = null;
bestLetterIdx = null;
Expand Down Expand Up @@ -196,7 +193,6 @@
if (bestLetter is not null && bestLetterIdx is not null)
{
score += bestLetterScore;
matchedIndices.Add((int)bestLetterIdx);
}

wholePatternIsPresent = patternIdx == patternLength;
Expand Down