mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-04-05 11:20:36 +02:00
shared: add nm_utils_strv_find_first() helper
Make _nm_utils_strv_find_first() accessible outside of libnm-core by copying it from "libnm-core/nm-core-internal.h" (and rename it).
This commit is contained in:
parent
6404c79e4d
commit
a165907554
2 changed files with 52 additions and 0 deletions
|
|
@ -158,6 +158,54 @@ _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 ma
|
|||
|
||||
/*****************************************************************************/
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
gint
|
||||
_nm_utils_ascii_str_to_bool (const char *str,
|
||||
gint default_value)
|
||||
|
|
|
|||
|
|
@ -43,6 +43,10 @@ void nm_utils_strbuf_append_str (char **buf, gsize *len, const char *str);
|
|||
|
||||
/*****************************************************************************/
|
||||
|
||||
gssize nm_utils_strv_find_first (char **list, gssize len, const char *needle);
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
|
||||
|
||||
gint _nm_utils_ascii_str_to_bool (const char *str,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue