diff --git a/Code/Light.GuardClauses.Tests/StringAssertions/IsEmptyOrWhiteSpaceTests.cs b/Code/Light.GuardClauses.Tests/StringAssertions/IsEmptyOrWhiteSpaceTests.cs new file mode 100644 index 0000000..2048883 --- /dev/null +++ b/Code/Light.GuardClauses.Tests/StringAssertions/IsEmptyOrWhiteSpaceTests.cs @@ -0,0 +1,120 @@ +using System; +using FluentAssertions; +using Xunit; + +namespace Light.GuardClauses.Tests.StringAssertions; + +public static class IsEmptyOrWhiteSpaceTests +{ + [Fact] + public static void EmptySpan() + { + var span = new Span([]); + span.IsEmptyOrWhiteSpace().Should().BeTrue(); + } + + [Fact] + public static void WhiteSpaceSpan() + { + var span = new Span(" \t\n".ToCharArray()); + span.IsEmptyOrWhiteSpace().Should().BeTrue(); + } + + [Fact] + public static void NonWhiteSpaceSpan() + { + var span = new Span("abc".ToCharArray()); + span.IsEmptyOrWhiteSpace().Should().BeFalse(); + } + + [Fact] + public static void MixedSpan() + { + var span = new Span(" a\t".ToCharArray()); + span.IsEmptyOrWhiteSpace().Should().BeFalse(); + } + + [Fact] + public static void EmptyReadOnlySpan() + { + var readOnlySpan = string.Empty.AsSpan(); + readOnlySpan.IsEmptyOrWhiteSpace().Should().BeTrue(); + } + + [Fact] + public static void WhiteSpaceReadOnlySpan() + { + var readOnlySpan = " \t\n".AsSpan(); + readOnlySpan.IsEmptyOrWhiteSpace().Should().BeTrue(); + } + + [Fact] + public static void NonWhiteSpaceReadOnlySpan() + { + var readOnlySpan = "abc".AsSpan(); + readOnlySpan.IsEmptyOrWhiteSpace().Should().BeFalse(); + } + + [Fact] + public static void MixedReadOnlySpan() + { + var readOnlySpan = " a\t".AsSpan(); + readOnlySpan.IsEmptyOrWhiteSpace().Should().BeFalse(); + } + + [Fact] + public static void EmptyMemory() + { + var memory = new Memory([]); + memory.IsEmptyOrWhiteSpace().Should().BeTrue(); + } + + [Fact] + public static void WhiteSpaceMemory() + { + var memory = new Memory(" \t\n".ToCharArray()); + memory.IsEmptyOrWhiteSpace().Should().BeTrue(); + } + + [Fact] + public static void NonWhiteSpaceMemory() + { + var memory = new Memory("abc".ToCharArray()); + memory.IsEmptyOrWhiteSpace().Should().BeFalse(); + } + + [Fact] + public static void MixedMemory() + { + var memory = new Memory(" a\t".ToCharArray()); + memory.IsEmptyOrWhiteSpace().Should().BeFalse(); + } + + [Fact] + public static void EmptyReadOnlyMemory() + { + var readOnlyMemory = new ReadOnlyMemory([]); + readOnlyMemory.IsEmptyOrWhiteSpace().Should().BeTrue(); + } + + [Fact] + public static void WhiteSpaceReadOnlyMemory() + { + var readOnlyMemory = new ReadOnlyMemory(" \t\n".ToCharArray()); + readOnlyMemory.IsEmptyOrWhiteSpace().Should().BeTrue(); + } + + [Fact] + public static void NonWhiteSpaceReadOnlyMemory() + { + var readOnlyMemory = new ReadOnlyMemory("abc".ToCharArray()); + readOnlyMemory.IsEmptyOrWhiteSpace().Should().BeFalse(); + } + + [Fact] + public static void MixedReadOnlyMemory() + { + var readOnlyMemory = new ReadOnlyMemory(" a\t".ToCharArray()); + readOnlyMemory.IsEmptyOrWhiteSpace().Should().BeFalse(); + } +} diff --git a/Code/Light.GuardClauses/Check.IsEmptyOrWhiteSpace.cs b/Code/Light.GuardClauses/Check.IsEmptyOrWhiteSpace.cs new file mode 100644 index 0000000..580c677 --- /dev/null +++ b/Code/Light.GuardClauses/Check.IsEmptyOrWhiteSpace.cs @@ -0,0 +1,51 @@ +using System; +using System.Runtime.CompilerServices; + +namespace Light.GuardClauses; + +public static partial class Check +{ + /// + /// Checks if the specified span is empty or contains only white space characters. + /// + /// The span to be checked. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsEmptyOrWhiteSpace(this Span span) => ((ReadOnlySpan) span).IsEmptyOrWhiteSpace(); + + /// + /// Checks if the specified span is empty or contains only white space characters. + /// + /// The span to be checked. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsEmptyOrWhiteSpace(this ReadOnlySpan span) + { + if (span.IsEmpty) + { + return true; + } + + foreach (var character in span) + { + if (!character.IsWhiteSpace()) + { + return false; + } + } + + return true; + } + + /// + /// Checks if the specified memory is empty or contains only white space characters. + /// + /// The memory to be checked. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsEmptyOrWhiteSpace(this Memory memory) => memory.Span.IsEmptyOrWhiteSpace(); + + /// + /// Checks if the specified memory is empty or contains only white space characters. + /// + /// The memory to be checked. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsEmptyOrWhiteSpace(this ReadOnlyMemory memory) => memory.Span.IsEmptyOrWhiteSpace(); +}