shared: automatically cast strv argument for nm_utils_strv_equal()/nm_utils_strv_cmp_n()

It's cumbersome if we always need to cast our arguments for
the strv helper functions. Depending on the situation, we often
have a "char **" or a "const char *const*" argument.

Use NM_CAST_STRV_CC() macros instead. This macro uses C11's _Generic()
and casts types that are presumed to be safe. This tends to be less
typing and more type-safe, because you don't need an explicit C cast
(which would overrule any warning that the compiler might have for you).
This commit is contained in:
Thomas Haller 2020-10-12 21:49:47 +02:00
parent cbcfc58794
commit 251ba8ea44
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 7 additions and 9 deletions

View file

@ -3997,7 +3997,7 @@ _nm_utils_strv_sort(const char **strv, gssize len)
}
/**
* nm_utils_strv_cmp_n:
* _nm_utils_strv_cmp_n:
* @strv1: a string array
* @len1: the length of @strv1, or -1 for NULL terminated array.
* @strv2: a string array
@ -4020,7 +4020,7 @@ _nm_utils_strv_sort(const char **strv, gssize len)
* Returns: 0 if the arrays are equal (using strcmp).
**/
int
nm_utils_strv_cmp_n(const char *const *strv1, gssize len1, const char *const *strv2, gssize len2)
_nm_utils_strv_cmp_n(const char *const *strv1, gssize len1, const char *const *strv2, gssize len2)
{
gsize n, n2;

View file

@ -1627,14 +1627,12 @@ void _nm_utils_strv_sort(const char **strv, gssize len);
#define nm_utils_strv_sort(strv, len) _nm_utils_strv_sort(NM_CAST_STRV_MC(strv), len)
int
nm_utils_strv_cmp_n(const char *const *strv1, gssize len1, const char *const *strv2, gssize len2);
_nm_utils_strv_cmp_n(const char *const *strv1, gssize len1, const char *const *strv2, gssize len2);
static inline gboolean
nm_utils_strv_equal(char **strv1, char **strv2)
{
return nm_utils_strv_cmp_n((const char *const *) strv1, -1, (const char *const *) strv2, -1)
== 0;
}
#define nm_utils_strv_cmp_n(strv1, len1, strv2, len2) \
_nm_utils_strv_cmp_n(NM_CAST_STRV_CC(strv1), (len1), NM_CAST_STRV_CC(strv2), (len2))
#define nm_utils_strv_equal(strv1, strv2) (nm_utils_strv_cmp_n((strv1), -1, (strv2), -1) == 0)
/*****************************************************************************/