Skip to content

Commit

Permalink
Simplify some more CompareTo methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lilinus committed Apr 25, 2024
1 parent 6647c6f commit 5dab47d
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public int CompareTo(object? value)
{
return 1;
}
if (!(value is byte))
if (value is not byte other)
{
throw new ArgumentException(SR.Arg_MustBeByte);
}

return m_value - (((byte)value).m_value);
return CompareTo(other);
}

public int CompareTo(byte value)
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ public int CompareTo(object? value)
{
return 1;
}
if (!(value is char))
if (value is not char other)
{
throw new ArgumentException(SR.Arg_MustBeChar);
}

return m_value - ((char)value).m_value;
return CompareTo(other);
}

public int CompareTo(char value)
Expand Down
5 changes: 2 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Decimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,10 @@ public int CompareTo(object? value)
{
if (value == null)
return 1;
if (!(value is decimal))
if (value is not decimal other)
throw new ArgumentException(SR.Arg_MustBeDecimal);

decimal other = (decimal)value;
return DecCalc.VarDecCmp(in this, in other);
return CompareTo(other);
}

public int CompareTo(decimal value)
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Int16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public int CompareTo(object? value)
return 1;
}

if (value is short)
if (value is short other)
{
return m_value - ((short)value).m_value;
return CompareTo(other);
}

throw new ArgumentException(SR.Arg_MustBeInt16);
Expand Down
6 changes: 1 addition & 5 deletions src/libraries/System.Private.CoreLib/src/System/Int32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ public int CompareTo(object? value)
return 1;
}

// NOTE: Cannot use return (_value - value) as this causes a wrap
// around in cases where _value - value > MaxValue.
if (value is int i)
{
if (m_value < i) return -1;
if (m_value > i) return 1;
return 0;
return CompareTo(i);
}

throw new ArgumentException(SR.Arg_MustBeInt32);
Expand Down
6 changes: 1 addition & 5 deletions src/libraries/System.Private.CoreLib/src/System/Int64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,9 @@ public int CompareTo(object? value)
return 1;
}

// Need to use compare because subtraction will wrap
// to positive for very large neg numbers, etc.
if (value is long i)
{
if (m_value < i) return -1;
if (m_value > i) return 1;
return 0;
return CompareTo(i);
}

throw new ArgumentException(SR.Arg_MustBeInt64);
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/SByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public int CompareTo(object? obj)
{
return 1;
}
if (!(obj is sbyte))
if (obj is not sbyte other)
{
throw new ArgumentException(SR.Arg_MustBeSByte);
}
return m_value - ((sbyte)obj).m_value;
return CompareTo(other);
}

public int CompareTo(sbyte value)
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/UInt16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public int CompareTo(object? value)
{
return 1;
}
if (value is ushort)
if (value is ushort other)
{
return (int)m_value - (int)(((ushort)value).m_value);
return CompareTo(other);
}
throw new ArgumentException(SR.Arg_MustBeUInt16);
}
Expand Down
6 changes: 1 addition & 5 deletions src/libraries/System.Private.CoreLib/src/System/UInt32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@ public int CompareTo(object? value)
return 1;
}

// Need to use compare because subtraction will wrap
// to positive for very large neg numbers, etc.
if (value is uint i)
{
if (m_value < i) return -1;
if (m_value > i) return 1;
return 0;
return CompareTo(i);
}

throw new ArgumentException(SR.Arg_MustBeUInt32);
Expand Down
6 changes: 1 addition & 5 deletions src/libraries/System.Private.CoreLib/src/System/UInt64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@ public int CompareTo(object? value)
return 1;
}

// Need to use compare because subtraction will wrap
// to positive for very large neg numbers, etc.
if (value is ulong i)
{
if (m_value < i) return -1;
if (m_value > i) return 1;
return 0;
return CompareTo(i);
}

throw new ArgumentException(SR.Arg_MustBeUInt64);
Expand Down

0 comments on commit 5dab47d

Please sign in to comment.