Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 23 additions & 38 deletions src/LogExpert.Core/Classes/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

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?


if (i < 0)
{
i = -1;
}

return fileName[(i + 1)..];
}
public static string StripExtension (string fileName) => fileName is null ? string.Empty : Path.GetFileNameWithoutExtension(fileName);
Copy link
Contributor

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?


//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('.');
Copy link
Contributor

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?


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)
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
I would like to know in case we got here with null and prevent it in the future.

s2 ??= string.Empty;

fixed (char* p1 = s1)
fixed (char* p2 = s2)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -628,4 +613,4 @@ private static unsafe int MemchrRPLC (char* buffer, char c, int count)
}

#endregion
}
}
Loading