diff --git a/FuzzySearch.Test/FuzzyTests.cs b/FuzzySearch.Test/FuzzyTests.cs index aa3ecef..9517c73 100644 --- a/FuzzySearch.Test/FuzzyTests.cs +++ b/FuzzySearch.Test/FuzzyTests.cs @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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"; @@ -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 diff --git a/FuzzySearch/Fuzzy.cs b/FuzzySearch/Fuzzy.cs index 9b096d6..40d0ff9 100644 --- a/FuzzySearch/Fuzzy.cs +++ b/FuzzySearch/Fuzzy.cs @@ -219,11 +219,11 @@ internal static int CalculateScoreCore(ReadOnlySpan subject, ReadOnlySpan< 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); @@ -257,7 +257,7 @@ internal static int CalculateScoreCore(ReadOnlySpan subject, ReadOnlySpan< 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) @@ -318,10 +318,7 @@ internal static int CalculateScoreCore(ReadOnlySpan subject, ReadOnlySpan< prevMatched = false; } - // "clever" isLetter check. - bool isLetter = strLower != strUpper; - - prevLower = strChar == strLower && isLetter; + prevLower = strIsLower; prevSeparator = IsSeparator(strChar); strIdx += strCharLength; @@ -469,6 +466,53 @@ private static bool SurrogatePairsEqualIgnoringCase(ReadOnlySpan left, Rea #endif } + /// + /// Determines whether the codepoint at the given index is an uppercase or a lowercase letter. + /// + /// The span holding the codepoint. + /// The index of the first code unit of the codepoint. + /// The length of the codepoint in code units. + /// When this method returns, whether the codepoint is an uppercase letter. + /// When this method returns, whether the codepoint is a lowercase letter. + /// + /// 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 + /// has no case mapping for either half of a surrogate pair. + /// + internal static void GetLetterCase(ReadOnlySpan 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; + } + /// /// Determines whether two characters are the same, ignoring case. /// @@ -587,12 +631,10 @@ internal static bool IsAscii(ReadOnlySpan value) /// Whether the previous character was a match. /// Whether the previous character was lowercase. /// Whether the previous character was a separator. - /// The current character being considered. - /// The lowercase form of the current character. - /// The uppercase form of the current character. + /// Whether the current codepoint is an uppercase letter. /// The current score to apply bonuses to. /// The updated score after applying any applicable bonuses. - 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) @@ -606,8 +648,8 @@ internal static int ApplyBonuses(bool prevMatched, bool prevLower, bool prevSepa 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; }