C
C#2y ago
Bujju

Levenshtein distance method throwing `IndexOutOfRangeException` [Answered]

I have this code to calculate levenshtein distance:
public static int CompareStrings(string string1, string string2)
{
if (string.IsNullOrEmpty(string1) && string.IsNullOrEmpty(string2)) return 0;
if (string.IsNullOrEmpty(string1)) return string2.Length;
if (string.IsNullOrEmpty(string2)) return string1.Length;

if (string1.Length > string2.Length)
{
string temp = string1;

string1 = string2;
string2 = temp;
}

var distances = new int[string1.Length + 1, string2.Length + 1];
for (int i = 0; i <= string1.Length; i++)
{
for (int j = 1; i <= string2.Length; j++)
{
int a = Math.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1);
int b = string2[j - 1] == string1[i - 1] ? 0 : 1;

distances[i, j] = Math.Min(a, b);
}
}

return distances[string1.Length, string2.Length];
}
public static int CompareStrings(string string1, string string2)
{
if (string.IsNullOrEmpty(string1) && string.IsNullOrEmpty(string2)) return 0;
if (string.IsNullOrEmpty(string1)) return string2.Length;
if (string.IsNullOrEmpty(string2)) return string1.Length;

if (string1.Length > string2.Length)
{
string temp = string1;

string1 = string2;
string2 = temp;
}

var distances = new int[string1.Length + 1, string2.Length + 1];
for (int i = 0; i <= string1.Length; i++)
{
for (int j = 1; i <= string2.Length; j++)
{
int a = Math.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1);
int b = string2[j - 1] == string1[i - 1] ? 0 : 1;

distances[i, j] = Math.Min(a, b);
}
}

return distances[string1.Length, string2.Length];
}
When string1 is empty and string2 is "Example", it works. When string1 is "Ex" and string2 is "Example", it throws an IndexOutOfRangeException.
21 Replies
canton7
canton72y ago
Which line throws the exception?
Bujju
Bujju2y ago
I don't remember how to find that
canton7
canton72y ago
VS should highlight the line in yellow when the exception is thrown. It will also be in the stack trace
Bujju
Bujju2y ago
It doesn't highlight it in yellow, but it shows up in the Debug output
canton7
canton72y ago
What exactly do you see in the debug output?
Bujju
Bujju2y ago
Exception thrown: 'System.IndexOutOfRangeException' in RainBOT.dll
canton7
canton72y ago
Is something higher up catching the exception?
Bujju
Bujju2y ago
The method is being run in an async task if that could be causing the issue
canton7
canton72y ago
I'm guessing it's not being awaited?
Bujju
Bujju2y ago
public Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteContext ctx) =>
Task.FromResult(myList.OrderBy(x => Utilities.CompareStrings((string)ctx.OptionValue, x.Name)));
public Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteContext ctx) =>
Task.FromResult(myList.OrderBy(x => Utilities.CompareStrings((string)ctx.OptionValue, x.Name)));
canton7
canton72y ago
Debug -> Windows -> Exception Settings, change the little checkbox next to "Common Language Exceptions" from grey to a tick. Don't get confused by "C++ Exceptions" or "Win32 Exceptions"
canton7
canton72y ago
(when you're done here, set it back with this button: )
Bujju
Bujju2y ago
It's already on
canton7
canton72y ago
That's the one Click it until it's a tick
Bujju
Bujju2y ago
Ok
canton7
canton72y ago
That'll make it break when the exception happens and you can debug it from there
Bujju
Bujju2y ago
canton7
canton72y ago
There you go Now you can inspect those variables and arrays, see what's out of bounds and why
Bujju
Bujju2y ago
Thanks
canton7
canton72y ago
👍
Accord
Accord2y ago
✅ This post has been marked as answered!