diff --git a/SacsStandardBase/Base/Bus/SysId.cs b/SacsStandardBase/Base/Bus/SysId.cs index b568f5a..f848f58 100644 --- a/SacsStandardBase/Base/Bus/SysId.cs +++ b/SacsStandardBase/Base/Bus/SysId.cs @@ -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(); } //------------------------------------------------------------------- diff --git a/SacsStandardBase/Base/Helpers/SacsDataValidationHelper.cs b/SacsStandardBase/Base/Helpers/SacsDataValidationHelper.cs index 4318f7f..fbb0302 100644 --- a/SacsStandardBase/Base/Helpers/SacsDataValidationHelper.cs +++ b/SacsStandardBase/Base/Helpers/SacsDataValidationHelper.cs @@ -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; + } + //------------------------------------------------------------------- /// /// Ensure that the website address is a valid URL, ie. at least starts with '//'. diff --git a/SacsStandardBase/Base/Utils/StringUtil.cs b/SacsStandardBase/Base/Utils/StringUtil.cs index 39ef2a6..d10c054 100644 --- a/SacsStandardBase/Base/Utils/StringUtil.cs +++ b/SacsStandardBase/Base/Utils/StringUtil.cs @@ -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) + /// Return the first value in the list that is not empty. Default if all are empty is empty string. + 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) + /// Return the first value in the list that is not blank. Default if all are blank is empty string. + 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 } //------------------------------------------------------------------- /// Test for Hex character. - 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'); } /// Test for Hex string. - 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 //------------------------------------------------------------------- /// Trim leading, trailling and multiple internal spaces. - 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 /// /// /// - 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); } /// Replace any of the characters in the search string with the specified replacement character. - 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. /// NB Use String.Replace if the replacement contains the search string /// - 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; } /// Duplicate every instance of the specified character. - 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 /// /// /// - 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. /// Convert hex character to integer value. - 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; } /// Convert pair of hex character to integer value. - public static byte ToHex (char cHex1, char cHex2) + public static byte ToHex(char cHex1, char cHex2) { return (byte)((ToHex(cHex1) * 16) + ToHex(cHex2)); } /// Convert hex strings to integer value. - 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,12 +524,12 @@ 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]; - char targetBrace = '\0'; // Target character to be found. - bool openBraceFound = false; // Identifies forward or backwards search (close or open bracket to be found). + char sourceBrace = value[braceOpenPos]; + char targetBrace = '\0'; // Target character to be found. + bool openBraceFound = false; // Identifies forward or backwards search (close or open bracket to be found). // Identify the target bracket character: switch (sourceBrace)