New function to find matching prefix in char* array

This commit is contained in:
Jari Matilainen 2017-01-13 01:04:58 +00:00
commit adc5779b01
2 changed files with 20 additions and 0 deletions

View file

@ -168,6 +168,23 @@ int strarray_find(char **array, const char *item)
return -1; return -1;
} }
int strarray_find_prefix(char **array, const char *item)
{
char **tmp;
int index;
g_return_val_if_fail(array != NULL, -1);
g_return_val_if_fail(item != NULL, -1);
index = 0;
for (tmp = array; *tmp != NULL; tmp++, index++) {
if (g_str_has_prefix(*tmp, item))
return index;
}
return -1;
}
GSList *gslist_find_string(GSList *list, const char *key) GSList *gslist_find_string(GSList *list, const char *key)
{ {
for (; list != NULL; list = list->next) for (; list != NULL; list = list->next)

View file

@ -100,6 +100,9 @@ char *replace_chars(char *str, char from, char to);
/* return index of `item' in `array' or -1 if not found */ /* return index of `item' in `array' or -1 if not found */
int strarray_find(char **array, const char *item); int strarray_find(char **array, const char *item);
/* return index of element in `array' starting with `item' or -1 if not found */
gboolean strarray_find_prefix(char **array, const char *item);
/* string -> uoff_t */ /* string -> uoff_t */
uoff_t str_to_uofft(const char *str); uoff_t str_to_uofft(const char *str);