diff --git a/BizHawk.Common/Ranges.cs b/BizHawk.Common/Ranges.cs index 68064fdb53..3f20469d02 100644 --- a/BizHawk.Common/Ranges.cs +++ b/BizHawk.Common/Ranges.cs @@ -30,29 +30,30 @@ namespace BizHawk.Common /// public MutableRange(T start, T endInclusive) => Overwrite(start, endInclusive); - /// (from setter) > + /// (from setter) > public T Start { get => r.Start; set => r.Start = r.EndInclusive.CompareTo(value) < 0 - ? throw new ArgumentException("attempted to set start > end", nameof(value)) + ? throw new ArgumentOutOfRangeException(nameof(value), value, "attempted to set start > end") : value; } - /// (from setter) < + /// (from setter) < public T EndInclusive { get => r.EndInclusive; set => r.EndInclusive = value.CompareTo(r.Start) < 0 - ? throw new ArgumentException("attempted to set end < start", nameof(value)) + ? throw new ArgumentOutOfRangeException(nameof(value), value, "attempted to set end < start") : value; } - /// < , or is / and either bound is + /// is / and either bound is + /// < public void Overwrite(T start, T endInclusive) { var range = new RangeStruct { Start = start, EndInclusive = endInclusive }; - if (range.EndInclusive.CompareTo(range.Start) < 0) throw new ArgumentException("range end < start", nameof(range)); + if (range.EndInclusive.CompareTo(range.Start) < 0) throw new ArgumentOutOfRangeException(nameof(range), range, "range end < start"); if (range is RangeStruct fr && (float.IsNaN(fr.Start) || float.IsNaN(fr.EndInclusive)) || range is RangeStruct dr && (double.IsNaN(dr.Start) || double.IsNaN(dr.EndInclusive))) { @@ -69,10 +70,10 @@ namespace BizHawk.Common /// public static class RangeExtensions { - private const string EXCL_RANGE_ARITH_EXC_TEXT = "exclusive range end is min value of integral type"; - private const ulong MIN_LONG_NEGATION_AS_ULONG = 9223372036854775808UL; + private static readonly ArithmeticException ExclusiveRangeMinValExc = new ArithmeticException("exclusive range end is min value of integral type"); + /// if it's contained in , or else whichever bound of is closest to public static T ConstrainWithin(this T value, Range range) where T : unmanaged, IComparable => value.CompareTo(range.Start) < 0 ? range.Start @@ -176,42 +177,42 @@ namespace BizHawk.Common /// public static MutableRange MutableRangeToExclusive(this byte start, byte endExclusive) => endExclusive == byte.MinValue - ? throw new ArgumentException(EXCL_RANGE_ARITH_EXC_TEXT, nameof(endExclusive)) + ? throw ExclusiveRangeMinValExc : new MutableRange(start, (byte) (endExclusive - 1U)); /// public static MutableRange MutableRangeToExclusive(this int start, int endExclusive) => endExclusive == int.MinValue - ? throw new ArgumentException(EXCL_RANGE_ARITH_EXC_TEXT, nameof(endExclusive)) + ? throw ExclusiveRangeMinValExc : new MutableRange(start, endExclusive - 1); /// public static MutableRange MutableRangeToExclusive(this long start, long endExclusive) => endExclusive == long.MinValue - ? throw new ArgumentException(EXCL_RANGE_ARITH_EXC_TEXT, nameof(endExclusive)) + ? throw ExclusiveRangeMinValExc : new MutableRange(start, endExclusive - 1L); /// public static MutableRange MutableRangeToExclusive(this sbyte start, sbyte endExclusive) => endExclusive == sbyte.MinValue - ? throw new ArgumentException(EXCL_RANGE_ARITH_EXC_TEXT, nameof(endExclusive)) + ? throw ExclusiveRangeMinValExc : new MutableRange(start, (sbyte) (endExclusive - 1)); /// public static MutableRange MutableRangeToExclusive(this short start, short endExclusive) => endExclusive == short.MinValue - ? throw new ArgumentException(EXCL_RANGE_ARITH_EXC_TEXT, nameof(endExclusive)) + ? throw ExclusiveRangeMinValExc : new MutableRange(start, (short) (endExclusive - 1)); /// public static MutableRange MutableRangeToExclusive(this uint start, uint endExclusive) => endExclusive == uint.MinValue - ? throw new ArgumentException(EXCL_RANGE_ARITH_EXC_TEXT, nameof(endExclusive)) + ? throw ExclusiveRangeMinValExc : new MutableRange(start, endExclusive - 1U); /// public static MutableRange MutableRangeToExclusive(this ulong start, ulong endExclusive) => endExclusive == ulong.MinValue - ? throw new ArgumentException(EXCL_RANGE_ARITH_EXC_TEXT, nameof(endExclusive)) + ? throw ExclusiveRangeMinValExc : new MutableRange(start, endExclusive - 1UL); /// public static MutableRange MutableRangeToExclusive(this ushort start, ushort endExclusive) => endExclusive == ushort.MinValue - ? throw new ArgumentException(EXCL_RANGE_ARITH_EXC_TEXT, nameof(endExclusive)) + ? throw ExclusiveRangeMinValExc : new MutableRange(start, (ushort) (endExclusive - 1U)); /// @@ -220,7 +221,7 @@ namespace BizHawk.Common /// public static Range RangeToExclusive(this byte start, byte endExclusive) => MutableRangeToExclusive(start, endExclusive); - /// (empty ranges where = are not permitted) + /// (empty ranges where = are not permitted) /// is min value of integral type (therefore ) public static Range RangeToExclusive(this int start, int endExclusive) => MutableRangeToExclusive(start, endExclusive);