diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index 5a2d28373..76dc9355c 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -115,6 +115,8 @@ GPtrArray *_nm_utils_copy_object_array (const GPtrArray *array); gboolean _nm_utils_string_in_list (const char *str, const char **valid_strings); +gssize _nm_utils_strv_find_first (char **list, gssize len, const char *needle); + char ** _nm_utils_strsplit_set (const char *str, const char *delimiters, int max_tokens); diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index bca343b9f..4a02c98c6 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -434,13 +434,53 @@ nm_utils_same_ssid (const guint8 *ssid1, gsize len1, gboolean _nm_utils_string_in_list (const char *str, const char **valid_strings) { - int i; + return _nm_utils_strv_find_first ((char **) valid_strings, -1, str) >= 0; +} - for (i = 0; valid_strings[i]; i++) - if (strcmp (str, valid_strings[i]) == 0) - break; +/** + * _nm_utils_strv_find_first: + * @list: the strv list to search + * @len: the length of the list, or a negative value if @list is %NULL terminated. + * @needle: the value to search for. The search is done using strcmp(). + * + * Searches @list for @needle and returns the index of the first match (based + * on strcmp()). + * + * For convenience, @list has type 'char**' instead of 'const char **'. + * + * Returns: index of first occurrence or -1 if @needle is not found in @list. + */ +gssize +_nm_utils_strv_find_first (char **list, gssize len, const char *needle) +{ + gssize i; - return valid_strings[i] != NULL; + if (len > 0) { + g_return_val_if_fail (list, -1); + + if (!needle) { + /* if we search a list with known length, %NULL is a valid @needle. */ + for (i = 0; i < len; i++) { + if (!list[i]) + return i; + } + } else { + for (i = 0; i < len; i++) { + if (list[i] && !strcmp (needle, list[i])) + return i; + } + } + } else if (len < 0) { + g_return_val_if_fail (needle, -1); + + if (list) { + for (i = 0; list[i]; i++) { + if (strcmp (needle, list[i]) == 0) + return i; + } + } + } + return -1; } gboolean