mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 19:28:48 +00:00
Add IsEmpty/IsWhiteSpace and IsNullOrEmpty/WhiteSpace for string
This commit is contained in:
parent
041181cc5a
commit
799bc77ec8
@ -56,6 +56,10 @@
|
||||
- X10D: Added `Span<T>.Split(Span<T>)`
|
||||
- X10D: Added `string.CountSubstring(char)`
|
||||
- X10D: Added `string.CountSubstring(string[, StringComparison])`
|
||||
- X10D: Added `string.IsEmpty()`
|
||||
- X10D: Added `string.IsWhiteSpace()`
|
||||
- X10D: Added `string.IsNullOrEmpty()`
|
||||
- X10D: Added `string.IsNullOrWhiteSpace()`
|
||||
- X10D: Added `TimeSpan.TryParse(ReadOnlySpan<char>, out TimeSpan)`
|
||||
- X10D: Added `Quaternion.Multiply(Vector3)` - this functions as an equivalent to Unity's `Quaternion * Vector3` operator
|
||||
- X10D: Added `Vector2.Deconstruct()`
|
||||
|
@ -278,6 +278,32 @@ public class StringTests
|
||||
Assert.IsFalse("World".IsEmoji());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEmpty_ShouldReturnTrue_GivenEmptyString()
|
||||
{
|
||||
Assert.IsTrue("".IsEmpty());
|
||||
Assert.IsTrue(string.Empty.IsEmpty());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEmpty_ShouldReturnFalse_GivenWhiteSpaceString()
|
||||
{
|
||||
Assert.IsFalse(" ".IsEmpty());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEmpty_ShouldReturnFalse_GivenNonEmptyString()
|
||||
{
|
||||
Assert.IsFalse("Hello World".IsEmpty());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEmpty_ShouldThrowArgumentNullException_GivenNullString()
|
||||
{
|
||||
string? value = null;
|
||||
Assert.ThrowsException<ArgumentNullException>(() => value!.IsEmpty());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsLower_ShouldReturnTrue_GivenLowercaseString()
|
||||
{
|
||||
@ -303,6 +329,58 @@ public class StringTests
|
||||
Assert.ThrowsException<ArgumentNullException>(() => value!.IsLower());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsNullOrEmpty_ShouldReturnTrue_GivenEmptyString()
|
||||
{
|
||||
Assert.IsTrue("".IsNullOrEmpty());
|
||||
Assert.IsTrue(string.Empty.IsNullOrEmpty());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsNullOrEmpty_ShouldReturnFalse_GivenWhiteSpaceString()
|
||||
{
|
||||
Assert.IsFalse(" ".IsNullOrEmpty());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsNullOrEmpty_ShouldReturnFalse_GivenNonEmptyString()
|
||||
{
|
||||
Assert.IsFalse("Hello World".IsNullOrEmpty());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsNullOrEmpty_ShouldReturnTrue_GivenNullString()
|
||||
{
|
||||
string? value = null;
|
||||
Assert.IsTrue(value.IsNullOrEmpty());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsNullOrWhiteSpace_ShouldReturnTrue_GivenEmptyString()
|
||||
{
|
||||
Assert.IsTrue("".IsNullOrWhiteSpace());
|
||||
Assert.IsTrue(string.Empty.IsNullOrWhiteSpace());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsNullOrWhiteSpace_ShouldReturnTrue_GivenWhiteSpaceString()
|
||||
{
|
||||
Assert.IsTrue(" ".IsNullOrWhiteSpace());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsNullOrWhiteSpace_ShouldReturnFalse_GivenNonEmptyString()
|
||||
{
|
||||
Assert.IsFalse("Hello World".IsNullOrWhiteSpace());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsNullOrWhiteSpace_ShouldReturnTrue_GivenNullString()
|
||||
{
|
||||
string? value = null;
|
||||
Assert.IsTrue(value.IsNullOrWhiteSpace());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsPalindrome_ShouldBeCorrect_GivenString()
|
||||
{
|
||||
@ -358,6 +436,32 @@ public class StringTests
|
||||
Assert.ThrowsException<ArgumentNullException>(() => value!.IsUpper());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsWhiteSpace_ShouldReturnTrue_GivenEmptyString()
|
||||
{
|
||||
Assert.IsTrue("".IsWhiteSpace());
|
||||
Assert.IsTrue(string.Empty.IsWhiteSpace());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsWhiteSpace_ShouldReturnTrue_GivenWhiteSpaceString()
|
||||
{
|
||||
Assert.IsTrue(" ".IsWhiteSpace());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsWhiteSpace_ShouldReturnFalse_GivenNonEmptyString()
|
||||
{
|
||||
Assert.IsFalse("Hello World".IsWhiteSpace());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsWhiteSpace_ShouldThrowArgumentNullException_GivenNullString()
|
||||
{
|
||||
string? value = null;
|
||||
Assert.ThrowsException<ArgumentNullException>(() => value!.IsWhiteSpace());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Randomize_ShouldReorder_GivenString()
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
@ -409,6 +409,34 @@ public static class StringExtensions
|
||||
return EmojiRegex.Value.IsMatch(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current string represents an empty string.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is empty; otherwise, <see langword="false" />.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
|
||||
[Pure]
|
||||
#if NETSTANDARD2_1
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
#else
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
#endif
|
||||
public static bool IsEmpty(this string value)
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
#else
|
||||
if (value is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
#endif
|
||||
|
||||
return value.Length == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if all alpha characters in this string are considered lowercase.
|
||||
/// </summary>
|
||||
@ -465,6 +493,46 @@ public static class StringExtensions
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current string is <see langword="null" /> (<see langword="Nothing" /> in Visual
|
||||
/// Basic), or represents an empty string.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is <see langword="null" /> or empty; otherwise,
|
||||
/// <see langword="false" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
#if NETSTANDARD2_1
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
#else
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
#endif
|
||||
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? value)
|
||||
{
|
||||
return string.IsNullOrEmpty(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current string is <see langword="null" /> (<see langword="Nothing" /> in Visual
|
||||
/// Basic), represents an empty string, or consists of only whitespace characters.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is <see langword="null" />, empty, or consists of only
|
||||
/// whitespace; otherwise, <see langword="false" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
#if NETSTANDARD2_1
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
#else
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
#endif
|
||||
public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? value)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the current string is considered palindromic; that is, the letters within the string are the
|
||||
/// same when reversed.
|
||||
@ -602,6 +670,49 @@ public static class StringExtensions
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current string represents an empty string, or consists of only whitespace
|
||||
/// characters.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is empty or consists of only whitespace; otherwise,
|
||||
/// <see langword="false" />.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
|
||||
[Pure]
|
||||
#if NETSTANDARD2_1
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
#else
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
#endif
|
||||
public static bool IsWhiteSpace(this string value)
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
#else
|
||||
if (value is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
#endif
|
||||
|
||||
if (value.Length == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
for (var index = 0; index < value.Length; index++)
|
||||
{
|
||||
if (!char.IsWhiteSpace(value[index]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Repeats a string a specified number of times.
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user