Skip to content

Commit 821e49f

Browse files
authored
Add StringBuilder.IsNullOrEmpty and StringBuilder.NotNullNorEmpty.
1 parent bdfa897 commit 821e49f

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Text;
2+
3+
using NUnit.Framework;
4+
5+
namespace CodeJam.Strings
6+
{
7+
[TestFixture]
8+
public class StringBuilderExtensionsTests
9+
{
10+
[Test]
11+
public void IsNullOrEmptyConstructor()
12+
{
13+
Assert.IsTrue(new StringBuilder().IsNullOrEmpty());
14+
}
15+
16+
[TestCase(null, ExpectedResult = true)]
17+
[TestCase("", ExpectedResult = true)]
18+
[TestCase("abc", ExpectedResult = false)]
19+
public bool IsNullOrEmpty(string source) => new StringBuilder(source).IsNullOrEmpty();
20+
21+
[Test]
22+
public void NotNullNorEmptyConstructor()
23+
{
24+
Assert.IsFalse(new StringBuilder().NotNullNorEmpty());
25+
}
26+
27+
[TestCase(null, ExpectedResult = false)]
28+
[TestCase("", ExpectedResult = false)]
29+
[TestCase("abc", ExpectedResult = true)]
30+
public bool NotNullNorEmpty(string source) => new StringBuilder(source).NotNullNorEmpty();
31+
}
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Text;
3+
4+
using JetBrains.Annotations;
5+
6+
namespace CodeJam.Strings
7+
{
8+
/// <summary>
9+
/// <see cref="StringBuilder"/> class extensions.
10+
/// </summary>
11+
[PublicAPI]
12+
public static class StringBuilderExtensions
13+
{
14+
/// <summary>
15+
/// Determines whether the StringBuilder is null or empty.
16+
/// </summary>
17+
/// <param name="value">The StringBuilder to test.</param>
18+
/// <returns>
19+
/// <c>true</c> if <paramref name="value"/> is null or empty; otherwise, <c>false</c>.
20+
/// </returns>
21+
[Pure]
22+
[ContractAnnotation("value:null => true")]
23+
public static bool IsNullOrEmpty([CanBeNull] this StringBuilder value) =>
24+
value == null || value.Length == 0;
25+
26+
/// <summary>
27+
/// Determines whether the StringBuilder is neither null nor empty.
28+
/// </summary>
29+
/// <param name="value">The StringBuilder to test.</param>
30+
/// <returns>
31+
/// <c>true</c> if <paramref name="value"/> is not null and not empty; otherwise, <c>false</c>.
32+
/// </returns>
33+
[Pure]
34+
[ContractAnnotation("value:null => false")]
35+
public static bool NotNullNorEmpty([CanBeNull] this StringBuilder value) =>
36+
!IsNullOrEmpty(value);
37+
}
38+
}

0 commit comments

Comments
 (0)