diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fcd310..79c63c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,10 @@ - X10D: Added `Span.Split(Span)` - 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, out TimeSpan)` - X10D: Added `Quaternion.Multiply(Vector3)` - this functions as an equivalent to Unity's `Quaternion * Vector3` operator - X10D: Added `Vector2.Deconstruct()` diff --git a/X10D.Tests/src/Text/StringTests.cs b/X10D.Tests/src/Text/StringTests.cs index 529124a..a5a8fbf 100644 --- a/X10D.Tests/src/Text/StringTests.cs +++ b/X10D.Tests/src/Text/StringTests.cs @@ -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(() => value!.IsEmpty()); + } + [TestMethod] public void IsLower_ShouldReturnTrue_GivenLowercaseString() { @@ -303,6 +329,58 @@ public class StringTests Assert.ThrowsException(() => 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(() => 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(() => value!.IsWhiteSpace()); + } + [TestMethod] public void Randomize_ShouldReorder_GivenString() { diff --git a/X10D/src/Text/StringExtensions.cs b/X10D/src/Text/StringExtensions.cs index 64dc85c..23ffd1e 100644 --- a/X10D/src/Text/StringExtensions.cs +++ b/X10D/src/Text/StringExtensions.cs @@ -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); } + /// + /// Returns a value indicating whether the current string represents an empty string. + /// + /// The value to check. + /// + /// if is empty; otherwise, . + /// + /// is . + [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; + } + /// /// Determines if all alpha characters in this string are considered lowercase. /// @@ -465,6 +493,46 @@ public static class StringExtensions return true; } + /// + /// Returns a value indicating whether the current string is ( in Visual + /// Basic), or represents an empty string. + /// + /// The value to check. + /// + /// if is or empty; otherwise, + /// . + /// + [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); + } + + /// + /// Returns a value indicating whether the current string is ( in Visual + /// Basic), represents an empty string, or consists of only whitespace characters. + /// + /// The value to check. + /// + /// if is , empty, or consists of only + /// whitespace; otherwise, . + /// + [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); + } + /// /// 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; } + /// + /// Returns a value indicating whether the current string represents an empty string, or consists of only whitespace + /// characters. + /// + /// The value to check. + /// + /// if is empty or consists of only whitespace; otherwise, + /// . + /// + /// is . + [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; + } + /// /// Repeats a string a specified number of times. ///