ソースを参照

Added string utilities

Kenric Nugteren 1 年間 前
コミット
d2d27a1c55

+ 0 - 1
InABox.Client.Local/InABox.Client.Local.csproj

@@ -7,7 +7,6 @@
     </PropertyGroup>
 
     <ItemGroup>
-        <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
         <ProjectReference Include="..\InABox.Database\InABox.Database.csproj" />
         <ProjectReference Include="..\InABox.WebSocket.Shared\InABox.WebSocket.Shared.csproj" />
     </ItemGroup>

+ 93 - 53
InABox.Core/CoreUtils.cs

@@ -16,6 +16,7 @@ using System.Text.RegularExpressions;
 using System.Diagnostics.CodeAnalysis;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
+using System.Runtime.CompilerServices;
 
 //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)
         //{
 
@@ -1629,19 +1617,6 @@ namespace InABox.Core
         }
         //static readonly Regex StripQuotes = new Regex(@"&quot;", 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;
         }
+        public static string? GetCaptionOrNull(this Type type, bool inherit = false)
+        {
+            return type.GetCustomAttribute<Caption>(inherit)?.Text;
+        }
 
         public static string SanitiseFileName(string filename)
         {
@@ -1678,26 +1657,6 @@ namespace InABox.Core
             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>>();
 
         public static decimal GetPropertySequence(Type type, string column)
@@ -2602,11 +2561,97 @@ namespace InABox.Core
             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)
         {
 
             char[] numbers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
-            
+
             if (string.IsNullOrWhiteSpace(text))
                 return "??";
 
@@ -2626,13 +2671,8 @@ namespace InABox.Core
             }
             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
 
     }
 }

+ 0 - 1
InABox.Database/InABox.Database.csproj

@@ -14,7 +14,6 @@
     </ItemGroup>
 
     <ItemGroup>
-        <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
         <ProjectReference Include="..\inabox.logging.shared\InABox.Logging.Shared.csproj" />
         <ProjectReference Include="..\InABox.Scripting\InABox.Scripting.csproj" />
     </ItemGroup>

+ 0 - 1
InABox.Poster.CSV/InABox.Poster.CSV.csproj

@@ -12,7 +12,6 @@
 	</ItemGroup>
 
 	<ItemGroup>
-		<ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
 		<ProjectReference Include="..\inabox.scripting\InABox.Scripting.csproj" />
 	</ItemGroup>
 

+ 0 - 1
InABox.Server/InABox.Server.csproj

@@ -15,7 +15,6 @@
     </ItemGroup>
 
     <ItemGroup>
-        <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
         <ProjectReference Include="..\InABox.Database\InABox.Database.csproj" />
         <ProjectReference Include="..\InABox.IPC.Shared\InABox.IPC.Shared.csproj" />
         <ProjectReference Include="..\InABox.Logging.Shared\InABox.Logging.Shared.csproj" />

+ 0 - 1
inabox.client.ipc/InABox.Client.IPC.csproj

@@ -13,7 +13,6 @@
   </ItemGroup>
 
   <ItemGroup>
-    <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
     <ProjectReference Include="..\InABox.IPC.Shared\InABox.IPC.Shared.csproj" />
   </ItemGroup>
 

+ 2 - 2
inabox.client.rest/InABox.Client.Rest/InABox.Client.Rest.csproj

@@ -7,11 +7,11 @@
     </PropertyGroup>
 
     <ItemGroup>
-      <ProjectReference Include="..\..\InABox.Core\InABox.Core.csproj" />
+      <PackageReference Include="RestSharp" Version="110.2.0" />
     </ItemGroup>
 
     <ItemGroup>
-      <PackageReference Include="RestSharp" Version="110.2.0" />
+      <ProjectReference Include="..\..\InABox.Core\InABox.Core.csproj" />
     </ItemGroup>
 
 </Project>

+ 0 - 1
inabox.database.sqlite/InABox.Database.SQLite.csproj

@@ -33,7 +33,6 @@
     </ItemGroup>
 
     <ItemGroup>
-        <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
         <ProjectReference Include="..\InABox.Database\InABox.Database.csproj" />
         <ProjectReference Include="..\inabox.logging.shared\InABox.Logging.Shared.csproj" />
     </ItemGroup>

+ 0 - 1
inabox.ipc.shared/InABox.IPC.Shared.csproj

@@ -7,7 +7,6 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
     <ProjectReference Include="..\InABox.Database\InABox.Database.csproj" />
   </ItemGroup>
 

+ 6 - 6
inabox.mailer.exchange/InABox.Mailer.Exchange.csproj

@@ -7,24 +7,24 @@
     </PropertyGroup>
 
     <ItemGroup>
-        <Compile Remove="archive\**"/>
+        <Compile Remove="archive\**" />
     </ItemGroup>
 
     <ItemGroup>
-        <EmbeddedResource Remove="archive\**"/>
+        <EmbeddedResource Remove="archive\**" />
     </ItemGroup>
 
     <ItemGroup>
-        <None Remove="archive\**"/>
-        <None Remove=".gitignore"/>
+        <None Remove="archive\**" />
+        <None Remove=".gitignore" />
     </ItemGroup>
 
     <ItemGroup>
-        <ProjectReference Include="..\InABox.Core\InABox.Core.csproj"/>
+        <PackageReference Include="Microsoft.Exchange.WebServices.NETStandard" Version="1.1.3" />
     </ItemGroup>
 
     <ItemGroup>
-        <PackageReference Include="Microsoft.Exchange.WebServices.NETStandard" Version="1.1.3"/>
+      <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
     </ItemGroup>
 
 </Project>

+ 6 - 6
inabox.mailer.imap/InABox.Mailer.IMAP.csproj

@@ -7,24 +7,24 @@
     </PropertyGroup>
 
     <ItemGroup>
-        <Compile Remove="archive\**"/>
+        <Compile Remove="archive\**" />
     </ItemGroup>
 
     <ItemGroup>
-        <EmbeddedResource Remove="archive\**"/>
+        <EmbeddedResource Remove="archive\**" />
     </ItemGroup>
 
     <ItemGroup>
-        <None Remove="archive\**"/>
-        <None Remove=".gitignore"/>
+        <None Remove="archive\**" />
+        <None Remove=".gitignore" />
     </ItemGroup>
 
     <ItemGroup>
-        <ProjectReference Include="..\InABox.Core\InABox.Core.csproj"/>
+        <PackageReference Include="MailKit" Version="3.3.0" />
     </ItemGroup>
 
     <ItemGroup>
-        <PackageReference Include="MailKit" Version="3.3.0"/>
+      <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
     </ItemGroup>
 
 </Project>

+ 0 - 1
inabox.wpf/InABox.Wpf.csproj

@@ -119,7 +119,6 @@
 
     <ItemGroup>
         <ProjectReference Include="..\..\3rdpartylibs\roslynpad\src\RoslynPad.Editor.Windows\RoslynPad.Editor.Windows.csproj" />
-        <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
         <ProjectReference Include="..\InABox.Dxf\InABox.Dxf.csproj" />
         <ProjectReference Include="..\InABox.Scripting\InABox.Scripting.csproj" />
     </ItemGroup>