-
Notifications
You must be signed in to change notification settings - Fork 180
Added Null Checks to Util.cs (issue 403) #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: Development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not throw an exception in case fileName is null? Why hide it? |
||
|
||
//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('.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not throw an exception in case fileName is null? Why hide it? |
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why to hide? so we will know how to prevent this in the future |
||
{ | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure this is a correct way to handle s1 and s2 in case of null. You are hiding the fact that there is a null there and I think that in case of null you should not even call this method. In short: |
||
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 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not throw an exception in case fileName is null? Why hide it?