Welcome to little lamb

Code » limb » commit 4121821

byte_get_match*: Set arg `first` for exact match

author Olivier Brunel
2023-04-02 14:37:56 UTC
committer Olivier Brunel
2023-05-20 18:06:34 UTC
parent 4f8818d9d13b3b930be01709c0df9a11c78df4cc

byte_get_match*: Set arg `first` for exact match

src/doc/bytestr.h/byte_get_match_full.3.md +2 -0
src/liblimb/bytestr.h/byte_get_match_full.c +4 -1

diff --git a/src/doc/bytestr.h/byte_get_match_full.3.md b/src/doc/bytestr.h/byte_get_match_full.3.md
index 7db9f43..c48c0b3 100644
--- a/src/doc/bytestr.h/byte_get_match_full.3.md
+++ b/src/doc/bytestr.h/byte_get_match_full.3.md
@@ -42,6 +42,8 @@ string pointer that is NULL.
 ! - `first` is not changed if there are no matches. To distinguish between no
 !   matches and more than one, it should be initialized to e.g. -1 prior to
 !   calling `byte_get_match_full`().
+! - `first` is set in case of an exact match, regardless of whether there were
+!    partial matches before or not.
 
 The `byte_get_match`() function is similar, but simply takes `list` as a NULL
 terminated array of NUL-terminated string pointers.
diff --git a/src/liblimb/bytestr.h/byte_get_match_full.c b/src/liblimb/bytestr.h/byte_get_match_full.c
index bc3e3ca..9ac5489 100644
--- a/src/liblimb/bytestr.h/byte_get_match_full.c
+++ b/src/liblimb/bytestr.h/byte_get_match_full.c
@@ -16,7 +16,10 @@ byte_get_match_full(int *first, const char *str, size_t slen,
         /* str matches an element */
         if (!strncmp(el, str, slen)) {
             /* exact match? */
-            if (strlen(el) == slen) return i;
+            if (strlen(el) == slen) {
+                if (first) *first = i;
+                return i;
+            }
 
             if (m < 0) {
                 /* first partial match */