From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kb1xL-00054Q-QJ for barebox@lists.infradead.org; Fri, 06 Nov 2020 13:39:14 +0000 From: Sascha Hauer Date: Fri, 6 Nov 2020 14:38:44 +0100 Message-Id: <20201106133900.2656-11-s.hauer@pengutronix.de> In-Reply-To: <20201106133900.2656-1-s.hauer@pengutronix.de> References: <20201106133900.2656-1-s.hauer@pengutronix.de> MIME-Version: 1.0 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "barebox" Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: [PATCH 10/26] lib: Add match_string() To: Barebox List Signed-off-by: Sascha Hauer --- include/linux/string.h | 2 ++ lib/string.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 2b699957e8..e7f471b139 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -135,4 +135,6 @@ static inline void *kmemdup(const void *src, size_t len, gfp_t gfp) extern int kstrtobool(const char *s, bool *res); +int match_string(const char * const *array, size_t n, const char *string); + #endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index 003070fa53..2d0a99eab7 100644 --- a/lib/string.c +++ b/lib/string.c @@ -852,3 +852,37 @@ int strtobool(const char *str, int *val) return -EINVAL; } EXPORT_SYMBOL(strtobool); + +/** + * match_string - matches given string in an array + * @array: array of strings + * @n: number of strings in the array or -1 for NULL terminated arrays + * @string: string to match with + * + * This routine will look for a string in an array of strings up to the + * n-th element in the array or until the first NULL element. + * + * Historically the value of -1 for @n, was used to search in arrays that + * are NULL terminated. However, the function does not make a distinction + * when finishing the search: either @n elements have been compared OR + * the first NULL element was found. + * + * Return: + * index of a @string in the @array if matches, or %-EINVAL otherwise. + */ +int match_string(const char * const *array, size_t n, const char *string) +{ + int index; + const char *item; + + for (index = 0; index < n; index++) { + item = array[index]; + if (!item) + break; + if (!strcmp(item, string)) + return index; + } + + return -EINVAL; +} +EXPORT_SYMBOL(match_string); -- 2.20.1 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox