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
16 changes: 16 additions & 0 deletions FuzzySearch.Test/FuzzyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,22 @@ public void Score_ExactMatch_OutranksSameTextWithSuffix(string exact, string wit
Assert.IsGreaterThan(ScoreOf(withSuffix, exact), ScoreOf(exact, exact), $"'{exact}' should outrank '{withSuffix}' for pattern '{exact}'.");
}

[TestMethod]
[DataRow("sp", "AddressSpace", "AddresSpace")]
[DataRow("sel", "classSelector", "clasSelector")]
[DataRow("se", "cross_section", "cros_section")]
public void Score_DoubledLetterBeforeWordBoundary_DoesNotBlockBoundaryBonus(string pattern, string real, string misspelt)
{
// The real word has one more letter than the misspelling, so it may trail by that letter's penalty but no more.
Assert.IsGreaterThanOrEqualTo(ScoreOf(misspelt, pattern) - 1, ScoreOf(real, pattern), $"'{real}' should score about as well as '{misspelt}' for pattern '{pattern}'.");
}

[TestMethod]
public void Score_InsertingJunkBeforeABoundaryCopy_DoesNotRaiseTheScore()
{
Assert.IsGreaterThanOrEqualTo(ScoreOf("ba1aA", "a"), ScoreOf("baaA", "a"), "Inserting a junk character should not raise the score.");
}

#endregion

#region Separator Tests
Expand Down
21 changes: 20 additions & 1 deletion FuzzySearch/Fuzzy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,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 CalculateScoreCore(ReadOnlySpan<char> subject, ReadOnlySpan<char> pattern, out bool wholePatternIsPresent)

Check warning on line 168 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 168 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 168 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 168 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 168 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 168 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 38 to the 15 allowed.
{
if (pattern.IsEmpty)
{
Expand Down Expand Up @@ -267,6 +267,7 @@

// Update best letter in stringToSearch which may be for a "next" letter or a "rematch"
bool displacesRun = rematch && !nextMatch && bestLetterContinuesRun;
bool continuesRun = ContinuesRun(prevMatched, rematch, nextMatch, bestLetterIdx, bestLetterLength, strIdx);
if (newScore >= bestLetterScore && !displacesRun)
{
if (ReplacesProvisionalFirstLetter(firstLetterProvisional, rematch, nextMatch))
Expand All @@ -289,7 +290,7 @@
bestLetterIdx = strIdx;
bestLetterLength = strCharLength;
bestLetterScore = newScore;
bestLetterContinuesRun = prevMatched;
bestLetterContinuesRun = continuesRun;
}
else
{
Expand Down Expand Up @@ -348,6 +349,24 @@
private static bool ReplacesProvisionalFirstLetter(bool firstLetterProvisional, bool rematch, bool nextMatch) =>
firstLetterProvisional && rematch && !nextMatch;

/// <summary>
/// Determines whether a match continues a run of matched codepoints. Following the copy of the best letter that
/// it replaces does not count: that copy is no longer matched, and counting it would let the doubled letter in
/// "address" stop the camelCase copy in "Space" from displacing it.
/// </summary>
/// <param name="prevMatched">Whether the previous subject codepoint matched.</param>
/// <param name="rematch">Whether the subject codepoint repeats the best letter.</param>
/// <param name="nextMatch">Whether the subject codepoint matches the next pattern codepoint.</param>
/// <param name="bestLetterIdx">The index of the best letter in the subject, if there is one.</param>
/// <param name="bestLetterLength">The length of the best letter in code units.</param>
/// <param name="strIdx">The index of the subject codepoint being matched.</param>
/// <returns><c>true</c> if the match continues a run; otherwise, <c>false</c>.</returns>
private static bool ContinuesRun(bool prevMatched, bool rematch, bool nextMatch, int? bestLetterIdx, int bestLetterLength, int strIdx)
{
bool followsOwnCopy = rematch && !nextMatch && bestLetterIdx is not null && bestLetterIdx.Value + bestLetterLength == strIdx;
return prevMatched && !followsOwnCopy;
}

/// <summary>
/// Records a per-codepoint charge made while the first matched letter may still be replaced, so that a
/// replacement can refund it.
Expand Down Expand Up @@ -523,7 +542,7 @@

if (char.IsHighSurrogate(c) && i + 1 < value.Length && char.IsLowSurrogate(value[i + 1]))
{
i++;

Check warning on line 545 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.

Check warning on line 545 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.

Check warning on line 545 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.

Check warning on line 545 in FuzzySearch/Fuzzy.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Do not update the stop condition variable 'i' in the body of the for loop.
continue;
}

Expand Down
Loading