// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace SixLabors { /// /// Provides methods to protect against invalid parameters. /// [DebuggerStepThrough] internal static partial class Guard { /// /// Ensures that the value is not null. /// /// The target object, which cannot be null. /// The name of the parameter that is to be checked. /// The type of the value. /// is null. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void NotNull([NotNull]TValue? value, [CallerArgumentExpression("value")] string? parameterName = null) where TValue : class => ArgumentNullException.ThrowIfNull(value, parameterName); /// /// Ensures that the target value is not null, empty, or whitespace. /// /// The target string, which should be checked against being null or empty. /// Name of the parameter. /// is null. /// is empty or contains only blanks. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void NotNullOrWhiteSpace([NotNull]string? value, string parameterName) { if (!string.IsNullOrWhiteSpace(value)) { return; } ThrowHelper.ThrowArgumentExceptionForNotNullOrWhitespace(value, parameterName); } /// /// Ensures that the specified value is less than a maximum value. /// /// The target value, which should be validated. /// The maximum value. /// The name of the parameter that is to be checked. /// The type of the value. /// /// is greater than the maximum value. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThan(TValue value, TValue max, string parameterName) where TValue : IComparable { if (value.CompareTo(max) < 0) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThan(value, max, parameterName); } /// /// Verifies that the specified value is less than or equal to a maximum value /// and throws an exception if it is not. /// /// The target value, which should be validated. /// The maximum value. /// The name of the parameter that is to be checked. /// The type of the value. /// /// is greater than the maximum value. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeLessThanOrEqualTo(TValue value, TValue max, string parameterName) where TValue : IComparable { if (value.CompareTo(max) <= 0) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(value, max, parameterName); } /// /// Verifies that the specified value is greater than a minimum value /// and throws an exception if it is not. /// /// The target value, which should be validated. /// The minimum value. /// The name of the parameter that is to be checked. /// The type of the value. /// /// is less than the minimum value. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThan(TValue value, TValue min, string parameterName) where TValue : IComparable { if (value.CompareTo(min) > 0) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(value, min, parameterName); } /// /// Verifies that the specified value is greater than or equal to a minimum value /// and throws an exception if it is not. /// /// The target value, which should be validated. /// The minimum value. /// The name of the parameter that is to be checked. /// The type of the value. /// /// is less than the minimum value. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeGreaterThanOrEqualTo(TValue value, TValue min, string parameterName) where TValue : IComparable { if (value.CompareTo(min) >= 0) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(value, min, parameterName); } /// /// Verifies that the specified value is greater than or equal to a minimum value and less than /// or equal to a maximum value and throws an exception if it is not. /// /// The target value, which should be validated. /// The minimum value. /// The maximum value. /// The name of the parameter that is to be checked. /// The type of the value. /// /// is less than the minimum value of greater than the maximum value. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeBetweenOrEqualTo(TValue value, TValue min, TValue max, string parameterName) where TValue : IComparable { if (value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(value, min, max, parameterName); } /// /// Verifies, that the method parameter with specified target value is true /// and throws an exception if it is found to be so. /// /// The target value, which cannot be false. /// The name of the parameter that is to be checked. /// The error message, if any to add to the exception. /// /// is false. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsTrue(bool target, string parameterName, string message) { if (target) { return; } ThrowHelper.ThrowArgumentException(message, parameterName); } /// /// Verifies, that the method parameter with specified target value is false /// and throws an exception if it is found to be so. /// /// The target value, which cannot be true. /// The name of the parameter that is to be checked. /// The error message, if any to add to the exception. /// /// is true. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsFalse(bool target, string parameterName, string message) { if (!target) { return; } ThrowHelper.ThrowArgumentException(message, parameterName); } /// /// Verifies, that the `source` span has the length of 'minLength', or longer. /// /// The element type of the spans. /// The source span. /// The minimum length. /// The name of the parameter that is to be checked. /// /// has less than items. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeSizedAtLeast(ReadOnlySpan source, int minLength, string parameterName) { if (source.Length >= minLength) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(minLength, parameterName); } /// /// Verifies, that the `source` span has the length of 'minLength', or longer. /// /// The element type of the spans. /// The target span. /// The minimum length. /// The name of the parameter that is to be checked. /// /// has less than items. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MustBeSizedAtLeast(Span source, int minLength, string parameterName) { if (source.Length >= minLength) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(minLength, parameterName); } /// /// Verifies that the 'destination' span is not shorter than 'source'. /// /// The source element type. /// The destination element type. /// The source span. /// The destination span. /// The name of the argument for 'destination'. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void DestinationShouldNotBeTooShort( ReadOnlySpan source, Span destination, string destinationParamName) { if (destination.Length >= source.Length) { return; } ThrowHelper.ThrowArgumentException("Destination span is too short!", destinationParamName); } /// /// Verifies that the 'destination' span is not shorter than 'source'. /// /// The source element type. /// The destination element type. /// The source span. /// The destination span. /// The name of the argument for 'destination'. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void DestinationShouldNotBeTooShort( Span source, Span destination, string destinationParamName) { if (destination.Length >= source.Length) { return; } ThrowHelper.ThrowArgumentException("Destination span is too short!", destinationParamName); } } }