2021-02-14 11:32:08 +03:00
// Licensed to the .NET Foundation under one or more agreements.
2020-03-15 04:13:04 +03:00
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System ;
2020-05-16 00:14:14 +03:00
using System.Diagnostics.CodeAnalysis ;
2020-03-15 04:13:04 +03:00
using System.Runtime.CompilerServices ;
2020-11-22 20:15:26 +03:00
#pragma warning disable CS8777
2020-03-15 04:13:04 +03:00
namespace Microsoft.Toolkit.Diagnostics
{
/// <summary>
/// Helper methods to verify conditions when running code.
/// </summary>
public static partial class Guard
{
/// <summary>
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/> or empty.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor empty.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNullOrEmpty ( string? text , string name )
{
2020-06-04 16:03:26 +03:00
if ( string . IsNullOrEmpty ( text ) )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForIsNullOrEmpty ( text , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or empty.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or empty.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-05-16 00:14:14 +03:00
public static void IsNotNullOrEmpty ( [ NotNull ] string? text , string name )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
if ( ! string . IsNullOrEmpty ( text ) )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForIsNotNullOrEmpty ( text , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/> or whitespace.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-10-01 18:11:25 +03:00
public static void IsNullOrWhiteSpace ( string? text , string name )
{
if ( string . IsNullOrWhiteSpace ( text ) )
{
return ;
}
ThrowHelper . ThrowArgumentExceptionForIsNullOrWhiteSpace ( text , name ) ;
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must be <see langword="null"/> or whitespace.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use " + nameof(IsNullOrWhiteSpace))]
2020-03-15 04:13:04 +03:00
public static void IsNullOrWhitespace ( string? text , string name )
{
2020-06-04 16:03:26 +03:00
if ( string . IsNullOrWhiteSpace ( text ) )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
2020-10-01 18:11:25 +03:00
ThrowHelper . ThrowArgumentExceptionForIsNullOrWhiteSpace ( text , name ) ;
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrWhiteSpace ( [ NotNull ] string? text , string name )
{
if ( ! string . IsNullOrWhiteSpace ( text ) )
{
return ;
}
ThrowHelper . ThrowArgumentExceptionForIsNotNullOrWhiteSpace ( text , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-10-01 18:11:25 +03:00
[Obsolete("Use " + nameof(IsNotNullOrWhiteSpace))]
2020-05-16 00:14:14 +03:00
public static void IsNotNullOrWhitespace ( [ NotNull ] string? text , string name )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
if ( ! string . IsNullOrWhiteSpace ( text ) )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
2020-10-01 18:11:25 +03:00
ThrowHelper . ThrowArgumentExceptionForIsNotNullOrWhiteSpace ( text , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must be empty.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsEmpty ( string text , string name )
{
2020-06-04 16:03:26 +03:00
if ( text . Length = = 0 )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForIsEmpty ( text , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be empty.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotEmpty ( string text , string name )
{
2020-06-04 16:03:26 +03:00
if ( text . Length ! = 0 )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForIsNotEmpty ( text , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must be whitespace.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-10-01 18:11:25 +03:00
public static void IsWhiteSpace ( string text , string name )
{
if ( string . IsNullOrWhiteSpace ( text ) )
{
return ;
}
ThrowHelper . ThrowArgumentExceptionForIsWhiteSpace ( text , name ) ;
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must be whitespace.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is neither <see langword="null"/> nor whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use " + nameof(IsWhiteSpace))]
2020-03-15 04:13:04 +03:00
public static void IsWhitespace ( string text , string name )
{
2020-06-04 16:03:26 +03:00
if ( string . IsNullOrWhiteSpace ( text ) )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
2020-10-01 18:11:25 +03:00
ThrowHelper . ThrowArgumentExceptionForIsWhiteSpace ( text , name ) ;
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotWhiteSpace ( string text , string name )
{
if ( ! string . IsNullOrWhiteSpace ( text ) )
{
return ;
}
ThrowHelper . ThrowArgumentExceptionForIsNotWhiteSpace ( text , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must not be <see langword="null"/> or whitespace.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-10-01 18:11:25 +03:00
[Obsolete("Use " + nameof(IsNotWhiteSpace))]
2020-03-15 04:13:04 +03:00
public static void IsNotWhitespace ( string text , string name )
{
2020-06-04 16:03:26 +03:00
if ( ! string . IsNullOrWhiteSpace ( text ) )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
2020-10-01 18:11:25 +03:00
ThrowHelper . ThrowArgumentExceptionForIsNotWhiteSpace ( text , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size of a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is != <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeEqualTo ( string text , int size , string name )
{
2020-06-04 16:03:26 +03:00
if ( text . Length = = size )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForHasSizeEqualTo ( text , size , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size not equal to a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is == <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeNotEqualTo ( string text , int size , string name )
{
2020-06-04 16:03:26 +03:00
if ( text . Length ! = size )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForHasSizeNotEqualTo ( text , size , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size over a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is <= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-05-16 00:14:14 +03:00
public static void HasSizeGreaterThan ( string text , int size , string name )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
if ( text . Length > size )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForHasSizeGreaterThan ( text , size , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size of at least specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is < <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-05-16 00:14:14 +03:00
public static void HasSizeGreaterThanOrEqualTo ( string text , int size , string name )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
if ( text . Length > = size )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo ( text , size , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size of less than a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is >= <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThan ( string text , int size , string name )
{
2020-06-04 16:03:26 +03:00
if ( text . Length < size )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForHasSizeLessThan ( text , size , name ) ;
2020-03-15 04:13:04 +03:00
}
/// <summary>
/// Asserts that the input <see cref="string"/> instance must have a size of less than or equal to a specified value.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to check the size for.</param>
/// <param name="size">The target size to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="text"/> is > <paramref name="size"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThanOrEqualTo ( string text , int size , string name )
{
2020-06-04 16:03:26 +03:00
if ( text . Length < = size )
2020-03-15 04:13:04 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-03-15 04:13:04 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForHasSizeLessThanOrEqualTo ( text , size , name ) ;
2020-03-15 04:13:04 +03:00
}
2020-05-16 00:14:14 +03:00
/// <summary>
/// Asserts that the source <see cref="string"/> instance must have the same size of a destination <see cref="string"/> instance.
/// </summary>
/// <param name="source">The source <see cref="string"/> instance to check the size for.</param>
/// <param name="destination">The destination <see cref="string"/> instance to check the size for.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="source"/> is != the one of <paramref name="destination"/>.</exception>
/// <remarks>The <see cref="string"/> type is immutable, but the name of this API is kept for consistency with the other overloads.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeEqualTo ( string source , string destination , string name )
{
2020-06-04 16:03:26 +03:00
if ( source . Length = = destination . Length )
2020-05-16 00:14:14 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-05-16 00:14:14 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForHasSizeEqualTo ( source , destination , name ) ;
2020-05-16 00:14:14 +03:00
}
/// <summary>
/// Asserts that the source <see cref="string"/> instance must have a size of less than or equal to that of a destination <see cref="string"/> instance.
/// </summary>
/// <param name="source">The source <see cref="string"/> instance to check the size for.</param>
/// <param name="destination">The destination <see cref="string"/> instance to check the size for.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="source"/> is > the one of <paramref name="destination"/>.</exception>
/// <remarks>The <see cref="string"/> type is immutable, but the name of this API is kept for consistency with the other overloads.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThanOrEqualTo ( string source , string destination , string name )
{
2020-06-04 16:03:26 +03:00
if ( source . Length < = destination . Length )
2020-05-16 00:14:14 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-05-16 00:14:14 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentExceptionForHasSizeLessThanOrEqualTo ( source , destination , name ) ;
2020-05-16 00:14:14 +03:00
}
/// <summary>
/// Asserts that the input index is valid for a given <see cref="string"/> instance.
/// </summary>
/// <param name="index">The input index to be used to access <paramref name="text"/>.</param>
/// <param name="text">The input <see cref="string"/> instance to use to validate <paramref name="index"/>.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not valid to access <paramref name="text"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsInRangeFor ( int index , string text , string name )
{
2020-06-04 16:03:26 +03:00
if ( ( uint ) index < ( uint ) text . Length )
2020-05-16 00:14:14 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-05-16 00:14:14 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentOutOfRangeExceptionForIsInRangeFor ( index , text , name ) ;
2020-05-16 00:14:14 +03:00
}
/// <summary>
/// Asserts that the input index is not valid for a given <see cref="string"/> instance.
/// </summary>
/// <param name="index">The input index to be used to access <paramref name="text"/>.</param>
/// <param name="text">The input <see cref="string"/> instance to use to validate <paramref name="index"/>.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is valid to access <paramref name="text"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotInRangeFor ( int index , string text , string name )
{
2020-06-04 16:03:26 +03:00
if ( ( uint ) index > = ( uint ) text . Length )
2020-05-16 00:14:14 +03:00
{
2020-06-04 16:03:26 +03:00
return ;
2020-05-16 00:14:14 +03:00
}
2020-06-04 16:03:26 +03:00
ThrowHelper . ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor ( index , text , name ) ;
2020-05-16 00:14:14 +03:00
}
2020-03-15 04:13:04 +03:00
}
2021-02-14 11:32:05 +03:00
}