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
66 changes: 46 additions & 20 deletions FuzzySearch.Test/FuzzyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,11 @@ public void ApplyBonuses_PrevMatched_AddsAdjacentMatchBonus()
bool prevMatched = true;
bool prevLower = false;
bool prevSeparator = false;
char strChar = 'a';
char strLower = 'a';
char strUpper = 'A';
bool isUpper = false;
int initialScore = 0;

// Act
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, strChar, strLower, strUpper, initialScore);
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, isUpper, initialScore);

// Assert
Assert.AreEqual(Fuzzy.adjacentMatchBonus, result);
Expand All @@ -264,13 +262,11 @@ public void ApplyBonuses_PrevSeparator_AddsMatchAfterSeparatorBonus()
bool prevMatched = false;
bool prevLower = false;
bool prevSeparator = true;
char strChar = 'a';
char strLower = 'a';
char strUpper = 'A';
bool isUpper = false;
int initialScore = 0;

// Act
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, strChar, strLower, strUpper, initialScore);
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, isUpper, initialScore);

// Assert
Assert.AreEqual(Fuzzy.matchAfterSeparatorBonus, result);
Expand All @@ -283,13 +279,11 @@ public void ApplyBonuses_CamelCaseBoundary_AddsCamelCaseMatchBonus()
bool prevMatched = false;
bool prevLower = true;
bool prevSeparator = false;
char strChar = 'A'; // Capital letter
char strLower = 'a';
char strUpper = 'A';
bool isUpper = true;
int initialScore = 0;

// Act
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, strChar, strLower, strUpper, initialScore);
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, isUpper, initialScore);

// Assert
Assert.AreEqual(Fuzzy.camelCaseMatchBonus, result);
Expand All @@ -302,14 +296,12 @@ public void ApplyBonuses_AllBonusesApply_AddsAllBonuses()
bool prevMatched = true;
bool prevLower = true;
bool prevSeparator = true;
char strChar = 'A'; // Capital letter
char strLower = 'a';
char strUpper = 'A';
bool isUpper = true;
int initialScore = 0;
int expectedBonus = Fuzzy.adjacentMatchBonus + Fuzzy.matchAfterSeparatorBonus + Fuzzy.camelCaseMatchBonus;

// Act
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, strChar, strLower, strUpper, initialScore);
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, isUpper, initialScore);

// Assert
Assert.AreEqual(expectedBonus, result);
Expand All @@ -322,13 +314,11 @@ public void ApplyBonuses_NoBonusesApply_ScoreUnchanged()
bool prevMatched = false;
bool prevLower = false;
bool prevSeparator = false;
char strChar = 'a';
char strLower = 'a';
char strUpper = 'A';
bool isUpper = false;
int initialScore = 5;

// Act
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, strChar, strLower, strUpper, initialScore);
int result = Fuzzy.ApplyBonuses(prevMatched, prevLower, prevSeparator, isUpper, initialScore);

// Assert
Assert.AreEqual(initialScore, result);
Expand Down Expand Up @@ -786,6 +776,7 @@ public void Contains_WithScore_SupplementaryPlanePrefix_DoesNotExceedTheUncapped
private const string DeseretCapitalLongI = "\U00010400";
private const string DeseretSmallLongI = "\U00010428";
private const string DeseretCapitalLongE = "\U00010401";
private const string DeseretSmallLongE = "\U00010429";
private const string AdlamCapitalAlif = "\U0001E900";
private const string AdlamSmallAlif = "\U0001E922";
private const string OsageCapitalAOsage = "\U000104B0\U000104B1";
Expand Down Expand Up @@ -832,6 +823,41 @@ public void Contains_SupplementaryPlaneLetter_DoesNotMatchADifferentLetterInOthe
Assert.IsFalse(result, "Ignoring case must not make two different supplementary-plane letters equal.");
}

[TestMethod]
[DataRow("ab" + DeseretCapitalLongI, DeseretSmallLongI, "abC", DisplayName = "Deseret capital, lowercase pattern")]
[DataRow("ab" + DeseretCapitalLongI, DeseretCapitalLongI, "abC", DisplayName = "Deseret capital, uppercase pattern")]
[DataRow("ab\U000104B0", "\U000104B0", "abC", DisplayName = "Osage capital")]
[DataRow(DeseretSmallLongE + DeseretSmallLongE + DeseretCapitalLongI, DeseretCapitalLongI, "abC", DisplayName = "Deseret lower, lower, upper")]
[DataRow(DeseretSmallLongE + "C", "c", "aC", DisplayName = "Deseret lowercase before a basic-plane capital")]
public void Contains_WithScore_SupplementaryPlaneCamelCaseBoundary_ScoresLikeTheBasicPlaneBoundary(string subject, string pattern, string basicPlaneSubject)
{
// Arrange
bool basicPlane = Fuzzy.Contains(basicPlaneSubject, "c", out int basicPlaneScore);

// Act
bool result = Fuzzy.Contains(subject, pattern, out int score);

// Assert
Assert.IsTrue(basicPlane, "The basic-plane pattern should match.");
Assert.IsTrue(result, "The pattern should match.");
Assert.AreEqual(basicPlaneScore, score, "A supplementary-plane camelCase boundary must earn the same bonus as a basic-plane one.");
}

[TestMethod]
public void Contains_WithScore_InsideASupplementaryPlaneCapitalRun_EarnsNoCamelCaseBonus()
{
// Arrange
bool basicPlane = Fuzzy.Contains("ABC", "c", out int basicPlaneScore);

// Act
bool result = Fuzzy.Contains(DeseretCapitalLongE + DeseretCapitalLongE + DeseretCapitalLongI, DeseretSmallLongI, out int score);

// Assert
Assert.IsTrue(basicPlane, "The basic-plane pattern should match.");
Assert.IsTrue(result, "The pattern should match.");
Assert.AreEqual(basicPlaneScore, score, "A capital following capitals is not a camelCase boundary.");
}

#endregion

#region Case Folding Tests
Expand Down
72 changes: 57 additions & 15 deletions 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 37 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 37 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 37 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 37 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 37 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 37 to the 15 allowed.
{
if (pattern.IsEmpty)
{
Expand Down Expand Up @@ -219,11 +219,11 @@
int patternCharLength = hasPatternChar ? CodepointLengthAt(pattern, patternIdx) : 0;
int strCharLength = CodepointLengthAt(subject, strIdx);

// The leading code unit carries the case and separator properties of the codepoint: a surrogate is
// caseless and is never a separator, which is exactly how a supplementary-plane codepoint should behave.
// Case is read from the whole codepoint, so a supplementary-plane capital such as Deseret U+10400 marks a
// camelCase boundary like a basic-plane one. The leading code unit is enough to tell a separator, because
// every separator is a basic-plane character and a surrogate never is one.
char strChar = subject[strIdx];
char strLower = char.ToLowerInvariant(strChar);
char strUpper = char.ToUpperInvariant(strChar);
GetLetterCase(subject, strIdx, strCharLength, out bool strIsUpper, out bool strIsLower);

bool nextMatch = hasPatternChar && CodepointsEqual(pattern, patternIdx, patternCharLength, subject, strIdx, strCharLength);
bool rematch = bestLetterIdx is not null && CodepointsEqual(subject, bestLetterIdx.Value, bestLetterLength, subject, strIdx, strCharLength);
Expand Down Expand Up @@ -257,7 +257,7 @@
provisionalCharges = 0;
}

newScore = ApplyBonuses(prevMatched, prevLower, prevSeparator, strChar, strLower, strUpper, newScore);
newScore = ApplyBonuses(prevMatched, prevLower, prevSeparator, strIsUpper, newScore);

// Update pattern index IF the next pattern codepoint was matched
if (nextMatch)
Expand Down Expand Up @@ -318,10 +318,7 @@
prevMatched = false;
}

// "clever" isLetter check.
bool isLetter = strLower != strUpper;

prevLower = strChar == strLower && isLetter;
prevLower = strIsLower;
prevSeparator = IsSeparator(strChar);

strIdx += strCharLength;
Expand Down Expand Up @@ -469,6 +466,53 @@
#endif
}

/// <summary>
/// Determines whether the codepoint at the given index is an uppercase or a lowercase letter.
/// </summary>
/// <param name="value">The span holding the codepoint.</param>
/// <param name="index">The index of the first code unit of the codepoint.</param>
/// <param name="length">The length of the codepoint in code units.</param>
/// <param name="isUpper">When this method returns, whether the codepoint is an uppercase letter.</param>
/// <param name="isLower">When this method returns, whether the codepoint is a lowercase letter.</param>
/// <remarks>
/// A codepoint counts as a letter only when its lowercase and uppercase forms differ, so caseless characters are
/// neither. A supplementary-plane codepoint is cased as a whole, because <see cref="char.ToLowerInvariant(char)"/>
/// has no case mapping for either half of a surrogate pair.
/// </remarks>
internal static void GetLetterCase(ReadOnlySpan<char> value, int index, int length, out bool isUpper, out bool isLower)
{
if (length == 2)
{
#if NETCOREAPP3_0_OR_GREATER
Rune rune = new(value[index], value[index + 1]);
Rune runeLower = Rune.ToLowerInvariant(rune);
Rune runeUpper = Rune.ToUpperInvariant(rune);
bool runeIsCased = runeLower != runeUpper;

isUpper = runeIsCased && rune == runeUpper;
isLower = runeIsCased && rune == runeLower;
#else
// Rune is not available here, but the invariant string casing maps a surrogate pair as one codepoint.
string pair = value.Slice(index, 2).ToString();
string pairLower = pair.ToLowerInvariant();
string pairUpper = pair.ToUpperInvariant();
bool pairIsCased = !string.Equals(pairLower, pairUpper, StringComparison.Ordinal);

isUpper = pairIsCased && string.Equals(pair, pairUpper, StringComparison.Ordinal);
isLower = pairIsCased && string.Equals(pair, pairLower, StringComparison.Ordinal);
#endif
return;
}

char c = value[index];
char lower = char.ToLowerInvariant(c);
char upper = char.ToUpperInvariant(c);
bool isCased = lower != upper;

isUpper = isCased && c == upper;
isLower = isCased && c == lower;
}

/// <summary>
/// Determines whether two characters are the same, ignoring case.
/// </summary>
Expand Down Expand Up @@ -542,7 +586,7 @@

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

Check warning on line 589 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 589 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 589 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 589 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 Expand Up @@ -587,12 +631,10 @@
/// <param name="prevMatched">Whether the previous character was a match.</param>
/// <param name="prevLower">Whether the previous character was lowercase.</param>
/// <param name="prevSeparator">Whether the previous character was a separator.</param>
/// <param name="strChar">The current character being considered.</param>
/// <param name="strLower">The lowercase form of the current character.</param>
/// <param name="strUpper">The uppercase form of the current character.</param>
/// <param name="isUpper">Whether the current codepoint is an uppercase letter.</param>
/// <param name="newScore">The current score to apply bonuses to.</param>
/// <returns>The updated score after applying any applicable bonuses.</returns>
internal static int ApplyBonuses(bool prevMatched, bool prevLower, bool prevSeparator, char strChar, char strLower, char strUpper, int newScore)
internal static int ApplyBonuses(bool prevMatched, bool prevLower, bool prevSeparator, bool isUpper, int newScore)
{
// Apply bonus for consecutive bonuses
if (prevMatched)
Expand All @@ -606,8 +648,8 @@
newScore += matchAfterSeparatorBonus;
}

// Apply bonus across camel case boundaries. Includes "clever" isLetter check.
if (prevLower && strChar == strUpper && strLower != strUpper)
// Apply bonus across camel case boundaries
if (prevLower && isUpper)
{
newScore += camelCaseMatchBonus;
}
Expand Down
Loading