Saxonite 1.2.11.0

This commit is contained in:
Andy Howard
2023-07-06 18:35:55 +01:00
parent b39b28d2d2
commit 048194e935
3 changed files with 82 additions and 58 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ namespace Sarand.Net.Standard.Base.Bus
}
public static SysId InvalidOnNull(SysId sysId)
{
return sysId != null ? sysId : new SysId();
return sysId ?? new SysId();
}
//-------------------------------------------------------------------
@@ -126,6 +126,24 @@ namespace Sarand.Net.Standard.Base.Helpers
return false;
}
// YouTube URLs. [[https:]//][www.]youtube.com/...
private static readonly Regex reYouTubeUrl = new Regex("^(?:(?:http(?:s)?:)?//)?(?:www\\.)?" + Regex.Escape("youtube.com/") + ".+", RegexOptions.IgnoreCase);
public static bool IsValidYouTubeUrl(String value)
{
if (!IsValidUrl(value)) return false;
if (reYouTubeUrl.Matches(value).Count > 0) return true;
return false;
}
// Twitch URLs. [[https:]//][www.]twitch.com/...
private static readonly Regex reTwitchUrl = new Regex("^(?:(?:http(?:s)?:)?//)?(?:www\\.)?" + Regex.Escape("twitch.tv/") + ".+", RegexOptions.IgnoreCase);
public static bool IsValidTwitchUrl(String value)
{
if (!IsValidUrl(value)) return false;
if (reTwitchUrl.Matches(value).Count > 0) return true;
return false;
}
//-------------------------------------------------------------------
/// <summary>
/// Ensure that the website address is a valid URL, ie. at least starts with '//'.
+60 -54
View File
@@ -20,7 +20,7 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// Properties.
//-------------------------------------------------------------------
public static int GetLength (String value)
public static int GetLength(String value)
{
if (String.IsNullOrEmpty(value)) return 0;
return value.Length;
@@ -41,21 +41,21 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// Comparison.
//-------------------------------------------------------------------
public static bool EqualsCI (this String value1, String value2)
public static bool EqualsCI(this String value1, String value2)
{
return String.Equals(value1, value2, StringComparison.OrdinalIgnoreCase);
}
public static bool StartsWithCI (this String value1, String value2)
public static bool StartsWithCI(this String value1, String value2)
{
if (IsEmpty(value1) || IsEmpty(value2)) return false;
return (value1.StartsWith(value2, StringComparison.OrdinalIgnoreCase));
}
public static bool EndsWithCI (this String value1, String value2)
public static bool EndsWithCI(this String value1, String value2)
{
if (IsEmpty(value1) || IsEmpty(value2)) return false;
return (value1.EndsWith(value2, StringComparison.OrdinalIgnoreCase));
}
public static bool ContainsCI (this String value1, String value2)
public static bool ContainsCI(this String value1, String value2)
{
if (IsEmpty(value1) || IsEmpty(value2)) return false;
return (value1.IndexOf(value2, StringComparison.OrdinalIgnoreCase) >= 0);
@@ -108,35 +108,41 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
public static String EmptyOnNull(String value)
{
return (value == null ? String.Empty : value);
return value ?? String.Empty;
}
public static String OnEmpty(String value, String defaultValue)
/// <summary>Return the first value in the list that is not empty. Default if all are empty is empty string.</summary>
public static String OnEmpty(params String[] values)
{
return (!IsEmpty(value)) ? value : defaultValue;
foreach (String value in values)
if (!IsEmpty(value)) return value;
return "";
}
public static String OnBlank(String value, String defaultValue)
/// <summary>Return the first value in the list that is not blank. Default if all are blank is empty string.</summary>
public static String OnBlank(params String[] values)
{
return (!IsBlank(value)) ? value : defaultValue;
foreach (String value in values)
if (!IsBlank(value)) return value;
return "";
}
//-------------------------------------------------------------------
// C o n t e n t
//-------------------------------------------------------------------
public static bool IsAlpha (this string value)
public static bool IsAlpha(this string value)
{
return EmptyOnNull(value).All(Char.IsLetter);
}
public static bool IsAlphaNumeric (this string value)
public static bool IsAlphaNumeric(this string value)
{
return EmptyOnNull(value).All(Char.IsLetterOrDigit);
}
public static bool IsNumeric (this string value, bool allowNegative = false)
public static bool IsNumeric(this string value, bool allowNegative = false)
{
Int64 number = 0;
if (!Int64.TryParse(value, out number)) return false;
return (allowNegative || number >= 0);
}
public static bool IsDecimal (this string value, bool allowNegative = false)
public static bool IsDecimal(this string value, bool allowNegative = false)
{
Decimal number = 0;
if (!Decimal.TryParse(value, out number)) return false;
@@ -144,12 +150,12 @@ namespace Sarand.Net.Standard.Base.Utils
}
//-------------------------------------------------------------------
/// <summary>Test for Hex character.</summary>
public static bool IsHex (this char c)
public static bool IsHex(this char c)
{
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
/// <summary>Test for Hex string.</summary>
public static bool IsHex (this String value)
public static bool IsHex(this String value)
{
if (IsEmpty(value)) return false;
foreach (char c in value)
@@ -160,14 +166,14 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// P a d d i n g
//-------------------------------------------------------------------
public static String PadLeft (this String value, int length, char pad = ' ')
public static String PadLeft(this String value, int length, char pad = ' ')
{
value = StringUtil.EmptyOnNull(value);
while (value.Length < length)
value = pad + value;
return value;
}
public static String PadRight (this String value, int length, char pad = ' ')
public static String PadRight(this String value, int length, char pad = ' ')
{
value = StringUtil.EmptyOnNull(value);
while (value.Length < length)
@@ -178,13 +184,13 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// C o n v e r t
//-------------------------------------------------------------------
public static String CapitaliseFirst (this String value, TextInfo textInfo = null)
public static String CapitaliseFirst(this String value, TextInfo textInfo = null)
{
textInfo = (textInfo ?? CultureInfo.CurrentCulture.TextInfo);
if (StringUtil.IsEmpty(value)) return value;
return textInfo.ToUpper(value.First()) + value.Substring(1);
}
public static string ToTitleCase (this string value, TextInfo textInfo = null)
public static string ToTitleCase(this string value, TextInfo textInfo = null)
{
textInfo = (textInfo ?? CultureInfo.CurrentCulture.TextInfo);
String[] tokens = value.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
@@ -201,7 +207,7 @@ namespace Sarand.Net.Standard.Base.Utils
// C l e a n s e
//-------------------------------------------------------------------
/// <summary>Trim leading, trailling and multiple internal spaces.</summary>
public static String TrimAll (this String value)
public static String TrimAll(this String value)
{
if (IsEmpty(value)) return value;
value = value.Trim().TrimInternalSpaces();
@@ -215,17 +221,17 @@ namespace Sarand.Net.Standard.Base.Utils
/// <param name="value"></param>
/// <param name="trimCharacters"></param>
/// <returns></returns>
public static String TrimAll (this String value, char[] trimChars, String replacement = " ")
public static String TrimAll(this String value, char[] trimChars, String replacement = " ")
{
if (IsEmpty(value) || trimChars.Length < 1) return value;
// Trim leading and trailing.
value = value.Trim(trimChars);
// Use regex to replace multiple internal.
Regex regexMultipleInternal = new Regex("["+Regex.Escape(new String(trimChars))+"]{2,}", RegexOptions.None);
Regex regexMultipleInternal = new Regex("[" + Regex.Escape(new String(trimChars)) + "]{2,}", RegexOptions.None);
return regexMultipleInternal.Replace(value, replacement);
}
private static Regex regexMultipleSpaces = new Regex("[ ]{2,}", RegexOptions.Compiled);
public static String TrimInternalSpaces (this String value)
public static String TrimInternalSpaces(this String value)
{
return regexMultipleSpaces.Replace(value, " ");
}
@@ -244,7 +250,7 @@ namespace Sarand.Net.Standard.Base.Utils
return value.Replace(search, replace, true, cultureInfo);
}
/// <summary>Replace any of the characters in the search string with the specified replacement character.</summary>
public static string ReplaceAny (this string value, String searchChars, char replaceChar)
public static string ReplaceAny(this string value, String searchChars, char replaceChar)
{
for (int i = 0; i < searchChars.Length; i++)
value = value.Replace(searchChars[i], replaceChar);
@@ -254,11 +260,11 @@ namespace Sarand.Net.Standard.Base.Utils
/// Replace all occurences of any of the search strings with the replace string, even where search values remain after the each replacement.
/// <para>NB Use String.Replace if the replacement contains the search string</para>
/// </summary>
public static string ReplaceAll (this string value, String search, String replace)
public static string ReplaceAll(this string value, String search, String replace)
{
return value.ReplaceAll(new String[] { search }, replace);
}
public static string ReplaceAll (this string value, String[] searches, String replace)
public static string ReplaceAll(this string value, String[] searches, String replace)
{
foreach (String search in searches)
{
@@ -272,7 +278,7 @@ namespace Sarand.Net.Standard.Base.Utils
return value;
}
/// <summary>Duplicate every instance of the specified character.</summary>
public static String Duplicate (this String value, char search)
public static String Duplicate(this String value, char search)
{
return value.Replace(search.ToString(), search.ToString() + search.ToString());
}
@@ -292,13 +298,13 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// E x t r a c t i o n
//-------------------------------------------------------------------
public static string Left (this string value, int length)
public static string Left(this string value, int length)
{
if (String.IsNullOrEmpty(value)) return value;
if (length < 0) return String.Empty;
return (length >= value.Length ? value : value.Substring(0, length));
}
public static string Right (this string value, int length)
public static string Right(this string value, int length)
{
if (String.IsNullOrEmpty(value)) return value;
if (length < 0) return String.Empty;
@@ -316,7 +322,7 @@ namespace Sarand.Net.Standard.Base.Utils
/// <param name="start"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string SubstringSafe (this string value, int start, int length = -1)
public static string SubstringSafe(this string value, int start, int length = -1)
{
if (String.IsNullOrEmpty(value)) return String.Empty;
start = Math.Max(0, start);
@@ -330,15 +336,15 @@ namespace Sarand.Net.Standard.Base.Utils
// C o n v e r s i o n
//-------------------------------------------------------------------
// Integer values.
public static short ToShort (this string value, short defaultValue = 0)
public static short ToShort(this string value, short defaultValue = 0)
{
return (short)ToLong(value, defaultValue);
}
public static int ToInt (this string value, int defaultValue = 0)
public static int ToInt(this string value, int defaultValue = 0)
{
return (int)ToLong(value, defaultValue);
}
public static long ToLong (this string value, long defaultValue = 0)
public static long ToLong(this string value, long defaultValue = 0)
{
if (String.IsNullOrEmpty(value)) return defaultValue;
try
@@ -350,15 +356,15 @@ namespace Sarand.Net.Standard.Base.Utils
catch (Exception) { }
return defaultValue;
}
public static short ToShort (Object value, short defaultValue = 0)
public static short ToShort(Object value, short defaultValue = 0)
{
return (short)ToLong(value, defaultValue);
}
public static int ToInt (Object value, int defaultValue = 0)
public static int ToInt(Object value, int defaultValue = 0)
{
return (int)ToLong(value, defaultValue);
}
public static long ToLong (Object value, long defaultValue = 0)
public static long ToLong(Object value, long defaultValue = 0)
{
if (value == null) return defaultValue;
try
@@ -379,7 +385,7 @@ namespace Sarand.Net.Standard.Base.Utils
//---------------------------------------------------------------------------
// Hex values to integer.
/// <summary>Convert hex character to integer value.</summary>
public static byte ToHex (this char value)
public static byte ToHex(this char value)
{
if (value >= '0' && value <= '9') return (byte)(value - '0');
if (value >= 'A' && value <= 'F') return (byte)(value - 'A' + 10);
@@ -387,12 +393,12 @@ namespace Sarand.Net.Standard.Base.Utils
return 0;
}
/// <summary>Convert pair of hex character to integer value.</summary>
public static byte ToHex (char cHex1, char cHex2)
public static byte ToHex(char cHex1, char cHex2)
{
return (byte)((ToHex(cHex1) * 16) + ToHex(cHex2));
}
/// <summary>Convert hex strings to integer value.</summary>
public static ulong ToHex (this String value)
public static ulong ToHex(this String value)
{
if (IsEmpty(value)) return 0;
ulong intValue = 0;
@@ -401,7 +407,7 @@ namespace Sarand.Net.Standard.Base.Utils
// Shift previous value half a byte (nibble) - equivalent to single hex character.
intValue <<= 4;
// Get value of next hex character.
byte charValue = ToHex (c);
byte charValue = ToHex(c);
intValue += charValue;
}
return intValue;
@@ -409,11 +415,11 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// Floating point values.
public static float ToSingle (this string value, float defaultValue = 0)
public static float ToSingle(this string value, float defaultValue = 0)
{
return Convert.ToSingle(ToDouble(value, defaultValue));
}
public static double ToDouble (this string value, double defaultValue = 0)
public static double ToDouble(this string value, double defaultValue = 0)
{
if (String.IsNullOrEmpty(value)) return defaultValue;
try
@@ -425,11 +431,11 @@ namespace Sarand.Net.Standard.Base.Utils
catch (Exception) { }
return defaultValue;
}
public static float ToSingle (Object value, float defaultValue = 0)
public static float ToSingle(Object value, float defaultValue = 0)
{
return Convert.ToSingle(ToDouble(value, defaultValue));
}
public static double ToDouble (Object value, double defaultValue = 0)
public static double ToDouble(Object value, double defaultValue = 0)
{
if (value == null) return defaultValue;
try
@@ -442,7 +448,7 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// Decimal values.
public static decimal ToDecimal (this string value, decimal defaultValue = 0)
public static decimal ToDecimal(this string value, decimal defaultValue = 0)
{
if (String.IsNullOrEmpty(value)) return defaultValue;
try
@@ -454,7 +460,7 @@ namespace Sarand.Net.Standard.Base.Utils
catch (Exception) { }
return defaultValue;
}
public static decimal ToDecimal (Object value, decimal defaultValue = 0)
public static decimal ToDecimal(Object value, decimal defaultValue = 0)
{
if (value == null) return defaultValue;
try
@@ -467,11 +473,11 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// Boolean values.
public static bool ToBool (this string value)
public static bool ToBool(this string value)
{
return value.IsTrue();
}
public static bool IsTrue (this string value)
public static bool IsTrue(this string value)
{
if (String.IsNullOrEmpty(value)) return false;
if (value.ToLong() != 0) return true;
@@ -484,12 +490,12 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// F o r m a t t i n g
//-------------------------------------------------------------------
public static String FormatNumeric (double numericValue, ushort decimalPlaces = 0, ushort sigFigs = 0, char thousandsDelim = '\0', char decimalDelim = '.')
public static String FormatNumeric(double numericValue, ushort decimalPlaces = 0, ushort sigFigs = 0, char thousandsDelim = '\0', char decimalDelim = '.')
{
String value = String.Empty;
try
{
String format = "F"+decimalPlaces.ToString();
String format = "F" + decimalPlaces.ToString();
if (thousandsDelim != '\0')
format = "N" + decimalPlaces.ToString();
value = numericValue.ToString(format);
@@ -500,12 +506,12 @@ namespace Sarand.Net.Standard.Base.Utils
}
return value;
}
public static String FormatNumeric (decimal numericValue, ushort decimalPlaces = 0, ushort sigFigs = 0, char thousandsDelim = '\0', char decimalDelim = '.')
public static String FormatNumeric(decimal numericValue, ushort decimalPlaces = 0, ushort sigFigs = 0, char thousandsDelim = '\0', char decimalDelim = '.')
{
String value = String.Empty;
try
{
String format = "F"+decimalPlaces.ToString();
String format = "F" + decimalPlaces.ToString();
if (thousandsDelim != '\0')
format = "N" + decimalPlaces.ToString();
value = numericValue.ToString(format);
@@ -518,7 +524,7 @@ namespace Sarand.Net.Standard.Base.Utils
}
//-------------------------------------------------------------------
// Specialist finding.
public static int FindMatchingBrace (this String value, int braceOpenPos)
public static int FindMatchingBrace(this String value, int braceOpenPos)
{
if (String.IsNullOrEmpty(value) || braceOpenPos < 0 || braceOpenPos >= value.Length) return -1;
char sourceBrace = value[braceOpenPos];