Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / bytestr.h / byte_get_match_full.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <limb/bytestr.h>

int
byte_get_match_full(int *first, const char *str, size_t slen,
                    const void *list_, size_t offset, size_t llen)
{
    const char *list = list_;
    int i, m;

    for (i = 0, m = -1; ; list += llen, ++i) {
        const char *el = * (const char **) (list + offset);
        if (!el) break;
        /* str matches an element */
        if (!strncmp(el, str, slen)) {
            /* exact match? */
            if (strlen(el) == slen) {
                if (first) *first = i;
                return i;
            }

            if (m < 0) {
                /* first partial match */
                if (first) *first = i;
                m = i;
            } else {
                /* more than one match */
                return -1;
            }
        }
    }
    return m;
}