Skip to content

Commit dc005d1

Browse files
authored
Merge pull request #106 from feO2x/features/span-is-empty-or-white-space
IsEmptyOrWhiteSpace for Span<T> and Memory<T>
2 parents 0f52dd5 + e8db798 commit dc005d1

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace Light.GuardClauses.Tests.StringAssertions;
6+
7+
public static class IsEmptyOrWhiteSpaceTests
8+
{
9+
[Fact]
10+
public static void EmptySpan()
11+
{
12+
var span = new Span<char>([]);
13+
span.IsEmptyOrWhiteSpace().Should().BeTrue();
14+
}
15+
16+
[Fact]
17+
public static void WhiteSpaceSpan()
18+
{
19+
var span = new Span<char>(" \t\n".ToCharArray());
20+
span.IsEmptyOrWhiteSpace().Should().BeTrue();
21+
}
22+
23+
[Fact]
24+
public static void NonWhiteSpaceSpan()
25+
{
26+
var span = new Span<char>("abc".ToCharArray());
27+
span.IsEmptyOrWhiteSpace().Should().BeFalse();
28+
}
29+
30+
[Fact]
31+
public static void MixedSpan()
32+
{
33+
var span = new Span<char>(" a\t".ToCharArray());
34+
span.IsEmptyOrWhiteSpace().Should().BeFalse();
35+
}
36+
37+
[Fact]
38+
public static void EmptyReadOnlySpan()
39+
{
40+
var readOnlySpan = string.Empty.AsSpan();
41+
readOnlySpan.IsEmptyOrWhiteSpace().Should().BeTrue();
42+
}
43+
44+
[Fact]
45+
public static void WhiteSpaceReadOnlySpan()
46+
{
47+
var readOnlySpan = " \t\n".AsSpan();
48+
readOnlySpan.IsEmptyOrWhiteSpace().Should().BeTrue();
49+
}
50+
51+
[Fact]
52+
public static void NonWhiteSpaceReadOnlySpan()
53+
{
54+
var readOnlySpan = "abc".AsSpan();
55+
readOnlySpan.IsEmptyOrWhiteSpace().Should().BeFalse();
56+
}
57+
58+
[Fact]
59+
public static void MixedReadOnlySpan()
60+
{
61+
var readOnlySpan = " a\t".AsSpan();
62+
readOnlySpan.IsEmptyOrWhiteSpace().Should().BeFalse();
63+
}
64+
65+
[Fact]
66+
public static void EmptyMemory()
67+
{
68+
var memory = new Memory<char>([]);
69+
memory.IsEmptyOrWhiteSpace().Should().BeTrue();
70+
}
71+
72+
[Fact]
73+
public static void WhiteSpaceMemory()
74+
{
75+
var memory = new Memory<char>(" \t\n".ToCharArray());
76+
memory.IsEmptyOrWhiteSpace().Should().BeTrue();
77+
}
78+
79+
[Fact]
80+
public static void NonWhiteSpaceMemory()
81+
{
82+
var memory = new Memory<char>("abc".ToCharArray());
83+
memory.IsEmptyOrWhiteSpace().Should().BeFalse();
84+
}
85+
86+
[Fact]
87+
public static void MixedMemory()
88+
{
89+
var memory = new Memory<char>(" a\t".ToCharArray());
90+
memory.IsEmptyOrWhiteSpace().Should().BeFalse();
91+
}
92+
93+
[Fact]
94+
public static void EmptyReadOnlyMemory()
95+
{
96+
var readOnlyMemory = new ReadOnlyMemory<char>([]);
97+
readOnlyMemory.IsEmptyOrWhiteSpace().Should().BeTrue();
98+
}
99+
100+
[Fact]
101+
public static void WhiteSpaceReadOnlyMemory()
102+
{
103+
var readOnlyMemory = new ReadOnlyMemory<char>(" \t\n".ToCharArray());
104+
readOnlyMemory.IsEmptyOrWhiteSpace().Should().BeTrue();
105+
}
106+
107+
[Fact]
108+
public static void NonWhiteSpaceReadOnlyMemory()
109+
{
110+
var readOnlyMemory = new ReadOnlyMemory<char>("abc".ToCharArray());
111+
readOnlyMemory.IsEmptyOrWhiteSpace().Should().BeFalse();
112+
}
113+
114+
[Fact]
115+
public static void MixedReadOnlyMemory()
116+
{
117+
var readOnlyMemory = new ReadOnlyMemory<char>(" a\t".ToCharArray());
118+
readOnlyMemory.IsEmptyOrWhiteSpace().Should().BeFalse();
119+
}
120+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
4+
namespace Light.GuardClauses;
5+
6+
public static partial class Check
7+
{
8+
/// <summary>
9+
/// Checks if the specified span is empty or contains only white space characters.
10+
/// </summary>
11+
/// <param name="span">The span to be checked.</param>
12+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13+
public static bool IsEmptyOrWhiteSpace(this Span<char> span) => ((ReadOnlySpan<char>) span).IsEmptyOrWhiteSpace();
14+
15+
/// <summary>
16+
/// Checks if the specified span is empty or contains only white space characters.
17+
/// </summary>
18+
/// <param name="span">The span to be checked.</param>
19+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20+
public static bool IsEmptyOrWhiteSpace(this ReadOnlySpan<char> span)
21+
{
22+
if (span.IsEmpty)
23+
{
24+
return true;
25+
}
26+
27+
foreach (var character in span)
28+
{
29+
if (!character.IsWhiteSpace())
30+
{
31+
return false;
32+
}
33+
}
34+
35+
return true;
36+
}
37+
38+
/// <summary>
39+
/// Checks if the specified memory is empty or contains only white space characters.
40+
/// </summary>
41+
/// <param name="memory">The memory to be checked.</param>
42+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43+
public static bool IsEmptyOrWhiteSpace(this Memory<char> memory) => memory.Span.IsEmptyOrWhiteSpace();
44+
45+
/// <summary>
46+
/// Checks if the specified memory is empty or contains only white space characters.
47+
/// </summary>
48+
/// <param name="memory">The memory to be checked.</param>
49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50+
public static bool IsEmptyOrWhiteSpace(this ReadOnlyMemory<char> memory) => memory.Span.IsEmptyOrWhiteSpace();
51+
}

0 commit comments

Comments
 (0)