提交 4b402939 编写于 作者: P pgavlin

Rationalize ByteSequenceComparer.

ByteSequenceComparer has been updated as follows:
- The implementation of IEqualityComparer<IEnumerable<byte>> has been removed
- The implementations of IEqualityComparer<{byte[], ImmutableArray<byte>}> are now explicit
- The various overloads of EqualsValue and GetHashCodeValue have been renamed to Equals and GetHasCode, respectively.
***NO_CI***
 (changeset 1395450)
上级 54a66c37
......@@ -966,7 +966,7 @@ protected IVTConclusion PerformIVTCheck(ImmutableArray<byte> key, AssemblyIdenti
bool q1 = otherIdentity.IsStrongName;
bool q2 = !key.IsDefaultOrEmpty;
bool q3 = !this.PublicKey.IsDefaultOrEmpty;
bool q4 = (q2 & q3) && ByteSequenceComparer.Instance.Equals(key, this.PublicKey);
bool q4 = (q2 & q3) && ByteSequenceComparer.Equals(key, this.PublicKey);
// Cases 2, 3, 7 and 8:
if (q2 && !q4)
......
......@@ -77,7 +77,7 @@ public void PubKeyFromKeyFileAttribute()
var other = CreateCompilationWithMscorlib(s, options: TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider));
other.VerifyDiagnostics();
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, other.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, other.Assembly.Identity.PublicKey));
CompileAndVerify(other, symbolValidator: (ModuleSymbol m) =>
{
......@@ -121,7 +121,7 @@ public void PubKeyFromKeyFileAttribute_AssemblyKeyFileResolver()
comp.VerifyDiagnostics();
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, comp.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, comp.Assembly.Identity.PublicKey));
}
[Fact]
......@@ -149,7 +149,7 @@ public void PubKeyFromKeyFileAttribute_AssemblyKeyFileResolver_RelativeToCurrent
TestOptions.ReleaseDll.WithStrongNameProvider(GetProviderWithPath(PathUtilities.CombineAbsoluteAndRelativePaths(keyFileDir, @"TempSubDir\"))));
Assert.Empty(comp.GetDiagnostics());
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, comp.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, comp.Assembly.Identity.PublicKey));
}
[Fact]
......@@ -160,7 +160,7 @@ public void PubKeyFromKeyContainerAttribute()
var other = CreateCompilationWithMscorlib(s, options: TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider));
other.VerifyDiagnostics();
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, other.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, other.Assembly.Identity.PublicKey));
CompileAndVerify(other, symbolValidator: (ModuleSymbol m) =>
{
......@@ -186,7 +186,7 @@ public void PubKeyFromKeyFileOptions()
var other = CreateCompilationWithMscorlib(s, options: TestOptions.ReleaseDll.WithCryptoKeyFile(KeyPairFile).WithStrongNameProvider(DefaultProvider));
other.VerifyDiagnostics();
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, other.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, other.Assembly.Identity.PublicKey));
}
[Fact]
......@@ -215,7 +215,7 @@ public void PubKeyFromKeyFileOptions_ReferenceResolver()
TestOptions.ReleaseDll.WithCryptoKeyFile(keyFileName).WithStrongNameProvider(GetProviderWithPath(keyFileDir)));
Assert.Empty(comp.GetDiagnostics());
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, comp.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, comp.Assembly.Identity.PublicKey));
}
[Fact]
......@@ -225,7 +225,7 @@ public void PubKeyFromKeyFileOptionsJustPublicKey()
var other = CreateCompilationWithMscorlib(s,
options: TestOptions.ReleaseDll.WithCryptoKeyFile(PublicKeyFile).WithDelaySign(true).WithStrongNameProvider(DefaultProvider));
other.VerifyDiagnostics();
Assert.True(ByteSequenceComparer.Instance.Equals(TestResources.SymbolsTests.General.snPublicKey.AsImmutableOrNull(), other.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(TestResources.SymbolsTests.General.snPublicKey.AsImmutableOrNull(), other.Assembly.Identity.PublicKey));
}
[Fact]
......@@ -257,7 +257,7 @@ public void PubKeyFromKeyFileOptionsJustPublicKey_ReferenceResolver()
new[] { MscorlibRef },
TestOptions.ReleaseDll.WithCryptoKeyFile(publicKeyFileName).WithDelaySign(true).WithStrongNameProvider(GetProviderWithPath(publicKeyFileDir)));
Assert.Empty(comp.GetDiagnostics());
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, comp.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, comp.Assembly.Identity.PublicKey));
}
[Fact]
......@@ -313,7 +313,7 @@ public void KeyFileAttributeOptionConflict()
options: TestOptions.ReleaseDll.WithCryptoKeyFile(KeyPairFile).WithStrongNameProvider(DefaultProvider));
other.VerifyDiagnostics(Diagnostic(ErrorCode.WRN_CmdOptionConflictsSource).WithArguments("CryptoKeyFile", "System.Reflection.AssemblyKeyFileAttribute"));
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, other.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, other.Assembly.Identity.PublicKey));
}
[Fact]
......@@ -325,7 +325,7 @@ public void KeyContainerAttributeOptionConflict()
options: TestOptions.ReleaseDll.WithCryptoKeyContainer("RoslynTestContainer").WithStrongNameProvider(DefaultProvider));
other.VerifyDiagnostics(Diagnostic(ErrorCode.WRN_CmdOptionConflictsSource).WithArguments("CryptoKeyContainer", "System.Reflection.AssemblyKeyNameAttribute"));
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, other.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, other.Assembly.Identity.PublicKey));
}
[Fact]
......@@ -527,7 +527,7 @@ public class A
assemblyName: "John",
options: TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider));
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, requestor.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, requestor.Assembly.Identity.PublicKey));
Assert.Empty(requestor.GetDiagnostics());
}
......@@ -554,7 +554,7 @@ public class A
assemblyName: "John",
options: TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider));
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, requestor.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, requestor.Assembly.Identity.PublicKey));
requestor.VerifyDiagnostics(Diagnostic(ErrorCode.ERR_FriendRefSigningMismatch, null, new object[] { "Paul, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" }));
}
......@@ -582,7 +582,7 @@ public class A
assemblyName: "John",
options: TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider));
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, requestor.Assembly.Identity.PublicKey));
Assert.True(ByteSequenceComparer.Equals(PublicKey, requestor.Assembly.Identity.PublicKey));
requestor.VerifyDiagnostics(Diagnostic(ErrorCode.ERR_FriendRefNotEqualToThis, null, new object[] { "Paul, Version=0.0.0.0, Culture=neutral, PublicKeyToken=ce65828c82a341f2" }));
}
......
......@@ -295,7 +295,7 @@ internal static ImmutableArray<byte> GetPublicKey(byte[] keyFileContents)
try
{
var lastSeen = lastSeenKeyPair;
if (lastSeen != null && ByteSequenceComparer.ValueEquals(lastSeen.Item1, keyFileContents))
if (lastSeen != null && ByteSequenceComparer.Equals(lastSeen.Item1, keyFileContents))
{
return lastSeen.Item2;
}
......
......@@ -2,13 +2,14 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Collections
{
internal sealed class ByteSequenceComparer : IEqualityComparer<byte[]>, IEqualityComparer<IEnumerable<byte>>, IEqualityComparer<ImmutableArray<byte>>
internal sealed class ByteSequenceComparer : IEqualityComparer<byte[]>, IEqualityComparer<ImmutableArray<byte>>
{
internal static readonly ByteSequenceComparer Instance = new ByteSequenceComparer();
......@@ -16,41 +17,30 @@ private ByteSequenceComparer()
{
}
public bool Equals(IEnumerable<byte> x, IEnumerable<byte> y)
internal static bool Equals(ImmutableArray<byte> x, ImmutableArray<byte> y)
{
if (ReferenceEquals(x, y))
if (x == y)
{
return true;
}
if (x == null || y == null)
if (x.IsDefault || y.IsDefault || x.Length != y.Length)
{
return false;
}
var ax = x as byte[];
var ay = y as byte[];
return (ax != null && ay != null)
? Equals(ax, ay)
: x.SequenceEqual(y);
}
public bool Equals(ImmutableArray<byte> x, ImmutableArray<byte> y)
{
return ValueEquals(x, y);
}
internal static bool ValueEquals(ImmutableArray<byte> x, ImmutableArray<byte> y)
{
return x.SequenceEqual(y);
}
for (var i = 0; i < x.Length; i++)
{
if (x[i] != y[i])
{
return false;
}
}
public bool Equals(byte[] x, byte[] y)
{
return ValueEquals(x, y);
return true;
}
internal static bool ValueEquals(byte[] x, byte[] y)
internal static bool Equals(byte[] x, byte[] y)
{
if (ReferenceEquals(x, y))
{
......@@ -73,54 +63,38 @@ internal static bool ValueEquals(byte[] x, byte[] y)
return true;
}
public int GetHashCode(IEnumerable<byte> x)
{
if (x == null)
{
return 0;
}
// Both hash computations below use the FNV-1a algorithm (http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function).
var ax = x as byte[];
if (ax != null)
{
return GetHashCode(ax);
}
var result = 7;
foreach (var b in x)
{
result = (result << 5) ^ b;
}
internal static int GetHashCode(byte[] x)
{
Debug.Assert(x != null);
return Hash.GetFNVHashCode(x);
}
return result;
internal static int GetHashCode(ImmutableArray<byte> x)
{
Debug.Assert(!x.IsDefault);
return Hash.GetFNVHashCode(x);
}
public int GetHashCode(byte[] x)
bool IEqualityComparer<byte[]>.Equals(byte[] x, byte[] y)
{
return GetValueHashCode(x);
return Equals(x, y);
}
/// <summary>
/// Compute the FNV-1a hash code for a sequence of bytes.
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="x">The sequence of bytes</param>
/// <returns>The FNV-1a hash code for the input sequence</returns>
/// <exception cref="System.NullReferenceException">The input sequence was null (IsDefault)</exception>
public int GetHashCode(ImmutableArray<byte> x)
int IEqualityComparer<byte[]>.GetHashCode(byte[] x)
{
return Hash.GetFNVHashCode(x);
return GetHashCode(x);
}
// This uses FNV1a as a string hash
internal static int GetValueHashCode(byte[] x)
bool IEqualityComparer<ImmutableArray<byte>>.Equals(ImmutableArray<byte> x, ImmutableArray<byte> y)
{
if (x == null)
{
return 0;
}
return Equals(x, y);
}
return Hash.GetFNVHashCode(x);
int IEqualityComparer<ImmutableArray<byte>>.GetHashCode(ImmutableArray<byte> x)
{
return GetHashCode(x);
}
}
}
\ No newline at end of file
}
......@@ -2492,7 +2492,7 @@ public bool Equals(IAssemblyReference x, IAssemblyReference y)
return
x.Version.Equals(y.Version) &&
ByteSequenceComparer.Instance.Equals(x.PublicKeyToken, y.PublicKeyToken) &&
ByteSequenceComparer.Equals(x.PublicKeyToken, y.PublicKeyToken) &&
x.Name == y.Name &&
x.Culture == y.Culture;
}
......@@ -2500,7 +2500,7 @@ public bool Equals(IAssemblyReference x, IAssemblyReference y)
public int GetHashCode(IAssemblyReference reference)
{
return Hash.Combine(reference.Version,
Hash.Combine(ByteSequenceComparer.Instance.GetHashCode(reference.PublicKeyToken),
Hash.Combine(ByteSequenceComparer.GetHashCode(reference.PublicKeyToken),
Hash.Combine(reference.Name.GetHashCode(),
Hash.Combine(reference.Culture, 0))));
}
......
......@@ -565,7 +565,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
' key is NullOrEmpty, Me.PublicKey is not.
result = IVTConclusion.NoRelationshipClaimed
End If
ElseIf ByteSequenceComparer.Instance.Equals(key, Me.PublicKey) Then
ElseIf ByteSequenceComparer.Equals(key, Me.PublicKey) Then
result = If(otherIdentity.IsStrongName, IVTConclusion.Match, IVTConclusion.OneSignedOneNot)
Else
result = IVTConclusion.PublicKeyDoesntMatch
......@@ -646,4 +646,4 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
#End Region
End Class
End Namespace
\ No newline at end of file
End Namespace
......@@ -50,7 +50,7 @@ Public Class InternalsVisibleToAndStrongNameTests
TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider))
other.VerifyDiagnostics()
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, other.Assembly.Identity.PublicKey))
Assert.True(ByteSequenceComparer.Equals(PublicKey, other.Assembly.Identity.PublicKey))
End Sub
<Fact>
......@@ -79,7 +79,7 @@ Public Class InternalsVisibleToAndStrongNameTests
TestOptions.ReleaseDll.WithStrongNameProvider(GetProviderWithPath(keyFileDir)))
comp.VerifyDiagnostics()
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, comp.Assembly.Identity.PublicKey))
Assert.True(ByteSequenceComparer.Equals(PublicKey, comp.Assembly.Identity.PublicKey))
End Sub
<Fact>
......@@ -108,7 +108,7 @@ Public Class InternalsVisibleToAndStrongNameTests
options:=TestOptions.ReleaseDll.WithStrongNameProvider(GetProviderWithPath(PathUtilities.CombineAbsoluteAndRelativePaths(keyFileDir, "TempSubDir\"))))
comp.VerifyDiagnostics()
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, comp.Assembly.Identity.PublicKey))
Assert.True(ByteSequenceComparer.Equals(PublicKey, comp.Assembly.Identity.PublicKey))
End Sub
<Fact>
......@@ -126,7 +126,7 @@ End Class
</compilation>, TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider))
other.VerifyDiagnostics()
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, other.Assembly.Identity.PublicKey))
Assert.True(ByteSequenceComparer.Equals(PublicKey, other.Assembly.Identity.PublicKey))
End Sub
<Fact>
......@@ -144,7 +144,7 @@ End Class
options:=TestOptions.ReleaseDll.WithCryptoKeyFile(KeyPairFile).WithStrongNameProvider(DefaultProvider))
other.VerifyDiagnostics()
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, other.Assembly.Identity.PublicKey))
Assert.True(ByteSequenceComparer.Equals(PublicKey, other.Assembly.Identity.PublicKey))
End Sub
<Fact>
......@@ -181,7 +181,7 @@ End Class
options:=TestOptions.ReleaseDll.WithCryptoKeyFile(keyFileName).WithStrongNameProvider(GetProviderWithPath(keyFileDir)))
comp.VerifyDiagnostics()
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, comp.Assembly.Identity.PublicKey))
Assert.True(ByteSequenceComparer.Equals(PublicKey, comp.Assembly.Identity.PublicKey))
End Sub
<Fact>
......@@ -199,7 +199,7 @@ End Class
Dim other = CreateCompilationWithMscorlib(s, options:=TestOptions.ReleaseDll.WithCryptoKeyFile(PublicKeyFile).WithDelaySign(True).WithStrongNameProvider(DefaultProvider))
Assert.Empty(other.GetDiagnostics())
Assert.True(ByteSequenceComparer.Instance.Equals(TestResources.SymbolsTests.General.snPublicKey.AsImmutableOrNull(), other.Assembly.Identity.PublicKey))
Assert.True(ByteSequenceComparer.Equals(TestResources.SymbolsTests.General.snPublicKey.AsImmutableOrNull(), other.Assembly.Identity.PublicKey))
End Sub
<Fact>
......@@ -239,7 +239,7 @@ End Class
options:=TestOptions.ReleaseDll.WithCryptoKeyFile(publicKeyFileName).WithDelaySign(True).WithStrongNameProvider(GetProviderWithPath(publicKeyFileDir)))
comp.VerifyDiagnostics()
Assert.True(ByteSequenceComparer.Instance.Equals(PublicKey, comp.Assembly.Identity.PublicKey))
Assert.True(ByteSequenceComparer.Equals(PublicKey, comp.Assembly.Identity.PublicKey))
End Sub
<Fact>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册