Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 17 additions & 281 deletions src/libraries/System.Private.CoreLib/src/System/Guid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,8 +1104,6 @@ private static bool EqualsCore(in Guid left, in Guid right)
&& Unsafe.Add(ref rA, 3) == Unsafe.Add(ref rB, 3);
}

private static int GetResult(uint me, uint them) => me < them ? -1 : 1;

public int CompareTo(object? value)
{
if (value == null)
Expand All @@ -1123,60 +1121,30 @@ public int CompareTo(Guid value)
{
if (value._a != _a)
{
return GetResult((uint)_a, (uint)value._a);
}

if (value._b != _b)
{
return GetResult((uint)_b, (uint)value._b);
}

if (value._c != _c)
{
return GetResult((uint)_c, (uint)value._c);
}

if (value._d != _d)
{
return GetResult(_d, value._d);
}

if (value._e != _e)
{
return GetResult(_e, value._e);
}

if (value._f != _f)
{
return GetResult(_f, value._f);
}

if (value._g != _g)
{
return GetResult(_g, value._g);
}

if (value._h != _h)
{
return GetResult(_h, value._h);
return (uint)_a < (uint)value._a ? -1 : 1;
}

if (value._i != _i)
int r = ((ushort)_b).CompareTo((ushort)value._b);
if (r != 0)
{
return GetResult(_i, value._i);
return r;
}

if (value._j != _j)
r = ((ushort)_c).CompareTo((ushort)value._c);
if (r != 0)
{
return GetResult(_j, value._j);
return r;
}

if (value._k != _k)
ulong x = Unsafe.BitCast<Guid, (ulong, ulong)>(this).Item2;
ulong y = Unsafe.BitCast<Guid, (ulong, ulong)>(value).Item2;
if (BitConverter.IsLittleEndian)
{
return GetResult(_k, value._k);
x = BinaryPrimitives.ReverseEndianness(x);
y = BinaryPrimitives.ReverseEndianness(y);
}
Comment on lines +1139 to 1145
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still introducing new unsafe code and isn't something that we want to do without proof of significant benefit. The only way I see something like that happening is if we approved some general BitConverter.GuidToInt128Bits which handles such fixups centrally.


Comment thread
pentp marked this conversation as resolved.
return 0;
return x.CompareTo(y);
}

public static bool operator ==(Guid a, Guid b) => EqualsCore(a, b);
Expand Down Expand Up @@ -1586,248 +1554,16 @@ private static (Vector128<byte>, Vector128<byte>, Vector128<byte>) FormatGuidVec
//

/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_LessThan(TSelf, TOther)" />
public static bool operator <(Guid left, Guid right)
{
if (left._a != right._a)
{
return (uint)left._a < (uint)right._a;
}

if (left._b != right._b)
{
return (uint)left._b < (uint)right._b;
}

if (left._c != right._c)
{
return (uint)left._c < (uint)right._c;
}

if (left._d != right._d)
{
return left._d < right._d;
}

if (left._e != right._e)
{
return left._e < right._e;
}

if (left._f != right._f)
{
return left._f < right._f;
}

if (left._g != right._g)
{
return left._g < right._g;
}

if (left._h != right._h)
{
return left._h < right._h;
}

if (left._i != right._i)
{
return left._i < right._i;
}

if (left._j != right._j)
{
return left._j < right._j;
}

if (left._k != right._k)
{
return left._k < right._k;
}

return false;
}
public static bool operator <(Guid left, Guid right) => left.CompareTo(right) < 0;

/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_LessThanOrEqual(TSelf, TOther)" />
public static bool operator <=(Guid left, Guid right)
{
if (left._a != right._a)
{
return (uint)left._a < (uint)right._a;
}

if (left._b != right._b)
{
return (uint)left._b < (uint)right._b;
}

if (left._c != right._c)
{
return (uint)left._c < (uint)right._c;
}

if (left._d != right._d)
{
return left._d < right._d;
}

if (left._e != right._e)
{
return left._e < right._e;
}

if (left._f != right._f)
{
return left._f < right._f;
}

if (left._g != right._g)
{
return left._g < right._g;
}

if (left._h != right._h)
{
return left._h < right._h;
}

if (left._i != right._i)
{
return left._i < right._i;
}

if (left._j != right._j)
{
return left._j < right._j;
}

if (left._k != right._k)
{
return left._k < right._k;
}

return true;
}
public static bool operator <=(Guid left, Guid right) => left.CompareTo(right) <= 0;

/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_GreaterThan(TSelf, TOther)" />
public static bool operator >(Guid left, Guid right)
{
if (left._a != right._a)
{
return (uint)left._a > (uint)right._a;
}

if (left._b != right._b)
{
return (uint)left._b > (uint)right._b;
}

if (left._c != right._c)
{
return (uint)left._c > (uint)right._c;
}

if (left._d != right._d)
{
return left._d > right._d;
}

if (left._e != right._e)
{
return left._e > right._e;
}

if (left._f != right._f)
{
return left._f > right._f;
}

if (left._g != right._g)
{
return left._g > right._g;
}

if (left._h != right._h)
{
return left._h > right._h;
}

if (left._i != right._i)
{
return left._i > right._i;
}

if (left._j != right._j)
{
return left._j > right._j;
}

if (left._k != right._k)
{
return left._k > right._k;
}

return false;
}
public static bool operator >(Guid left, Guid right) => left.CompareTo(right) > 0;

/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_GreaterThanOrEqual(TSelf, TOther)" />
public static bool operator >=(Guid left, Guid right)
{
if (left._a != right._a)
{
return (uint)left._a > (uint)right._a;
}

if (left._b != right._b)
{
return (uint)left._b > (uint)right._b;
}

if (left._c != right._c)
{
return (uint)left._c > (uint)right._c;
}

if (left._d != right._d)
{
return left._d > right._d;
}

if (left._e != right._e)
{
return left._e > right._e;
}

if (left._f != right._f)
{
return left._f > right._f;
}

if (left._g != right._g)
{
return left._g > right._g;
}

if (left._h != right._h)
{
return left._h > right._h;
}

if (left._i != right._i)
{
return left._i > right._i;
}

if (left._j != right._j)
{
return left._j > right._j;
}

if (left._k != right._k)
{
return left._k > right._k;
}

return true;
}
public static bool operator >=(Guid left, Guid right) => left.CompareTo(right) >= 0;

//
// IParsable
Expand Down
Loading