From b6a9ed3c6dd2e55cc813d975f53a309dc748a926 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Fri, 29 Jun 2018 18:36:07 +0200 Subject: [PATCH] New function(s): strnmatch() and strmatch() These two functions look for a string in a (terminate) list of strings and returns the index to that string, or -1 if not found or error. Signed-off-by: Joachim Nilsson --- src/Makefile.am | 1 + src/strlite.h | 3 ++ src/strmatch.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/strmatch.c diff --git a/src/Makefile.am b/src/Makefile.am index f973335..b6401ce 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,6 +14,7 @@ libite_la_SOURCES = chomp.c copyfile.c \ reallocarray.c rsync.c \ strlcpy.c strlcat.c strtonum.c \ strdupa.h strndupa.h strnlen.h \ + strmatch.c \ tempfile.c which.c \ lite.h strlite.h \ queue.h tree.h diff --git a/src/strlite.h b/src/strlite.h index cbdbc79..dea4a15 100644 --- a/src/strlite.h +++ b/src/strlite.h @@ -49,6 +49,9 @@ }) #endif +int strnmatch (const char *str, const char **list, size_t num); +int strmatch (const char *str, const char **list); + #ifndef strlcpy size_t strlcpy (char *dst, const char *src, size_t siz); #endif diff --git a/src/strmatch.c b/src/strmatch.c new file mode 100644 index 0000000..4304892 --- /dev/null +++ b/src/strmatch.c @@ -0,0 +1,89 @@ +/* Simple string matcher function, finds partial matches. + * + * Copyright (c) 2009-2018 Joachim Nilsson + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + + +/** + * strnmatch - Finds matching strings from a finite list + * @str: String to look for + * @list: List of strings to search. + * @num: Number of entries in @list. + * + * This function searches the @list of strings for @str. If a (partial) match + * is found it returns the index in the @list. + * + * Very similar in function to strmatch(), but works for sets of strings that + * are not %NULL terminated. + * + * Returns: + * -1 on error, otherwise the index to the matching string. + */ +int strnmatch(const char *str, const char **list, size_t num) +{ + size_t i; + + if (!str || !list) { + errno = EINVAL; + return -1; + } + + for (i = 0; i < num; i++) { + if (!strncasecmp (str, list[i], strlen (str))) + return i; + } + + errno = ENOENT; + return -1; +} + +/** + * strmatch - Finds matching strings from a list + * @str: String to look for + * @list: %NULL terminated list of strings to search. + * + * This function searches the @list of strings for @str. If a (partial) match + * is found it returns the index in the @list. + * + * Please note, the @list MUST be terminated by a %NULL element. If that is + * not possible for you, we recommend using strnmatch() instead. + * + * Returns: + * -1 on error, otherwise the index to the matching string. + */ +int strmatch(const char *str, const char **list) +{ + size_t i; + + if (!list) { + errno = EINVAL; + return -1; + } + + for (i = 0; list[i]; i++) + ; + + return strnmatch(str, list, i); +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */