-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[browser] HybridGlobalization
correct HashCode
ranges of skipped unicodes
#97351
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c100191
Add perf measurements.
ilonatommy 4a4d284
Correct ranges.
ilonatommy bb39070
Feedback.
ilonatommy da34518
Apply v8 / spidermonkey skippable unicodes.
ilonatommy 5da154d
Slower version of tests (one icall for each test case).
ilonatommy 029c6fb
Merge branch 'main' into correct-hashcode-skip-ranges
ilonatommy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,42 +74,47 @@ private void TryAddToCustomDictionary(StringComparer comparer, string str1, stri | |
} | ||
} | ||
|
||
public static IEnumerable<object[]> CheckHashingOfSkippedChars_TestData() | ||
public static IEnumerable<object[]> CharsIgnoredByEqualFunction() | ||
{ | ||
// one char from each ignored category that is skipped on ICU | ||
yield return new object[] { '\u0008', s_invariantCompare }; // Control: BACKSPACE | ||
yield return new object[] { '\u200B', s_invariantCompare }; // Format: ZERO WIDTH SPACE | ||
yield return new object[] { '\u180A', s_invariantCompare }; // OtherPunctuation: MONGOLIAN NIRUGU | ||
yield return new object[] { '\uFE73', s_invariantCompare }; // OtherLetter: THAI CHARACTER PAIYANNOI | ||
yield return new object[] { '\u0F3E', s_invariantCompare }; // SpacingCombiningMark: "TIBETAN MARK GTER YIG MGO UM RTAGS GNYIS | ||
yield return new object[] { '\u0640', s_invariantCompare }; // ModifierLetter: ARABIC TATWEEL | ||
yield return new object[] { '\u0488', s_invariantCompare }; // EnclosingMark: COMBINING CYRILLIC HUNDRED THOUSANDS SIGN | ||
yield return new object[] { '\u034F', s_invariantCompare }; // NonSpacingMark: DIAERESIS | ||
CompareInfo thaiCmpInfo = new CultureInfo("th-TH").CompareInfo; | ||
yield return new object[] { '\u0020', thaiCmpInfo }; // SpaceSeparator: SPACE | ||
yield return new object[] { '\u0028', thaiCmpInfo }; // OpenPunctuation: LEFT PARENTHESIS | ||
yield return new object[] { '\u007D', thaiCmpInfo }; // ClosePunctuation: RIGHT PARENTHESIS | ||
yield return new object[] { '\u2013', thaiCmpInfo }; // DashPunctuation: EN DASH | ||
yield return new object[] { '\u005F', thaiCmpInfo }; // ConnectorPunctuation: LOW LINE | ||
yield return new object[] { '\u2018', thaiCmpInfo }; // InitialQuotePunctuation: LEFT SINGLE QUOTATION MARK | ||
yield return new object[] { '\u2019', thaiCmpInfo }; // FinalQuotePunctuation: RIGHT SINGLE QUOTATION MARK | ||
yield return new object[] { '\u2028', thaiCmpInfo }; // LineSeparator: LINE SEPARATOR | ||
yield return new object[] { '\u2029', thaiCmpInfo }; // ParagraphSeparator: PARAGRAPH SEPARATOR | ||
string str1 = "ab"; | ||
// browser supports 240 cultures (e.g. "en-US"), out of which 54 neutral cultures (e.g. "en") | ||
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures); | ||
List<int> ignoredCodepoints = new(); | ||
foreach(var culture in cultures) | ||
{ | ||
// japanese with None is not supported, JS always ignores Kana when localeCompare is used | ||
CompareOptions options = culture.Name == "ja" ? | ||
CompareOptions.IgnoreKanaType : | ||
CompareOptions.None; | ||
CompareInfo cmpInfo = culture.CompareInfo; | ||
var hashCode1 = cmpInfo.GetHashCode(str1, options); | ||
for(int codePoint = 0; codePoint < 0x10FFFF; codePoint++) | ||
{ | ||
char character = (char)codePoint; | ||
string str2 = $"a{character}b"; | ||
// in HybridGlobalization CompareInfo.Compare uses JS's localeCompare() | ||
if (cmpInfo.Compare(str1, str2, options) == 0) | ||
{ | ||
// do not test same codepoint with different cultures | ||
if (!ignoredCodepoints.Contains(codePoint)) | ||
{ | ||
ignoredCodepoints.Add(codePoint); | ||
yield return new object[] { hashCode1, str2, cmpInfo, options }; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsIcuGlobalization))] | ||
[MemberData(nameof(CheckHashingOfSkippedChars_TestData))] | ||
public void CheckHashingOfSkippedChars(char character, CompareInfo cmpInfo) | ||
// In non-hybrid we have hashing and Equal function from the same source - ICU4C, so this test is not necessary | ||
// Hybrid has Equal function from JS and hashing from managed invariant algorithm, they might start diverging at some point | ||
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsHybridGlobalizationOnBrowser))] | ||
[MemberData(nameof(CharsIgnoredByEqualFunction))] | ||
public void CheckHashingOfSkippedChars(int hashCode1, string str2, CompareInfo cmpInfo, CompareOptions options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to also have another test like
After we learned how bad the hash collisions are, we could comment out |
||
{ | ||
string str1 = $"a{character}b"; | ||
string str2 = "ab"; | ||
CompareOptions options = CompareOptions.None; | ||
var hashCode1 = cmpInfo.GetHashCode(str1, options); | ||
var hashCode2 = cmpInfo.GetHashCode(str2, options); | ||
bool areHashCodesEqual = hashCode1 == hashCode2; | ||
Assert.True(areHashCodesEqual); | ||
StringComparer stringComparer = new CustomComparer(cmpInfo, options); | ||
TryAddToCustomDictionary(stringComparer, str1, str2, areHashCodesEqual); | ||
} | ||
|
||
public static IEnumerable<object[]> GetHashCodeTestData => new[] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just silly idea: is it possible that codepoints are skipped only when are before or after another specific code point ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect only surrogates to work this way. This cast might not work for surrogates, though (they are 2 chars, not one). I need to check it, thanks