// Licensed to the .NET Foundation under one or more agreements.
// 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;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
#pragma warning disable CS8777
namespace CommunityToolkit.Diagnostics;
///
/// Helper methods to verify conditions when running code.
///
public static partial class Guard
{
///
/// Asserts that the input instance must be or empty.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is neither nor empty.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNullOrEmpty(string? text, [CallerArgumentExpression("text")] string name = "")
{
if (string.IsNullOrEmpty(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNullOrEmpty(text, name);
}
///
/// Asserts that the input instance must not be or empty.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is .
/// Thrown if is empty.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrEmpty([NotNull] string? text, [CallerArgumentExpression("text")] string name = "")
{
if (!string.IsNullOrEmpty(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrEmpty(text, name);
}
///
/// Asserts that the input instance must be or whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is neither nor whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNullOrWhiteSpace(string? text, [CallerArgumentExpression("text")] string name = "")
{
if (string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNullOrWhiteSpace(text, name);
}
///
/// Asserts that the input instance must be or whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is neither nor whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use " + nameof(IsNullOrWhiteSpace))]
public static void IsNullOrWhitespace(string? text, [CallerArgumentExpression("text")] string name = "")
{
if (string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNullOrWhiteSpace(text, name);
}
///
/// Asserts that the input instance must not be or whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is .
/// Thrown if is whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrWhiteSpace([NotNull] string? text, [CallerArgumentExpression("text")] string name = "")
{
if (!string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrWhiteSpace(text, name);
}
///
/// Asserts that the input instance must not be or whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is or whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use " + nameof(IsNotNullOrWhiteSpace))]
public static void IsNotNullOrWhitespace([NotNull] string? text, [CallerArgumentExpression("text")] string name = "")
{
if (!string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrWhiteSpace(text, name);
}
///
/// Asserts that the input instance must be empty.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is empty.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsEmpty(string text, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length == 0)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsEmpty(text, name);
}
///
/// Asserts that the input instance must not be empty.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is empty.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotEmpty(string text, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length != 0)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(text, name);
}
///
/// Asserts that the input instance must be whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is neither nor whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsWhiteSpace(string text, [CallerArgumentExpression("text")] string name = "")
{
if (string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsWhiteSpace(text, name);
}
///
/// Asserts that the input instance must be whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is neither nor whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use " + nameof(IsWhiteSpace))]
public static void IsWhitespace(string text, [CallerArgumentExpression("text")] string name = "")
{
if (string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsWhiteSpace(text, name);
}
///
/// Asserts that the input instance must not be or whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is or whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotWhiteSpace(string text, [CallerArgumentExpression("text")] string name = "")
{
if (!string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotWhiteSpace(text, name);
}
///
/// Asserts that the input instance must not be or whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is or whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use " + nameof(IsNotWhiteSpace))]
public static void IsNotWhitespace(string text, [CallerArgumentExpression("text")] string name = "")
{
if (!string.IsNullOrWhiteSpace(text))
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotWhiteSpace(text, name);
}
///
/// Asserts that the input instance must have a size of a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is != .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeEqualTo(string text, int size, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length == size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(text, size, name);
}
///
/// Asserts that the input instance must have a size not equal to a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is == .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeNotEqualTo(string text, int size, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length != size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(text, size, name);
}
///
/// Asserts that the input instance must have a size over a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is <= .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeGreaterThan(string text, int size, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length > size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(text, size, name);
}
///
/// Asserts that the input instance must have a size of at least specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is < .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeGreaterThanOrEqualTo(string text, int size, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length >= size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(text, size, name);
}
///
/// Asserts that the input instance must have a size of less than a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is >= .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThan(string text, int size, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length < size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(text, size, name);
}
///
/// Asserts that the input instance must have a size of less than or equal to a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is > .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThanOrEqualTo(string text, int size, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length <= size)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(text, size, name);
}
///
/// Asserts that the source instance must have the same size of a destination instance.
///
/// The source instance to check the size for.
/// The destination instance to check the size for.
/// The name of the input parameter being tested.
/// Thrown if the size of is != the one of .
/// The type is immutable, but the name of this API is kept for consistency with the other overloads.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeEqualTo(string text, string destination, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length == destination.Length)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(text, destination, name);
}
///
/// Asserts that the source instance must have a size of less than or equal to that of a destination instance.
///
/// The source instance to check the size for.
/// The destination instance to check the size for.
/// The name of the input parameter being tested.
/// Thrown if the size of is > the one of .
/// The type is immutable, but the name of this API is kept for consistency with the other overloads.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThanOrEqualTo(string text, string destination, [CallerArgumentExpression("text")] string name = "")
{
if (text.Length <= destination.Length)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(text, destination, name);
}
///
/// Asserts that the input index is valid for a given instance.
///
/// The input index to be used to access .
/// The input instance to use to validate .
/// The name of the input parameter being tested.
/// Thrown if is not valid to access .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsInRangeFor(int index, string text, [CallerArgumentExpression("index")] string name = "")
{
if ((uint)index < (uint)text.Length)
{
return;
}
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, text, name);
}
///
/// Asserts that the input index is not valid for a given instance.
///
/// The input index to be used to access .
/// The input instance to use to validate .
/// The name of the input parameter being tested.
/// Thrown if is valid to access .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotInRangeFor(int index, string text, [CallerArgumentExpression("index")] string name = "")
{
if ((uint)index >= (uint)text.Length)
{
return;
}
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, text, name);
}
}