index_lookup()

Explanation

A global function that matches a tested string (lookup) to a list of strings in a list (index). Uses the length of the string to be tested (lookup) as the number of characters to compare.

Example

ind = index_lookup (econ_flags, econ3) // ind if of type INTEGER

 

 

Creating lists, one must be careful that they do not create the following:

Improper List
index[] {
"north",
"northeast",
"near",
"northeast",
"ne",
"\n" // These lists must ALWAYS end with "\n"
}

 

If the call is made with "ne" (short for northeast), the value returned will be 2 rather than 3, matching 'near'. Even though you have 'ne' in the list, it will not match correctly, so care must be taken to make sure lists are made in such a way as to avoid this problem.


Here is the actual code of the function.

index_lookup
index_lookup (const char* const* index, const char* const lookup)
{
unsigned int i;
for (i = 0; *index[i] != '\n'; i++)
if (!strn_cmp (index[i], lookup, strlen (lookup)))
return i;
return -1;
}

 

 

Copyright 2015 Shadows of Isildur