]> git.kaiwu.me - haproxy.git/commit
MINOR: tools: add find_word_index_in_array() to find a keyword
authorWilly Tarreau <w@1wt.eu>
Mon, 19 Oct 2026 16:22:49 +0000 (18:22 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 21 Oct 2026 15:40:55 +0000 (17:40 +0200)
commit113005dd1b0a0b4ae203f5ccfed1630af0d3cfb4
tree4ed56f59066159dd97e4dcce068585b5264d404e
parent3aab8420b503bf50c72ca0e80f82e1aadda7f9f9
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;
    }
include/haproxy/tools.h
src/tools.c