Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce case sensitive comparison optimization for FrozenDictionary in some cases #95232

Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static AnalysisResults CreateAnalysisResults(
// actually perform the comparison as case-sensitive even if case-insensitive
// was requested, as there's nothing that would compare equally to the substring
// other than the substring itself.
bool canSwitchIgnoreCaseHashToCaseSensitive = !isSubstring;
bool canSwitchIgnoreCaseHashToCaseSensitive = true;

foreach (string s in uniqueStrings)
{
Expand All @@ -144,11 +144,21 @@ private static AnalysisResults CreateAnalysisResults(
break;
}

// All substrings so far are still ASCII only. If this one contains any ASCII
// letters, mark that we can't switch to case-sensitive.
if (canSwitchIgnoreCaseHashToCaseSensitive && ContainsAnyLetters(substring))
// All substrings so far are still ASCII only. Now we check whether
// the entire string either is not ASCII or contains ASCII letters,
// in which case we disable the switch to case sensitive.
if (canSwitchIgnoreCaseHashToCaseSensitive)
{
canSwitchIgnoreCaseHashToCaseSensitive = false;
// If we are testing the entire string then s == substring
// and we have already checked IsAllAscii(s).
if (isSubstring && !IsAllAscii(s.AsSpan()))
andrewjsaid marked this conversation as resolved.
Show resolved Hide resolved
{
canSwitchIgnoreCaseHashToCaseSensitive = false;
}
else if (ContainsAnyLetters(s.AsSpan()))
andrewjsaid marked this conversation as resolved.
Show resolved Hide resolved
{
canSwitchIgnoreCaseHashToCaseSensitive = false;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public static void LeftHandCaseInsensitive()
Assert.Equal(0, r.HashIndex);
Assert.Equal(1, r.HashCount);

r = RunAnalysis(new[] { "0001", "0002", "0003", "0004", "0005", "0006" }, true);
Assert.False(r.RightJustifiedSubstring);
Assert.False(r.IgnoreCase);
Assert.True(r.AllAsciiIfIgnoreCase);
Assert.Equal(3, r.HashIndex);
Assert.Equal(1, r.HashCount);

}

[Fact]
Expand Down