Saxonite v1.2.9

This commit is contained in:
Andy Howard
2022-10-12 13:51:56 +01:00
parent 0cab7c6a9d
commit 20addc0d92
2 changed files with 110 additions and 16 deletions
@@ -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, ' '));
+76 -14
View File
@@ -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)
/// <summary>Test for null or empty value.</summary>
public static bool IsEmpty(this string value)
{
return String.IsNullOrEmpty(value);
}
/// <summary>Test for null, empty or a value that consists of whitespace characters only.</summary>
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.
//-------------------------------------------------------------------
/// <summary>
/// Create Regex from (quoted) expression and apply Regex.Match to String value and return Match.Success.
/// <para>- Case sensitive.</para>
/// </summary>
public static bool Match(this String value, String expression)
{
return value.Match(expression, RegexOptions.None);
}
/// <summary>
/// Create Regex from (quoted) expression and apply Regex.Match to String value and return Match.Success.
/// <para>- Case insensitive.</para>
/// </summary>
public static bool MatchCI(this String value, String expression)
{
return value.Match(expression, RegexOptions.IgnoreCase);
}
/// <summary>
/// Create Regex from (quoted) expression and apply Regex.Match to String value and return Match.Success.
/// <para>- Supply options as required.</para>
/// </summary>
public static bool Match(this String value, String expression, RegexOptions options)
{
return new Regex(Regex.Escape(expression), options).Match(value).Success;
}
/// <summary>
/// Apply Regex.Match to String value and return Match.Success.
/// </summary>
public static bool Match(this String value, Regex re)
{
return re.Match(value).Success;
}
/// <summary>
/// Apply Regex.Matches to String value and return true if any matches found.
/// </summary>
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
//-------------------------------------------------------------------