diff --git a/src/libraries/System.Private.Uri/src/System/Uri.cs b/src/libraries/System.Private.Uri/src/System/Uri.cs index e8d936349be31f..88f9df2992f551 100644 --- a/src/libraries/System.Private.Uri/src/System/Uri.cs +++ b/src/libraries/System.Private.Uri/src/System/Uri.cs @@ -149,7 +149,6 @@ private class MoreInfo public string? Query; public string? Fragment; public string? AbsoluteUri; - public int Hash; public string? RemoteUrl; }; @@ -1505,39 +1504,26 @@ public static int FromHex(char digit) => (uint)(digit - 'a') <= 'f' - 'a' ? digit - 'a' + 10 : throw new ArgumentException(nameof(digit)); - // - // GetHashCode - // - // Overrides default function (in Object class) - // - // public override int GetHashCode() { if (IsNotAbsoluteUri) { - return CalculateCaseInsensitiveHashCode(OriginalString); - } - - // Consider moving hash code storage from m_Info.MoreInfo to m_Info - UriInfo info = EnsureUriInfo(); - if ((object?)info.MoreInfo == null) - { - info.MoreInfo = new MoreInfo(); + return OriginalString.GetHashCode(); } - int tempHash = info.MoreInfo.Hash; - if (tempHash == 0) + else { - string? chkString = info.MoreInfo.RemoteUrl; - if ((object?)chkString == null) - chkString = GetParts(UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped); - tempHash = CalculateCaseInsensitiveHashCode(chkString); - if (tempHash == 0) + MoreInfo moreInfo = EnsureUriInfo().MoreInfo ??= new MoreInfo(); + string remoteUrl = moreInfo.RemoteUrl ??= GetParts(UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped); + + if (IsUncOrDosPath) + { + return remoteUrl.GetHashCode(StringComparison.OrdinalIgnoreCase); + } + else { - tempHash = 0x1000000; //making it not zero still large enough to be mapped to zero by a hashtable + return remoteUrl.GetHashCode(); } - info.MoreInfo.Hash = tempHash; } - return tempHash; } // @@ -4943,11 +4929,6 @@ private static char[] Compress(char[] dest, ushort start, ref int destLength, Ur return dest; } - internal static int CalculateCaseInsensitiveHashCode(string text) - { - return text.ToLowerInvariant().GetHashCode(); - } - // // CombineUri // diff --git a/src/libraries/System.Runtime/tests/System/Uri.MethodsTests.cs b/src/libraries/System.Runtime/tests/System/Uri.MethodsTests.cs index 633baab3ae544b..64efed81d4d69c 100644 --- a/src/libraries/System.Runtime/tests/System/Uri.MethodsTests.cs +++ b/src/libraries/System.Runtime/tests/System/Uri.MethodsTests.cs @@ -389,15 +389,17 @@ public static IEnumerable Equals_TestData() public void Equals(Uri uri1, object obj, bool expected) { Uri uri2 = obj as Uri; + if (uri1 != null) { Assert.Equal(expected, uri1.Equals(obj)); - if (uri2 != null) + + if (uri2 != null && expected) { - bool onlyCaseDifference = string.Equals(uri1.OriginalString, uri2.OriginalString, StringComparison.OrdinalIgnoreCase); - Assert.Equal(expected || onlyCaseDifference, uri1.GetHashCode().Equals(uri2.GetHashCode())); + Assert.Equal(uri1.GetHashCode(), uri2.GetHashCode()); } } + if (!(obj is string)) { Assert.Equal(expected, uri1 == uri2);