PropertyRef
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System.Text.Json.Serialization.Metadata
{
internal readonly struct PropertyRef : IEquatable<PropertyRef>
{
public readonly ulong Key;
public readonly JsonPropertyInfo Info;
public readonly byte[] Utf8PropertyName;
public PropertyRef(ulong key, JsonPropertyInfo info, byte[] utf8PropertyName)
{
Key = key;
Info = info;
Utf8PropertyName = utf8PropertyName;
}
public bool Equals(PropertyRef other)
{
return Equals(other.Utf8PropertyName, other.Key);
}
public override bool Equals(object obj)
{
if (obj is PropertyRef) {
PropertyRef other = (PropertyRef)obj;
return Equals(other);
}
return false;
}
public override int GetHashCode()
{
return Key.GetHashCode();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(ReadOnlySpan<byte> propertyName, ulong key)
{
if (key == Key) {
if (propertyName.Length > 7)
return propertyName.SequenceEqual(Utf8PropertyName);
return true;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong GetKey(ReadOnlySpan<byte> name)
{
int length = name.Length;
ulong num = (ulong)(byte)length << 56;
ulong num2;
switch (length) {
case 0:
num2 = 0;
break;
case 1:
num2 = name[0];
break;
case 2:
num2 = MemoryMarshal.Read<ushort>(name);
break;
case 3:
num2 = (MemoryMarshal.Read<ushort>(name) | ((ulong)name[2] << 16));
break;
case 4:
num2 = MemoryMarshal.Read<uint>(name);
break;
case 5:
num2 = (MemoryMarshal.Read<uint>(name) | ((ulong)name[4] << 32));
break;
case 6:
num2 = (MemoryMarshal.Read<uint>(name) | ((ulong)MemoryMarshal.Read<ushort>(name.Slice(4, 2)) << 32));
break;
case 7:
num2 = (MemoryMarshal.Read<uint>(name) | ((ulong)MemoryMarshal.Read<ushort>(name.Slice(4, 2)) << 32) | ((ulong)name[6] << 48));
break;
default:
num2 = (MemoryMarshal.Read<ulong>(name) & 72057594037927935);
break;
}
return num | num2;
}
}
}