diff --git a/src/LogExpert.Core/Classes/Util.cs b/src/LogExpert.Core/Classes/Util.cs index 14523f5b..2b8628b4 100644 --- a/src/LogExpert.Core/Classes/Util.cs +++ b/src/LogExpert.Core/Classes/Util.cs @@ -11,45 +11,14 @@ public class Util { #region Public methods - public static string GetNameFromPath (string fileName) - { - var i = fileName.LastIndexOf('\\'); - - if (i < 0) - { - i = fileName.LastIndexOf('/'); - } + public static string GetNameFromPath (string fileName) => fileName is null ? string.Empty : Path.GetFileName(fileName); - if (i < 0) - { - i = -1; - } - return fileName[(i + 1)..]; - } + public static string StripExtension (string fileName) => fileName is null ? string.Empty : Path.GetFileNameWithoutExtension(fileName); - //TODO Add Null Check (https://github.com/LogExperts/LogExpert/issues/403) - public static string StripExtension (string fileName) - { - var i = fileName.LastIndexOf('.'); - if (i < 0) - { - i = fileName.Length - 1; - } + public static string GetExtension (string fileName) => fileName is null ? string.Empty : Path.GetExtension(fileName).TrimStart('.'); - return fileName[..i]; - } - - //TODO Add Null Check (https://github.com/LogExperts/LogExpert/issues/403) - public static string GetExtension (string fileName) - { - var i = fileName.LastIndexOf('.'); - - return i < 0 || i >= fileName.Length - 1 - ? string.Empty - : fileName[(i + 1)..]; - } public static string GetFileSizeAsText (long size) @@ -102,9 +71,17 @@ public static bool TestFilterCondition (FilterParams filterParams, ILogLine line return match; } - //TODO Add Null Checks (https://github.com/LogExperts/LogExpert/issues/403) public static int DamerauLevenshteinDistance (string src, string dest) { + if (dest is null || dest.Length == 0) + { + return 0; + } + if (src is null || src.Length == 0) + { + return int.MaxValue; + } + var d = new int[src.Length + 1, dest.Length + 1]; int i, j, cost; var str1 = src.ToCharArray(); @@ -142,9 +119,12 @@ public static int DamerauLevenshteinDistance (string src, string dest) return d[str1.Length, str2.Length]; } - //TODO Add Null Checks (https://github.com/LogExperts/LogExpert/issues/403) + public static unsafe int YetiLevenshtein (string s1, string s2) { + s1 ??= string.Empty; + s2 ??= string.Empty; + fixed (char* p1 = s1) fixed (char* p2 = s2) { @@ -409,10 +389,15 @@ public static void AssertTrue (bool condition, string msg) } } - //TODO Add Null Check (https://github.com/LogExperts/LogExpert/issues/403) [SupportedOSPlatform("windows")] public string? GetWordFromPos (int xPos, string text, Graphics g, Font font) { + if (text is null) + return null; + //todo check for null in referenced objects + ArgumentNullException.ThrowIfNull(g); + ArgumentNullException.ThrowIfNull(font); + var words = text.Split([' ', '.', ':', ';']); var index = 0; @@ -628,4 +613,4 @@ private static unsafe int MemchrRPLC (char* buffer, char c, int count) } #endregion -} \ No newline at end of file +}