I am working on some debugging of pktriggercord and have come across a function that seems to have some flawed logic in it.. either that or I am for some reason not seeing why this function is written how it is written.
String compare function:
find in array function:
My question is this:
Looking at (string_length > found_index_length), what does it matter if this is even in there?
Is there any point to even checking to see if the string length in the current array index is greater than the string length in the previous array index?
edit: I see what it is doing. It is going through the whole array, looking for the array index where the string most matches the string being searched for.
1. If it finds a partial match, it marks that index
2. If it finds another partial match, it then compares the length of that match to the previous match. If the current match is longer than the previous match, it changes the index marked.
String compare function:
Code:
// case insenstive comparison
// strnicmp
int str_comparison_i (const char *s1, const char *s2, int n) {
if( s1 == NULL ) {
return s2 == NULL ? 0 : -(*s2);
}
if (s2 == NULL) {
return *s1;
}
char c1='\0', c2='\0';
int length=0;
while( length<n && (c1 = tolower (*s1)) == (c2 = tolower (*s2))) {
if (*s1 == '\0') break;
++s1;
++s2;
++length;
}
return c1 - c2;
}
find in array function:
Code:
int find_in_array( const char** an_array, int length, char* str ) {
int i;
int found_index=-1;
size_t found_index_length = 0;
size_t string_length;
for( i = 0; i<length; ++i ) {
string_length = strlen(an_array[i]);
if( (str_comparison_i( an_array[i], str, string_length ) == 0) && (string_length > found_index_length) ) {
found_index_length = string_length;
found_index = i;
}
}
return found_index;
}
My question is this:
Looking at (string_length > found_index_length), what does it matter if this is even in there?
Is there any point to even checking to see if the string length in the current array index is greater than the string length in the previous array index?
edit: I see what it is doing. It is going through the whole array, looking for the array index where the string most matches the string being searched for.
1. If it finds a partial match, it marks that index
2. If it finds another partial match, it then compares the length of that match to the previous match. If the current match is longer than the previous match, it changes the index marked.
Last edited: