From eb40549006149c0f4be019d0131ec82ffab66b28 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 18 Aug 2019 17:41:28 +0200 Subject: [PATCH] [SL.Core] use shared guards from standards --- src/SixLabors.Core/Helpers/DebugGuard.cs | 308 ---------------------- src/SixLabors.Core/Helpers/Guard.cs | 309 ----------------------- src/SixLabors.Core/SixLabors.Core.csproj | 10 +- standards | 2 +- 4 files changed, 10 insertions(+), 619 deletions(-) delete mode 100644 src/SixLabors.Core/Helpers/DebugGuard.cs delete mode 100644 src/SixLabors.Core/Helpers/Guard.cs diff --git a/src/SixLabors.Core/Helpers/DebugGuard.cs b/src/SixLabors.Core/Helpers/DebugGuard.cs deleted file mode 100644 index 31d7d23f2..000000000 --- a/src/SixLabors.Core/Helpers/DebugGuard.cs +++ /dev/null @@ -1,308 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.Diagnostics; -using System.Runtime.CompilerServices; - -namespace SixLabors -{ - /// - /// Provides methods to protect against invalid parameters for a DEBUG build. - /// - [DebuggerStepThrough] - internal static class DebugGuard - { - /// - /// 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. - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void NotNull(TValue value, string parameterName) - where TValue : class - { - if (value is null) - { - ThrowArgumentNullException(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. - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void NotNullOrWhiteSpace(string value, string parameterName) - { - if (value is null) - { - ThrowArgumentNullException(parameterName); - } - - if (string.IsNullOrWhiteSpace(value)) - { - ThrowArgumentException("Must not be empty or whitespace.", 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. - /// - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void MustBeLessThan(TValue value, TValue max, string parameterName) - where TValue : IComparable - { - if (value.CompareTo(max) >= 0) - { - ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than {max}."); - } - } - - /// - /// 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. - /// - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void MustBeLessThanOrEqualTo(TValue value, TValue max, string parameterName) - where TValue : IComparable - { - if (value.CompareTo(max) > 0) - { - ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than or equal to {max}."); - } - } - - /// - /// 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. - /// - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void MustBeGreaterThan(TValue value, TValue min, string parameterName) - where TValue : IComparable - { - if (value.CompareTo(min) <= 0) - { - ThrowArgumentOutOfRangeException( - parameterName, - $"Value {value} must be greater than {min}."); - } - } - - /// - /// 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. - /// - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void MustBeGreaterThanOrEqualTo(TValue value, TValue min, string parameterName) - where TValue : IComparable - { - if (value.CompareTo(min) < 0) - { - ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min}."); - } - } - - /// - /// 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. - /// - [Conditional("DEBUG")] - [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) - { - ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}."); - } - } - - /// - /// 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. - /// - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void IsTrue(bool target, string parameterName, string message) - { - if (!target) - { - 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. - /// - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void IsFalse(bool target, string parameterName, string message) - { - if (target) - { - 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. - /// - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void MustBeSizedAtLeast(ReadOnlySpan source, int minLength, string parameterName) - { - if (source.Length < minLength) - { - ThrowArgumentException($"Span-s must be at least of length {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. - /// - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void MustBeSizedAtLeast(Span source, int minLength, string parameterName) - { - if (source.Length < minLength) - { - ThrowArgumentException($"The size must be at least {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'. - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void DestinationShouldNotBeTooShort( - ReadOnlySpan source, - Span destination, - string destinationParamName) - { - if (destination.Length < source.Length) - { - 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'. - [Conditional("DEBUG")] - [DebuggerStepThrough] - public static void DestinationShouldNotBeTooShort( - Span source, - Span destination, - string destinationParamName) - { - if (destination.Length < source.Length) - { - ThrowArgumentException($"Destination span is too short!", destinationParamName); - } - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentException(string message, string parameterName) - { - throw new ArgumentException(message, parameterName); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentOutOfRangeException(string parameterName, string message) - { - throw new ArgumentOutOfRangeException(parameterName, message); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentNullException(string parameterName) - { - throw new ArgumentNullException(parameterName); - } - } -} \ No newline at end of file diff --git a/src/SixLabors.Core/Helpers/Guard.cs b/src/SixLabors.Core/Helpers/Guard.cs deleted file mode 100644 index 09c8675d3..000000000 --- a/src/SixLabors.Core/Helpers/Guard.cs +++ /dev/null @@ -1,309 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Runtime.CompilerServices; - -namespace SixLabors -{ - /// - /// Provides methods to protect against invalid parameters. - /// - [DebuggerStepThrough] - internal static 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)] - [DebuggerStepThrough] - public static void NotNull(TValue value, string parameterName) - where TValue : class - { - if (value is null) - { - ThrowArgumentNullException(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)] - [DebuggerStepThrough] - public static void NotNullOrWhiteSpace(string value, string parameterName) - { - if (value is null) - { - ThrowArgumentNullException(parameterName); - } - - if (string.IsNullOrWhiteSpace(value)) - { - ThrowArgumentException("Must not be empty or whitespace.", 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)] - [DebuggerStepThrough] - public static void MustBeLessThan(TValue value, TValue max, string parameterName) - where TValue : IComparable - { - if (value.CompareTo(max) >= 0) - { - ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than {max}."); - } - } - - /// - /// 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)] - [DebuggerStepThrough] - public static void MustBeLessThanOrEqualTo(TValue value, TValue max, string parameterName) - where TValue : IComparable - { - if (value.CompareTo(max) > 0) - { - ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than or equal to {max}."); - } - } - - /// - /// 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)] - [DebuggerStepThrough] - public static void MustBeGreaterThan(TValue value, TValue min, string parameterName) - where TValue : IComparable - { - if (value.CompareTo(min) <= 0) - { - ThrowArgumentOutOfRangeException( - parameterName, - $"Value {value} must be greater than {min}."); - } - } - - /// - /// 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)] - [DebuggerStepThrough] - public static void MustBeGreaterThanOrEqualTo(TValue value, TValue min, string parameterName) - where TValue : IComparable - { - if (value.CompareTo(min) < 0) - { - ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min}."); - } - } - - /// - /// 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)] - [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) - { - ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}."); - } - } - - /// - /// 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)] - [DebuggerStepThrough] - public static void IsTrue(bool target, string parameterName, string message) - { - if (!target) - { - 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)] - [DebuggerStepThrough] - public static void IsFalse(bool target, string parameterName, string message) - { - if (target) - { - 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)] - [DebuggerStepThrough] - public static void MustBeSizedAtLeast(ReadOnlySpan source, int minLength, string parameterName) - { - if (source.Length < minLength) - { - ThrowArgumentException($"Span-s must be at least of length {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)] - [DebuggerStepThrough] - public static void MustBeSizedAtLeast(Span source, int minLength, string parameterName) - { - if (source.Length < minLength) - { - ThrowArgumentException($"The size must be at least {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)] - [DebuggerStepThrough] - public static void DestinationShouldNotBeTooShort( - ReadOnlySpan source, - Span destination, - string destinationParamName) - { - if (destination.Length < source.Length) - { - 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)] - [DebuggerStepThrough] - public static void DestinationShouldNotBeTooShort( - Span source, - Span destination, - string destinationParamName) - { - if (destination.Length < source.Length) - { - ThrowArgumentException($"Destination span is too short!", destinationParamName); - } - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentException(string message, string parameterName) - { - throw new ArgumentException(message, parameterName); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentOutOfRangeException(string parameterName, string message) - { - throw new ArgumentOutOfRangeException(parameterName, message); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentNullException(string parameterName) - { - throw new ArgumentNullException(parameterName); - } - } -} diff --git a/src/SixLabors.Core/SixLabors.Core.csproj b/src/SixLabors.Core/SixLabors.Core.csproj index 0c6c537f4..61b8fa4b6 100644 --- a/src/SixLabors.Core/SixLabors.Core.csproj +++ b/src/SixLabors.Core/SixLabors.Core.csproj @@ -34,7 +34,11 @@ $(DefineConstants);SUPPORTS_HASHCODE - + + + + + All @@ -55,4 +59,8 @@ + + + + diff --git a/standards b/standards index 4e6ebb923..948a02984 160000 --- a/standards +++ b/standards @@ -1 +1 @@ -Subproject commit 4e6ebb923f20a5280ada67aad1fa7478f45c44cc +Subproject commit 948a02984c6b62dca1c40b02b289f6d1c13d79b1