-
Notifications
You must be signed in to change notification settings - Fork 182
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 1 commit
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,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); | ||
|
Contributor
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? |
||
|
|
||
|
|
||
| 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('.'); | ||
|
Contributor
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]; | ||
| } | ||
|
|
||
|
|
||
| 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) | ||
|
Contributor
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(); | ||
|
|
@@ -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; | ||
|
|
||
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?