From 811bfb842347b41cf4c52b7612b051924dc11ef5 Mon Sep 17 00:00:00 2001 From: B0risev1ch <122359715+B0risev1ch@users.noreply.github.com> Date: Fri, 6 Jun 2025 21:38:54 +0500 Subject: [PATCH 1/3] Added Null Checks to Util.cs Added Null Checks (https://github.com/LogExperts/LogExpert/issues/403) --- src/LogExpert.Core/Classes/Util.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/LogExpert.Core/Classes/Util.cs b/src/LogExpert.Core/Classes/Util.cs index 14523f5b..faea0676 100644 --- a/src/LogExpert.Core/Classes/Util.cs +++ b/src/LogExpert.Core/Classes/Util.cs @@ -28,9 +28,10 @@ public static string GetNameFromPath (string fileName) return fileName[(i + 1)..]; } - //TODO Add Null Check (https://github.com/LogExperts/LogExpert/issues/403) + public static string StripExtension (string fileName) { + ArgumentNullException.ThrowIfNull(fileName); var i = fileName.LastIndexOf('.'); if (i < 0) @@ -41,9 +42,10 @@ public static string StripExtension (string fileName) return fileName[..i]; } - //TODO Add Null Check (https://github.com/LogExperts/LogExpert/issues/403) + public static string GetExtension (string fileName) { + ArgumentNullException.ThrowIfNull(fileName); var i = fileName.LastIndexOf('.'); return i < 0 || i >= fileName.Length - 1 @@ -102,9 +104,11 @@ 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) { + ArgumentNullException.ThrowIfNull(src); + ArgumentNullException.ThrowIfNull(dest); + var d = new int[src.Length + 1, dest.Length + 1]; int i, j, cost; var str1 = src.ToCharArray(); @@ -142,9 +146,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) { + ArgumentNullException.ThrowIfNull(s1); + ArgumentNullException.ThrowIfNull(s2); + fixed (char* p1 = s1) fixed (char* p2 = s2) { @@ -409,10 +416,13 @@ 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; + ArgumentNullException.ThrowIfNull(g); + ArgumentNullException.ThrowIfNull(font); + var words = text.Split([' ', '.', ':', ';']); var index = 0; @@ -628,4 +638,4 @@ private static unsafe int MemchrRPLC (char* buffer, char c, int count) } #endregion -} \ No newline at end of file +} From 267ca9bc4fa018193158e1b8ffc546f0b9b4d2b2 Mon Sep 17 00:00:00 2001 From: Andrey Barisevich Date: Sat, 14 Jun 2025 21:51:02 +0500 Subject: [PATCH 2/3] Refactored Util.cs: added null checks and improved file name utilities --- src/LogExpert.Core/Classes/Util.cs | 61 +++++++++--------------------- 1 file changed, 18 insertions(+), 43 deletions(-) diff --git a/src/LogExpert.Core/Classes/Util.cs b/src/LogExpert.Core/Classes/Util.cs index faea0676..4286290b 100644 --- a/src/LogExpert.Core/Classes/Util.cs +++ b/src/LogExpert.Core/Classes/Util.cs @@ -11,47 +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); - - public static string StripExtension (string fileName) - { - ArgumentNullException.ThrowIfNull(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]; - } - - - public static string GetExtension (string fileName) - { - ArgumentNullException.ThrowIfNull(fileName); - var i = fileName.LastIndexOf('.'); - - return i < 0 || i >= fileName.Length - 1 - ? string.Empty - : fileName[(i + 1)..]; - } public static string GetFileSizeAsText (long size) @@ -106,9 +73,15 @@ public static bool TestFilterCondition (FilterParams filterParams, ILogLine line public static int DamerauLevenshteinDistance (string src, string dest) { - ArgumentNullException.ThrowIfNull(src); - ArgumentNullException.ThrowIfNull(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(); @@ -146,12 +119,12 @@ public static int DamerauLevenshteinDistance (string src, string dest) return d[str1.Length, str2.Length]; } - + public static unsafe int YetiLevenshtein (string s1, string s2) { ArgumentNullException.ThrowIfNull(s1); ArgumentNullException.ThrowIfNull(s2); - + fixed (char* p1 = s1) fixed (char* p2 = s2) { @@ -419,10 +392,12 @@ public static void AssertTrue (bool condition, string msg) [SupportedOSPlatform("windows")] public string? GetWordFromPos (int xPos, string text, Graphics g, Font font) { - if (text is null) return null; + 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; From e8fa909b9ef47a55d224ebb7305016383105b908 Mon Sep 17 00:00:00 2001 From: Andrey Barisevich Date: Sat, 14 Jun 2025 21:52:36 +0500 Subject: [PATCH 3/3] Substitution cost in YetiLevenshtein(string s1, string s2) now treats null strings as empty strings; --- src/LogExpert.Core/Classes/Util.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LogExpert.Core/Classes/Util.cs b/src/LogExpert.Core/Classes/Util.cs index 4286290b..2b8628b4 100644 --- a/src/LogExpert.Core/Classes/Util.cs +++ b/src/LogExpert.Core/Classes/Util.cs @@ -122,8 +122,8 @@ public static int DamerauLevenshteinDistance (string src, string dest) public static unsafe int YetiLevenshtein (string s1, string s2) { - ArgumentNullException.ThrowIfNull(s1); - ArgumentNullException.ThrowIfNull(s2); + s1 ??= string.Empty; + s2 ??= string.Empty; fixed (char* p1 = s1) fixed (char* p2 = s2)