From 20addc0d928ee75f1cfb5c3a500a037c1b535024 Mon Sep 17 00:00:00 2001 From: Andy Howard Date: Wed, 12 Oct 2022 13:51:56 +0100 Subject: [PATCH] Saxonite v1.2.9 --- .../Standard/Base/Utils/StringUtil_Tester.cs | 36 +++++++- SacsStandardBase/Base/Utils/StringUtil.cs | 90 ++++++++++++++++--- 2 files changed, 110 insertions(+), 16 deletions(-) diff --git a/SacsCoreUnitTests/Standard/Base/Utils/StringUtil_Tester.cs b/SacsCoreUnitTests/Standard/Base/Utils/StringUtil_Tester.cs index dcdd71e..9e1c803 100644 --- a/SacsCoreUnitTests/Standard/Base/Utils/StringUtil_Tester.cs +++ b/SacsCoreUnitTests/Standard/Base/Utils/StringUtil_Tester.cs @@ -12,10 +12,42 @@ namespace Sarand.UnitTests.Core.Base.Utils private Random random = new Random(); //------------------------------------------------------------------- - // P a d d i n g + // Properties. //------------------------------------------------------------------- [TestMethod] - public void StringUtil_PadLeft () + public void StringUtil_IsEmpty() + { + // Leading spaces. + Assert.AreEqual(true, StringUtil.IsEmpty(null)); + Assert.AreEqual(true, StringUtil.IsEmpty("")); + Assert.AreEqual(false, StringUtil.IsEmpty(" ")); + Assert.AreEqual(false, StringUtil.IsEmpty("A")); + Assert.AreEqual(false, StringUtil.IsEmpty("Abc")); + Assert.AreEqual(true, "".IsEmpty()); + Assert.AreEqual(false, "Abc".IsEmpty()); + } + + [TestMethod] + public void StringUtil_IsBlank() + { + // Leading spaces. + Assert.AreEqual(true, StringUtil.IsBlank(null)); + Assert.AreEqual(true, StringUtil.IsBlank("")); + Assert.AreEqual(true, StringUtil.IsBlank(" ")); + Assert.AreEqual(true, StringUtil.IsBlank(" ")); + Assert.AreEqual(true, StringUtil.IsBlank(" \n\t ")); + Assert.AreEqual(false, StringUtil.IsBlank("A")); + Assert.AreEqual(false, StringUtil.IsBlank("Abc")); + Assert.AreEqual(true, "".IsBlank()); + Assert.AreEqual(true, " ".IsBlank()); + Assert.AreEqual(false, "Abc".IsBlank()); + } + + //------------------------------------------------------------------- + // Padding. + //------------------------------------------------------------------- + [TestMethod] + public void StringUtil_PadLeft() { // Leading spaces. Assert.AreEqual(" ", "".PadLeft(3, ' ')); diff --git a/SacsStandardBase/Base/Utils/StringUtil.cs b/SacsStandardBase/Base/Utils/StringUtil.cs index 7b5e236..3d0be83 100644 --- a/SacsStandardBase/Base/Utils/StringUtil.cs +++ b/SacsStandardBase/Base/Utils/StringUtil.cs @@ -12,30 +12,34 @@ namespace Sarand.Net.Standard.Base.Utils public static class StringUtil { //------------------------------------------------------------------- - // U t i l i t i e s + // Definitions. + public static Regex RegEx_Whitespace = new Regex("\\s+"); + public static Regex RegEx_Whitespace_Match = new Regex("^\\s+$"); + + //------------------------------------------------------------------- + // Properties. //------------------------------------------------------------------- - public static String EmptyOnNull (String value) - { - return (value == null ? String.Empty : value); - } - public static String OnEmpty (String value, String defaultValue) - { - if (IsEmpty(value)) return defaultValue; - return value; - } public static int GetLength (String value) { if (String.IsNullOrEmpty(value)) return 0; return value.Length; } - //------------------------------------------------------------------- - // C o m p a r i s o n - //------------------------------------------------------------------- - public static bool IsEmpty (this string value) + /// Test for null or empty value. + public static bool IsEmpty(this string value) { return String.IsNullOrEmpty(value); } + + /// Test for null, empty or a value that consists of whitespace characters only. + public static bool IsBlank(this string value) + { + return String.IsNullOrEmpty(value) || Match(value, RegEx_Whitespace_Match); + } + + //------------------------------------------------------------------- + // Comparison. + //------------------------------------------------------------------- public static bool EqualsCI (this String value1, String value2) { return String.Equals(value1, value2, StringComparison.OrdinalIgnoreCase); @@ -56,6 +60,64 @@ namespace Sarand.Net.Standard.Base.Utils return (value1.IndexOf(value2, StringComparison.OrdinalIgnoreCase) >= 0); } + //------------------------------------------------------------------- + // Regular expressions. + //------------------------------------------------------------------- + /// + /// Create Regex from (quoted) expression and apply Regex.Match to String value and return Match.Success. + /// - Case sensitive. + /// + public static bool Match(this String value, String expression) + { + return value.Match(expression, RegexOptions.None); + } + /// + /// Create Regex from (quoted) expression and apply Regex.Match to String value and return Match.Success. + /// - Case insensitive. + /// + public static bool MatchCI(this String value, String expression) + { + return value.Match(expression, RegexOptions.IgnoreCase); + } + /// + /// Create Regex from (quoted) expression and apply Regex.Match to String value and return Match.Success. + /// - Supply options as required. + /// + public static bool Match(this String value, String expression, RegexOptions options) + { + return new Regex(Regex.Escape(expression), options).Match(value).Success; + } + /// + /// Apply Regex.Match to String value and return Match.Success. + /// + public static bool Match(this String value, Regex re) + { + return re.Match(value).Success; + } + /// + /// Apply Regex.Matches to String value and return true if any matches found. + /// + public static bool Matches(this String value, Regex re) + { + return re.Matches(value).Count < 1; + } + + //------------------------------------------------------------------- + // Alternative Content. + //------------------------------------------------------------------- + public static String EmptyOnNull(String value) + { + return (value == null ? String.Empty : value); + } + public static String OnEmpty(String value, String defaultValue) + { + return (!IsEmpty(value)) ? value : defaultValue; + } + public static String OnBlank(String value, String defaultValue) + { + return (!IsBlank(value)) ? value : defaultValue; + } + //------------------------------------------------------------------- // C o n t e n t //-------------------------------------------------------------------