For calculating the length of an array of strings with a NULL sentinel, introduce a strv_length helper.
Signed-off-by: Ahmad Fatoum <a.fat...@barebox.org> --- v1 -> v2: - no change --- include/string.h | 7 +++++++ lib/string.c | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/string.h b/include/string.h index b54a8fc3f38a..71affe48b640 100644 --- a/include/string.h +++ b/include/string.h @@ -57,4 +57,11 @@ static inline bool is_nul_terminated(const char *val, size_t len) return strnlen(val, len) != len; } +typedef union { + char * const *sl; + const char * const *sl_const; +} strv_t __attribute__((transparent_union)); + +size_t strv_length(strv_t) __pure; + #endif /* __STRING_H */ diff --git a/lib/string.c b/lib/string.c index 96eac6d1daf6..73637cd9710a 100644 --- a/lib/string.c +++ b/lib/string.c @@ -1137,3 +1137,20 @@ char *strreplace(char *str, char old, char new) return str; } EXPORT_SYMBOL(strreplace); + +/** + * strv_length - calculate length of string vector + * @strv: NULL-terminated array of string + * + * Return: size of the vector + */ +size_t strv_length(const char * const *l) +{ + size_t n = 0; + + for (const char *const *s, *const *i = (l); (s = i) && *i; i++) + n++; + + return n; +} +EXPORT_SYMBOL(strv_length); -- 2.39.5