Skip to content

Commit

Permalink
Add equality members for MetadataRange.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed Sep 17, 2022
1 parent 3bb6b6a commit 751141f
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/AsmResolver.PE/DotNet/Metadata/Tables/MetadataRange.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -7,7 +8,7 @@ namespace AsmResolver.PE.DotNet.Metadata.Tables
/// <summary>
/// Represents a range of metadata tokens, indicated by a starting and ending row identifier within a metadata table.
/// </summary>
public readonly struct MetadataRange : IEnumerable<MetadataToken>
public readonly struct MetadataRange : IEnumerable<MetadataToken>, IEquatable<MetadataRange>
{
/// <summary>
/// Represents the empty metadata range.
Expand Down Expand Up @@ -67,6 +68,11 @@ public uint EndRid
get;
}

/// <summary>
/// Gets the number of metadata rows this range spans.
/// </summary>
public int Count => (int) (EndRid - StartRid);

/// <summary>
/// Gets a value indicating whether the range is empty or not.
/// </summary>
Expand All @@ -86,11 +92,6 @@ public uint EndRid
[MemberNotNullWhen(true, nameof(RedirectionTable))]
public bool IsRedirected => RedirectionTable is not null;

/// <summary>
/// Gets the number of metadata rows this range spans.
/// </summary>
public int Count => (int) (EndRid - StartRid);

/// <summary>
/// Obtains an enumerator that enumerates all metadata tokens within the range.
/// </summary>
Expand All @@ -110,6 +111,40 @@ public override string ToString()
return $"[0x{start.ToString()}..0x{end.ToString()})";
}

/// <inheritdoc />
public bool Equals(MetadataRange other)
{
if (IsEmpty && other.IsEmpty)
return true;

return Table == other.Table
&& StartRid == other.StartRid
&& EndRid == other.EndRid
&& Equals(RedirectionTable, other.RedirectionTable);
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
return obj is MetadataRange other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
if (IsEmpty)
return 0;

unchecked
{
int hashCode = (int) Table;
hashCode = (hashCode * 397) ^ (int) StartRid;
hashCode = (hashCode * 397) ^ (int) EndRid;
hashCode = (hashCode * 397) ^ (RedirectionTable is not null ? RedirectionTable.GetHashCode() : 0);
return hashCode;
}
}

/// <summary>
/// Represents an enumerator that enumerates all metadata tokens within a token range.
/// </summary>
Expand Down

0 comments on commit 751141f

Please sign in to comment.