|
@@ -16,6 +16,7 @@ using System.Text.RegularExpressions;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
+using System.Runtime.CompilerServices;
|
|
|
|
|
|
//using Serialize.Linq.Serializers;
|
|
//using Serialize.Linq.Serializers;
|
|
|
|
|
|
@@ -1317,19 +1318,6 @@ namespace InABox.Core
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public static bool IsBase64String(this string s)
|
|
|
|
- {
|
|
|
|
- s = s.Trim();
|
|
|
|
- return s.Length % 4 == 0 && Regex.IsMatch(s, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static string CalculateCRC(byte[] data)
|
|
|
|
- {
|
|
|
|
- var hash = Crc32.Compute(data);
|
|
|
|
- return string.Format("{0:X8}", hash);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
//public static TimeSpan Round(this TimeSpan timespan, TimeSpan interval)
|
|
//public static TimeSpan Round(this TimeSpan timespan, TimeSpan interval)
|
|
//{
|
|
//{
|
|
|
|
|
|
@@ -1629,19 +1617,6 @@ namespace InABox.Core
|
|
}
|
|
}
|
|
//static readonly Regex StripQuotes = new Regex(@""", RegexOptions.Compiled);
|
|
//static readonly Regex StripQuotes = new Regex(@""", RegexOptions.Compiled);
|
|
|
|
|
|
- public static string StripHTML(this string html)
|
|
|
|
- {
|
|
|
|
- if (string.IsNullOrWhiteSpace(html))
|
|
|
|
- return "";
|
|
|
|
-
|
|
|
|
- var result = StripHtmlStyles.Replace(html, "");
|
|
|
|
- result = StripHtmlTags.Replace(result, "");
|
|
|
|
- result = StripSpecialCharacters.Replace(result, "");
|
|
|
|
- //result = StripQuotes.Replace(result, "\"");
|
|
|
|
- result = StripLineBreaks.Replace(result, "$1");
|
|
|
|
- result = result.Replace("\0", "").Trim();
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1655,6 +1630,10 @@ namespace InABox.Core
|
|
: "";
|
|
: "";
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
+ public static string? GetCaptionOrNull(this Type type, bool inherit = false)
|
|
|
|
+ {
|
|
|
|
+ return type.GetCustomAttribute<Caption>(inherit)?.Text;
|
|
|
|
+ }
|
|
|
|
|
|
public static string SanitiseFileName(string filename)
|
|
public static string SanitiseFileName(string filename)
|
|
{
|
|
{
|
|
@@ -1678,26 +1657,6 @@ namespace InABox.Core
|
|
return sanitisedNamePart;
|
|
return sanitisedNamePart;
|
|
}
|
|
}
|
|
|
|
|
|
- private static Regex NeatifyRegex = new Regex(@"
|
|
|
|
- (?<=[A-Z])(?=[A-Z][a-z]) |
|
|
|
|
- (?<=[^A-Z])(?=[A-Z]) |
|
|
|
|
- (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
|
|
|
|
- public static string Neatify(string caption)
|
|
|
|
- {
|
|
|
|
- if (string.Equals(caption, "ID"))
|
|
|
|
- return caption;
|
|
|
|
-
|
|
|
|
- var result = NeatifyRegex.Replace(caption.Replace(".", ""), " ");
|
|
|
|
-
|
|
|
|
- while (result.Contains(" "))
|
|
|
|
- result = result.Replace(" ", " ");
|
|
|
|
-
|
|
|
|
- if (result.EndsWith(" ID"))
|
|
|
|
- result = string.Join(" ", result.Split(' ').Reverse().Skip(1).Reverse());
|
|
|
|
-
|
|
|
|
- return result.Trim();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
private static List<Tuple<Type, String, decimal>> _columnsequences = new List<Tuple<Type, string, decimal>>();
|
|
private static List<Tuple<Type, String, decimal>> _columnsequences = new List<Tuple<Type, string, decimal>>();
|
|
|
|
|
|
public static decimal GetPropertySequence(Type type, string column)
|
|
public static decimal GetPropertySequence(Type type, string column)
|
|
@@ -2602,11 +2561,97 @@ namespace InABox.Core
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
|
|
|
|
+ {
|
|
|
|
+ int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
|
|
|
|
+ return dt.AddDays(-1 * diff).Date;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #region String Utilities
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Check if <paramref name="s"/> is <see cref="string.IsNullOrWhiteSpace(string)"/>.
|
|
|
|
+ /// If it is empty, returns <paramref name="def"/>, otherwise, returns <paramref name="s"/>.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <remarks>
|
|
|
|
+ /// Basically the null-coalescing operator ?? for strings.
|
|
|
|
+ /// </remarks>
|
|
|
|
+ /// <param name="s"></param>
|
|
|
|
+ /// <param name="def"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ [return: NotNullIfNotNull("def")]
|
|
|
|
+ public static string? NotWhiteSpaceOr(this string? s, string? def = null)
|
|
|
|
+ {
|
|
|
|
+ if (string.IsNullOrWhiteSpace(s))
|
|
|
|
+ {
|
|
|
|
+ return def;
|
|
|
|
+ }
|
|
|
|
+ return s;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Extension method version of <see cref="string.IsNullOrWhiteSpace(string)"/>.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="s">The string to check</param>
|
|
|
|
+ /// <returns>true if <paramref name="s"/> is <see langword="null"/> or composed only of whitespace.</returns>
|
|
|
|
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
+ public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? s)
|
|
|
|
+ {
|
|
|
|
+ return string.IsNullOrWhiteSpace(s);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static bool IsBase64String(this string s)
|
|
|
|
+ {
|
|
|
|
+ s = s.Trim();
|
|
|
|
+ return s.Length % 4 == 0 && Regex.IsMatch(s, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static string CalculateCRC(byte[] data)
|
|
|
|
+ {
|
|
|
|
+ var hash = Crc32.Compute(data);
|
|
|
|
+ return string.Format("{0:X8}", hash);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static string StripHTML(this string html)
|
|
|
|
+ {
|
|
|
|
+ if (string.IsNullOrWhiteSpace(html))
|
|
|
|
+ return "";
|
|
|
|
+
|
|
|
|
+ var result = StripHtmlStyles.Replace(html, "");
|
|
|
|
+ result = StripHtmlTags.Replace(result, "");
|
|
|
|
+ result = StripSpecialCharacters.Replace(result, "");
|
|
|
|
+ //result = StripQuotes.Replace(result, "\"");
|
|
|
|
+ result = StripLineBreaks.Replace(result, "$1");
|
|
|
|
+ result = result.Replace("\0", "").Trim();
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static Regex NeatifyRegex = new Regex(@"
|
|
|
|
+ (?<=[A-Z])(?=[A-Z][a-z]) |
|
|
|
|
+ (?<=[^A-Z])(?=[A-Z]) |
|
|
|
|
+ (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
|
|
|
|
+ public static string Neatify(string caption)
|
|
|
|
+ {
|
|
|
|
+ if (string.Equals(caption, "ID"))
|
|
|
|
+ return caption;
|
|
|
|
+
|
|
|
|
+ var result = NeatifyRegex.Replace(caption.Replace(".", ""), " ");
|
|
|
|
+
|
|
|
|
+ while (result.Contains(" "))
|
|
|
|
+ result = result.Replace(" ", " ");
|
|
|
|
+
|
|
|
|
+ if (result.EndsWith(" ID"))
|
|
|
|
+ result = string.Join(" ", result.Split(' ').Reverse().Skip(1).Reverse());
|
|
|
|
+
|
|
|
|
+ return result.Trim();
|
|
|
|
+ }
|
|
|
|
+
|
|
public static string Codify(string? text)
|
|
public static string Codify(string? text)
|
|
{
|
|
{
|
|
|
|
|
|
char[] numbers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
|
char[] numbers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
|
-
|
|
|
|
|
|
+
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
return "??";
|
|
return "??";
|
|
|
|
|
|
@@ -2626,13 +2671,8 @@ namespace InABox.Core
|
|
}
|
|
}
|
|
return string.IsNullOrWhiteSpace(result) ? "??" : result;
|
|
return string.IsNullOrWhiteSpace(result) ? "??" : result;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
|
|
- public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
|
|
|
|
- {
|
|
|
|
- int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
|
|
|
|
- return dt.AddDays(-1 * diff).Date;
|
|
|
|
- }
|
|
|
|
|
|
+ #endregion
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|