Skip to content

Commit

Permalink
string-list: new for_each_string_list() function
Browse files Browse the repository at this point in the history
Add a convenience function for iterating over a string_list's items via
a callback.

Signed-off-by: Jay Soffian <jaysoffian@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jay Soffian authored and Junio C Hamano committed Feb 27, 2009
1 parent 5f48cb9 commit c6f5a7a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions string-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ struct string_list_item *string_list_lookup(const char *string, struct string_li
return list->items + i;
}

int for_each_string_list(string_list_each_func_t fn,
struct string_list *list, void *cb_data)
{
int i, ret = 0;
for (i = 0; i < list->nr; i++)
if ((ret = fn(&list->items[i], cb_data)))
break;
return ret;
}

void string_list_clear(struct string_list *list, int free_util)
{
if (list->items) {
Expand Down
5 changes: 5 additions & 0 deletions string-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ void string_list_clear(struct string_list *list, int free_util);
typedef void (*string_list_clear_func_t)(void *p, const char *str);
void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);

/* Use this function to iterate over each item */
typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
int for_each_string_list(string_list_each_func_t,
struct string_list *list, void *cb_data);

/* Use these functions only on sorted lists: */
int string_list_has_string(const struct string_list *list, const char *string);
int string_list_find_insert_index(const struct string_list *list, const char *string,
Expand Down

0 comments on commit c6f5a7a

Please sign in to comment.