diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index ef928c2800..cd53e3d69a 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -21,12 +21,13 @@ namespace SixLabors.ImageSharp /// The name of the parameter that is to be checked. /// is null [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void NotNull(T value, string parameterName) where T : class { if (value is null) { - throw new ArgumentNullException(parameterName); + ThrowArgumentNullException(parameterName); } } @@ -38,16 +39,17 @@ namespace SixLabors.ImageSharp /// is null. /// is empty or contains only blanks. [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void NotNullOrWhiteSpace(string value, string parameterName) { if (value is null) { - throw new ArgumentNullException(parameterName); + ThrowArgumentNullException(parameterName); } if (string.IsNullOrWhiteSpace(value)) { - throw new ArgumentException("Must not be empty or whitespace.", parameterName); + ThrowArgumentException("Must not be empty or whitespace.", parameterName); } } @@ -60,16 +62,17 @@ namespace SixLabors.ImageSharp /// is null. /// is empty. [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void NotNullOrEmpty(ICollection value, string parameterName) { if (value is null) { - throw new ArgumentNullException(parameterName); + ThrowArgumentNullException(parameterName); } if (value.Count == 0) { - throw new ArgumentException("Must not be empty.", parameterName); + ThrowArgumentException("Must not be empty.", parameterName); } } @@ -84,12 +87,13 @@ namespace SixLabors.ImageSharp /// is greater than the maximum value. /// [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void MustBeLessThan(TValue value, TValue max, string parameterName) where TValue : IComparable { if (value.CompareTo(max) >= 0) { - throw new ArgumentOutOfRangeException(parameterName, $"Value {value} must be less than {max}."); + ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than {max}."); } } @@ -105,12 +109,13 @@ namespace SixLabors.ImageSharp /// is greater than the maximum value. /// [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void MustBeLessThanOrEqualTo(TValue value, TValue max, string parameterName) where TValue : IComparable { if (value.CompareTo(max) > 0) { - throw new ArgumentOutOfRangeException(parameterName, $"Value {value} must be less than or equal to {max}."); + ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than or equal to {max}."); } } @@ -126,12 +131,13 @@ namespace SixLabors.ImageSharp /// is less than the minimum value. /// [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void MustBeGreaterThan(TValue value, TValue min, string parameterName) where TValue : IComparable { if (value.CompareTo(min) <= 0) { - throw new ArgumentOutOfRangeException( + ThrowArgumentOutOfRangeException( parameterName, $"Value {value} must be greater than {min}."); } @@ -149,12 +155,13 @@ namespace SixLabors.ImageSharp /// is less than the minimum value. /// [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void MustBeGreaterThanOrEqualTo(TValue value, TValue min, string parameterName) where TValue : IComparable { if (value.CompareTo(min) < 0) { - throw new ArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min}."); + ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min}."); } } @@ -171,12 +178,13 @@ namespace SixLabors.ImageSharp /// is less than the minimum value of greater than the maximum value. /// [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void MustBeBetweenOrEqualTo(TValue value, TValue min, TValue max, string parameterName) where TValue : IComparable { if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) { - throw new ArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}."); + ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}."); } } @@ -191,11 +199,12 @@ namespace SixLabors.ImageSharp /// is false /// [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void IsTrue(bool target, string parameterName, string message) { if (!target) { - throw new ArgumentException(message, parameterName); + ThrowArgumentException(message, parameterName); } } @@ -210,11 +219,12 @@ namespace SixLabors.ImageSharp /// is true /// [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void IsFalse(bool target, string parameterName, string message) { if (target) { - throw new ArgumentException(message, parameterName); + ThrowArgumentException(message, parameterName); } } @@ -229,11 +239,12 @@ namespace SixLabors.ImageSharp /// has less than items /// [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void MustBeSizedAtLeast(ReadOnlySpan source, int minLength, string parameterName) { if (source.Length < minLength) { - throw new ArgumentException($"Span-s must be at least of length {minLength}!", parameterName); + ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName); } } @@ -246,6 +257,7 @@ namespace SixLabors.ImageSharp /// The destination span /// The name of the argument for 'destination' [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void DestinationShouldNotBeTooShort( ReadOnlySpan source, Span destination, @@ -253,7 +265,7 @@ namespace SixLabors.ImageSharp { if (destination.Length < source.Length) { - throw new ArgumentException($"Destination span is too short!", destinationParamName); + ThrowArgumentException($"Destination span is too short!", destinationParamName); } } @@ -268,46 +280,31 @@ namespace SixLabors.ImageSharp /// has less than items /// [MethodImpl(InliningOptions.ShortMethod)] + [DebuggerStepThrough] public static void MustBeSizedAtLeast(Span source, int minLength, string parameterName) { if (source.Length < minLength) { - throw new ArgumentException($"Span-s must be at least of length {minLength}!", parameterName); + ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName); } } - /// - /// Verifies that the given 'source' and 'dest' spans are at least of 'minLength' size. - /// Throwing an if the condition is not met. - /// - /// The source element type - /// The destination element type - /// The source span - /// The source parameter name - /// The destination span - /// The destination parameter name - /// The minimum length - public static void SpansMustBeSizedAtLeast( - ReadOnlySpan source, - string sourceParamName, - Span dest, - string destParamName, - int minLength) + [MethodImpl(InliningOptions.ColdPath)] + private static void ThrowArgumentException(string message, string parameterName) { - MustBeSizedAtLeast(source, minLength, sourceParamName); - MustBeSizedAtLeast(dest, minLength, destParamName); + throw new ArgumentException(message, parameterName); } [MethodImpl(InliningOptions.ColdPath)] - private static void ThrowArgumentException(string message, string parameterName) + private static void ThrowArgumentOutOfRangeException(string parameterName, string message) { - throw new ArgumentException(message, parameterName); + throw new ArgumentOutOfRangeException(parameterName, message); } [MethodImpl(InliningOptions.ColdPath)] - private static void ThrowArgumentNullException(string message) + private static void ThrowArgumentNullException(string parameterName) { - throw new ArgumentException(message); + throw new ArgumentNullException(parameterName); } } }