MINOR: tools: add find_word_index_in_array() to find a keyword
This will be used to find a word among a set in an array of
arbitrary size and layout provided that the words are always
accessible via a pointer at a fixed location in each item. The
goal here is to simplify call places which want to match a word
among a list, find the associated entry, and propose a list of
possible matches.
This will return the following values depending on the looked up
word:
0 = not found
1 = found a different one which looks like a candidate
2 = found exact match
When non-zero is returned, an index pointer will designate the
associated entry in the array so that the caller can find the
context of the sought word, or at least report the mostly similar
one in the error message, like this:
size_t idx;
match = find_word_index_in_array(kw_array, sizeof(kw_array[0]), -1,
offsetof(struct keyword, name), word, &idx);
if (match != 2) {
memprintf(err, "Invalid keyword '%s'.", word);
if (match == 1)
memprintf(err, "%s Maybe you meant '%s' ?", *err, kw_array[idx].name);
goto fail;
}